Skip to content
0.2.0-dev Download ZIP
245 lines ·7.3 KB Raw
1/* clang-format off */
2#[modes]
3
4mode_default = #define MODE_SIMPLE_COPY
5mode_copy_section = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY
6mode_copy_section_source = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY \n#define MODE_COPY_FROM
7mode_copy_section_3d = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY \n#define USE_TEXTURE_3D
8mode_copy_section_2d_array = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY \n#define USE_TEXTURE_2D_ARRAY
9mode_lens_distortion = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY \n#define USE_TEXTURE_2D_ARRAY \n#define APPLY_LENS_DISTORTION
10mode_screen = #define MODE_SIMPLE_COPY \n#define MODE_MULTIPLY
11mode_gaussian_blur = #define MODE_GAUSSIAN_BLUR
12mode_mipmap = #define MODE_MIPMAP
13mode_simple_color = #define MODE_SIMPLE_COLOR \n#define USE_COPY_SECTION
14mode_cube_to_octahedral = #define CUBE_TO_OCTAHEDRAL \n#define USE_COPY_SECTION
15mode_cube_to_panorama = #define CUBE_TO_PANORAMA
16mode_apply_linear_exposure_to_srgb = #define MODE_SIMPLE_COPY \n#define MODE_MULTIPLY \n#define APPLY_LINEAR_EXPOSURE_TO_SRGB
17
18#[specializations]
19
20CONVERT_LINEAR_TO_SRGB = false
21
22#[vertex]
23
24layout(location = 0) in vec2 vertex_attrib;
25
26out vec2 uv_interp;
27/* clang-format on */
28
29#if defined(USE_COPY_SECTION) || defined(MODE_GAUSSIAN_BLUR)
30// Defined in 0-1 coords.
31uniform highp vec4 copy_section;
32#endif
33#if defined(MODE_GAUSSIAN_BLUR) || defined(MODE_COPY_FROM)
34uniform highp vec4 source_section;
35#endif
36
37void main() {
38 uv_interp = vertex_attrib * 0.5 + 0.5;
39 gl_Position = vec4(vertex_attrib, 1.0, 1.0);
40
41#if defined(USE_COPY_SECTION) || defined(MODE_GAUSSIAN_BLUR)
42 gl_Position.xy = (copy_section.xy + uv_interp.xy * copy_section.zw) * 2.0 - 1.0;
43#endif
44#if defined(MODE_GAUSSIAN_BLUR) || defined(MODE_COPY_FROM)
45 uv_interp = source_section.xy + uv_interp * source_section.zw;
46#endif
47}
48
49/* clang-format off */
50#[fragment]
51
52in vec2 uv_interp;
53/* clang-format on */
54#if defined(USE_TEXTURE_3D) || defined(USE_TEXTURE_2D_ARRAY)
55uniform float layer;
56uniform float lod;
57#endif
58
59#ifdef MODE_SIMPLE_COLOR
60uniform vec4 color_in;
61#endif
62
63#ifdef MODE_MULTIPLY
64uniform float multiply;
65#endif
66
67#ifdef MODE_GAUSSIAN_BLUR
68// Defined in 0-1 coords.
69uniform highp vec2 pixel_size;
70#endif
71
72#ifdef CUBE_TO_OCTAHEDRAL
73vec3 oct_to_vec3(vec2 e) {
74 vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
75 float t = max(-v.z, 0.0);
76 v.xy += t * -sign(v.xy);
77 return normalize(v);
78}
79#endif
80
81#ifdef CUBE_TO_PANORAMA
82uniform lowp float mip_level;
83#endif
84
85#if defined(CUBE_TO_OCTAHEDRAL) || defined(CUBE_TO_PANORAMA)
86uniform samplerCube source_cube; // texunit:0
87
88#else // ~(defined(CUBE_TO_OCTAHEDRAL) || defined(CUBE_TO_PANORAMA))
89
90#if defined(USE_TEXTURE_3D)
91uniform sampler3D source_3d; // texunit:0
92#elif defined(USE_TEXTURE_2D_ARRAY)
93uniform sampler2DArray source_2d_array; // texunit:0
94#else
95uniform sampler2D source; // texunit:0
96#endif
97
98#endif // !(defined(CUBE_TO_OCTAHEDRAL) || defined(CUBE_TO_PANORAMA))
99
100#ifdef APPLY_LENS_DISTORTION
101uniform vec2 eye_center;
102uniform float k1;
103uniform float k2;
104uniform float upscale;
105uniform float aspect_ratio;
106#endif // APPLY_LENS_DISTORTION
107
108#ifdef APPLY_LINEAR_EXPOSURE_TO_SRGB
109uniform float exposure;
110#endif
111
112layout(location = 0) out vec4 frag_color;
113
114// This approximation expects non-negative input; negative input behaves poorly.
115vec3 srgb_to_linear(vec3 color) {
116 // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
117 return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
118}
119
120// This approximation expects non-negative input; negative input is undefined behavior.
121vec3 linear_to_srgb(vec3 color) {
122 // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
123 return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
124}
125
126void main() {
127#ifdef MODE_SIMPLE_COPY
128
129 vec2 uv = uv_interp;
130
131#ifdef APPLY_LENS_DISTORTION
132 uv = uv * 2.0 - 1.0;
133 vec2 offset = uv - eye_center;
134
135 // take aspect ratio into account
136 offset.y /= aspect_ratio;
137
138 // distort
139 vec2 offset_sq = offset * offset;
140 float radius_sq = offset_sq.x + offset_sq.y;
141 float radius_s4 = radius_sq * radius_sq;
142 float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4);
143 offset *= distortion_scale;
144
145 // reapply aspect ratio
146 offset.y *= aspect_ratio;
147
148 // add our eye center back in
149 uv = offset + eye_center;
150 uv /= upscale;
151
152 // and check our color
153 if (uv.x < -1.0 || uv.y < -1.0 || uv.x > 1.0 || uv.y > 1.0) {
154 frag_color = vec4(0.0, 0.0, 0.0, 1.0);
155 } else {
156 uv = uv * 0.5 + 0.5;
157#endif // APPLY_LENS_DISTORTION
158
159#ifdef USE_TEXTURE_3D
160 vec4 color = textureLod(source_3d, vec3(uv, layer), lod);
161#elif defined(USE_TEXTURE_2D_ARRAY)
162 vec4 color = textureLod(source_2d_array, vec3(uv, layer), lod);
163#else
164 vec4 color = texture(source, uv);
165#endif // USE_TEXTURE_3D
166
167#ifdef CONVERT_LINEAR_TO_SRGB
168 // Reading from a *_SRGB texture source will have converted data to linear,
169 // but we should output in sRGB!
170 color.rgb = linear_to_srgb(color.rgb);
171#endif
172
173#ifdef APPLY_LINEAR_EXPOSURE_TO_SRGB
174 color.rgb = srgb_to_linear(color.rgb);
175 color.rgb *= exposure;
176 color.rgb = linear_to_srgb(color.rgb);
177#endif
178
179#ifdef MODE_MULTIPLY
180 color *= multiply;
181#endif // MODE_MULTIPLY
182
183 frag_color = color;
184
185#ifdef APPLY_LENS_DISTORTION
186 }
187#endif // APPLY_LENS_DISTORTION
188
189#endif // MODE_SIMPLE_COPY
190
191#ifdef MODE_SIMPLE_COLOR
192 frag_color = color_in;
193#endif
194
195// Efficient box filter from Jimenez: http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
196// Approximates a Gaussian in a single pass.
197#ifdef MODE_GAUSSIAN_BLUR
198 vec4 A = textureLod(source, uv_interp + pixel_size * vec2(-1.0, -1.0), 0.0);
199 vec4 B = textureLod(source, uv_interp + pixel_size * vec2(0.0, -1.0), 0.0);
200 vec4 C = textureLod(source, uv_interp + pixel_size * vec2(1.0, -1.0), 0.0);
201 vec4 D = textureLod(source, uv_interp + pixel_size * vec2(-0.5, -0.5), 0.0);
202 vec4 E = textureLod(source, uv_interp + pixel_size * vec2(0.5, -0.5), 0.0);
203 vec4 F = textureLod(source, uv_interp + pixel_size * vec2(-1.0, 0.0), 0.0);
204 vec4 G = textureLod(source, uv_interp, 0.0);
205 vec4 H = textureLod(source, uv_interp + pixel_size * vec2(1.0, 0.0), 0.0);
206 vec4 I = textureLod(source, uv_interp + pixel_size * vec2(-0.5, 0.5), 0.0);
207 vec4 J = textureLod(source, uv_interp + pixel_size * vec2(0.5, 0.5), 0.0);
208 vec4 K = textureLod(source, uv_interp + pixel_size * vec2(-1.0, 1.0), 0.0);
209 vec4 L = textureLod(source, uv_interp + pixel_size * vec2(0.0, 1.0), 0.0);
210 vec4 M = textureLod(source, uv_interp + pixel_size * vec2(1.0, 1.0), 0.0);
211
212 float weight = 0.5 / 4.0;
213 float lesser_weight = 0.125 / 4.0;
214
215 frag_color = (D + E + I + J) * weight;
216 frag_color += (A + B + G + F) * lesser_weight;
217 frag_color += (B + C + H + G) * lesser_weight;
218 frag_color += (F + G + L + K) * lesser_weight;
219 frag_color += (G + H + M + L) * lesser_weight;
220#endif
221
222#ifdef CUBE_TO_OCTAHEDRAL
223 // Treat the UV coordinates as 0-1 encoded octahedral coordinates.
224 vec3 dir = oct_to_vec3(uv_interp * 2.0 - 1.0);
225 frag_color = texture(source_cube, dir);
226
227#endif
228
229#ifdef CUBE_TO_PANORAMA
230
231 const float PI = 3.14159265359;
232
233 float phi = uv_interp.x * 2.0 * PI;
234 float theta = uv_interp.y * PI;
235
236 vec3 normal;
237 normal.x = sin(phi) * sin(theta) * -1.0;
238 normal.y = cos(theta);
239 normal.z = cos(phi) * sin(theta) * -1.0;
240
241 vec3 color = srgb_to_linear(textureLod(source_cube, normal, mip_level).rgb);
242 frag_color = vec4(color, 1.0);
243
244#endif
245}