diff options
| author | 2021-10-20 18:36:06 -0500 | |
|---|---|---|
| committer | 2021-11-16 22:11:32 +0100 | |
| commit | 48cf3764626e1ed30450d15e00befb75a4eae329 (patch) | |
| tree | d05aa128a7b07349ceb09ebbf515682036fbea9c /src/video_core/host_shaders | |
| parent | Frontend: Add anti-aliasing method setting (diff) | |
| download | yuzu-48cf3764626e1ed30450d15e00befb75a4eae329.tar.gz yuzu-48cf3764626e1ed30450d15e00befb75a4eae329.tar.xz yuzu-48cf3764626e1ed30450d15e00befb75a4eae329.zip | |
OpenGL: Implement FXAA
Diffstat (limited to 'src/video_core/host_shaders')
| -rw-r--r-- | src/video_core/host_shaders/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/host_shaders/fxaa.frag | 72 | ||||
| -rw-r--r-- | src/video_core/host_shaders/fxaa.vert | 40 |
3 files changed, 114 insertions, 0 deletions
diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt index b0e15773c..6b5ea649a 100644 --- a/src/video_core/host_shaders/CMakeLists.txt +++ b/src/video_core/host_shaders/CMakeLists.txt | |||
| @@ -13,6 +13,8 @@ set(SHADER_FILES | |||
| 13 | convert_depth_to_float.frag | 13 | convert_depth_to_float.frag |
| 14 | convert_float_to_depth.frag | 14 | convert_float_to_depth.frag |
| 15 | full_screen_triangle.vert | 15 | full_screen_triangle.vert |
| 16 | fxaa.frag | ||
| 17 | fxaa.vert | ||
| 16 | opengl_copy_bc4.comp | 18 | opengl_copy_bc4.comp |
| 17 | opengl_present.frag | 19 | opengl_present.frag |
| 18 | opengl_present.vert | 20 | opengl_present.vert |
diff --git a/src/video_core/host_shaders/fxaa.frag b/src/video_core/host_shaders/fxaa.frag new file mode 100644 index 000000000..23f910d4c --- /dev/null +++ b/src/video_core/host_shaders/fxaa.frag | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | // Adapted from | ||
| 2 | // https://www.geeks3d.com/20110405/fxaa-fast-approximate-anti-aliasing-demo-glsl-opengl-test-radeon-geforce/3/ | ||
| 3 | |||
| 4 | #version 460 | ||
| 5 | |||
| 6 | #ifdef VULKAN | ||
| 7 | |||
| 8 | #define BINDING_COLOR_TEXTURE 1 | ||
| 9 | |||
| 10 | #else // ^^^ Vulkan ^^^ // vvv OpenGL vvv | ||
| 11 | |||
| 12 | #define BINDING_COLOR_TEXTURE 0 | ||
| 13 | |||
| 14 | #endif | ||
| 15 | |||
| 16 | layout (location = 0) in vec4 posPos; | ||
| 17 | |||
| 18 | layout (location = 0) out vec4 frag_color; | ||
| 19 | |||
| 20 | layout (binding = BINDING_COLOR_TEXTURE) uniform sampler2D input_texture; | ||
| 21 | |||
| 22 | const float FXAA_SPAN_MAX = 8.0; | ||
| 23 | const float FXAA_REDUCE_MUL = 1.0 / 8.0; | ||
| 24 | const float FXAA_REDUCE_MIN = 1.0 / 128.0; | ||
| 25 | |||
| 26 | #define FxaaTexLod0(t, p) textureLod(t, p, 0.0) | ||
| 27 | #define FxaaTexOff(t, p, o) textureLodOffset(t, p, 0.0, o) | ||
| 28 | |||
| 29 | vec3 FxaaPixelShader(vec4 posPos, sampler2D tex) { | ||
| 30 | |||
| 31 | vec3 rgbNW = FxaaTexLod0(tex, posPos.zw).xyz; | ||
| 32 | vec3 rgbNE = FxaaTexOff(tex, posPos.zw, ivec2(1,0)).xyz; | ||
| 33 | vec3 rgbSW = FxaaTexOff(tex, posPos.zw, ivec2(0,1)).xyz; | ||
| 34 | vec3 rgbSE = FxaaTexOff(tex, posPos.zw, ivec2(1,1)).xyz; | ||
| 35 | vec3 rgbM = FxaaTexLod0(tex, posPos.xy).xyz; | ||
| 36 | /*---------------------------------------------------------*/ | ||
| 37 | vec3 luma = vec3(0.299, 0.587, 0.114); | ||
| 38 | float lumaNW = dot(rgbNW, luma); | ||
| 39 | float lumaNE = dot(rgbNE, luma); | ||
| 40 | float lumaSW = dot(rgbSW, luma); | ||
| 41 | float lumaSE = dot(rgbSE, luma); | ||
| 42 | float lumaM = dot(rgbM, luma); | ||
| 43 | /*---------------------------------------------------------*/ | ||
| 44 | float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); | ||
| 45 | float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); | ||
| 46 | /*---------------------------------------------------------*/ | ||
| 47 | vec2 dir; | ||
| 48 | dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); | ||
| 49 | dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); | ||
| 50 | /*---------------------------------------------------------*/ | ||
| 51 | float dirReduce = max( | ||
| 52 | (lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), | ||
| 53 | FXAA_REDUCE_MIN); | ||
| 54 | float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce); | ||
| 55 | dir = min(vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX), | ||
| 56 | max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), | ||
| 57 | dir * rcpDirMin)) / textureSize(tex, 0); | ||
| 58 | /*--------------------------------------------------------*/ | ||
| 59 | vec3 rgbA = (1.0 / 2.0) * ( | ||
| 60 | FxaaTexLod0(tex, posPos.xy + dir * (1.0 / 3.0 - 0.5)).xyz + | ||
| 61 | FxaaTexLod0(tex, posPos.xy + dir * (2.0 / 3.0 - 0.5)).xyz); | ||
| 62 | vec3 rgbB = rgbA * (1.0 / 2.0) + (1.0 / 4.0) * ( | ||
| 63 | FxaaTexLod0(tex, posPos.xy + dir * (0.0 / 3.0 - 0.5)).xyz + | ||
| 64 | FxaaTexLod0(tex, posPos.xy + dir * (3.0 / 3.0 - 0.5)).xyz); | ||
| 65 | float lumaB = dot(rgbB, luma); | ||
| 66 | if((lumaB < lumaMin) || (lumaB > lumaMax)) return rgbA; | ||
| 67 | return rgbB; | ||
| 68 | } | ||
| 69 | |||
| 70 | void main() { | ||
| 71 | frag_color = vec4(FxaaPixelShader(posPos, input_texture), 1.0); | ||
| 72 | } | ||
diff --git a/src/video_core/host_shaders/fxaa.vert b/src/video_core/host_shaders/fxaa.vert new file mode 100644 index 000000000..715fce462 --- /dev/null +++ b/src/video_core/host_shaders/fxaa.vert | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #version 460 | ||
| 6 | |||
| 7 | out gl_PerVertex { | ||
| 8 | vec4 gl_Position; | ||
| 9 | }; | ||
| 10 | |||
| 11 | const vec2 vertices[4] = | ||
| 12 | vec2[4](vec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0), vec2(1.0, -1.0)); | ||
| 13 | |||
| 14 | layout (location = 0) out vec4 posPos; | ||
| 15 | |||
| 16 | #ifdef VULKAN | ||
| 17 | |||
| 18 | #define BINDING_COLOR_TEXTURE 1 | ||
| 19 | |||
| 20 | #else // ^^^ Vulkan ^^^ // vvv OpenGL vvv | ||
| 21 | |||
| 22 | #define BINDING_COLOR_TEXTURE 0 | ||
| 23 | |||
| 24 | #endif | ||
| 25 | |||
| 26 | layout (binding = BINDING_COLOR_TEXTURE) uniform sampler2D input_texture; | ||
| 27 | |||
| 28 | const float FXAA_SUBPIX_SHIFT = 0; | ||
| 29 | |||
| 30 | void main() { | ||
| 31 | #ifdef VULKAN | ||
| 32 | vec2 vertex = vertices[gl_VertexIndex]; | ||
| 33 | #else | ||
| 34 | vec2 vertex = vertices[gl_VertexID]; | ||
| 35 | #endif | ||
| 36 | gl_Position = vec4(vertex, 0.0, 1.0); | ||
| 37 | vec2 vert_tex_coord = (vertex + 1.0) / 2.0; | ||
| 38 | posPos.xy = vert_tex_coord; | ||
| 39 | posPos.zw = vert_tex_coord - (0.5 + FXAA_SUBPIX_SHIFT) / textureSize(input_texture, 0); | ||
| 40 | } | ||