Debian Codegraph @

src/scene/glsl/source/screen_effect_library.glsl

raw
1 /* The library of common functions for GLSL screen effects.
2 The ObjectPascal code that includes this will take care to define (or not)
3 symbols MULTI_SAMPLING_x, DEPTH at the beginning of this file.
4 Screen effect code using these functions is safe to work both with and without
5 multi-sampling.
6 */
7
8 #ifdef MULTI_SAMPLING
9 #extension GL_ARB_texture_multisample : enable
10 uniform sampler2DMS screen;
11 #ifdef DEPTH
12 uniform sampler2DMS screen_depth;
13 #endif
14 #else
15 uniform sampler2D screen;
16 #ifdef DEPTH
17 uniform sampler2D screen_depth;
18 #endif
19 #endif
20
21 varying vec2 screenf_01_position;
22
23 uniform int screen_height;
24 uniform int screen_width;
25
26 /* Library of integer-based functions ---------------------------------------- */
27
28 ivec2 screen_position()
29 {
30 return ivec2(
31 int(screenf_01_position.s * float(screen_width)),
32 int(screenf_01_position.t * float(screen_height)));
33 }
34
35 int screen_x()
36 {
37 return int(screenf_01_position.s * float(screen_width));
38 }
39
40 int screen_y()
41 {
42 return int(screenf_01_position.t * float(screen_height));
43 }
44
45 vec4 screen_get_color(ivec2 position)
46 {
47 /* TODO: it would be nice to autogenerate this code */
48 #ifdef MULTI_SAMPLING_16
49 return ( texelFetch(screen, position, 0) +
50 texelFetch(screen, position, 1) +
51 texelFetch(screen, position, 2) +
52 texelFetch(screen, position, 3) +
53 texelFetch(screen, position, 4) +
54 texelFetch(screen, position, 5) +
55 texelFetch(screen, position, 6) +
56 texelFetch(screen, position, 7) +
57 texelFetch(screen, position, 8) +
58 texelFetch(screen, position, 9) +
59 texelFetch(screen, position, 10) +
60 texelFetch(screen, position, 11) +
61 texelFetch(screen, position, 12) +
62 texelFetch(screen, position, 13) +
63 texelFetch(screen, position, 14) +
64 texelFetch(screen, position, 15) ) / 16.0;
65 #else
66 #ifdef MULTI_SAMPLING_8
67 return ( texelFetch(screen, position, 0) +
68 texelFetch(screen, position, 1) +
69 texelFetch(screen, position, 2) +
70 texelFetch(screen, position, 3) +
71 texelFetch(screen, position, 4) +
72 texelFetch(screen, position, 5) +
73 texelFetch(screen, position, 6) +
74 texelFetch(screen, position, 7) ) / 8.0;
75 #else
76 #ifdef MULTI_SAMPLING_4
77 return ( texelFetch(screen, position, 0) +
78 texelFetch(screen, position, 1) +
79 texelFetch(screen, position, 2) +
80 texelFetch(screen, position, 3) ) / 4.0;
81 #else
82 #ifdef MULTI_SAMPLING_2
83 return ( texelFetch(screen, position, 0) +
84 texelFetch(screen, position, 1) ) / 2.0;
85 #else
86 return texture2D(screen,
87 /* Texture coordinates provided in screenf_01_position are already hitting
88 exactly the middle of the pixel.
89 But functions screen_position, screen_x, screen_y cut off the half pixel
90 size. So we restore it now by "+ vec2(0.5)". */
91 (vec2(position) + vec2(0.5)) / vec2(screen_width, screen_height));
92 #endif
93 #endif
94 #endif
95 #endif
96 }
97
98 #ifdef DEPTH
99 float screen_get_depth(ivec2 position)
100 {
101 /* TODO: it would be nice to autogenerate this code */
102 #ifdef MULTI_SAMPLING_16
103 return ( texelFetch(screen_depth, position, 0).r +
104 texelFetch(screen_depth, position, 1).r +
105 texelFetch(screen_depth, position, 2).r +
106 texelFetch(screen_depth, position, 3).r +
107 texelFetch(screen_depth, position, 4).r +
108 texelFetch(screen_depth, position, 5).r +
109 texelFetch(screen_depth, position, 6).r +
110 texelFetch(screen_depth, position, 7).r +
111 texelFetch(screen_depth, position, 8).r +
112 texelFetch(screen_depth, position, 9).r +
113 texelFetch(screen_depth, position, 10).r +
114 texelFetch(screen_depth, position, 11).r +
115 texelFetch(screen_depth, position, 12).r +
116 texelFetch(screen_depth, position, 13).r +
117 texelFetch(screen_depth, position, 14).r +
118 texelFetch(screen_depth, position, 15).r ) / 16.0;
119 #else
120 #ifdef MULTI_SAMPLING_8
121 return ( texelFetch(screen_depth, position, 0).r +
122 texelFetch(screen_depth, position, 1).r +
123 texelFetch(screen_depth, position, 2).r +
124 texelFetch(screen_depth, position, 3).r +
125 texelFetch(screen_depth, position, 4).r +
126 texelFetch(screen_depth, position, 5).r +
127 texelFetch(screen_depth, position, 6).r +
128 texelFetch(screen_depth, position, 7).r ) / 8.0;
129 #else
130 #ifdef MULTI_SAMPLING_4
131 return ( texelFetch(screen_depth, position, 0).r +
132 texelFetch(screen_depth, position, 1).r +
133 texelFetch(screen_depth, position, 2).r +
134 texelFetch(screen_depth, position, 3).r ) / 4.0;
135 #else
136 #ifdef MULTI_SAMPLING_2
137 return ( texelFetch(screen_depth, position, 0).r +
138 texelFetch(screen_depth, position, 1).r ) / 2.0;
139 #else
140 return texture2D(screen_depth,
141 (vec2(position) + vec2(0.5)) / vec2(screen_width, screen_height)).r;
142 #endif
143 #endif
144 #endif
145 #endif
146 }
147
148 float screen_get_depth_fast(ivec2 position)
149 {
150 #ifdef MULTI_SAMPLING
151 return texelFetch(screen_depth, position, 0).r;
152 #else
153 return texture2D(screen_depth,
154 (vec2(position) + vec2(0.5)) / vec2(screen_width, screen_height)).r;
155 #endif
156 }
157 #endif
158
159 /* Library of float-based functions ------------------------------------------ */
160
161 vec2 screenf_position()
162 {
163 return screenf_01_position *
164 vec2(float(screen_width), float(screen_height));
165 }
166
167 float screenf_x()
168 {
169 return screenf_01_position.s * float(screen_width);
170 }
171
172 float screenf_y()
173 {
174 return screenf_01_position.t * float(screen_height);
175 }
176
177 vec4 screenf_get_color(vec2 position)
178 {
179 /* TODO: it would be nice to autogenerate this code */
180 #ifdef MULTI_SAMPLING_16
181 return ( texelFetch(screen, ivec2(position), 0) +
182 texelFetch(screen, ivec2(position), 1) +
183 texelFetch(screen, ivec2(position), 2) +
184 texelFetch(screen, ivec2(position), 3) +
185 texelFetch(screen, ivec2(position), 4) +
186 texelFetch(screen, ivec2(position), 5) +
187 texelFetch(screen, ivec2(position), 6) +
188 texelFetch(screen, ivec2(position), 7) +
189 texelFetch(screen, ivec2(position), 8) +
190 texelFetch(screen, ivec2(position), 9) +
191 texelFetch(screen, ivec2(position), 10) +
192 texelFetch(screen, ivec2(position), 11) +
193 texelFetch(screen, ivec2(position), 12) +
194 texelFetch(screen, ivec2(position), 13) +
195 texelFetch(screen, ivec2(position), 14) +
196 texelFetch(screen, ivec2(position), 15) ) / 16.0;
197 #else
198 #ifdef MULTI_SAMPLING_8
199 return ( texelFetch(screen, ivec2(position), 0) +
200 texelFetch(screen, ivec2(position), 1) +
201 texelFetch(screen, ivec2(position), 2) +
202 texelFetch(screen, ivec2(position), 3) +
203 texelFetch(screen, ivec2(position), 4) +
204 texelFetch(screen, ivec2(position), 5) +
205 texelFetch(screen, ivec2(position), 6) +
206 texelFetch(screen, ivec2(position), 7) ) / 8.0;
207 #else
208 #ifdef MULTI_SAMPLING_4
209 return ( texelFetch(screen, ivec2(position), 0) +
210 texelFetch(screen, ivec2(position), 1) +
211 texelFetch(screen, ivec2(position), 2) +
212 texelFetch(screen, ivec2(position), 3) ) / 4.0;
213 #else
214 #ifdef MULTI_SAMPLING_2
215 return ( texelFetch(screen, ivec2(position), 0) +
216 texelFetch(screen, ivec2(position), 1) ) / 2.0;
217 #else
218 return texture2D(screen, position / vec2(screen_width, screen_height));
219 #endif
220 #endif
221 #endif
222 #endif
223 }
224
225 vec4 screenf_get_original_color()
226 {
227 #ifdef MULTI_SAMPLING
228 // We cannot do something much more optimal in this case, so just call full screenf_get_color
229 return screenf_get_color(screenf_position());
230 #else
231 return texture2D(screen, screenf_01_position);
232 #endif
233 }
234
235 vec4 screenf_01_get_color(vec2 position_01)
236 {
237 #ifdef MULTI_SAMPLING
238 // We cannot do something much more optimal in this case, so just call full screenf_get_color
239 return screenf_get_color(position_01 * vec2(float(screen_width), float(screen_height)));
240 #else
241 return texture2D(screen, position_01);
242 #endif
243 }
244
245 #ifdef DEPTH
246 float screenf_get_depth(vec2 position)
247 {
248 /* TODO: it would be nice to autogenerate this code */
249 #ifdef MULTI_SAMPLING_16
250 return ( texelFetch(screen_depth, ivec2(position), 0).r +
251 texelFetch(screen_depth, ivec2(position), 1).r +
252 texelFetch(screen_depth, ivec2(position), 2).r +
253 texelFetch(screen_depth, ivec2(position), 3).r +
254 texelFetch(screen_depth, ivec2(position), 4).r +
255 texelFetch(screen_depth, ivec2(position), 5).r +
256 texelFetch(screen_depth, ivec2(position), 6).r +
257 texelFetch(screen_depth, ivec2(position), 7).r +
258 texelFetch(screen_depth, ivec2(position), 8).r +
259 texelFetch(screen_depth, ivec2(position), 9).r +
260 texelFetch(screen_depth, ivec2(position), 10).r +
261 texelFetch(screen_depth, ivec2(position), 11).r +
262 texelFetch(screen_depth, ivec2(position), 12).r +
263 texelFetch(screen_depth, ivec2(position), 13).r +
264 texelFetch(screen_depth, ivec2(position), 14).r +
265 texelFetch(screen_depth, ivec2(position), 15).r ) / 16.0;
266 #else
267 #ifdef MULTI_SAMPLING_8
268 return ( texelFetch(screen_depth, ivec2(position), 0).r +
269 texelFetch(screen_depth, ivec2(position), 1).r +
270 texelFetch(screen_depth, ivec2(position), 2).r +
271 texelFetch(screen_depth, ivec2(position), 3).r +
272 texelFetch(screen_depth, ivec2(position), 4).r +
273 texelFetch(screen_depth, ivec2(position), 5).r +
274 texelFetch(screen_depth, ivec2(position), 6).r +
275 texelFetch(screen_depth, ivec2(position), 7).r ) / 8.0;
276 #else
277 #ifdef MULTI_SAMPLING_4
278 return ( texelFetch(screen_depth, ivec2(position), 0).r +
279 texelFetch(screen_depth, ivec2(position), 1).r +
280 texelFetch(screen_depth, ivec2(position), 2).r +
281 texelFetch(screen_depth, ivec2(position), 3).r ) / 4.0;
282 #else
283 #ifdef MULTI_SAMPLING_2
284 return ( texelFetch(screen_depth, ivec2(position), 0).r +
285 texelFetch(screen_depth, ivec2(position), 1).r ) / 2.0;
286 #else
287 return texture2D(screen_depth, position / vec2(screen_width, screen_height)).r;
288 #endif
289 #endif
290 #endif
291 #endif
292 }
293
294 float screenf_01_get_depth(vec2 position_01)
295 {
296 #ifdef MULTI_SAMPLING
297 // We cannot do something much more optimal in this case, so just call full screenf_get_depth
298 return screenf_get_depth(position_01 * vec2(float(screen_width), float(screen_height)));
299 #else
300 return texture2D(screen_depth, position_01).r;
301 #endif
302 }
303
304 float screenf_get_original_depth()
305 {
306 #ifdef MULTI_SAMPLING
307 // We cannot do something much more optimal in this case, so just call full screenf_get_depth
308 return screenf_get_depth(screenf_position());
309 #else
310 return texture2D(screen_depth, screenf_01_position).r;
311 #endif
312 }
313
314 float screenf_get_depth_fast(vec2 position)
315 {
316 #ifdef MULTI_SAMPLING
317 return texelFetch(screen_depth, ivec2(position), 0).r;
318 #else
319 return texture2D(screen_depth, position / vec2(screen_width, screen_height)).r;
320 #endif
321 }
322 #endif