Implemented FxUpscale

This commit is contained in:
Joshua Barretto 2022-10-25 21:46:55 +01:00
parent 779cccd238
commit f29b1da583
8 changed files with 179 additions and 135 deletions

View File

@ -1,119 +1,4 @@
/**
Basic FXAA implementation based on the code on geeks3d.com with the
modification that the texture2DLod stuff was removed since it's
unsupported by WebGL.
--
From:
https://github.com/mitsuhiko/webgl-meincraft
Copyright (c) 2011 by Armin Ronacher.
Some rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FXAA_REDUCE_MIN
#define FXAA_REDUCE_MIN (1.0/ 128.0)
#endif
#ifndef FXAA_REDUCE_MUL
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#endif
#ifndef FXAA_SPAN_MAX
#define FXAA_SPAN_MAX 8.0
#endif
//optimized version for mobile, where dependent
//texture reads can be a bottleneck
vec4 fxaa(texture2D tex, sampler smplr, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;
mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture(sampler2D(tex, smplr), v_rgbNW).xyz;
vec3 rgbNE = texture(sampler2D(tex, smplr), v_rgbNE).xyz;
vec3 rgbSW = texture(sampler2D(tex, smplr), v_rgbSW).xyz;
vec3 rgbSE = texture(sampler2D(tex, smplr), v_rgbSE).xyz;
vec4 texColor = texture(sampler2D(tex, smplr), v_rgbM);
vec3 rgbM = texColor.xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
mediump vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;
vec3 rgbA = 0.5 * (
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * -0.5).xyz +
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * 0.5).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = vec4(rgbA, texColor.a);
else
color = vec4(rgbB, texColor.a);
return color;
}
void texcoords(vec2 fragCoord, vec2 resolution,
out vec2 v_rgbNW, out vec2 v_rgbNE,
out vec2 v_rgbSW, out vec2 v_rgbSE,
out vec2 v_rgbM) {
vec2 inverseVP = 1.0 / resolution.xy;
v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
v_rgbM = vec2(fragCoord * inverseVP);
}
#include <fxaa.glsl>
vec4 aa_apply(
texture2D tex, sampler smplr,
@ -121,20 +6,5 @@ vec4 aa_apply(
vec2 fragCoord,
vec2 resolution
) {
mediump vec2 v_rgbNW;
mediump vec2 v_rgbNE;
mediump vec2 v_rgbSW;
mediump vec2 v_rgbSE;
mediump vec2 v_rgbM;
float fxaa_scale = textureSize(sampler2D(tex, smplr), 0).x * 1.25 / resolution.x;
vec2 scaled_fc = fragCoord * fxaa_scale;
vec2 scaled_res = resolution * fxaa_scale;
//compute the texture coords
texcoords(scaled_fc, scaled_res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
//compute FXAA
return fxaa(tex, smplr, scaled_fc, scaled_res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
return fxaa_apply(tex, smplr, fragCoord, resolution);
}

View File

@ -0,0 +1,24 @@
#include <fxaa.glsl>
vec4 aa_apply(
texture2D tex, sampler smplr,
texture2D depth_tex, sampler depth_smplr,
vec2 fragCoord,
vec2 resolution
) {
vec4 aa_color = fxaa_apply(tex, smplr, fragCoord, resolution);
vec2 sz = textureSize(sampler2D(tex, smplr), 0).xy;
vec4 closest = vec4(1000);
float closest_dist = 1000.0;
ivec2 dirs[] = { ivec2(-1, 0), ivec2(1, 0), ivec2(0, -1), ivec2(0, 1), /*ivec2(-1, -1), ivec2(-1, 1), ivec2(1, -1), ivec2(1, 1)*/ };
for (uint i = 0u; i < dirs.length(); i ++) {
vec4 col_at = texelFetch(sampler2D(tex, smplr), ivec2(fragCoord / screen_res.xy * sz) + dirs[i], 0);
float dist = dot(pow(aa_color.rgb - col_at.rgb, ivec3(2)), vec3(1));
if (dist < closest_dist) {
closest = col_at;
closest_dist = dist;
}
}
return mix(aa_color, closest, clamp(1.0 - sqrt(closest_dist), 0, 1));
}

View File

