diff --git a/src/video_core/host_shaders/fxaa.frag b/src/video_core/host_shaders/fxaa.frag index 192a602c14..9f85777b90 100644 --- a/src/video_core/host_shaders/fxaa.frag +++ b/src/video_core/host_shaders/fxaa.frag @@ -7,69 +7,65 @@ #version 460 #ifdef VULKAN - #define BINDING_COLOR_TEXTURE 1 - #else // ^^^ Vulkan ^^^ // vvv OpenGL vvv - #define BINDING_COLOR_TEXTURE 0 - #endif layout (location = 0) in vec4 posPos; - layout (location = 0) out vec4 frag_color; - layout (binding = BINDING_COLOR_TEXTURE) uniform sampler2D input_texture; const float FXAA_SPAN_MAX = 8.0; const float FXAA_REDUCE_MUL = 1.0 / 8.0; const float FXAA_REDUCE_MIN = 1.0 / 128.0; -#define FxaaTexLod0(t, p) textureLod(t, p, 0.0) -#define FxaaTexOff(t, p, o) textureLodOffset(t, p, 0.0, o) - vec3 FxaaPixelShader(vec4 posPos, sampler2D tex) { - - vec3 rgbNW = FxaaTexLod0(tex, posPos.zw).xyz; - vec3 rgbNE = FxaaTexOff(tex, posPos.zw, ivec2(1,0)).xyz; - vec3 rgbSW = FxaaTexOff(tex, posPos.zw, ivec2(0,1)).xyz; - vec3 rgbSE = FxaaTexOff(tex, posPos.zw, ivec2(1,1)).xyz; - vec3 rgbM = FxaaTexLod0(tex, posPos.xy).xyz; -/*---------------------------------------------------------*/ + mat4x3 rgb_matrix = mat4x3( + textureLod(tex, posPos.zw, 0.0).xyz, + textureLodOffset(tex, posPos.zw, 0.0, ivec2(1, 0)).xyz, + textureLodOffset(tex, posPos.zw, 0.0, ivec2(0, 1)).xyz, + textureLodOffset(tex, posPos.zw, 0.0, ivec2(1, 1)).xyz + ); + vec3 rgbM = textureLod(tex, posPos.xy, 0.0).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); + // vec4(dot(m1, l), dot(m2, l), ...) => m * l + vec4 lume_per_rgb = luma * rgb_matrix; 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))); -/*---------------------------------------------------------*/ - 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)) / textureSize(tex, 0); -/*--------------------------------------------------------*/ + float lumaMin = min(lumaM, min(min(lume_per_rgb.x, lume_per_rgb.y), min(lume_per_rgb.z, lume_per_rgb.w))); + float lumaMax = max(lumaM, max(max(lume_per_rgb.x, lume_per_rgb.y), max(lume_per_rgb.z, lume_per_rgb.w))); + vec2 dir = vec2( + -((lume_per_rgb.x + lume_per_rgb.y) - (lume_per_rgb.z + lume_per_rgb.w)), + +((lume_per_rgb.x + lume_per_rgb.z) - (lume_per_rgb.y + lume_per_rgb.w)) + ); + float rcp_dir_min = 1.0 / (min(abs(dir.x), abs(dir.y)) + max( + (lume_per_rgb.x + lume_per_rgb.y + lume_per_rgb.z + lume_per_rgb.w) * (0.25 * FXAA_REDUCE_MUL), + FXAA_REDUCE_MIN + )); + dir = min( + vec2(FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX), dir * rcp_dir_min) + ); + // Constants (calculating via division is faster) + mat4x2 const_dir = mat4x2( + (1.0 / 3.0 - 0.5) / textureSize(tex, 0), + (2.0 / 3.0 - 0.5) / textureSize(tex, 0), + (0.0 / 3.0 - 0.5) / textureSize(tex, 0), + (3.0 / 3.0 - 0.5) / textureSize(tex, 0) + ); + // dir * const_dir[i] => dir * const_dir => vec4 vec3 rgbA = (1.0 / 2.0) * ( - FxaaTexLod0(tex, posPos.xy + dir * (1.0 / 3.0 - 0.5)).xyz + - FxaaTexLod0(tex, posPos.xy + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * (1.0 / 2.0) + (1.0 / 4.0) * ( - FxaaTexLod0(tex, posPos.xy + dir * (0.0 / 3.0 - 0.5)).xyz + - FxaaTexLod0(tex, posPos.xy + dir * (3.0 / 3.0 - 0.5)).xyz); + textureLod(tex, fma(dir, const_dir[0], posPos.xy), 0.0).xyz + + textureLod(tex, fma(dir, const_dir[1], posPos.xy), 0.0).xyz + ); + vec3 rgbB = fma(rgbA, vec3(1.0 / 2.0), (1.0 / 4.0) * ( + textureLod(tex, fma(dir, const_dir[2], posPos.xy), 0.0).xyz + + textureLod(tex, fma(dir, const_dir[3], posPos.xy), 0.0).xyz + )); float lumaB = dot(rgbB, luma); - if((lumaB < lumaMin) || (lumaB > lumaMax)) return rgbA; - return rgbB; + return ((lumaB < lumaMin) || (lumaB > lumaMax)) ? rgbA : rgbB; } void main() { - frag_color = vec4(FxaaPixelShader(posPos, input_texture), texture(input_texture, posPos.xy).a); + frag_color = vec4(FxaaPixelShader(posPos, input_texture), texture(input_texture, posPos.xy).a); } diff --git a/src/video_core/host_shaders/fxaa.vert b/src/video_core/host_shaders/fxaa.vert index 223ab785e0..30d43eb4fc 100644 --- a/src/video_core/host_shaders/fxaa.vert +++ b/src/video_core/host_shaders/fxaa.vert @@ -7,31 +7,35 @@ out gl_PerVertex { vec4 gl_Position; }; -const vec2 vertices[3] = - vec2[3](vec2(-1,-1), vec2(3,-1), vec2(-1, 3)); - layout (location = 0) out vec4 posPos; #ifdef VULKAN - #define BINDING_COLOR_TEXTURE 0 #define VERTEX_ID gl_VertexIndex - #else // ^^^ Vulkan ^^^ // vvv OpenGL vvv - #define BINDING_COLOR_TEXTURE 0 #define VERTEX_ID gl_VertexID - #endif layout (binding = BINDING_COLOR_TEXTURE) uniform sampler2D input_texture; const float FXAA_SUBPIX_SHIFT = 0; - void main() { - vec2 vertex = vertices[VERTEX_ID]; - gl_Position = vec4(vertex, 0.0, 1.0); - vec2 vert_tex_coord = (vertex + 1.0) / 2.0; - posPos.xy = vert_tex_coord; - posPos.zw = vert_tex_coord - (0.5 + FXAA_SUBPIX_SHIFT) / textureSize(input_texture, 0); + // 0b00 -> (-1, -1) 0b01 -> (-1, 3) 0b10 -> (3, -1) + // ((x * 4) - 1) => 3 + // :: (v + 1) / 2 => ((x * 4) + 1) / 2 + // => (x * 4 + 1) / 2 => x * (4 / 2) + 1 / 2 + // :: (v+1)/2 => v/2 + 1/2 => v*(1/2) + (1/2) + gl_Position = vec4(vec2( + float(((VERTEX_ID & 1) << 2) - 1), + float(((VERTEX_ID & 2) << 1) - 1) + ), 0.0, 1.0); + posPos = vec2( + float((VERTEX_ID & 1) << 1), + float((VERTEX_ID & 2) << 0) + ).xyxy - vec4( + 0.0, + 0.0, + (0.5 + FXAA_SUBPIX_SHIFT) / textureSize(input_texture, 0) + ); }