Search
lxdream.org :: lxdream/src/drivers/video_gl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gl.c
changeset 1246:887f7b0ac7f3
prev1244:6b54ef5ed413
next1251:b8ab59d39756
author nkeynes
date Sat Mar 03 00:17:36 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Fix GLX compile
file annotate diff log raw
nkeynes@635
     1
/**
nkeynes@635
     2
 * $Id$
nkeynes@635
     3
 *
nkeynes@635
     4
 * Common GL code that doesn't depend on a specific implementation
nkeynes@635
     5
 *
nkeynes@635
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@635
     7
 *
nkeynes@635
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@635
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@635
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@635
    11
 * (at your option) any later version.
nkeynes@635
    12
 *
nkeynes@635
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@635
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@635
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@635
    16
 * GNU General Public License for more details.
nkeynes@635
    17
 */
nkeynes@635
    18
nkeynes@635
    19
#include <sys/time.h>
nkeynes@635
    20
nkeynes@635
    21
#include "display.h"
nkeynes@635
    22
#include "pvr2/pvr2.h"
nkeynes@1236
    23
#include "pvr2/glutil.h"
nkeynes@1240
    24
#include "pvr2/shaders.h"
nkeynes@635
    25
#include "drivers/video_gl.h"
nkeynes@635
    26
nkeynes@1239
    27
/* FIXME: Need to actually handle this case */
nkeynes@1239
    28
#ifndef GL_PACK_ROW_LENGTH
nkeynes@1239
    29
#define glPixelStorei(key,val)
nkeynes@1239
    30
#endif
nkeynes@1239
    31
nkeynes@1236
    32
uint32_t video_width, video_height;
nkeynes@1236
    33
struct video_vertex {
nkeynes@1236
    34
    float x,y;
nkeynes@1236
    35
    float u,v;
nkeynes@1240
    36
    float r,g,b,a;
nkeynes@1236
    37
};
nkeynes@1236
    38
nkeynes@1236
    39
static struct video_box_t {
nkeynes@1236
    40
    float viewMatrix[16];
nkeynes@1236
    41
    struct video_vertex gap1[4];
nkeynes@1236
    42
    struct video_vertex gap2[4];
nkeynes@1236
    43
    struct video_vertex video_view[4];
nkeynes@1236
    44
    struct video_vertex invert_view[4];
nkeynes@1236
    45
} video_box;
nkeynes@1236
    46
nkeynes@1236
    47