@ -0,0 +1,139 @@
/**
Basic FXAA implementation based on the code on geeks3d.com with the
modification that the texture2DLod stuff was removed since it's
unsupported by WebGL.
--
From:
https://github.com/mitsuhiko/webgl-meincraft
Copyright (c) 2011 by Armin Ronacher.
Some rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FXAA_REDUCE_MIN
#define FXAA_REDUCE_MIN (1.0/ 128.0)
#endif
#ifndef FXAA_REDUCE_MUL
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#endif
#ifndef FXAA_SPAN_MAX
#define FXAA_SPAN_MAX 8.0
#endif
//optimized version for mobile, where dependent
//texture reads can be a bottleneck
vec4 fxaa(texture2D tex, sampler smplr, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;
mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture(sampler2D(tex, smplr), v_rgbNW).xyz;
vec3 rgbNE = texture(sampler2D(tex, smplr), v_rgbNE).xyz;
vec3 rgbSW = texture(sampler2D(tex, smplr), v_rgbSW).xyz;
vec3 rgbSE = texture(sampler2D(tex, smplr), v_rgbSE).xyz;
vec4 texColor = texture(sampler2D(tex, smplr), v_rgbM);
vec3 rgbM = texColor.xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
mediump vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;
vec3 rgbA = 0.5 * (
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * -0.5).xyz +
texture(sampler2D(tex, smplr), fragCoord * inverseVP + dir * 0.5).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = vec4(rgbA, texColor.a);
else
color = vec4(rgbB, texColor.a);
return color;
}
void texcoords(vec2 fragCoord, vec2 resolution,
out vec2 v_rgbNW, out vec2 v_rgbNE,
out vec2 v_rgbSW, out vec2 v_rgbSE,
out vec2 v_rgbM) {
vec2 inverseVP = 1.0 / resolution.xy;
const float scale = 0.75;
v_rgbNW = (fragCoord + vec2(-scale, -scale)) * inverseVP;
v_rgbNE = (fragCoord + vec2(scale, -scale)) * inverseVP;
v_rgbSW = (fragCoord + vec2(-scale, scale)) * inverseVP;
v_rgbSE = (fragCoord + vec2(scale, scale)) * inverseVP;
v_rgbM = vec2(fragCoord * inverseVP);
}
vec4 fxaa_apply(
texture2D tex, sampler smplr,
vec2 fragCoord,
vec2 resolution
) {
mediump vec2 v_rgbNW;
mediump vec2 v_rgbNE;
mediump vec2 v_rgbSW;
mediump vec2 v_rgbSE;
mediump vec2 v_rgbM;
float fxaa_scale = textureSize(sampler2D(tex, smplr), 0).x / resolution.x;
vec2 scaled_fc = fragCoord * fxaa_scale;
vec2 scaled_res = resolution * fxaa_scale;
//compute the texture coords
texcoords(scaled_fc, scaled_res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
//compute FXAA
return fxaa(tex, smplr, scaled_fc, scaled_res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
}

View File

@ -217,7 +217,6 @@ void main() {
vec4 aa_color = aa_apply(t_src_color, s_src_color, t_src_depth, s_src_depth, sample_uv * screen_res.xy, screen_res.xy);
#ifdef EXPERIMENTAL_SOBEL
vec3 s[8];
s[0] = aa_sample(uv, vec2(-1, 1));

View File

@ -806,6 +806,7 @@ impl<'a> Widget for Video<'a> {
/* AaMode::MsaaX4,
AaMode::MsaaX8,
AaMode::MsaaX16, */
AaMode::FxUpscale,
AaMode::Hqx,
];
let mode_label_list = [
@ -814,6 +815,7 @@ impl<'a> Widget for Video<'a> {
/* "MSAA x4",
"MSAA x8",
"MSAA x16 (experimental)", */
"FXUpscale",
"HQX",
];

View File

@ -93,12 +93,17 @@ pub enum AaMode {
/// also struggle in the future with deferred shading, so they may be
/// removed in the future.
MsaaX16,
/// Fast upscaling re-aliasing.
/// Fast edge-detecting upscaling.
///
/// Screen-space technique that attempts to reconstruct lines and edges
/// in the original image. Useless at internal resolutions higher than 1.0x,
/// but potentially very effective at much lower internal resolutions.
Hqx,
/// Fast upscaling informed by FXAA.
///
/// Screen-space technique that uses a combination of FXAA and
/// nearest-neighbour sample retargeting to produce crisp, clean upscaling.
FxUpscale,
#[serde(other)]
None,
}
@ -106,7 +111,7 @@ pub enum AaMode {
impl AaMode {
pub fn samples(&self) -> u32 {
match self {
AaMode::None | AaMode::Fxaa | AaMode::Hqx => 1,
AaMode::None | AaMode::Fxaa | AaMode::Hqx | AaMode::FxUpscale => 1,
AaMode::MsaaX4 => 4,
AaMode::MsaaX8 => 8,
AaMode::MsaaX16 => 16,

View File

@ -164,6 +164,7 @@ impl ShaderModules {
let shadows = shaders.get("include.shadows").unwrap();
let rain_occlusion = shaders.get("include.rain_occlusion").unwrap();
let point_glow = shaders.get("include.point_glow").unwrap();
let fxaa = shaders.get("include.fxaa").unwrap();
// We dynamically add extra configuration settings to the constants file.
let mut constants = format!(
@ -255,6 +256,7 @@ impl ShaderModules {
AaMode::MsaaX8 => "antialias.msaa-x8",
AaMode::MsaaX16 => "antialias.msaa-x16",
AaMode::Hqx => "antialias.hqx",
AaMode::FxUpscale => "antialias.fxupscale",
})
.unwrap();
@ -285,6 +287,7 @@ impl ShaderModules {
"anti-aliasing.glsl" => anti_alias.0.to_owned(),
"cloud.glsl" => cloud.0.to_owned(),
"point_glow.glsl" => point_glow.0.to_owned(),
"fxaa.glsl" => fxaa.0.to_owned(),
other => {
return Err(format!(
"Include {} in {} is not defined",

View File

@ -36,12 +36,14 @@ impl assets::Compound for Shaders {
"include.shadows",
"include.rain_occlusion",
"include.point_glow",
"include.fxaa",
"antialias.none",
"antialias.fxaa",
"antialias.msaa-x4",
"antialias.msaa-x8",
"antialias.msaa-x16",
"antialias.hqx",
"antialias.fxupscale",
"include.cloud.none",
"include.cloud.regular",
"figure-vert",