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