void gl_set_video_size( uint32_t width, uint32_t height )
nkeynes@1236
    48
{
nkeynes@1236
    49
    video_width = width;
nkeynes@1236
    50
    video_height = height;
nkeynes@1236
    51
nkeynes@1236
    52
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@1236
    53
nkeynes@1236
    54
    int ah = video_width * 0.75;
nkeynes@1236
    55
nkeynes@1236
    56
    if( ah > video_height ) {
nkeynes@1236
    57
        int w = (video_height/0.75);
nkeynes@1236
    58
        x1 = (video_width - w)/2;
nkeynes@1236
    59
        x2 -= x1;
nkeynes@1236
    60
        video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
nkeynes@1236
    61
        video_box.gap1[1].x = x1; video_box.gap1[1].y = 0;
nkeynes@1236
    62
        video_box.gap1[2].x = 0; video_box.gap1[2].y = video_height;
nkeynes@1236
    63
        video_box.gap1[3].x = x2; video_box.gap1[3].y = video_height;
nkeynes@1236
    64
        video_box.gap2[0].x = x2; video_box.gap2[0].y = 0;
nkeynes@1236
    65
        video_box.gap2[1].x = video_width; video_box.gap2[1].y = 0;
nkeynes@1236
    66
        video_box.gap2[2].x = x2; video_box.gap2[2].y = video_height;
nkeynes@1236
    67
        video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
nkeynes@1236
    68
    } else if( ah < video_height ) {
nkeynes@1236
    69
        y1 = (video_height - ah)/2;
nkeynes@1236
    70
        y2 -= y1;
nkeynes@1236
    71
nkeynes@1236
    72
        video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
nkeynes@1236
    73
        video_box.gap1[1].x = video_width; video_box.gap1[1].y = 0;
nkeynes@1236
    74
        video_box.gap1[2].x = 0; video_box.gap1[2].y = y1;
nkeynes@1236
    75
        video_box.gap1[3].x = video_width; video_box.gap1[3].y = y1;
nkeynes@1236
    76
        video_box.gap2[0].x = 0; video_box.gap2[0].y = y2;
nkeynes@1236
    77
        video_box.gap2[1].x = video_width; video_box.gap2[1].y = y2;
nkeynes@1236
    78
        video_box.gap2[2].x = 0; video_box.gap2[2].y = video_height;
nkeynes@1236
    79
        video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
nkeynes@1236
    80
    }
nkeynes@1236
    81
nkeynes@1236
    82
    video_box.video_view[0].x = x1; video_box.video_view[0].y = y1;
nkeynes@1236
    83
    video_box.video_view[0].u = 0; video_box.video_view[0].v = 0;
nkeynes@1236
    84
    video_box.video_view[1].x = x2; video_box.video_view[1].y = y1;
nkeynes@1236
    85
    video_box.video_view[1].u = 1; video_box.video_view[1].v = 0;
nkeynes@1236
    86
    video_box.video_view[2].x = x1; video_box.video_view[2].y = y2;
nkeynes@1236
    87
    video_box.video_view[2].u = 0; video_box.video_view[2].v = 1;
nkeynes@1236
    88
    video_box.video_view[3].x = x2; video_box.video_view[3].y = y2;
nkeynes@1236
    89
    video_box.video_view[3].u = 1; video_box.video_view[3].v = 1;
nkeynes@1236
    90
nkeynes@1236
    91
    memcpy( &video_box.invert_view, &video_box.video_view, sizeof(video_box.video_view) );
nkeynes@1236
    92
    video_box.invert_view[0].v = 1; video_box.invert_view[1].v = 1;
nkeynes@1236
    93
    video_box.invert_view[2].v = 0; video_box.invert_view[3].v = 0;
nkeynes@1236
    94
nkeynes@1236
    95
    defineOrthoMatrix(video_box.viewMatrix, video_width, video_height, 0, 65535);
nkeynes@1236
    96
}
nkeynes@635
    97
nkeynes@1239
    98
#ifdef HAVE_OPENGL_FIXEDFUNC
nkeynes@635
    99
/**
nkeynes@1236
   100
 * Setup the gl context for writes to the display output.
nkeynes@635
   101
 */
nkeynes@1246
   102
void gl_framebuffer_setup()
nkeynes@635
   103
{
nkeynes@1239
   104
    glViewport( 0, 0, video_width, video_height );
nkeynes@1236
   105
    glLoadMatrixf(video_box.viewMatrix);
nkeynes@1236
   106
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1236
   107
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@1236
   108
    glVertexPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].x);
nkeynes@1236
   109
    glColorPointer(3, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].r);
nkeynes@1236
   110
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].u);
nkeynes@1236
   111
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@1236
   112
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@1236
   113
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@635
   114
}
nkeynes@635
   115
nkeynes@1246
   116
void gl_framebuffer_cleanup()
nkeynes@1240
   117
{
nkeynes@1240
   118
    glDisableClientState( GL_VERTEX_ARRAY );
nkeynes@1240
   119
    glDisableClientState( GL_COLOR_ARRAY );
nkeynes@1240
   120
    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@1240
   121
}
nkeynes@1239
   122
#else
nkeynes@1246
   123
void gl_framebuffer_setup()
nkeynes@1239
   124
{
nkeynes@1240
   125
    glViewport( 0, 0, video_width, video_height );
nkeynes@1240
   126
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1240
   127
    glsl_use_basic_shader();
nkeynes@1240
   128
    glsl_set_basic_shader_view_matrix(video_box.viewMatrix);
nkeynes@1240
   129
    glsl_set_basic_shader_in_vertex_pointer(&video_box.gap1[0].x, sizeof(struct video_vertex));
nkeynes@1240
   130
    glsl_set_basic_shader_in_colour_pointer(&video_box.gap1[0].r, sizeof(struct video_vertex));
nkeynes@1240
   131
    glsl_set_basic_shader_in_texcoord_pointer(&video_box.gap1[0].u, sizeof(struct video_vertex));
nkeynes@1240
   132
    glsl_set_basic_shader_primary_texture(0);
nkeynes@1240
   133
}
nkeynes@1240
   134
