Debian Codegraph @

drivers/gles3/shaders/effects/copy.glsl

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