Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Forcing texture compression in OpenGL apps
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
monoid
n00b
n00b


Joined: 17 Aug 2008
Posts: 1
Location: Moscow

PostPosted: Sun Aug 17, 2008 4:37 pm    Post subject: [HOWTO] Forcing texture compression in OpenGL apps Reply with quote

Forcing compressed texture formats can improve graphics performance a lot, especially on low-end graphics cards (like my intel 852 gm). The described hack is quite simple and straightforward (does not require patching Mesa/DRI, etc.).

Code:

/* Compile with:
gcc $(CFLAGS) -shared -ldl -nostartfiles -nodefaultlibs -o force-comptex.so
   Usage:
LD_PRELOAD=/path/to/force-comptex.so your-favorite-3d-app
*/
#include <GL/gl.h>
#include <dlfcn.h>

static void GLAPIENTRY (*glTexImage2D_real) ( GLenum target, GLint level,
                                    GLint internalFormat,
                                    GLsizei width, GLsizei height,
                                    GLint border, GLenum format, GLenum type,
                                    const GLvoid *pixels ) = NULL;

GLAPI void GLAPIENTRY glTexImage2D( GLenum target, GLint level,
                                    GLint internalFormat,
                                    GLsizei width, GLsizei height,
                                    GLint border, GLenum format, GLenum type,
                                    const GLvoid *pixels )
{
  GLint cformat = internalFormat;
  if (cformat == GL_RGBA || cformat == 4 || cformat == GL_RGBA8)
    cformat = GL_COMPRESSED_RGBA_FXT1_3DFX;
  else if (cformat == GL_RGB || cformat == 3)
    cformat = GL_COMPRESSED_RGB_FXT1_3DFX;
  glTexImage2D_real (target, level, cformat, width, height, border, format,
            type, pixels);
}

void _init()
{
  void *handle = dlopen ("libGL.so.1", RTLD_LAZY);
  glTexImage2D_real = dlsym (handle, "glTexImage2D");
}



The result of compilation is a shared library overriding one of libGL's functions to enforce a compressed format. Placing it into LD_PRELOAD exposes the "fake" function to the 3d app, allowing to change texture format to a compressed one on every glTexImage2D call.

You may want to change *_FXT_3DFX constants to their S3TC counterparts if S3TC texture compression is supported on your system. For further experimentation you may want to disable compression for small textures or pick DXT3/DXT5 format based on alpha channel.

Feedback appreciated
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum