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 424:421d68e78c46
prev405:570d93abb5b7
next477:9a373f2ff009
author nkeynes
date Sun Oct 07 05:42:25 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
view annotate diff log raw
     1 /**
     2  * $Id: gl_sl.c,v 1.2 2007-10-07 05:42:25 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  */
    21 #define GL_GLEXT_PROTOTYPES 1
    23 #include <GL/gl.h>
    24 #include <GL/glext.h>
    25 #include "drivers/gl_common.h"
    26 #include "display.h"
    28 #define MAX_ERROR_BUF 4096
    30 static GLuint glsl_program, glsl_vert_shader, glsl_frag_shader;
    32 gboolean glsl_is_supported()
    33 {
    34     return isGLExtensionSupported("GL_ARB_fragment_shader") &&
    35 	isGLExtensionSupported("GL_ARB_vertex_shader") &&
    36 	isGLExtensionSupported("GL_ARB_shading_language_100");
    37 }
    39 #ifdef GL_ARB_shader_objects
    40 void glsl_print_error( char *msg, GLhandleARB obj )
    41 {
    42     char buf[MAX_ERROR_BUF];
    43     GLsizei length;
    44     glGetInfoLogARB( obj, sizeof(buf), &length, buf );
    45     ERROR( "%s: %s", msg, buf );
    46 }
    48 gboolean glsl_check_shader_error( char *msg, GLhandleARB obj )
    49 {
    50     GLint value;
    52     glGetObjectParameterivARB(obj, GL_OBJECT_COMPILE_STATUS_ARB, &value);
    53     if( value == 0 ) {
    54 	glsl_print_error(msg, obj);
    55 	return FALSE;
    56     }
    57     return TRUE;
    58 }
    60 gboolean glsl_check_program_error( char *msg, GLhandleARB obj )
    61 {
    62     if( glGetError() != GL_NO_ERROR ) {
    63 	glsl_print_error(msg, obj);
    64     }
    65     return TRUE;
    66 }
    69 gboolean glsl_load_shaders( const char *vertex_src, const char *fragment_src )
    70 {
    71     gboolean vsok, fsok, pok = FALSE;
    72     glsl_program = glCreateProgramObjectARB();
    74     glsl_vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    75     glsl_frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
    76     glShaderSourceARB( glsl_vert_shader, 1, &vertex_src, NULL );
    77     glCompileShaderARB(glsl_vert_shader);
    78     vsok = glsl_check_shader_error("Failed to compile vertex shader", glsl_vert_shader);
    79     glShaderSourceARB( glsl_frag_shader, 1, &fragment_src, NULL );
    80     glCompileShaderARB(glsl_frag_shader);
    81     fsok = glsl_check_shader_error("Failed to compile fragment shader", glsl_frag_shader);
    83     if( vsok && fsok ) {
    84 	glAttachObjectARB(glsl_program, glsl_vert_shader);
    85 	glAttachObjectARB(glsl_program, glsl_frag_shader);
    86 	glLinkProgramARB(glsl_program);
    87 	pok = glsl_check_program_error( "Failed to link shader program", glsl_program );
    88     }
    89     if( pok ) {
    90 	glUseProgramObjectARB(glsl_program);
    91 	pok = glsl_check_program_error( "Failed to apply shader program", glsl_program );
    92     } else {
    93 	glsl_unload_shaders();
    94     }
    95     return pok;
    96 }
    98 void glsl_unload_shaders(void)
    99 {
   100     glUseProgramObjectARB(0);
   101     glDetachObjectARB(glsl_program, glsl_vert_shader);
   102     glDetachObjectARB(glsl_program, glsl_frag_shader);
   103     glDeleteObjectARB(glsl_program);
   104     glDeleteObjectARB(glsl_vert_shader);
   105     glDeleteObjectARB(glsl_frag_shader);
   106 }
   108 #else
   109 gboolean glsl_check_shader_error( char *msg, GLuint shader )
   110 {
   111     GLint value;
   113     glGetShaderiv( shader, GL_COMPILE_STATUS, &value );
   114     if( value == 0 ) {
   115 	char buf[MAX_ERROR_BUF];
   116 	GLsizei length;
   117 	glGetShaderInfoLog( shader, sizeof(buf), &length, buf );
   118 	ERROR( "%s: %s", msg, buf );
   119 	return FALSE;
   120     }
   121     return TRUE;
   122 }
   123 gboolean glsl_check_program_error( char *msg, GLuint program )
   124 {
   125     if( glGetError() != GL_NO_ERROR ) {
   126 	char buf[MAX_ERROR_BUF];
   127 	GLsizei length;
   128 	glGetProgramInfoLog( program, sizeof(buf), &length, buf );
   129 	ERROR( "%s: %s", msg, buf );
   130 	return FALSE;
   131     }
   132     return TRUE;
   133 }
   135 gboolean glsl_load_shaders( const char *vertex_src, const char *fragment_src )
   136 {
   137     gboolean vsok, fsok, pok = FALSE;
   138     glsl_program = glCreateProgram();
   139     glsl_vert_shader = glCreateShader(GL_VERTEX_SHADER);
   140     glsl_frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
   141     glShaderSource( glsl_vert_shader, 1, &vertex_src, NULL );
   142     glCompileShader(glsl_vert_shader);
   143     vsok = glsl_check_shader_error( "Failed to compile vertex shader", glsl_vert_shader );
   144     glShaderSource( glsl_frag_shader, 1, &fragment_src, NULL );
   145     glCompileShader(glsl_frag_shader);
   146     fsok = glsl_check_shader_error( "Failed to compile fragment shader", glsl_frag_shader );
   148     if( vsok && fsok ) {
   149 	glAttachShader(glsl_program, glsl_vert_shader);
   150 	glAttachShader(glsl_program, glsl_frag_shader);
   151 	glLinkProgram(glsl_program);
   152 	pok = glsl_check_program_error( "Failed to link shader program", glsl_program );
   153     }
   155     if( pok ) {
   156 	glUseProgram(glsl_program);
   157     } else {
   158 	glsl_unload_shaders();
   159     }
   160     return pok;
   161 }
   163 void glsl_unload_shaders(void)
   164 {
   165     glUseProgram(0);
   166     glDetachShader(glsl_program, glsl_vert_shader);
   167     glDetachShader(glsl_program, glsl_frag_shader);
   168     glDeleteProgram(glsl_program);
   169     glDeleteShader(glsl_vert_shader);
   170     glDeleteShader(glsl_frag_shader);
   171 }
   172 #endif
.