aboutsummaryrefslogtreecommitdiff
path: root/src/video_core/host_shaders
diff options
context:
space:
mode:
authorJPikachu <jpikachu.eden@gmail.com>2025-06-21 13:35:04 +0000
committerJPikachu <jpikachu@noreply.localhost>2025-06-21 13:35:04 +0000
commit2946cdbd2d4ab802e80ab7daa6cd3004067ae848 (patch)
treee480667338c74958975d1ddbc342eee4e74d566d /src/video_core/host_shaders
parent8c33b0bb5df5b755a73434ae74a15edc9ceb4c82 (diff)
renderer: add area sampling scaling method (#201)
Adds Area Sampling to the list of scaling options. Works well to achieve a high-quality, smooth super-sampling effect. Dolphin has had this for a while and so has Ryujinx, so lui decided to port it. Adapted from these two PRs: https://github.com/Ryujinx/Ryujinx/pull/7304 https://github.com/dolphin-emu/dolphin/pull/11999 Credit: Torzu, lui Reviewed-on: http://vub63vv26q6v27xzv2dtcd25xumubshogm67yrpaz2rculqxs7jlfqad.onion/torzu-emu/torzu/pulls/57 Co-authored-by: lui <lui@vub63vv26q6v27xzv2dtcd25xumubshogm67yrpaz2rculqxs7jlfqad.onion> Co-committed-by: lui <lui@vub63vv26q6v27xzv2dtcd25xumubshogm67yrpaz2rculqxs7jlfqad.onion> Co-authored-by: lui <lui@vub63vv26q6v27xzv2dtcd25xumubshogm67yrpaz2rculqxs7jlfqad.onion> Co-authored-by: crueter <swurl@swurl.xyz> Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/201 Co-authored-by: JPikachu <jpikachu.eden@gmail.com> Co-committed-by: JPikachu <jpikachu.eden@gmail.com>
Diffstat (limited to 'src/video_core/host_shaders')
-rw-r--r--src/video_core/host_shaders/CMakeLists.txt1
-rw-r--r--src/video_core/host_shaders/present_area.frag107
2 files changed, 108 insertions, 0 deletions
diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt
index fe3e9e982c..688e10d2e4 100644
--- a/src/video_core/host_shaders/CMakeLists.txt
+++ b/src/video_core/host_shaders/CMakeLists.txt
@@ -42,6 +42,7 @@ set(SHADER_FILES
opengl_present_scaleforce.frag
opengl_smaa.glsl
pitch_unswizzle.comp
+ present_area.frag
present_bicubic.frag
present_gaussian.frag
queries_prefix_scan_sum.comp
diff --git a/src/video_core/host_shaders/present_area.frag b/src/video_core/host_shaders/present_area.frag
new file mode 100644
index 0000000000..41f5bc52fc
--- /dev/null
+++ b/src/video_core/host_shaders/present_area.frag
@@ -0,0 +1,107 @@
+#version 460 core
+
+layout(location = 0) in vec2 frag_tex_coord;
+layout(location = 0) out vec4 color;
+layout(binding = 0) uniform sampler2D color_texture;
+
+#ifdef VULKAN
+
+struct ScreenRectVertex {
+ vec2 position;
+ vec2 tex_coord;
+};
+layout (push_constant) uniform PushConstants {
+ mat4 modelview_matrix;
+ ScreenRectVertex vertices[4];
+};
+
+#else // OpenGL
+
+layout(location = 1) uniform uvec2 screen_size;
+
+#endif
+
+/***** Area Sampling *****/
+
+// By Sam Belliveau and Filippo Tarpini. Public Domain license.
+// Effectively a more accurate sharp bilinear filter when upscaling,
+// that also works as a mathematically perfect downscale filter.
+// https://entropymine.com/imageworsener/pixelmixing/
+// https://github.com/obsproject/obs-studio/pull/1715
+// https://legacy.imagemagick.org/Usage/filter/
+vec4 AreaSampling(sampler2D textureSampler, vec2 texCoords, vec2 source_size, vec2 target_size) {
+ // Determine the sizes of the source and target images.
+ vec2 inverted_target_size = vec2(1.0) / target_size;
+
+ // Determine the range of the source image that the target pixel will cover.
+ vec2 range = source_size * inverted_target_size;
+ vec2 beg = (texCoords.xy * source_size) - (range * 0.5);
+ vec2 end = beg + range;
+
+ // Compute the top-left and bottom-right corners of the pixel box.
+ ivec2 f_beg = ivec2(floor(beg));
+ ivec2 f_end = ivec2(floor(end));
+
+ // Compute how much of the start and end pixels are covered horizontally & vertically.
+ float area_w = 1.0 - fract(beg.x);
+ float area_n = 1.0 - fract(beg.y);
+ float area_e = fract(end.x);
+ float area_s = fract(end.y);
+
+ // Compute the areas of the corner pixels in the pixel box.
+ float area_nw = area_n * area_w;
+ float area_ne = area_n * area_e;
+ float area_sw = area_s * area_w;
+ float area_se = area_s * area_e;
+
+ // Initialize the color accumulator.
+ vec4 avg_color = vec4(0.0, 0.0, 0.0, 0.0);
+
+ // Accumulate corner pixels.
+ avg_color += area_nw * texelFetch(textureSampler, ivec2(f_beg.x, f_beg.y), 0);
+ avg_color += area_ne * texelFetch(textureSampler, ivec2(f_end.x, f_beg.y), 0);
+ avg_color += area_sw * texelFetch(textureSampler, ivec2(f_beg.x, f_end.y), 0);
+ avg_color += area_se * texelFetch(textureSampler, ivec2(f_end.x, f_end.y), 0);
+
+ // Determine the size of the pixel box.
+ int x_range = int(f_end.x - f_beg.x - 0.5);
+ int y_range = int(f_end.y - f_beg.y - 0.5);
+
+ // Accumulate top and bottom edge pixels.
+ for (int x = f_beg.x + 1; x <= f_beg.x + x_range; ++x) {
+ avg_color += area_n * texelFetch(textureSampler, ivec2(x, f_beg.y), 0);
+ avg_color += area_s * texelFetch(textureSampler, ivec2(x, f_end.y), 0);
+ }
+
+ // Accumulate left and right edge pixels and all the pixels in between.
+ for (int y = f_beg.y + 1; y <= f_beg.y + y_range; ++y) {
+ avg_color += area_w * texelFetch(textureSampler, ivec2(f_beg.x, y), 0);
+ avg_color += area_e * texelFetch(textureSampler, ivec2(f_end.x, y), 0);
+
+ for (int x = f_beg.x + 1; x <= f_beg.x + x_range; ++x) {
+ avg_color += texelFetch(textureSampler, ivec2(x, y), 0);
+ }
+ }
+
+ // Compute the area of the pixel box that was sampled.
+ float area_corners = area_nw + area_ne + area_sw + area_se;
+ float area_edges = float(x_range) * (area_n + area_s) + float(y_range) * (area_w + area_e);
+ float area_center = float(x_range) * float(y_range);
+
+ // Return the normalized average color.
+ return avg_color / (area_corners + area_edges + area_center);
+}
+
+void main() {
+ vec2 source_image_size = textureSize(color_texture, 0);
+ vec2 window_size;
+
+ #ifdef VULKAN
+ window_size.x = vertices[1].position.x - vertices[0].position.x;
+ window_size.y = vertices[2].position.y - vertices[0].position.y;
+ #else // OpenGL
+ window_size = screen_size;
+ #endif
+
+ color = AreaSampling(color_texture, frag_tex_coord, source_image_size, window_size);
+}