Fix 03cc0d6: Mark level crossings dirty when removing road from them, not from bridge...
[openttd-github.git] / src / table / opengl_shader.h
blobb644123d14f718fc66d19e2c505765ac1f597c6c
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file opengl_shader.h OpenGL shader programs. */
10 /** Vertex shader that positions a sprite on screen.. */
11 static const char *_vertex_shader_sprite[] = {
12 "#version 110\n",
13 "uniform vec4 sprite;",
14 "uniform vec2 screen;",
15 "attribute vec2 position, colour_uv;",
16 "varying vec2 colour_tex_uv;",
17 "void main() {",
18 " vec2 size = sprite.zw / screen.xy;",
19 " vec2 offset = ((2.0 * sprite.xy + sprite.zw) / screen.xy - 1.0) * vec2(1.0, -1.0);",
20 " colour_tex_uv = colour_uv;",
21 " gl_Position = vec4(position * size + offset, 0.0, 1.0);",
22 "}",
25 /** GLSL 1.50 vertex shader that positions a sprite on screen. */
26 static const char *_vertex_shader_sprite_150[] = {
27 "#version 150\n",
28 "uniform vec4 sprite;",
29 "uniform vec2 screen;",
30 "in vec2 position, colour_uv;",
31 "out vec2 colour_tex_uv;",
32 "void main() {",
33 " vec2 size = sprite.zw / screen.xy;",
34 " vec2 offset = ((2.0 * sprite.xy + sprite.zw) / screen.xy - 1.0) * vec2(1.0, -1.0);",
35 " colour_tex_uv = colour_uv;",
36 " gl_Position = vec4(position * size + offset, 0.0, 1.0);",
37 "}",
40 /** Fragment shader that reads the fragment colour from a 32bpp texture. */
41 static const char *_frag_shader_direct[] = {
42 "#version 110\n",
43 "uniform sampler2D colour_tex;",
44 "varying vec2 colour_tex_uv;",
45 "void main() {",
46 " gl_FragData[0] = texture2D(colour_tex, colour_tex_uv);",
47 "}",
50 /** GLSL 1.50 fragment shader that reads the fragment colour from a 32bpp texture. */
51 static const char *_frag_shader_direct_150[] = {
52 "#version 150\n",
53 "uniform sampler2D colour_tex;",
54 "in vec2 colour_tex_uv;",
55 "out vec4 colour;",
56 "void main() {",
57 " colour = texture(colour_tex, colour_tex_uv);",
58 "}",
61 /** Fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
62 static const char *_frag_shader_palette[] = {
63 "#version 110\n",
64 "uniform sampler2D colour_tex;",
65 "uniform sampler1D palette;",
66 "varying vec2 colour_tex_uv;",
67 "void main() {",
68 " float idx = texture2D(colour_tex, colour_tex_uv).r;",
69 " gl_FragData[0] = texture1D(palette, idx);",
70 "}",
73 /** GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
74 static const char *_frag_shader_palette_150[] = {
75 "#version 150\n",
76 "uniform sampler2D colour_tex;",
77 "uniform sampler1D palette;",
78 "in vec2 colour_tex_uv;",
79 "out vec4 colour;",
80 "void main() {",
81 " float idx = texture(colour_tex, colour_tex_uv).r;",
82 " colour = texture(palette, idx);",
83 "}",
87 /** Fragment shader function for remap brightness modulation. */
88 static const char *_frag_shader_remap_func = \
89 "float max3(vec3 v) {"
90 " return max(max(v.x, v.y), v.z);"
91 "}"
93 "vec3 adj_brightness(vec3 colour, float brightness) {"
94 " vec3 adj = colour * (brightness > 0.0 ? brightness / 0.5 : 1.0);"
95 " vec3 ob_vec = clamp(adj - 1.0, 0.0, 1.0);"
96 " float ob = (ob_vec.r + ob_vec.g + ob_vec.b) / 2.0;"
98 " return clamp(adj + ob * (1.0 - adj), 0.0, 1.0);"
99 "}";
101 /** Fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
102 static const char *_frag_shader_rgb_mask_blend[] = {
103 "#version 110\n",
104 "#extension GL_ATI_shader_texture_lod: enable\n",
105 "#extension GL_ARB_shader_texture_lod: enable\n",
106 "uniform sampler2D colour_tex;",
107 "uniform sampler1D palette;",
108 "uniform sampler2D remap_tex;",
109 "uniform bool rgb;",
110 "uniform float zoom;",
111 "varying vec2 colour_tex_uv;",
113 _frag_shader_remap_func,
115 "void main() {",
116 " float idx = texture2DLod(remap_tex, colour_tex_uv, zoom).r;",
117 " vec4 remap_col = texture1D(palette, idx);",
118 " vec4 rgb_col = texture2DLod(colour_tex, colour_tex_uv, zoom);",
120 " gl_FragData[0].a = rgb ? rgb_col.a : remap_col.a;",
121 " gl_FragData[0].rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
122 "}",
125 /** GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
126 static const char *_frag_shader_rgb_mask_blend_150[] = {
127 "#version 150\n",
128 "uniform sampler2D colour_tex;",
129 "uniform sampler1D palette;",
130 "uniform sampler2D remap_tex;",
131 "uniform float zoom;",
132 "uniform bool rgb;",
133 "in vec2 colour_tex_uv;",
134 "out vec4 colour;",
136 _frag_shader_remap_func,
138 "void main() {",
139 " float idx = textureLod(remap_tex, colour_tex_uv, zoom).r;",
140 " vec4 remap_col = texture(palette, idx);",
141 " vec4 rgb_col = textureLod(colour_tex, colour_tex_uv, zoom);",
143 " colour.a = rgb ? rgb_col.a : remap_col.a;",
144 " colour.rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
145 "}",
148 /** Fragment shader that performs a palette lookup to read the colour from a sprite texture. */
149 static const char *_frag_shader_sprite_blend[] = {
150 "#version 110\n",
151 "#extension GL_ATI_shader_texture_lod: enable\n",
152 "#extension GL_ARB_shader_texture_lod: enable\n",
153 "uniform sampler2D colour_tex;",
154 "uniform sampler1D palette;",
155 "uniform sampler2D remap_tex;",
156 "uniform sampler1D pal;",
157 "uniform float zoom;",
158 "uniform bool rgb;",
159 "uniform bool crash;",
160 "varying vec2 colour_tex_uv;",
162 _frag_shader_remap_func,
164 "void main() {",
165 " float idx = texture2DLod(remap_tex, colour_tex_uv, zoom).r;",
166 " float r = texture1D(pal, idx).r;",
167 " vec4 remap_col = texture1D(palette, idx);",
168 " vec4 rgb_col = texture2DLod(colour_tex, colour_tex_uv, zoom);",
170 " if (crash && idx == 0.0) rgb_col.rgb = vec2(dot(rgb_col.rgb, vec3(0.199325561523, 0.391342163085, 0.076004028320)), 0.0).rrr;"
171 " gl_FragData[0].a = rgb && (r > 0.0 || idx == 0.0) ? rgb_col.a : remap_col.a;",
172 " gl_FragData[0].rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
173 "}",
176 /** GLSL 1.50 fragment shader that performs a palette lookup to read the colour from a sprite texture. */
177 static const char *_frag_shader_sprite_blend_150[] = {
178 "#version 150\n",
179 "uniform sampler2D colour_tex;",
180 "uniform sampler1D palette;",
181 "uniform sampler2D remap_tex;",
182 "uniform sampler1D pal;",
183 "uniform float zoom;",
184 "uniform bool rgb;",
185 "uniform bool crash;",
186 "in vec2 colour_tex_uv;",
187 "out vec4 colour;",
189 _frag_shader_remap_func,
191 "void main() {",
192 " float idx = textureLod(remap_tex, colour_tex_uv, zoom).r;"
193 " float r = texture(pal, idx).r;",
194 " vec4 remap_col = texture(palette, r);",
195 " vec4 rgb_col = textureLod(colour_tex, colour_tex_uv, zoom);",
197 " if (crash && idx == 0.0) rgb_col.rgb = vec2(dot(rgb_col.rgb, vec3(0.199325561523, 0.391342163085, 0.076004028320)), 0.0).rrr;"
198 " colour.a = rgb && (r > 0.0 || idx == 0.0) ? rgb_col.a : remap_col.a;",
199 " colour.rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
200 "}",