OpenTTD Source 20241224-master-gee860a5c8e
opengl_shader.h
Go to the documentation of this file.
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 */
7
11static 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 "}",
23};
24
26static 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 "}",
38};
39
41static 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 "}",
48};
49
51static 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 "}",
59};
60
62static 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 "}",
71};
72
74static 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 "}",
84};
85
86
88static const char *_frag_shader_remap_func = \
89 "float max3(vec3 v) {"
90 " return max(max(v.x, v.y), v.z);"
91 "}"
92 ""
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;"
97 ""
98 " return clamp(adj + ob * (1.0 - adj), 0.0, 1.0);"
99 "}";
100
102static 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;",
112 "",
114 "",
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);",
119 "",
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 "}",
123};
124
126static 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;",
135 "",
137 "",
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);",
142 "",
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 "}",
146};
147
149static 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;",
161 "",
163 "",
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);",
169 "",
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 "}",
174};
175
177static 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;",
188 "",
190 "",
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);",
196 "",
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 "}",
201};
static const char * _vertex_shader_sprite[]
Vertex shader that positions a sprite on screen.
static const char * _frag_shader_palette_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char * _frag_shader_direct[]
Fragment shader that reads the fragment colour from a 32bpp texture.
static const char * _frag_shader_rgb_mask_blend[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char * _frag_shader_palette[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char * _frag_shader_rgb_mask_blend_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char * _frag_shader_remap_func
Fragment shader function for remap brightness modulation.
static const char * _frag_shader_direct_150[]
GLSL 1.50 fragment shader that reads the fragment colour from a 32bpp texture.
static const char * _frag_shader_sprite_blend_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from a sprite texture.
static const char * _frag_shader_sprite_blend[]
Fragment shader that performs a palette lookup to read the colour from a sprite texture.
static const char * _vertex_shader_sprite_150[]
GLSL 1.50 vertex shader that positions a sprite on screen.