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/>.
8 /** @file opengl_shader.h OpenGL shader programs. */
10 /** Vertex shader that positions a sprite on screen.. */
11 static const char *_vertex_shader_sprite
[] = {
13 "uniform vec4 sprite;",
14 "uniform vec2 screen;",
15 "attribute vec2 position, colour_uv;",
16 "varying vec2 colour_tex_uv;",
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);",
25 /** GLSL 1.50 vertex shader that positions a sprite on screen. */
26 static const char *_vertex_shader_sprite_150
[] = {
28 "uniform vec4 sprite;",
29 "uniform vec2 screen;",
30 "in vec2 position, colour_uv;",
31 "out vec2 colour_tex_uv;",
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);",
40 /** Fragment shader that reads the fragment colour from a 32bpp texture. */
41 static const char *_frag_shader_direct
[] = {
43 "uniform sampler2D colour_tex;",
44 "varying vec2 colour_tex_uv;",
46 " gl_FragData[0] = texture2D(colour_tex, colour_tex_uv);",
50 /** GLSL 1.50 fragment shader that reads the fragment colour from a 32bpp texture. */
51 static const char *_frag_shader_direct_150
[] = {
53 "uniform sampler2D colour_tex;",
54 "in vec2 colour_tex_uv;",
57 " colour = texture(colour_tex, colour_tex_uv);",
61 /** Fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
62 static const char *_frag_shader_palette
[] = {
64 "uniform sampler2D colour_tex;",
65 "uniform sampler1D palette;",
66 "varying vec2 colour_tex_uv;",
68 " float idx = texture2D(colour_tex, colour_tex_uv).r;",
69 " gl_FragData[0] = texture1D(palette, idx);",
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
[] = {
76 "uniform sampler2D colour_tex;",
77 "uniform sampler1D palette;",
78 "in vec2 colour_tex_uv;",
81 " float idx = texture(colour_tex, colour_tex_uv).r;",
82 " colour = texture(palette, idx);",
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);"
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);"
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
[] = {
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;",
110 "uniform float zoom;",
111 "varying vec2 colour_tex_uv;",
113 _frag_shader_remap_func
,
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;",
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
[] = {
128 "uniform sampler2D colour_tex;",
129 "uniform sampler1D palette;",
130 "uniform sampler2D remap_tex;",
131 "uniform float zoom;",
133 "in vec2 colour_tex_uv;",
136 _frag_shader_remap_func
,
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;",
148 /** Fragment shader that performs a palette lookup to read the colour from a sprite texture. */
149 static const char *_frag_shader_sprite_blend
[] = {
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;",
159 "uniform bool crash;",
160 "varying vec2 colour_tex_uv;",
162 _frag_shader_remap_func
,
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;",
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
[] = {
179 "uniform sampler2D colour_tex;",
180 "uniform sampler1D palette;",
181 "uniform sampler2D remap_tex;",
182 "uniform sampler1D pal;",
183 "uniform float zoom;",
185 "uniform bool crash;",
186 "in vec2 colour_tex_uv;",
189 _frag_shader_remap_func
,
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;",