Repository navigation
Expand file tree
/
Copy pathAspectRatioSuite.fx
More file actions
304 lines (248 loc) · 6.54 KB
/
Copy pathAspectRatioSuite.fx
File metadata and controls
304 lines (248 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// #region Includes
#include "ReShade.fxh"
#include "ReShadeUI.fxh"
// #endregion
// #region Macros
#ifndef ASPECT_RATIO_SUITE_VALUES
#define ASPECT_RATIO_SUITE_VALUES 9/16., 1, 4/3., 16/9., 64/27., 32/9.
#endif
// #endregion
// #region Data Types
struct Mask
{
float value, is_horizontal;
};
struct Rect
{
float2 pos, size;
};
Rect Rect_new(float4 value)
{
Rect r;
r.pos = value.xy;
r.size = value.zw;
return r;
}
Rect Rect_new(float2 pos, float2 size)
{
return Rect_new(float4(pos, size));
}
Rect Rect_new(float x, float y, float width, float height)
{
return Rect_new(float4(x, y, width, height));
}
float4 Rect_values(Rect r)
{
return float4(r.pos, r.size);
}
float Rect_contains(Rect r, float2 p)
{
return
step(r.pos.x, p.x) * step(r.pos.y, p.y) *
step(p.x, r.pos.x + r.size.x) * step(p.y, r.pos.y + r.size.y);
}
// #endregion
// #region Constants
static const float ASPECT_RATIOS[] = {ASPECT_RATIO_SUITE_VALUES, 0.0};
// #endregion
// #region Uniforms
uniform int Mode
<
__UNIFORM_RADIO_INT1
ui_text =
"Set the mode you want to use and it's appropriate parameters:\n"
" - Letterbox: Displays bars on the image to 'correct' aspect ratio.\n"
" - Test Box: Displays a box in the center of the screen representing "
"the selected aspect ratio.\n"
" - Multi-Test Box: Same as Test Box but shows multiple boxes from "
"aspect ratios defined in the ASPECT_RATIO_SUITE_VALUES macro.\n"
"\nThe macro for Multi-Test Box mode is defined as a comma-separated "
"list of aspect ratios, which can be defined as the division of two "
"decimal numbers.\n"
"\nFor example, '4.0 / 3.0, 16.0 / 9.0, 1.0', would tell the effect "
"to display boxes for 4:3, 16:9 and 1:1 aspect ratios.\n"
"\nFor short-handing, you could also define the same values as "
"'4/3., 16/9., 1', since you only need one of the numbers in the "
"division to be decimal and it's decimal places can be omitted.\n"
"\nRemember you can also pass the mouse cursor over a parameter's name "
"to see a more detailed description and it's default value.\n"
"\n";
ui_label = "Mode";
ui_tooltip = "Default: Letterbox";
ui_items = "Letterbox\0Test Box\0Multi-Test Box\0";
> = 0;
uniform float2 AspectRatio
<
__UNIFORM_DRAG_FLOAT2
ui_label = "Aspect Ratio";
ui_tooltip =
"The aspect ratio to calculate the border/box from.\n"
"Unused for Multi-Test mode, which uses the "
"ASPECT_RATIO_SUITE_VALUES macro.\n"
"\nDefault: 4.0 3.0";
ui_min = 1;
ui_max = 100;
ui_step = 0.01;
> = float2(4.0, 3.0);
uniform float4 LetterBoxColor
<
__UNIFORM_COLOR_FLOAT4
ui_label = "Letterbox Color";
ui_tooltip =
"Color of the bars that appear in Letterbox mode.\n"
"\nDefault: 0 0 0 255";
> = float4(0.0, 0.0, 0.0, 1.0);
uniform float4 BackgroundColor
<
__UNIFORM_COLOR_FLOAT4
ui_label = "Background Color";
ui_tooltip =
"Background color applied in either test mode.\n"
"To disable, simply set the alpha value to zero.\n"
"\nDefault: 0 0 0 0";
> = float4(0.0, 0.0, 0.0, 0.0);
uniform float TestBoxSize
<
__UNIFORM_SLIDER_FLOAT1
ui_label = "Test Box Size";
ui_tooltip =
"Size of the box(es) that appear in either test mode.\n"
"1.0: full screen size\n"
"0.5: half screen size\n"
"0.0: hidden\n"
"\nDefault: 0.5";
ui_min = 0.0;
ui_max = 1.0;
> = 1.0;
uniform float4 TestBoxColor
<
__UNIFORM_COLOR_FLOAT4
ui_label = "Test Box Color";
ui_tooltip =
"Color of the Test Box mode's aspect ratio box.\n"
"The alpha value is used for transparency in Multi-Test Box mode.\n"
"\nDefault: 255 0 0 255";
> = float4(1.0, 0.0, 1.0, 1.0);
// #endregion
// #region Functions
/*
Scales a coordinate while keeping it anchored to a pivot.
*/
float2 scale_uv(float2 uv, float2 scale, float2 pivot)
{
return (uv - pivot) * scale + pivot;
}
/*
Convert an HSV color to RGB.
*/
float3 hsv_to_rgb(float3 hsv)
{
float4 k = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(hsv.xxx + k.xyz) * 6.0 - k.www);
return hsv.z * lerp(k.xxx, saturate(p - k.xxx), hsv.y);
}
/*
Check if x is between a and b.
*/
float inside(float x, float a, float b)
{
return step(a, x) * step(x, b);
}
/*
Get a mask based on the difference between the screen aspect ratio and
another one.
*/
Mask calc_mask(float ar)
{
Mask mask;
mask.is_horizontal = step(ReShade::AspectRatio, ar);
float ratio = lerp(
ar / ReShade::AspectRatio,
ReShade::AspectRatio / ar,
mask.is_horizontal
);
mask.value = (1.0 - ratio) * 0.5;
return mask;
}
/*
Calculate a border/letterbox from a mask calculated from calc_mask().
Returns whether or not the current coordinate is outside the borders.
*/
float calc_border(float2 uv, Mask mask)
{
// The uv component to use (mask.y tells whether the mask is vertical or
// horizontal).
float pos = lerp(uv.x, uv.y, mask.is_horizontal);
return step(mask.value, pos) * step(pos, 1.0 - mask.value);
}
/*
Calculate a box from the aspect ratio mask from calc_mask().
Returns whether the uv coordinate is within the box's outline.
*/
float calc_box(float2 uv, Mask m, float scale)
{
float4 uv_ps = float4(uv, ReShade::PixelSize);
uv_ps = lerp(uv_ps, uv_ps.yxwz, m.is_horizontal);
m.value = lerp(0.5, m.value, scale);
Rect r = Rect_new(
m.value,
0.5 - scale * 0.5,
1.0 - (2.0 * m.value),
scale
);
return
Rect_contains(r, uv_ps.xy) !=
Rect_contains(
Rect_new(r.pos + uv_ps.zw, r.size - uv_ps.zw * 2.0),
uv_ps.xy
);
}
// #endregion
// #region Effect
float4 MainPS(float4 p : SV_POSITION, float2 uv : TEXCOORD) : SV_TARGET
{
float4 color = tex2D(ReShade::BackBuffer, uv);
switch (Mode)
{
case 0: // Border
{
Mask mask = calc_mask(AspectRatio.x / AspectRatio.y);
float border = calc_border(uv, mask);
color.rgb = lerp(
LetterBoxColor.rgb,
color.rgb,
lerp(1.0, border, LetterBoxColor.a)
);
} break;
case 1: // Test Box
{
color.rgb = lerp(color.rgb, BackgroundColor.rgb, BackgroundColor.a);
Mask mask = calc_mask(AspectRatio.x / AspectRatio.y);
float box = calc_box(uv, mask, TestBoxSize);
box *= TestBoxColor.a;
color.rgb = lerp(color.rgb, TestBoxColor.rgb, box);
} break;
case 2: // Multi-Test
{
color.rgb = lerp(color.rgb, BackgroundColor.rgb, BackgroundColor.a);
for (int i = 0; ASPECT_RATIOS[i] != 0.0; ++i)
{
float3 ratio_color = hsv_to_rgb(float3(i / 10.0, 1.0, 1.0));
Mask mask = calc_mask(ASPECT_RATIOS[i]);
float box = calc_box(uv, mask, TestBoxSize);
box *= TestBoxColor.a;
color.rgb = lerp(color.rgb, ratio_color, box);
}
} break;
}
return color;
}
technique AspectRatioSuite
{
pass
{
VertexShader = PostProcessVS;
PixelShader = MainPS;
}
}
// #endregion