Debian Codegraph @

Data/Sys/Shaders/integer_scaling.glsl

raw
1 // Set aspect ratio to 'stretch'
2
3 // Integer Scaling shader by One_More_Try / TryTwo
4 // Uses Sharp Bilinear from
5 // https://github.com/libretro/slang-shaders/blob/master/interpolation/shaders/sharp-bilinear.slang
6 // by Themaister, Public Domain license
7
8 /*
9 [configuration]
10 [OptionBool]
11 GUIName = Please set aspect ratio to stretch
12 OptionName = ASPECT_MSG
13 DefaultValue = false
14
15 [OptionBool]
16 GUIName = Use non-integer width
17 OptionName = WIDTH_UNLOCK
18 DefaultValue = false
19
20 [OptionBool]
21 GUIName = Stretch width to window
22 OptionName = WIDTH_SKIP
23 DependentOption = WIDTH_UNLOCK
24 DefaultValue = false
25
26 [OptionBool]
27 GUIName = Scale width to fit 4:3
28 OptionName = WIDTH_43
29 DependentOption = WIDTH_UNLOCK
30 DefaultValue = false
31
32 [OptionBool]
33 GUIName = Scale width to fit 16:9
34 OptionName = WIDTH_169
35 DependentOption = WIDTH_UNLOCK
36 DefaultValue = false
37
38 [OptionBool]
39 GUIName = Apply sharp bilinear for custom widths
40 OptionName = SHARP_BILINEAR
41 DefaultValue = false
42
43 [OptionRangeFloat]
44 GUIName = Sharp bilinear factor (0 = auto)
45 OptionName = SHARP_PRESCALE
46 MinValue = 0.0
47 MaxValue = 16.0
48 StepAmount = 1.0
49 DefaultValue = 0.0
50
51 [OptionBool]
52 GUIName = Manual scale - Set IR first
53 OptionName = MANUALSCALE
54 DefaultValue = false
55
56 [OptionRangeFloat]
57 GUIName = Integer scale - No higher than IR
58 OptionName = INTEGER_SCALE
59 DependentOption = MANUALSCALE
60 MaxValue = 5.0
61 MinValue = 1.0
62 DefaultValue = 1.0
63 StepAmount = 1.0
64
65 [OptionRangeFloat]
66 GUIName = Scale width
67 OptionName = WIDTH_SCALE
68 DependentOption = MANUALSCALE
69 MaxValue = 5.0
70 MinValue = -2.0
71 DefaultValue = 0.0
72 StepAmount = 1.0
73
74 [OptionBool]
75 GUIName = Auto downscaling
76 OptionName = DOWNSCALE
77 DefaultValue = false
78
79 [/configuration]
80 */
81
82 void main()
83 {
84 float4 c0 = float4(0.0, 0.0, 0.0, 0.0);
85 float2 scale = float2(1.0, 1.0);
86 float2 xfb_res = GetResolution();
87 float2 win_res = GetWindowResolution();
88 float2 coords = GetCoordinates();
89
90 // ratio is used to rescale the coords to the xfb size, which allows for integer scaling.
91 // ratio can then be multiplied by an integer to upscale/downscale, but upscale isn't supported by
92 // point-sampling.
93 float2 ratio = win_res / xfb_res;
94
95 if (OptionEnabled(WIDTH_UNLOCK))
96 {
97 if (OptionEnabled(WIDTH_SKIP))
98 ratio.x = 1.0;
99 else if (OptionEnabled(WIDTH_43))
100 ratio.x = win_res.x / (xfb_res.y * 4 / 3);
101 else if (OptionEnabled(WIDTH_169))
102 ratio.x = win_res.x / (xfb_res.y * 16 / 9);
103 }
104
105 if (OptionEnabled(MANUALSCALE))
106 {
107 // There's no IR variable, so this guesses the IR, but may be off for some games.
108 float calc_ir = ceil(xfb_res.y / 500);
109 scale.y = calc_ir / GetOption(INTEGER_SCALE);
110 float manual_width = GetOption(WIDTH_SCALE);
111
112 if (manual_width < 0.0)
113 scale.x = scale.y * (abs(manual_width) + 1);
114 else
115 scale.x = scale.y / (manual_width + 1);
116
117 ratio = ratio * scale;
118 }
119 else if (OptionEnabled(DOWNSCALE) && (ratio.x < 1 || ratio.y < 1))
120 {
121 scale.x = ceil(max(1.0 / ratio.y, 1.0 / ratio.x));
122 scale.y = scale.x;
123 ratio = ratio * scale;
124 }
125
126 // y and x are used to determine black bars vs drawable space.
127 float y = win_res.y - xfb_res.y / scale.y;
128 float y_top = (y / 2.0) * GetInvWindowResolution().y;
129 float y_bottom = (win_res.y - y / 2.0) * GetInvWindowResolution().y;
130 float yloc = (coords.y - y_top) * ratio.y;
131
132 float x = win_res.x - xfb_res.x / scale.x;
133
134 if (OptionEnabled(WIDTH_UNLOCK))
135 {
136 if (OptionEnabled(WIDTH_SKIP))
137 x = 0.0;
138 else if (OptionEnabled(WIDTH_43))
139 x = win_res.x - xfb_res.y / scale.y * 4 / 3;
140 else if (OptionEnabled(WIDTH_169))
141 x = win_res.x - xfb_res.y / scale.y * 16 / 9;
142 }
143
144 float x_left = (x / 2.0) * GetInvWindowResolution().x;
145 float x_right = (win_res.x - x / 2.0) * GetInvWindowResolution().x;
146 float xloc = (coords.x - x_left) * ratio.x;
147
148 if (OptionEnabled(SHARP_BILINEAR) &&
149 (OptionEnabled(WIDTH_SKIP) || OptionEnabled(WIDTH_43) || OptionEnabled(WIDTH_169) ||
150 (OptionEnabled(MANUALSCALE) && GetOption(WIDTH_SCALE) != 0.0)))
151 {
152 float texel = xloc * xfb_res.x;
153 float texel_floored = floor(texel);
154 float s = frac(texel);
155 float scale_sharp = GetOption(SHARP_PRESCALE);
156
157 if (scale_sharp == 0)
158 {
159 if (OptionEnabled(WIDTH_43))
160 scale_sharp = (4 / 3 * xfb_res.y / xfb_res.x);
161 else if (OptionEnabled(WIDTH_169))
162 scale_sharp = (16 / 9 * xfb_res.y / xfb_res.x);
163 else
164 scale_sharp = ceil(win_res.x / xfb_res.x);
165 }
166
167 float region_range = 0.5 - 0.5 / scale_sharp;
168 float center_dist = s - 0.5;
169 float f = (center_dist - clamp(center_dist, -region_range, region_range)) * scale_sharp + 0.5;
170
171 float mod_texel = texel_floored + f;
172 xloc = mod_texel / xfb_res.x;
173 }
174
175 if (coords.x >= x_left && x_right >= coords.x && coords.y >= y_top && y_bottom >= coords.y)
176 {
177 float2 sample_loc = float2(xloc, yloc);
178 c0 = SampleLocation(sample_loc);
179 }
180
181 SetOutput(c0);
182 }