Search
lxdream.org :: lxdream/src/pvr2/glutil.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glutil.c
changeset 1282:9f445c5e252b
prev1276:e20a69167093
next1296:30ecee61f811
author nkeynes
date Tue Mar 27 08:23:52 2012 +1000 (10 years ago)
permissions -rw-r--r--
last change Android: Preserve render buffers when switching away from app.
- fix gl_read_render_buffer + gl_load_frame_buffer to work in GLES
a) glReadPixels only (guaranteed to) work for GL_RGBA,GL_UNSIGNED_BYTE
b) glTexSubImage2D can only load GL_RGBA into a GL_RGBA render buffer.
file annotate diff log raw
1.1 --- a/src/pvr2/glutil.c Tue Mar 20 17:54:58 2012 +1000
1.2 +++ b/src/pvr2/glutil.c Tue Mar 27 08:23:52 2012 +1000
1.3 @@ -16,6 +16,7 @@
1.4 * GNU General Public License for more details.
1.5 */
1.6
1.7 +#include <assert.h>
1.8 #include <string.h>
1.9 #include <stdlib.h>
1.10 #include <glib/gstrfuncs.h>
1.11 @@ -58,7 +59,6 @@
1.12 return !isOpenGLES2() && isGLExtensionSupported("GL_EXT_bgra");
1.13 }
1.14
1.15 -
1.16 gboolean isGLShaderSupported()
1.17 {
1.18 return isOpenGLES2() || (isGLExtensionSupported("GL_ARB_fragment_shader") &&
1.19 @@ -255,11 +255,117 @@
1.20 default: s = "Unknown error"; break;
1.21 }
1.22 if( context ) {
1.23 - WARN( "%s: GL error: %x (%s)\n", context, err, s );
1.24 + WARN( "%s: GL error: %x (%s)", context, err, s );
1.25 } else {
1.26 - WARN( "GL error: %x (%s)\n", err, s );
1.27 + WARN( "GL error: %x (%s)", err, s );
1.28 }
1.29 return FALSE;
1.30 }
1.31 return TRUE;
1.32 }
1.33 +
1.34 +static int bgra_to_rgba_type( int glFormatType )
1.35 +{
1.36 + switch( glFormatType ) {
1.37 + case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1.38 + return GL_UNSIGNED_SHORT_5_5_5_1;
1.39 + case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1.40 + return GL_UNSIGNED_SHORT_4_4_4_4;
1.41 + case GL_UNSIGNED_BYTE:
1.42 + return GL_UNSIGNED_BYTE;
1.43 + default:
1.44 + assert( 0 && "Unsupported BGRA format" );
1.45 + return glFormatType;
1.46 + }
1.47 +}
1.48 +
1.49 +/**
1.50 + * Convert BGRA data in buffer to RGBA format in-place (for systems that don't natively
1.51 + * support BGRA).
1.52 + * @return converted format type
1.53 + * @param data BGRA pixel data
1.54 + * @param nPixels total number of pixels (width*height)
1.55 + * @param glFormatType GL format of source data. One of
1.56 + * GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_SHORT_4_4_4_4_REV, or GL_UNSIGNED_BYTE
1.57 + */
1.58 +static int bgra_to_rgba( unsigned char *data, unsigned nPixels, int glFormatType )
1.59 +{
1.60 + unsigned i;
1.61 + switch( glFormatType ) {
1.62 + case GL_UNSIGNED_SHORT_1_5_5_5_REV: {
1.63 + uint16_t *p = (uint16_t *)data;
1.64 + uint16_t *end = p + nPixels;
1.65 + while( p != end ) {
1.66 + uint16_t v = *p;
1.67 + *p = (v >> 15) | (v<<1);
1.68 + p++;
1.69 + }
1.70 + return GL_UNSIGNED_SHORT_5_5_5_1;
1.71 + }
1.72 + case GL_UNSIGNED_SHORT_4_4_4_4_REV: { /* ARGB => RGBA */
1.73 + uint16_t *p = (uint16_t *)data;
1.74 + uint16_t *end = p + nPixels;
1.75 + while( p != end ) {
1.76 + uint16_t v = *p;
1.77 + *p = (v >> 12) | (v<<4);
1.78 + p++;
1.79 + }
1.80 + return GL_UNSIGNED_SHORT_4_4_4_4;
1.81 + }
1.82 + case GL_UNSIGNED_BYTE: { /* ARGB => ABGR */
1.83 + uint32_t *p = (uint32_t *)data;
1.84 + uint32_t *end = p + nPixels;
1.85 + while( p != end ) {
1.86 + uint32_t v = *p;
1.87 + *p = (v&0xFF000000) | ((v<<16) & 0x00FF0000) | (v & 0x0000FF00) | ((v>>16) & 0x000000FF);
1.88 + p++;
1.89 + }
1.90 + return GL_UNSIGNED_BYTE;
1.91 + }
1.92 + default:
1.93 + assert( 0 && "Unsupported BGRA format" );
1.94 + return glFormatType;
1.95 + }
1.96 +}
1.97 +
1.98 +void glTexImage2DBGRA( int level, GLint intFormat, int width, int height, GLint format, GLint type, unsigned char *data, int preserveData )
1.99 +{
1.100 + if( format == GL_BGRA && !display_driver->capabilities.has_bgra ) {
1.101 + if( preserveData ) {
1.102 + size_t size = width * height * (type == GL_UNSIGNED_BYTE ? 4 : 2);
1.103 + char buf[size];
1.104 + memcpy(buf, data, size);
1.105 + GLint rgbaType = bgra_to_rgba( buf, width*height, type );
1.106 + glTexImage2D( GL_TEXTURE_2D, level, intFormat, width, height, 0, GL_RGBA, rgbaType,
1.107 + buf );
1.108 + } else {
1.109 + GLint rgbaType = bgra_to_rgba( data, width*height, type );
1.110 + glTexImage2D( GL_TEXTURE_2D, level, intFormat, width, height, 0, GL_RGBA, rgbaType,
1.111 + data );
1.112 + }
1.113 + } else {
1.114 + glTexImage2D( GL_TEXTURE_2D, level, intFormat, width, height, 0, format, type,
1.115 + data );
1.116 + }
1.117 +}
1.118 +
1.119 +void glTexSubImage2DBGRA( int level, int xoff, int yoff, int width, int height, GLint format, GLint type, unsigned char *data, int preserveData )
1.120 +{
1.121 + if( format == GL_BGRA && !display_driver->capabilities.has_bgra ) {
1.122 + if( preserveData ) {
1.123 + size_t size = width * height * (type == GL_UNSIGNED_BYTE ? 4 : 2);
1.124 + char buf[size];
1.125 + memcpy(buf, data, size);
1.126 + GLint rgbaType = bgra_to_rgba( buf, width*height, type );
1.127 + glTexSubImage2D( GL_TEXTURE_2D, level, xoff, yoff, width, height, GL_RGBA, rgbaType,
1.128 + buf );
1.129 + } else {
1.130 + GLint rgbaType = bgra_to_rgba( data, width*height, type );
1.131 + glTexSubImage2D( GL_TEXTURE_2D, level, xoff, yoff, width, height, GL_RGBA, rgbaType,
1.132 + data );
1.133 + }
1.134 + } else {
1.135 + glTexSubImage2D( GL_TEXTURE_2D, level, xoff, yoff, width, height, format, type,
1.136 + data );
1.137 + }
1.138 +}
.