1 #ifndef PHOSPHOR_MASK_RESIZING_H
2 #define PHOSPHOR_MASK_RESIZING_H
3
4 ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
5
6 // crt-royale: A full-featured CRT shader, with cheese.
7 // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
8 //
9 // This program is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by the Free
11 // Software Foundation; either version 2 of the License, or any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 // more details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 // Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23 ////////////////////////////////// INCLUDES //////////////////////////////////
24
25 #include "../user-settings.h"
26 #include "derived-settings-and-constants.h"
27
28 ///////////////////////////// CODEPATH SELECTION /////////////////////////////
29
30 // Choose a looping strategy based on what's allowed:
31 // Dynamic loops not allowed: Use a flat static loop.
32 // Dynamic loops accomodated: Coarsely branch around static loops.
33 // Dynamic loops assumed allowed: Use a flat dynamic loop.
34 #ifndef DRIVERS_ALLOW_DYNAMIC_BRANCHES
35 #ifdef ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
36 #define BREAK_LOOPS_INTO_PIECES
37 #else
38 #define USE_SINGLE_STATIC_LOOP
39 #endif
40 #endif // No else needed: Dynamic loops assumed.
41
42
43 ////////////////////////////////// CONSTANTS /////////////////////////////////
44
45 // The larger the resized tile, the fewer samples we'll need for downsizing.
46 // See if we can get a static min tile size > mask_min_allowed_tile_size:
47 static const float mask_min_allowed_tile_size = ceil(
48 mask_min_allowed_triad_size * mask_triads_per_tile);
49 static const float mask_min_expected_tile_size =
50 mask_min_allowed_tile_size;
51 // Limit the number of sinc resize taps by the maximum minification factor:
52 static const float pi_over_lobes = pi/mask_sinc_lobes;
53 static const float max_sinc_resize_samples_float = 2.0 * mask_sinc_lobes *
54 mask_resize_src_lut_size.x/mask_min_expected_tile_size;
55 // Vectorized loops sample in multiples of 4. Round up to be safe:
56 static const float max_sinc_resize_samples_m4 = ceil(
57 max_sinc_resize_samples_float * 0.25) * 4.0;
58
59
60 ///////////////////////// RESAMPLING FUNCTION HELPERS ////////////////////////
61
get_dynamic_loop_size(const float magnification_scale)62 inline float get_dynamic_loop_size(const float magnification_scale)
63 {
64 // Requires: The following global constants must be defined:
65 // 1.) mask_sinc_lobes
66 // 2.) max_sinc_resize_samples_m4
67 // Returns: The minimum number of texture samples for a correct downsize
68 // at magnification_scale.
69 // We're downsizing, so the filter is sized across 2*lobes output pixels
70 // (not 2*lobes input texels). This impacts distance measurements and the
71 // minimum number of input samples needed.
72 const float min_samples_float = 2.0 * mask_sinc_lobes / magnification_scale;
73 const float min_samples_m4 = ceil(min_samples_float * 0.25) * 4.0;
74 #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
75 const float max_samples_m4 = max_sinc_resize_samples_m4;
76 #else // ifdef BREAK_LOOPS_INTO_PIECES
77 // Simulating loops with branches imposes a 128-sample limit.
78 const float max_samples_m4 = min(128.0, max_sinc_resize_samples_m4);
79 #endif
80 return min(min_samples_m4, max_samples_m4);
81 }
82
get_first_texel_tile_uv_and_dist(const float2 tex_uv,const float2 tex_size,const float dr,const float input_tiles_per_texture_r,const float samples,static const bool vertical)83 float2 get_first_texel_tile_uv_and_dist(const float2 tex_uv,
84 const float2 tex_size, const float dr,
85 const float input_tiles_per_texture_r, const float samples,
86 static const bool vertical)
87 {
88 // Requires: 1.) dr == du == 1.0/texture_size.x or
89 // dr == dv == 1.0/texture_size.y
90 // (whichever direction we're resampling in).
91 // It's a scalar to save register space.
92 // 2.) input_tiles_per_texture_r is the number of input tiles
93 // that can fit in the input texture in the direction we're
94 // resampling this pass.
95 // 3.) vertical indicates whether we're resampling vertically
96 // this pass (or horizontally).
97 // Returns: Pack and return the first sample's tile_uv coord in [0, 1]
98 // and its texel distance from the destination pixel, in the
99 // resized dimension only.
100 // We'll start with the topmost or leftmost sample and work down or right,
101 // so get the first sample location and distance. Modify both dimensions
102 // as if we're doing a one-pass 2D resize; we'll throw away the unneeded
103 // (and incorrect) dimension at the end.
104 const float2 curr_texel = tex_uv * tex_size;
105 const float2 prev_texel =
106 floor(curr_texel - float2(under_half)) + float2(0.5);
107 const float2 first_texel = prev_texel - float2(samples/2.0 - 1.0);
108 const float2 first_texel_uv_wrap_2D = first_texel * dr;
109 const float2 first_texel_dist_2D = curr_texel - first_texel;
110 // Convert from tex_uv to tile_uv coords so we can sub fracs for fmods.
111 const float2 first_texel_tile_uv_wrap_2D =
112 first_texel_uv_wrap_2D * input_tiles_per_texture_r;
113 // Project wrapped coordinates to the [0, 1] range. We'll do this with all
114 // samples,but the first texel is special, since it might be negative.
115 const float2 coord_negative =
116 float2((first_texel_tile_uv_wrap_2D.x < 0.),(first_texel_tile_uv_wrap_2D.y < 0.));
117 const float2 first_texel_tile_uv_2D =
118 frac(first_texel_tile_uv_wrap_2D) + coord_negative;
119 // Pack the first texel's tile_uv coord and texel distance in 1D:
120 const float2 tile_u_and_dist =
121 float2(first_texel_tile_uv_2D.x, first_texel_dist_2D.x);
122 const float2 tile_v_and_dist =
123 float2(first_texel_tile_uv_2D.y, first_texel_dist_2D.y);
124 return vertical ? tile_v_and_dist : tile_u_and_dist;
125 //return lerp(tile_u_and_dist, tile_v_and_dist, float(vertical));
126 }
127
tex2Dlod0try(const sampler2D tex,const float2 tex_uv)128 inline float4 tex2Dlod0try(const sampler2D tex, const float2 tex_uv)
129 {
130 // Mipmapping and anisotropic filtering get confused by sinc-resampling.
131 // One [slow] workaround is to select the lowest mip level:
132 #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
133 return textureLod(tex, float4(tex_uv, 0.0, 0.0).xy);
134 #else
135 #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
136 return tex2Dbias(tex, float4(tex_uv, 0.0, -16.0));
137 #else
138 return texture(tex, tex_uv);
139 #endif
140 #endif
141 }
142
143
144 ////////////////////////////// LOOP BODY MACROS //////////////////////////////
145
146 // Using inline functions can exceed the temporary register limit, so we're
147 // stuck with #define macros (I'm TRULY sorry). They're declared here instead
148 // of above to be closer to the actual invocation sites. Steps:
149 // 1.) Get the exact texel location.
150 // 2.) Sample the phosphor mask (already assumed encoded in linear RGB).
151 // 3.) Get the distance from the current pixel and sinc weight:
152 // sinc(dist) = sin(pi * dist)/(pi * dist)
153 // We can also use the slower/smoother Lanczos instead:
154 // L(x) = sinc(dist) * sinc(dist / lobes)
155 // 4.) Accumulate the weight sum in weights, and accumulate the weighted texels
156 // in pixel_color (we'll normalize outside the loop at the end).
157 // We vectorize the loop to help reduce the Lanczos window's cost.
158
159 // The r coord is the coord in the dimension we're resizing along (u or v),
160 // and first_texel_tile_uv_rrrr is a float4 of the first texel's u or v
161 // tile_uv coord in [0, 1]. tex_uv_r will contain the tile_uv u or v coord
162 // for four new texel samples.
163 #define CALCULATE_R_COORD_FOR_4_SAMPLES \
164 const float4 true_i = float4(i_base + i) + float4(0.0, 1.0, 2.0, 3.0); \
165 const float4 tile_uv_r = frac( \
166 first_texel_tile_uv_rrrr + true_i * tile_dr); \
167 const float4 tex_uv_r = tile_uv_r * tile_size_uv_r;
168
169 #ifdef PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
170 #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
171 const float4 pi_dist_over_lobes = pi_over_lobes * dist; \
172 const float4 weights = min(sin(pi_dist) * sin(pi_dist_over_lobes) /\
173 (pi_dist*pi_dist_over_lobes), float4(1.0));
174 #else
175 #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
176 const float4 weights = min(sin(pi_dist)/pi_dist, float4(1.0));
177 #endif
178
179 #define UPDATE_COLOR_AND_WEIGHT_SUMS \
180 const float4 dist = magnification_scale * \
181 abs(first_dist_unscaled - true_i); \
182 const float4 pi_dist = pi * dist; \
183 CALCULATE_SINC_RESAMPLE_WEIGHTS; \
184 pixel_color += new_sample0 * weights.xxx; \
185 pixel_color += new_sample1 * weights.yyy; \
186 pixel_color += new_sample2 * weights.zzz; \
187 pixel_color += new_sample3 * weights.www; \
188 weight_sum += weights;
189
190 #define VERTICAL_SINC_RESAMPLE_LOOP_BODY \
191 CALCULATE_R_COORD_FOR_4_SAMPLES; \
192 const float3 new_sample0 = tex2Dlod0try(tex, \
193 float2(tex_uv.x, tex_uv_r.x)).rgb; \
194 const float3 new_sample1 = tex2Dlod0try(tex, \
195 float2(tex_uv.x, tex_uv_r.y)).rgb; \
196 const float3 new_sample2 = tex2Dlod0try(tex, \
197 float2(tex_uv.x, tex_uv_r.z)).rgb; \
198 const float3 new_sample3 = tex2Dlod0try(tex, \
199 float2(tex_uv.x, tex_uv_r.w)).rgb; \
200 UPDATE_COLOR_AND_WEIGHT_SUMS;
201
202 #define HORIZONTAL_SINC_RESAMPLE_LOOP_BODY \
203 CALCULATE_R_COORD_FOR_4_SAMPLES; \
204 const float3 new_sample0 = tex2Dlod0try(tex, \
205 float2(tex_uv_r.x, tex_uv.y)).rgb; \
206 const float3 new_sample1 = tex2Dlod0try(tex, \
207 float2(tex_uv_r.y, tex_uv.y)).rgb; \
208 const float3 new_sample2 = tex2Dlod0try(tex, \
209 float2(tex_uv_r.z, tex_uv.y)).rgb; \
210 const float3 new_sample3 = tex2Dlod0try(tex, \
211 float2(tex_uv_r.w, tex_uv.y)).rgb; \
212 UPDATE_COLOR_AND_WEIGHT_SUMS;
213
214
215 //////////////////////////// RESAMPLING FUNCTIONS ////////////////////////////
216
downsample_vertical_sinc_tiled(const sampler2D tex,const float2 tex_uv,const float2 tex_size,static const float dr,const float magnification_scale,static const float tile_size_uv_r)217 float3 downsample_vertical_sinc_tiled(const sampler2D tex,
218 const float2 tex_uv, const float2 tex_size, static const float dr,
219 const float magnification_scale, static const float tile_size_uv_r)
220 {
221 // Requires: 1.) dr == du == 1.0/texture_size.x or
222 // dr == dv == 1.0/texture_size.y
223 // (whichever direction we're resampling in).
224 // It's a scalar to save register space.
225 // 2.) tile_size_uv_r is the number of texels an input tile
226 // takes up in the input texture, in the direction we're
227 // resampling this pass.
228 // 3.) magnification_scale must be <= 1.0.
229 // Returns: Return a [Lanczos] sinc-resampled pixel of a vertically
230 // downsized input tile embedded in an input texture. (The
231 // vertical version is special-cased though: It assumes the
232 // tile size equals the [static] texture size, since it's used
233 // on an LUT texture input containing one tile. For more
234 // generic use, eliminate the "static" in the parameters.)
235 // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
236 // we're resizing along, e.g. "dy" in this case.
237 #ifdef USE_SINGLE_STATIC_LOOP
238 // A static loop can be faster, but it might blur too much from using
239 // more samples than it should.
240 static const int samples = int(max_sinc_resize_samples_m4);
241 #else
242 const int samples = int(get_dynamic_loop_size(magnification_scale));
243 #endif
244
245 // Get the first sample location (scalar tile uv coord along the resized
246 // dimension) and distance from the output location (in texels):
247 static const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
248 // true = vertical resize:
249 const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
250 tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, true);
251 const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
252 const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
253 // Get the tile sample offset:
254 static const float tile_dr = dr * input_tiles_per_texture_r;
255
256 // Sum up each weight and weighted sample color, varying the looping
257 // strategy based on our expected dynamic loop capabilities. See the
258 // loop body macros above.
259 int i_base = 0;
260 float4 weight_sum = float4(0.0);
261 float3 pixel_color = float3(0.0);
262 static const int i_step = 4;
263 #ifdef BREAK_LOOPS_INTO_PIECES
264 if(samples - i_base >= 64)
265 {
266 for(int i = 0; i < 64; i += i_step)
267 {
268 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
269 }
270 i_base += 64;
271 }
272 if(samples - i_base >= 32)
273 {
274 for(int i = 0; i < 32; i += i_step)
275 {
276 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
277 }
278 i_base += 32;
279 }
280 if(samples - i_base >= 16)
281 {
282 for(int i = 0; i < 16; i += i_step)
283 {
284 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
285 }
286 i_base += 16;
287 }
288 if(samples - i_base >= 8)
289 {
290 for(int i = 0; i < 8; i += i_step)
291 {
292 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
293 }
294 i_base += 8;
295 }
296 if(samples - i_base >= 4)
297 {
298 for(int i = 0; i < 4; i += i_step)
299 {
300 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
301 }
302 i_base += 4;
303 }
304 // Do another 4-sample block for a total of 128 max samples.
305 if(samples - i_base > 0)
306 {
307 for(int i = 0; i < 4; i += i_step)
308 {
309 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
310 }
311 }
312 #else
313 for(int i = 0; i < samples; i += i_step)
314 {
315 VERTICAL_SINC_RESAMPLE_LOOP_BODY;
316 }
317 #endif
318 // Normalize so the weight_sum == 1.0, and return:
319 const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
320 const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
321 weight_sum_reduce.y);
322 return (pixel_color/scalar_weight_sum);
323 }
324
downsample_horizontal_sinc_tiled(const sampler2D tex,const float2 tex_uv,const float2 tex_size,const float dr,const float magnification_scale,const float tile_size_uv_r)325 float3 downsample_horizontal_sinc_tiled(const sampler2D tex,
326 const float2 tex_uv, const float2 tex_size, const float dr,
327 const float magnification_scale, const float tile_size_uv_r)
328 {
329 // Differences from downsample_horizontal_sinc_tiled:
330 // 1.) The dr and tile_size_uv_r parameters are not static consts.
331 // 2.) The "vertical" parameter to get_first_texel_tile_uv_and_dist is
332 // set to false instead of true.
333 // 3.) The horizontal version of the loop body is used.
334 // TODO: If we can get guaranteed compile-time dead code elimination,
335 // we can combine the vertical/horizontal downsampling functions by:
336 // 1.) Add an extra static const bool parameter called "vertical."
337 // 2.) Supply it with the result of get_first_texel_tile_uv_and_dist().
338 // 3.) Use a conditional assignment in the loop body macro. This is the
339 // tricky part: We DO NOT want to incur the extra conditional
340 // assignment in the inner loop at runtime!
341 // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
342 // we're resizing along, e.g. "dx" in this case.
343 #ifdef USE_SINGLE_STATIC_LOOP
344 // If we have to load all samples, we might as well use them.
345 static const int samples = int(max_sinc_resize_samples_m4);
346 #else
347 const int samples = int(get_dynamic_loop_size(magnification_scale));
348 #endif
349
350 // Get the first sample location (scalar tile uv coord along resized
351 // dimension) and distance from the output location (in texels):
352 const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
353 // false = horizontal resize:
354 const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
355 tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, false);
356 const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
357 const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
358 // Get the tile sample offset:
359 const float tile_dr = dr * input_tiles_per_texture_r;
360
361 // Sum up each weight and weighted sample color, varying the looping
362 // strategy based on our expected dynamic loop capabilities. See the
363 // loop body macros above.
364 int i_base = 0;
365 float4 weight_sum = float4(0.0);
366 float3 pixel_color = float3(0.0);
367 static const int i_step = 4;
368 #ifdef BREAK_LOOPS_INTO_PIECES
369 if(samples - i_base >= 64)
370 {
371 for(int i = 0; i < 64; i += i_step)
372 {
373 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
374 }
375 i_base += 64;
376 }
377 if(samples - i_base >= 32)
378 {
379 for(int i = 0; i < 32; i += i_step)
380 {
381 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
382 }
383 i_base += 32;
384 }
385 if(samples - i_base >= 16)
386 {
387 for(int i = 0; i < 16; i += i_step)
388 {
389 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
390 }
391 i_base += 16;
392 }
393 if(samples - i_base >= 8)
394 {
395 for(int i = 0; i < 8; i += i_step)
396 {
397 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
398 }
399 i_base += 8;
400 }
401 if(samples - i_base >= 4)
402 {
403 for(int i = 0; i < 4; i += i_step)
404 {
405 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
406 }
407 i_base += 4;
408 }
409 // Do another 4-sample block for a total of 128 max samples.
410 if(samples - i_base > 0)
411 {
412 for(int i = 0; i < 4; i += i_step)
413 {
414 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
415 }
416 }
417 #else
418 for(int i = 0; i < samples; i += i_step)
419 {
420 HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
421 }
422 #endif
423 // Normalize so the weight_sum == 1.0, and return:
424 const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
425 const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
426 weight_sum_reduce.y);
427 return (pixel_color/scalar_weight_sum);
428 }
429
430
431 //////////////////////////// TILE SIZE CALCULATION ///////////////////////////
432
get_resized_mask_tile_size(const float2 estimated_viewport_size,const float2 estimated_mask_resize_output_size,const bool solemnly_swear_same_inputs_for_every_pass)433 float2 get_resized_mask_tile_size(const float2 estimated_viewport_size,
434 const float2 estimated_mask_resize_output_size,
435 const bool solemnly_swear_same_inputs_for_every_pass)
436 {
437 // Requires: The following global constants must be defined according to
438 // certain constraints:
439 // 1.) mask_resize_num_triads: Must be high enough that our
440 // mask sampling method won't have artifacts later
441 // (long story; see derived-settings-and-constants.h)
442 // 2.) mask_resize_src_lut_size: Texel size of our mask LUT
443 // 3.) mask_triads_per_tile: Num horizontal triads in our LUT
444 // 4.) mask_min_allowed_triad_size: User setting (the more
445 // restrictive it is, the faster the resize will go)
446 // 5.) mask_min_allowed_tile_size_x < mask_resize_src_lut_size.x
447 // 6.) mask_triad_size_desired_{runtime, static}
448 // 7.) mask_num_triads_desired_{runtime, static}
449 // 8.) mask_specify_num_triads must be 0.0/1.0 (false/true)
450 // The function parameters must be defined as follows:
451 // 1.) estimated_viewport_size == (final viewport size);
452 // If mask_specify_num_triads is 1.0/true and the viewport
453 // estimate is wrong, the number of triads will differ from
454 // the user's preference by about the same factor.
455 // 2.) estimated_mask_resize_output_size: Must equal the
456 // output size of the MASK_RESIZE pass.
457 // Exception: The x component may be estimated garbage if
458 // and only if the caller throws away the x result.
459 // 3.) solemnly_swear_same_inputs_for_every_pass: Set to false,
460 // unless you can guarantee that every call across every
461 // pass will use the same sizes for the other parameters.
462 // When calling this across multiple passes, always use the
463 // same y viewport size/scale, and always use the same x
464 // viewport size/scale when using the x result.
465 // Returns: Return the final size of a manually resized mask tile, after
466 // constraining the desired size to avoid artifacts. Under
467 // unusual circumstances, tiles may become stretched vertically
468 // (see wall of text below).
469 // Stated tile properties must be correct:
470 static const float tile_aspect_ratio_inv =
471 mask_resize_src_lut_size.y/mask_resize_src_lut_size.x;
472 static const float tile_aspect_ratio = 1.0/tile_aspect_ratio_inv;
473 static const float2 tile_aspect = float2(1.0, tile_aspect_ratio_inv);
474 // If mask_specify_num_triads is 1.0/true and estimated_viewport_size.x is
475 // wrong, the user preference will be misinterpreted:
476 const float desired_tile_size_x = mask_triads_per_tile * lerp(
477 global.mask_triad_size_desired,
478 estimated_viewport_size.x / global.mask_num_triads_desired,
479 global.mask_specify_num_triads);
480 if(get_mask_sample_mode() > 0.5)
481 {
482 // We don't need constraints unless we're sampling MASK_RESIZE.
483 return desired_tile_size_x * tile_aspect;
484 }
485 // Make sure we're not upsizing:
486 const float temp_tile_size_x =
487 min(desired_tile_size_x, mask_resize_src_lut_size.x);
488 // Enforce min_tile_size and max_tile_size in both dimensions:
489 const float2 temp_tile_size = temp_tile_size_x * tile_aspect;
490 static const float2 min_tile_size =
491 mask_min_allowed_tile_size * tile_aspect;
492 const float2 max_tile_size =
493 estimated_mask_resize_output_size / mask_resize_num_tiles;
494 const float2 clamped_tile_size =
495 clamp(temp_tile_size, min_tile_size, max_tile_size);
496 // Try to maintain tile_aspect_ratio. This is the tricky part:
497 // If we're currently resizing in the y dimension, the x components
498 // could be MEANINGLESS. (If estimated_mask_resize_output_size.x is
499 // bogus, then so is max_tile_size.x and clamped_tile_size.x.)
500 // We can't adjust the y size based on clamped_tile_size.x. If it
501 // clamps when it shouldn't, it won't clamp again when later passes
502 // call this function with the correct sizes, and the discrepancy will
503 // break the sampling coords in MASKED_SCANLINES. Instead, we'll limit
504 // the x size based on the y size, but not vice versa, unless the
505 // caller swears the parameters were the same (correct) in every pass.
506 // As a result, triads could appear vertically stretched if:
507 // a.) mask_resize_src_lut_size.x > mask_resize_src_lut_size.y: Wide
508 // LUT's might clamp x more than y (all provided LUT's are square)
509 // b.) true_viewport_size.x < true_viewport_size.y: The user is playing
510 // with a vertically oriented screen (not accounted for anyway)
511 // c.) mask_resize_viewport_scale.x < masked_resize_viewport_scale.y:
512 // Viewport scales are equal by default.
513 // If any of these are the case, you can fix the stretching by setting:
514 // mask_resize_viewport_scale.x = mask_resize_viewport_scale.y *
515 // (1.0 / min_expected_aspect_ratio) *
516 // (mask_resize_src_lut_size.x / mask_resize_src_lut_size.y)
517 const float x_tile_size_from_y =
518 clamped_tile_size.y * tile_aspect_ratio;
519 const float y_tile_size_from_x = lerp(clamped_tile_size.y,
520 clamped_tile_size.x * tile_aspect_ratio_inv,
521 float(solemnly_swear_same_inputs_for_every_pass));
522 const float2 reclamped_tile_size = float2(
523 min(clamped_tile_size.x, x_tile_size_from_y),
524 min(clamped_tile_size.y, y_tile_size_from_x));
525 // We need integer tile sizes in both directions for tiled sampling to
526 // work correctly. Use floor (to make sure we don't round up), but be
527 // careful to avoid a rounding bug where floor decreases whole numbers:
528 const float2 final_resized_tile_size =
529 floor(reclamped_tile_size + float2(FIX_ZERO(0.0)));
530 return final_resized_tile_size;
531 }
532
533
534 ///////////////////////// FINAL MASK SAMPLING HELPERS ////////////////////////
535
get_mask_sampling_parameters(const float2 mask_resize_texture_size,const float2 mask_resize_video_size,const float2 true_viewport_size,out float2 mask_tiles_per_screen)536 float4 get_mask_sampling_parameters(const float2 mask_resize_texture_size,
537 const float2 mask_resize_video_size, const float2 true_viewport_size,
538 out float2 mask_tiles_per_screen)
539 {
540 // Requires: 1.) Requirements of get_resized_mask_tile_size() must be
541 // met, particularly regarding global constants.
542 // The function parameters must be defined as follows:
543 // 1.) mask_resize_texture_size == MASK_RESIZE.texture_size
544 // if get_mask_sample_mode() is 0 (otherwise anything)
545 // 2.) mask_resize_video_size == MASK_RESIZE.video_size
546 // if get_mask_sample_mode() is 0 (otherwise anything)
547 // 3.) true_viewport_size == IN.output_size for a pass set to
548 // 1.0 viewport scale (i.e. it must be correct)
549 // Returns: Return a float4 containing:
550 // xy: tex_uv coords for the start of the mask tile
551 // zw: tex_uv size of the mask tile from start to end
552 // mask_tiles_per_screen is an out parameter containing the
553 // number of mask tiles that will fit on the screen.
554 // First get the final resized tile size. The viewport size and mask
555 // resize viewport scale must be correct, but don't solemnly swear they
556 // were correct in both mask resize passes unless you know it's true.
557 // (We can better ensure a correct tile aspect ratio if the parameters are
558 // guaranteed correct in all passes...but if we lie, we'll get inconsistent
559 // sizes across passes, resulting in broken texture coordinates.)
560 const float mask_sample_mode = get_mask_sample_mode();
561 const float2 mask_resize_tile_size = get_resized_mask_tile_size(
562 true_viewport_size, mask_resize_video_size, false);
563 if(mask_sample_mode < 0.5)
564 {
565 // Sample MASK_RESIZE: The resized tile is a fraction of the texture
566 // size and starts at a nonzero offset to allow for border texels:
567 const float2 mask_tile_uv_size = mask_resize_tile_size /
568 mask_resize_texture_size;
569 const float2 skipped_tiles = mask_start_texels/mask_resize_tile_size;
570 const float2 mask_tile_start_uv = skipped_tiles * mask_tile_uv_size;
571 // mask_tiles_per_screen must be based on the *true* viewport size:
572 mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
573 return float4(mask_tile_start_uv, mask_tile_uv_size);
574 }
575 else
576 {
577 // If we're tiling at the original size (1:1 pixel:texel), redefine a
578 // "tile" to be the full texture containing many triads. Otherwise,
579 // we're hardware-resampling an LUT, and the texture truly contains a
580 // single unresized phosphor mask tile anyway.
581 static const float2 mask_tile_uv_size = float2(1.0);
582 static const float2 mask_tile_start_uv = float2(0.0);
583 if(mask_sample_mode > 1.5)
584 {
585 // Repeat the full LUT at a 1:1 pixel:texel ratio without resizing:
586 mask_tiles_per_screen = true_viewport_size/mask_texture_large_size;
587 }
588 else
589 {
590 // Hardware-resize the original LUT:
591 mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
592 }
593 return float4(mask_tile_start_uv, mask_tile_uv_size);
594 }
595 }
596 /*
597 float2 fix_tiling_discontinuities_normalized(const float2 tile_uv,
598 float2 duv_dx, float2 duv_dy)
599 {
600 // Requires: 1.) duv_dx == ddx(tile_uv)
601 // 2.) duv_dy == ddy(tile_uv)
602 // 3.) tile_uv contains tile-relative uv coords in [0, 1],
603 // such that (0.5, 0.5) is the center of a tile, etc.
604 // ("Tile" can mean texture, the video embedded in the
605 // texture, or some other "tile" embedded in a texture.)
606 // Returns: Return new tile_uv coords that contain no discontinuities
607 // across a 2x2 pixel quad.
608 // Description:
609 // When uv coords wrap from 1.0 to 0.0, they create a discontinuity in the
610 // derivatives, which we assume happened if the absolute difference between
611 // any fragment in a 2x2 block is > ~half a tile. If the current block has
612 // a u or v discontinuity and the current fragment is in the first half of
613 // the tile along that axis (i.e. it wrapped from 1.0 to 0.0), add a tile
614 // to that coord to make the 2x2 block continuous. (It will now have a
615 // coord > 1.0 in the padding area beyond the tile.) This function takes
616 // derivatives as parameters so the caller can reuse them.
617 // In case we're using high-quality (nVidia-style) derivatives, ensure
618 // diagonically opposite fragments see each other for correctness:
619 duv_dx = abs(duv_dx) + abs(ddy(duv_dx));
620 duv_dy = abs(duv_dy) + abs(ddx(duv_dy));
621 const float2 pixel_in_first_half_tile = float2((tile_uv.x < 0.5),(tile_uv.y < 0.5));
622 const float2 jump_exists = float2(((duv_dx + duv_dy).x > 0.5),((duv_dx + duv_dy).y > 0.5));
623 return tile_uv + jump_exists * pixel_in_first_half_tile;
624 }
625 */
convert_phosphor_tile_uv_wrap_to_tex_uv(const float2 tile_uv_wrap,const float4 mask_tile_start_uv_and_size)626 float2 convert_phosphor_tile_uv_wrap_to_tex_uv(const float2 tile_uv_wrap,
627 const float4 mask_tile_start_uv_and_size)
628 {
629 // Requires: 1.) tile_uv_wrap contains tile-relative uv coords, where the
630 // tile spans from [0, 1], such that (0.5, 0.5) is at the
631 // tile center. The input coords can range from [0, inf],
632 // and their fractional parts map to a repeated tile.
633 // ("Tile" can mean texture, the video embedded in the
634 // texture, or some other "tile" embedded in a texture.)
635 // 2.) mask_tile_start_uv_and_size.xy contains tex_uv coords
636 // for the start of the embedded tile in the full texture.
637 // 3.) mask_tile_start_uv_and_size.zw contains the [fractional]
638 // tex_uv size of the embedded tile in the full texture.
639 // Returns: Return tex_uv coords (used for texture sampling)
640 // corresponding to tile_uv_wrap.
641 if(get_mask_sample_mode() < 0.5)
642 {
643 // Manually repeat the resized mask tile to fill the screen:
644 // First get fractional tile_uv coords. Using frac/fmod on coords
645 // confuses anisotropic filtering; fix it as user options dictate.
646 // derived-settings-and-constants.h disables incompatible options.
647 #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
648 float2 tile_uv = frac(tile_uv_wrap * 0.5) * 2.0;
649 #else
650 float2 tile_uv = frac(tile_uv_wrap);
651 #endif
652 #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
653 const float2 tile_uv_dx = ddx(tile_uv);
654 const float2 tile_uv_dy = ddy(tile_uv);
655 tile_uv = fix_tiling_discontinuities_normalized(tile_uv,
656 tile_uv_dx, tile_uv_dy);
657 #endif
658 // The tile is embedded in a padded FBO, and it may start at a
659 // nonzero offset if border texels are used to avoid artifacts:
660 const float2 mask_tex_uv = mask_tile_start_uv_and_size.xy +
661 tile_uv * mask_tile_start_uv_and_size.zw;
662 return mask_tex_uv;
663 }
664 else
665 {
666 // Sample from the input phosphor mask texture with hardware tiling.
667 // If we're tiling at the original size (mode 2), the "tile" is the
668 // whole texture, and it contains a large number of triads mapped with
669 // a 1:1 pixel:texel ratio. OTHERWISE, the texture contains a single
670 // unresized tile. tile_uv_wrap already has correct coords for both!
671 return tile_uv_wrap;
672 }
673 }
674
675
676 #endif // PHOSPHOR_MASK_RESIZING_H
677
678