nkeynes@1246
   135
void gl_framebuffer_cleanup()
nkeynes@1240
   136
{
nkeynes@1240
   137
    glsl_clear_shader();
nkeynes@1239
   138
}
nkeynes@1239
   139
#endif
nkeynes@1239
   140
nkeynes@635
   141
void gl_display_render_buffer( render_buffer_t buffer )
nkeynes@635
   142
{
nkeynes@635
   143
    gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
nkeynes@635
   144
}
nkeynes@635
   145
nkeynes@854
   146
/**
nkeynes@854
   147
 * Convert window coordinates to dreamcast device coords (640x480) using the 
nkeynes@854
   148
 * same viewable area as gl_texture_window.
nkeynes@854
   149
 * If the coordinates are outside the viewable area, the result is -1,-1.
nkeynes@854
   150
 */ 
nkeynes@854
   151
void gl_window_to_system_coords( int *x, int *y )
nkeynes@854
   152
{
nkeynes@854
   153
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@854
   154
nkeynes@854
   155
    int ah = video_width * 0.75;
nkeynes@854
   156
nkeynes@854
   157
    if( ah > video_height ) {
nkeynes@854
   158
        int w = (video_height/0.75);
nkeynes@854
   159
        x1 = (video_width - w)/2;
nkeynes@854
   160
        x2 -= x1;
nkeynes@854
   161
    } else if( ah < video_height ) {
nkeynes@854
   162
        y1 = (video_height - ah)/2;
nkeynes@854
   163
        y2 -= y1;
nkeynes@854
   164
    }
nkeynes@854
   165
    if( *x < x1 || *x >= x2 || *y < y1 || *y >= y2 ) {
nkeynes@854
   166
        *x = -1;
nkeynes@854
   167
        *y = -1;
nkeynes@854
   168
    } else {
nkeynes@854
   169
        *x = (*x - x1) * DISPLAY_WIDTH / (x2-x1);
nkeynes@854
   170
        *y = (*y - y1) * DISPLAY_HEIGHT / (y2-y1);
nkeynes@854
   171
    }
nkeynes@854
   172
}
nkeynes@854
   173
nkeynes@635
   174
void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
nkeynes@635
   175
{
nkeynes@1240
   176
    /* Set video box tex alpha to 1 */
nkeynes@1240
   177
    video_box.video_view[0].a = video_box.video_view[1].a = video_box.video_view[2].a = video_box.video_view[3].a = 1;
nkeynes@1240
   178
nkeynes@635
   179
    /* Reset display parameters */
nkeynes@1236
   180
    gl_framebuffer_setup();
nkeynes@1236
   181
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
nkeynes@1236
   182
    glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
nkeynes@1236
   183
    glEnable(GL_TEXTURE_2D);
nkeynes@1236
   184
    glBindTexture(GL_TEXTURE_2D,tex_id);
nkeynes@1222
   185
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@1222
   186
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@1236
   187
    glDrawArrays(GL_TRIANGLE_STRIP, inverted ? 12 : 8, 4);
nkeynes@1236
   188
    glDisable(GL_TEXTURE_2D);
nkeynes@635
   189
    glFlush();
nkeynes@1240
   190
    gl_framebuffer_cleanup();
nkeynes@635
   191
}
nkeynes@635
   192
nkeynes@635
   193
gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
nkeynes@635
   194
{
nkeynes@635
   195
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@635
   196
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@635
   197
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@635
   198
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@736
   199
nkeynes@635
   200
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@1222
   201
    glBindTexture( GL_TEXTURE_2D, tex_id );
nkeynes@1222
   202
    glTexSubImage2D( GL_TEXTURE_2D, 0, 0,0,
nkeynes@736
   203
                     frame->width, frame->height, format, type, frame->data );
nkeynes@1236
   204
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@635
   205
    return TRUE;
nkeynes@635
   206
}
nkeynes@635
   207
