Search
lxdream.org :: lxdream/src/drivers/gl_sl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/gl_sl.c
changeset 405:570d93abb5b7
next424:421d68e78c46
author nkeynes
date Fri Sep 28 07:24:14 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add GLSL loader framework
view annotate diff log raw
     1 /**
     2  * $Id: gl_sl.c,v 1.1 2007-09-28 07:24:14 nkeynes Exp $
     3  *
     4  * GLSL shader loader/unloader. Current version assumes there's exactly
     5  * 1 shader program that's used globally. This may turn out not to be the
     6  * most efficient approach.
     7  *
     8  * Copyright (c) 2007 Nathan Keynes.
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License as published by
    12  * the Free Software Foundation; either version 2 of the License, or
    13  * (at your option) any later version.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  */
    22 #include <GL/gl.h>
    23 #include <GL/glext.h>
    24 #include "drivers/gl_common.h"
    25 #include "display.h"
    27 #define MAX_ERROR_BUF 4096
    29 static GLuint glsl_program, glsl_vert_shader, glsl_frag_shader;
    31 gboolean glsl_is_supported()
    32 {
    33     return isGLExtensionSupported("GL_ARB_fragment_shader") &&
    34 	isGLExtensionSupported("GL_ARB_vertex_shader") &&
    35 	isGLExtensionSupported("GL_ARB_shading_language_100");
    36 }
    38 #ifdef GL_ARB_shader_objects
    39 void glsl_print_error( char *msg, GLhandleARB obj )
    40 {
    41     char buf[MAX_ERROR_BUF];
    42     GLsizei length;
    43     glGetInfoLogARB( obj, sizeof(buf), &length, buf );
    44     ERROR( "%s: %s", msg, buf );
    45 }
    47 gboolean glsl_check_shader_error( char *msg, GLhandleARB obj )
    48 {
    49     GLint value;
    51     glGetObjectParameterivARB(obj, GL_OBJECT_COMPILE_STATUS_ARB, &value);
    52     if( value == 0 ) {
    53 	glsl_print_error(msg, obj);
    54 	return FALSE;
    55     }
    56     return TRUE;
    57 }
    59 gboolean glsl_check_program_error( char *msg, GLhandleARB obj )
    60 {
    61     if( glGetError() != GL_NO_ERROR ) {
    62 	glsl_print_error(msg, obj);
    63     }
    64     return TRUE;
    65 }
    68 gboolean glsl_load_shaders( const char *vertex_src, const char *fragment_src )
    69 {
    70     gboolean vsok, fsok, pok = FALSE;
    71     glsl_program = glCreateProgramObjectARB();
    73     glsl_vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    74     glsl_frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
    75     glShaderSourceARB( glsl_vert_shader, 1, &vertex_src, NULL );
    76     glCompileShaderARB(glsl_vert_shader);
    77     vsok = glsl_check_shader_error("Failed to compile vertex shader", glsl_vert_shader);
    78     glShaderSourceARB( glsl_frag_shader, 1, &fragment_src, NULL );
    79     glCompileShaderARB(glsl_frag_shader);
    80     fsok = glsl_check_shader_error("Failed to compile fragment shader", glsl_frag_shader);
    82     if( vsok && fsok ) {
    83 	glAttachObjectARB(glsl_program, glsl_vert_shader);
    84 	glAttachObjectARB(glsl_program, glsl_frag_shader);
    85 	glLinkProgramARB(glsl_program);
    86 	pok = glsl_check_program_error( "Failed to link shader program", glsl_program );
    87     }
    88     if( pok ) {
    89 	glUseProgramObjectARB(glsl_program);
    90 	pok = glsl_check_program_error( "Failed to apply shader program", glsl_program );
    91     } else {
    92 	glsl_unload_shaders();
    93     }
    94     return pok;
    95 }
    97 void glsl_unload_shaders(void)
    98 {
    99     glUseProgramObjectARB(0);
   100     glDetachObjectARB(glsl_program, glsl_vert_shader);
   101     glDetachObjectARB(glsl_program, glsl_frag_shader);
   102     glDeleteObjectARB(glsl_program);
   103     glDeleteObjectARB(glsl_vert_shader);
   104     glDeleteObjectARB(glsl_frag_shader);
   105 }
   107 #else
   108 gboolean glsl_check_shader_error( char *msg, GLuint shader )
   109 {
   110     GLint value;
   112     glGetShaderiv( shader, GL_COMPILE_STATUS, &value );
   113     if( value == 0 ) {
   114 	char buf[MAX_ERROR_BUF];
   115 	GLsizei length;
   116 	glGetShaderInfoLog( shader, sizeof(buf), &length, buf );
   117 	ERROR( "%s: %s", msg, buf );
   118 	return FALSE;
   119     }
   120     return TRUE;
   121 }
   122 gboolean glsl_check_program_error( char *msg, GLuint program )
   123 {
   124     if( glGetError() != GL_NO_ERROR ) {
   125 	char buf[MAX_ERROR_BUF];
   126 	GLsizei length;
   127 	glGetProgramInfoLog( program, sizeof(buf), &length, buf );
   128 	ERROR( "%s: %s", msg, buf );
   129 	return FALSE;
   130     }
   131     return TRUE;
   132 }
   134 gboolean glsl_load_shaders( const char *vertex_src, const char *fragment_src )
   135 {
   136     gboolean vsok, fsok, pok = FALSE;
   137     glsl_program = glCreateProgram();
   138     glsl_vert_shader = glCreateShader(GL_VERTEX_SHADER);
   139     glsl_frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
   140     glShaderSource( glsl_vert_shader, 1, &vertex_src, NULL );
   141     glCompileShader(glsl_vert_shader);
   142     vsok = glsl_check_shader_error( "Failed to compile vertex shader", glsl_vert_shader );
   143     glShaderSource( glsl_frag_shader, 1, &fragment_src, NULL );
   144     glCompileShader(glsl_frag_shader);
   145     fsok = glsl_check_shader_error( "Failed to compile fragment shader", glsl_frag_shader );
   147     if( vsok && fsok ) {
   148 	glAttachShader(glsl_program, glsl_vert_shader);
   149 	glAttachShader(glsl_program, glsl_frag_shader);
   150 	glLinkProgram(glsl_program);
   151 	pok = glsl_check_program_error( "Failed to link shader program", glsl_program );
   152     }
   154     if( pok ) {
   155 	glUseProgram(glsl_program);
   156     } else {
   157 	glsl_unload_shaders();
   158     }
   159     return pok;
   160 }
   162 void glsl_unload_shaders(void)
   163 {
   164     glUseProgram(0);
   165     glDetachShader(glsl_program, glsl_vert_shader);
   166     glDetachShader(glsl_program, glsl_frag_shader);
   167     glDeleteProgram(glsl_program);
   168     glDeleteShader(glsl_vert_shader);
   169     glDeleteShader(glsl_frag_shader);
   170 }
   171 #endif
.