filename | src/pvr2/shaders.glsl |
changeset | 1252:2fb29172ee79 |
prev | 1250:204dae47ab7a |
author | nkeynes |
date | Wed Mar 21 14:45:27 2012 +1000 (12 years ago) |
permissions | -rw-r--r-- |
last change | Make sure we destroy the render buffers as part of the GL shutdown (fixes bug with render buffers not being recreated when we re-init the GL). |
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
89 #ifdef HAVE_GLES2
90 precision mediump float;
91 #endif
92 uniform float alpha_ref;
93 uniform sampler2D primary_texture;
94 uniform sampler2D palette_texture;
95 uniform vec3 fog_colour1;
96 uniform vec3 fog_colour2;
97 varying vec4 frag_colour;
98 varying vec4 frag_colour2;
99 varying vec4 frag_texcoord;
101 void main()
102 {
103 vec4 tex = texture2D( primary_texture, frag_texcoord.xy );
104 if( frag_texcoord.z >= 0.0 ) {
105 tex = texture2D( palette_texture, vec2(frag_texcoord.z + (tex.a*0.249023), 0.5) );
106 }
107 /* HACK: unfortunately we have to maintain compatibility with GLSL 1.20,
108 * which only supports varying float. So since we're propagating texcoord
109 * anyway, overload the last component to indicate texture mode.
110 */
111 vec3 main_colour;
112 if( frag_texcoord.w == 0.0 ) {
113 main_colour = frag_colour.rgb * tex.rgb + frag_colour2.rgb;
114 gl_FragColor.a = frag_colour.a * tex.a;
115 } else if( frag_texcoord.w >= 1.5 ) {
116 main_colour = frag_colour.rgb;
117 gl_FragColor.a = frag_colour.a;
118 } else {
119 main_colour = mix(frag_colour.rgb,tex.rgb,tex.a) + frag_colour2.rgb;
120 gl_FragColor.a = frag_colour.a;
121 }
122 if( gl_FragColor.a < alpha_ref ) {
123 discard;
124 } else {
125 if( frag_colour2.a >= 0.0 ) {
126 gl_FragColor.rgb = mix( main_colour, fog_colour1, frag_colour2.a );
127 } else {
128 gl_FragColor.rgb = mix( main_colour, fog_colour2, -frag_colour2.a );
129 }
130 }
131 }
133 #program pvr2_shader = DEFAULT_VERTEX_SHADER DEFAULT_FRAGMENT_SHADER
135 #ifndef HAVE_OPENGL_FIXEDFUNC
136 /* In this case we also need a basic shader to actually display the output */
137 #vertex BASIC_VERTEX_SHADER
138 uniform mat4 view_matrix;
139 attribute vec2 in_vertex;
140 attribute vec4 in_colour;
141 attribute vec2 in_texcoord; /* uv = coord, z = palette, w = mode */
143 varying vec4 frag_colour;
144 varying vec2 frag_texcoord;
145 void main()
146 {
147 gl_Position = view_matrix * vec4(in_vertex.x,in_vertex.y,0.0,1.0);
148 frag_colour = in_colour;
149 frag_texcoord = in_texcoord;
150 }
152 #fragment BASIC_FRAGMENT_SHADER
153 #ifdef HAVE_GLES2
154 precision mediump float;
155 #endif
157 uniform sampler2D primary_texture;
158 varying vec4 frag_colour;
159 varying vec2 frag_texcoord;
161 void main()
162 {
163 vec4 tex = texture2D( primary_texture, frag_texcoord.xy );
164 gl_FragColor.rgb = mix( frag_colour.rgb, tex.rgb, frag_colour.a );
165 }
167 #program basic_shader = BASIC_VERTEX_SHADER BASIC_FRAGMENT_SHADER
168 #endif
.