Search
lxdream.org :: lxdream/src/pvr2/shaders.glsl
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/shaders.glsl
changeset 1240:190df8a791ca
prev1232:e5b12e2fe6ba
next1250:204dae47ab7a
author nkeynes
date Tue Feb 28 17:25:26 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Implement display output for the GLES2 case (no fixed function
rendering)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Assorted shader definitions (optionally) used by the PVR2 rendering
     5  * engine.
     6  * 
     7  * This file is preprocessed by genglsl to produce shaders.c and shaders.h.
     8  *
     9  * Copyright (c) 2007-2010 Nathan Keynes.
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  */
    22 /**
    23  * Quick reference for predefined variables
    25  * Vertex shader input variables:
    26  *   vec4 gl_Color;
    27  *   vec4 gl_SecondaryColor;
    28  *   vec3 gl_Normal;
    29  *   vec4 gl_Vertex;
    30  *   vec4 gl_MultiTexCoord0;
    31  *   vec4 gl_MultiTexCoord1; 
    32  *   vec4 gl_MultiTexCoord2;
    33  *   vec4 gl_MultiTexCoord3; 
    34  *   vec4 gl_MultiTexCoord4;
    35  *   vec4 gl_MultiTexCoord5;
    36  *   vec4 gl_MultiTexCoord6;
    37  *   vec4 gl_MultiTexCoord7;
    38  *   float gl_FogCoord;
    39  *
    40  * Vertex shader output variables:
    41  *   vec4 gl_Position;    // must be written to
    42  *   float gl_PointSize;  // may be written to
    43  *   vec4 gl_ClipVertex;  // may be written to
    44  *   varying vec4 gl_FrontColor; 
    45  *   varying vec4 gl_BackColor; 
    46  *   varying vec4 gl_FrontSecondaryColor; 
    47  *   varying vec4 gl_BackSecondaryColor; 
    48  *   varying vec4 gl_TexCoord[]; // at most will be gl_MaxTextureCoords
    49  *   varying float gl_FogFragCoord;
    50  *
    51  * Fragment shader input variables:
    52  *   varying vec4 gl_Color; 
    53  *   varying vec4 gl_SecondaryColor; 
    54  *   varying vec4 gl_TexCoord[]; // at most will be gl_MaxTextureCoords
    55  *   varying float gl_FogFragCoord;
    56  *   varying vec2 gl_PointCoord;
    57  *
    58  * Fragme shader output variables:
    59  *   vec4 gl_FragCoord; 
    60  *   bool gl_FrontFacing; 
    61  *   vec4 gl_FragColor; 
    62  *   vec4 gl_FragData[gl_MaxDrawBuffers]; 
    63  *   float gl_FragDepth;
    65  */
    66 #include "../config.h"
    68 #vertex DEFAULT_VERTEX_SHADER
    69 uniform mat4 view_matrix;
    70 attribute vec4 in_vertex;
    71 attribute vec4 in_colour;
    72 attribute vec4 in_colour2; /* rgb = colour, a = fog */
    73 attribute vec4 in_texcoord; /* uv = coord, z = palette, w = mode */
    75 varying vec4 frag_colour;
    76 varying vec4 frag_colour2;
    77 varying vec4 frag_texcoord;
    78 void main()
    79 {
    80     vec4 tmp = view_matrix * in_vertex;
    81     float w = in_vertex.z;
    82     gl_Position  = tmp * w;
    83     frag_colour = in_colour;
    84     frag_colour2 = in_colour2;
    85     frag_texcoord = in_texcoord;
    86 }
    88 #fragment DEFAULT_FRAGMENT_SHADER
    90 uniform float alpha_ref;
    91 uniform sampler2D primary_texture;
    92 uniform sampler2D palette_texture;
    93 uniform vec3 fog_colour1;
    94 uniform vec3 fog_colour2;
    95 varying vec4 frag_colour;
    96 varying vec4 frag_colour2;
    97 varying vec4 frag_texcoord;
    99 void main()
   100 {
   101 	vec4 tex = texture2D( primary_texture, frag_texcoord.xy );
   102 	if( frag_texcoord.z >= 0.0 ) {
   103 	    tex = texture2D( palette_texture, vec2(frag_texcoord.z + (tex.a*0.249023), 0.5) );
   104 	}
   105 	/* HACK: unfortunately we have to maintain compatibility with GLSL 1.20,
   106 	 * which only supports varying float. So since we're propagating texcoord
   107 	 * anyway, overload the last component to indicate texture mode. 
   108 	 */
   109         vec3 main_colour;
   110 	if( frag_texcoord.w == 0.0 ) {
   111             main_colour = frag_colour.rgb * tex.rgb + frag_colour2.rgb;
   112 	    gl_FragColor.a = frag_colour.a * tex.a;
   113 	} else if( frag_texcoord.w >= 1.5 ) {
   114             main_colour = frag_colour.rgb;
   115 	    gl_FragColor.a = frag_colour.a;
   116 	} else {
   117 	    main_colour =  mix(frag_colour.rgb,tex.rgb,tex.a) + frag_colour2.rgb;
   118 	    gl_FragColor.a = frag_colour.a;
   119 	}
   120         if( gl_FragColor.a < alpha_ref ) {
   121             discard;
   122         } else { 
   123    	    if( frag_colour2.a >= 0.0 ) {
   124                 gl_FragColor.rgb = mix( main_colour, fog_colour1, frag_colour2.a );
   125             } else {
   126                 gl_FragColor.rgb = mix( main_colour, fog_colour2, -frag_colour2.a );
   127             }
   128 	    gl_FragDepth = gl_FragCoord.z;
   129         } 
   130 }
   132 #program pvr2_shader = DEFAULT_VERTEX_SHADER DEFAULT_FRAGMENT_SHADER
   134 #ifndef HAVE_OPENGL_FIXEDFUNC
   135 /* In this case we also need a basic shader to actually display the output */
   136 #vertex BASIC_VERTEX_SHADER
   137 uniform mat4 view_matrix;
   138 attribute vec2 in_vertex;
   139 attribute vec4 in_colour;
   140 attribute vec2 in_texcoord; /* uv = coord, z = palette, w = mode */
   142 varying vec4 frag_colour;
   143 varying vec2 frag_texcoord;
   144 void main()
   145 {
   146     gl_Position = view_matrix * vec4(in_vertex.x,in_vertex.y,0,1);
   147     frag_colour = in_colour;
   148     frag_texcoord = in_texcoord;
   149 }
   151 #fragment BASIC_FRAGMENT_SHADER
   153 uniform sampler2D primary_texture;
   154 varying vec4 frag_colour;
   155 varying vec2 frag_texcoord;
   157 void main()
   158 {
   159 	vec4 tex = texture2D( primary_texture, frag_texcoord.xy );
   160         gl_FragColor.rgb = mix( frag_colour.rgb, tex.rgb, frag_colour.a );
   161         gl_FragDepth = gl_FragCoord.z;
   162 }
   164 #program basic_shader = BASIC_VERTEX_SHADER BASIC_FRAGMENT_SHADER
   165 #endif
.