nkeynes@669
   208
void gl_display_blank( uint32_t colour )
nkeynes@635
   209
{
nkeynes@1236
   210
    /* Set the video_box background colour */
nkeynes@1236
   211
    video_box.video_view[0].r = ((float)(((colour >> 16) & 0xFF) + 1)) / 256.0;
nkeynes@1236
   212
    video_box.video_view[0].g = ((float)(((colour >> 8) & 0xFF) + 1)) / 256.0;
nkeynes@1236
   213
    video_box.video_view[0].b = ((float)((colour & 0xFF) + 1)) / 256.0;
nkeynes@1240
   214
    video_box.video_view[0].a = 0;
nkeynes@1236
   215
    memcpy( &video_box.video_view[1].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   216
    memcpy( &video_box.video_view[2].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   217
    memcpy( &video_box.video_view[3].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   218
nkeynes@1236
   219
    /* And render */
nkeynes@1236
   220
    gl_framebuffer_setup();
nkeynes@1236
   221
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
nkeynes@1236
   222
    glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
nkeynes@1236
   223
    glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
nkeynes@635
   224
    glFlush();
nkeynes@1240
   225
    gl_framebuffer_cleanup();
nkeynes@635
   226
}
nkeynes@635
   227
nkeynes@635
   228
/**
nkeynes@635
   229
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@635
   230
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@635
   231
 * there's only one buffer this needs to be wrapped.
nkeynes@635
   232
 */
nkeynes@635
   233
gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@736
   234
                                int rowstride, int colour_format ) 
nkeynes@635
   235
{
nkeynes@635
   236
    glFinish();
nkeynes@635
   237
    GLenum type = colour_formats[colour_format].type;
nkeynes@635
   238
    GLenum format = colour_formats[colour_format].format;
nkeynes@635
   239
    // int line_size = buffer->width * colour_formats[colour_format].bpp;
nkeynes@635
   240
    // int size = line_size * buffer->height;
nkeynes@635
   241
    int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
nkeynes@635
   242
    glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
nkeynes@635
   243
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@1236
   244
    glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
nkeynes@635
   245
    return TRUE;
nkeynes@635
   246
}
nkeynes@1244
   247
nkeynes@1244
   248
static gboolean video_gl_init();
nkeynes@1244
   249
nkeynes@1244
   250
/**
nkeynes@1244
   251
 * Minimal GL driver (assuming that the GL context is already set up externally)
nkeynes@1244
   252
 * This requires FBO support (since otherwise we have no way to get a render buffer)
nkeynes@1244
   253
 */
nkeynes@1244
   254
struct display_driver display_gl_driver = {
nkeynes@1244
   255
        "gl", N_("OpenGL driver"), video_gl_init, NULL,
nkeynes@1244
   256
        NULL, NULL, NULL,
nkeynes@1244
   257
        NULL, NULL, NULL, NULL,
nkeynes@1244
   258
        gl_load_frame_buffer, gl_display_render_buffer, gl_display_blank,
nkeynes@1244
   259
        NULL, gl_read_render_buffer, NULL, NULL
nkeynes@1244
   260
};
nkeynes@1244
   261
nkeynes@1244
   262
static gboolean video_gl_init()
nkeynes@1244
   263
{
nkeynes@1244
   264
     if( gl_fbo_is_supported() ) {
nkeynes@1244
   265
         display_gl_driver.capabilities.has_gl = TRUE;
nkeynes@1244
   266
         gl_fbo_init(&display_gl_driver);
nkeynes@1244
   267
         gl_vbo_init(&display_gl_driver);
nkeynes@1244
   268
         return TRUE;
nkeynes@1244
   269
     } else {
nkeynes@1244
   270
         return FALSE;
nkeynes@1244
   271
     }
nkeynes@1244
   272
}
.