From 9bc89915a1126f0899772426d76c79323377a9a7 Mon Sep 17 00:00:00 2001 From: KojoZero Date: Sat, 2 May 2026 12:06:58 -0700 Subject: [PATCH] changing pipeline part 3 --- src/video_core/host_shaders/CMakeLists.txt | 10 + .../antialiasing/opengl_fxaa.frag | 256 ++++++++++++++++++ .../antialiasing/opengl_fxaa.vert | 13 + .../antialiasing/opengl_smaa_pass0.frag | 31 +++ .../antialiasing/opengl_smaa_pass0.vert | 29 ++ .../antialiasing/opengl_smaa_pass1.frag | 28 ++ .../antialiasing/opengl_smaa_pass1.vert | 29 ++ .../antialiasing/opengl_smaa_pass2.frag | 28 ++ .../antialiasing/opengl_smaa_pass2.vert | 29 ++ .../host_shaders/opengl_simple_present.frag | 1 - .../renderer_opengl/gl_resource_manager.cpp | 2 +- .../renderer_opengl/renderer_opengl.cpp | 177 +++++++----- .../renderer_opengl/renderer_opengl.h | 6 + 13 files changed, 576 insertions(+), 63 deletions(-) create mode 100644 src/video_core/host_shaders/antialiasing/opengl_fxaa.frag create mode 100644 src/video_core/host_shaders/antialiasing/opengl_fxaa.vert create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.frag create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.vert create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.frag create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.vert create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.frag create mode 100644 src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.vert diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt index 964000d92..e54140153 100644 --- a/src/video_core/host_shaders/CMakeLists.txt +++ b/src/video_core/host_shaders/CMakeLists.txt @@ -13,11 +13,21 @@ set(SHADER_FILES texture_filtering/mmpx.frag texture_filtering/x_gradient.frag texture_filtering/y_gradient.frag + antialiasing/opengl_fxaa.frag + antialiasing/opengl_fxaa.vert + antialiasing/opengl_smaa_pass0.frag + antialiasing/opengl_smaa_pass0.vert + antialiasing/opengl_smaa_pass1.frag + antialiasing/opengl_smaa_pass1.vert + antialiasing/opengl_smaa_pass2.frag + antialiasing/opengl_smaa_pass2.vert full_screen_triangle.vert opengl_present.frag opengl_present.vert opengl_present_anaglyph.frag opengl_present_interlaced.frag + opengl_simple_present.frag + opengl_simple_present.vert vulkan_depth_to_buffer.comp vulkan_present.frag vulkan_present.vert diff --git a/src/video_core/host_shaders/antialiasing/opengl_fxaa.frag b/src/video_core/host_shaders/antialiasing/opengl_fxaa.frag new file mode 100644 index 000000000..ac3d736cd --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_fxaa.frag @@ -0,0 +1,256 @@ +/** + * @license + * Copyright (c) 2011 NVIDIA Corporation. All rights reserved. + * + * TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED + * *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS + * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA + * OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT, OR + * CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS + * OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY + * OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, + * EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +/* +FXAA_PRESET - Choose compile-in knob preset 0-5. +------------------------------------------------------------------------------ +FXAA_EDGE_THRESHOLD - The minimum amount of local contrast required + to apply algorithm. + 1.0/3.0 - too little + 1.0/4.0 - good start + 1.0/8.0 - applies to more edges + 1.0/16.0 - overkill +------------------------------------------------------------------------------ +FXAA_EDGE_THRESHOLD_MIN - Trims the algorithm from processing darks. + Perf optimization. + 1.0/32.0 - visible limit (smaller isn't visible) + 1.0/16.0 - good compromise + 1.0/12.0 - upper limit (seeing artifacts) +------------------------------------------------------------------------------ +FXAA_SEARCH_STEPS - Maximum number of search steps for end of span. +------------------------------------------------------------------------------ +FXAA_SEARCH_THRESHOLD - Controls when to stop searching. + 1.0/4.0 - seems to be the best quality wise +------------------------------------------------------------------------------ +FXAA_SUBPIX_TRIM - Controls sub-pixel aliasing removal. + 1.0/2.0 - low removal + 1.0/3.0 - medium removal + 1.0/4.0 - default removal + 1.0/8.0 - high removal + 0.0 - complete removal +------------------------------------------------------------------------------ +FXAA_SUBPIX_CAP - Insures fine detail is not completely removed. + This is important for the transition of sub-pixel detail, + like fences and wires. + 3.0/4.0 - default (medium amount of filtering) + 7.0/8.0 - high amount of filtering + 1.0 - no capping of sub-pixel aliasing removal +*/ +//? #version 450 + +layout(location = 0) in vec2 frag_tex_coord; +layout(location = 0) out vec4 color; +layout(binding = 0) uniform sampler2D color_texture; + +uniform vec4 i_resolution; +uniform int convert_colors; + +#ifndef FXAA_PRESET + #define FXAA_PRESET 5 +#endif +#if (FXAA_PRESET == 3) + #define FXAA_EDGE_THRESHOLD (1.0/8.0) + #define FXAA_EDGE_THRESHOLD_MIN (1.0/16.0) + #define FXAA_SEARCH_STEPS 16 + #define FXAA_SEARCH_THRESHOLD (1.0/4.0) + #define FXAA_SUBPIX_CAP (3.0/4.0) + #define FXAA_SUBPIX_TRIM (1.0/4.0) +#endif +#if (FXAA_PRESET == 4) + #define FXAA_EDGE_THRESHOLD (1.0/8.0) + #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0) + #define FXAA_SEARCH_STEPS 24 + #define FXAA_SEARCH_THRESHOLD (1.0/4.0) + #define FXAA_SUBPIX_CAP (3.0/4.0) + #define FXAA_SUBPIX_TRIM (1.0/4.0) +#endif +#if (FXAA_PRESET == 5) + #define FXAA_EDGE_THRESHOLD (1.0/8.0) + #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0) + #define FXAA_SEARCH_STEPS 32 + #define FXAA_SEARCH_THRESHOLD (1.0/4.0) + #define FXAA_SUBPIX_CAP (3.0/4.0) + #define FXAA_SUBPIX_TRIM (1.0/4.0) +#endif + +#define FXAA_SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_SUBPIX_TRIM)) + +// Return the luma, the estimation of luminance from rgb inputs. +// This approximates luma using one FMA instruction, +// skipping normalization and tossing out blue. +// FxaaLuma() will range 0.0 to 2.963210702. +float FxaaLuma(vec3 rgb) { + return rgb.y * (0.587/0.299) + rgb.x; +} + +vec3 FxaaLerp3(vec3 a, vec3 b, float amountOfA) { + return (vec3(-amountOfA) * b) + ((a * vec3(amountOfA)) + b); +} + +vec4 FxaaTexOff(sampler2D tex, vec2 pos, ivec2 off, vec2 rcpFrame) { + float x = pos.x + float(off.x) * rcpFrame.x; + float y = pos.y + float(off.y) * rcpFrame.y; + return texture(tex, vec2(x, y)); +} + +// pos is the output of FxaaVertexShader interpolated across screen. +// xy -> actual texture position {0.0 to 1.0} +// rcpFrame should be a uniform equal to {1.0/frameWidth, 1.0/frameHeight} +vec3 FxaaPixelShader(vec2 pos, sampler2D tex, vec2 rcpFrame) +{ + vec3 rgbN = FxaaTexOff(tex, pos.xy, ivec2( 0,-1), rcpFrame).xyz; + vec3 rgbW = FxaaTexOff(tex, pos.xy, ivec2(-1, 0), rcpFrame).xyz; + vec3 rgbM = FxaaTexOff(tex, pos.xy, ivec2( 0, 0), rcpFrame).xyz; + vec3 rgbE = FxaaTexOff(tex, pos.xy, ivec2( 1, 0), rcpFrame).xyz; + vec3 rgbS = FxaaTexOff(tex, pos.xy, ivec2( 0, 1), rcpFrame).xyz; + + float lumaN = FxaaLuma(rgbN); + float lumaW = FxaaLuma(rgbW); + float lumaM = FxaaLuma(rgbM); + float lumaE = FxaaLuma(rgbE); + float lumaS = FxaaLuma(rgbS); + float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE))); + float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE))); + + float range = rangeMax - rangeMin; + if(range < max(FXAA_EDGE_THRESHOLD_MIN, rangeMax * FXAA_EDGE_THRESHOLD)) + { + return rgbM; + } + + vec3 rgbL = rgbN + rgbW + rgbM + rgbE + rgbS; + + float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25; + float rangeL = abs(lumaL - lumaM); + float blendL = max(0.0, (rangeL / range) - FXAA_SUBPIX_TRIM) * FXAA_SUBPIX_TRIM_SCALE; + blendL = min(FXAA_SUBPIX_CAP, blendL); + + vec3 rgbNW = FxaaTexOff(tex, pos.xy, ivec2(-1,-1), rcpFrame).xyz; + vec3 rgbNE = FxaaTexOff(tex, pos.xy, ivec2( 1,-1), rcpFrame).xyz; + vec3 rgbSW = FxaaTexOff(tex, pos.xy, ivec2(-1, 1), rcpFrame).xyz; + vec3 rgbSE = FxaaTexOff(tex, pos.xy, ivec2( 1, 1), rcpFrame).xyz; + rgbL += (rgbNW + rgbNE + rgbSW + rgbSE); + rgbL *= vec3(1.0/9.0); + + float lumaNW = FxaaLuma(rgbNW); + float lumaNE = FxaaLuma(rgbNE); + float lumaSW = FxaaLuma(rgbSW); + float lumaSE = FxaaLuma(rgbSE); + + float edgeVert = + abs((0.25 * lumaNW) + (-0.5 * lumaN) + (0.25 * lumaNE)) + + abs((0.50 * lumaW ) + (-1.0 * lumaM) + (0.50 * lumaE )) + + abs((0.25 * lumaSW) + (-0.5 * lumaS) + (0.25 * lumaSE)); + float edgeHorz = + abs((0.25 * lumaNW) + (-0.5 * lumaW) + (0.25 * lumaSW)) + + abs((0.50 * lumaN ) + (-1.0 * lumaM) + (0.50 * lumaS )) + + abs((0.25 * lumaNE) + (-0.5 * lumaE) + (0.25 * lumaSE)); + + bool horzSpan = edgeHorz >= edgeVert; + float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x; + + if(!horzSpan) + { + lumaN = lumaW; + lumaS = lumaE; + } + + float gradientN = abs(lumaN - lumaM); + float gradientS = abs(lumaS - lumaM); + lumaN = (lumaN + lumaM) * 0.5; + lumaS = (lumaS + lumaM) * 0.5; + + if (gradientN < gradientS) + { + lumaN = lumaS; + lumaN = lumaS; + gradientN = gradientS; + lengthSign *= -1.0; + } + + vec2 posN; + posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5); + posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0); + + gradientN *= FXAA_SEARCH_THRESHOLD; + + vec2 posP = posN; + vec2 offNP = horzSpan ? vec2(rcpFrame.x, 0.0) : vec2(0.0, rcpFrame.y); + float lumaEndN = lumaN; + float lumaEndP = lumaN; + bool doneN = false; + bool doneP = false; + posN += offNP * vec2(-1.0, -1.0); + posP += offNP * vec2( 1.0, 1.0); + + for(int i = 0; i < FXAA_SEARCH_STEPS; i++) { + if(!doneN) + { + lumaEndN = FxaaLuma(texture(tex, posN.xy).xyz); + } + if(!doneP) + { + lumaEndP = FxaaLuma(texture(tex, posP.xy).xyz); + } + + doneN = doneN || (abs(lumaEndN - lumaN) >= gradientN); + doneP = doneP || (abs(lumaEndP - lumaN) >= gradientN); + + if(doneN && doneP) + { + break; + } + if(!doneN) + { + posN -= offNP; + } + if(!doneP) + { + posP += offNP; + } + } + + float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y; + float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y; + bool directionN = dstN < dstP; + lumaEndN = directionN ? lumaEndN : lumaEndP; + + if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0)) + { + lengthSign = 0.0; + } + + + float spanLength = (dstP + dstN); + dstN = directionN ? dstN : dstP; + float subPixelOffset = (0.5 + (dstN * (-1.0/spanLength))) * lengthSign; + vec3 rgbF = texture(tex, vec2( + pos.x + (horzSpan ? 0.0 : subPixelOffset), + pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz; + return FxaaLerp3(rgbL, rgbF, blendL); +} + +vec3 sRGBToLinear(vec3 c) { + return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c)); +} + +void main() +{ + vec4 pixel = vec4(FxaaPixelShader(frag_tex_coord, color_texture, vec2(i_resolution.z, i_resolution.w)), 1.0) * 1.0; + if (convert_colors == 1){ + pixel = vec4(sRGBToLinear(pixel.rgb), pixel.a); + } + color = pixel; +} diff --git a/src/video_core/host_shaders/antialiasing/opengl_fxaa.vert b/src/video_core/host_shaders/antialiasing/opengl_fxaa.vert new file mode 100644 index 000000000..793dcc580 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_fxaa.vert @@ -0,0 +1,13 @@ +//? #version 450 +layout(location = 0) in vec2 vert_position; +layout(location = 1) in vec2 vert_tex_coord; +layout(location = 0) out vec2 frag_tex_coord; + +uniform vec4 i_resolution; + +void main() +{ + gl_Position = vec4(vert_position, 0.0, 1.0); + frag_tex_coord = vert_tex_coord; +} + diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.frag b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.frag new file mode 100644 index 000000000..e722cfbd7 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.frag @@ -0,0 +1,31 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Edge Detection Shaders (First Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 +#define SMAA_PRESET_ULTRA +#define SMAA_EDT 1.0 + +layout(location = 0) in vec2 frag_tex_coord; +layout(location = 1) in vec4 offset[3]; +layout(location = 0) out vec4 color; +layout(binding = 0) uniform sampler2D color_texture; + +#define SMAA_INCLUDE_VS 0 +//#include "SMAA.hlsl" + + + + + + +void main() { + if (SMAA_EDT == 0.0) { + color = vec4(SMAALumaEdgeDetectionPS(frag_tex_coord, offset, color_texture), 0.0, 0.0); + } else if (SMAA_EDT <= 1.0) { + color = vec4(SMAAColorEdgeDetectionPS(frag_tex_coord, offset, color_texture), 0.0, 0.0); + } +} diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.vert b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.vert new file mode 100644 index 000000000..90c5816c0 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass0.vert @@ -0,0 +1,29 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Edge Detection Shaders (First Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 +#define SMAA_PRESET_ULTRA +#define SMAA_EDT 1.0 + +layout(location = 0) in vec2 vert_position; +layout(location = 1) in vec2 vert_tex_coord; +layout(location = 0) out vec2 frag_tex_coord; +layout(location = 1) out vec4 offset[3]; + +#define SMAA_INCLUDE_PS 0 +// #include "SMAA.hlsl" + + + + + + +void main() { + gl_Position = vec4(vert_position, 0.0, 1.0); + frag_tex_coord = vert_tex_coord; + SMAAEdgeDetectionVS(vert_tex_coord, offset); +} \ No newline at end of file diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.frag b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.frag new file mode 100644 index 000000000..32bb0f552 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.frag @@ -0,0 +1,28 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Blending Weight Calculation Shader (Second Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 +#define SMAA_PRESET_ULTRA +#define SMAA_EDT 1.0 + +layout(location = 0) in vec2 frag_tex_coord; +layout(location = 1) in vec2 pixcoord; +layout(location = 2) in vec4 offset[3]; +layout(location = 0) out vec4 color; +layout(binding = 0) uniform sampler2D color_texture; +uniform sampler2D areaTex; +uniform sampler2D searchTex; + +#define SMAA_INCLUDE_VS 0 +//#include "SMAA.hlsl" + + + +void main() { + vec4 subsampleIndices = vec4(0.0); + color = SMAABlendingWeightCalculationPS(frag_tex_coord, pixcoord, offset, color_texture, areaTex, searchTex, subsampleIndices); +} diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.vert b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.vert new file mode 100644 index 000000000..91e87614e --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass1.vert @@ -0,0 +1,29 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Blending Weight Calculation Shader (Second Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 +#define SMAA_PRESET_ULTRA +#define SMAA_EDT 1.0 + +layout(location = 0) in vec2 vert_position; +layout(location = 1) in vec2 vert_tex_coord; +layout(location = 0) out vec2 frag_tex_coord; +layout(location = 1) out vec2 pixcoord; +layout(location = 2) out vec4 offset[3]; + +#define SMAA_INCLUDE_PS 0 +//#include "SMAA.hlsl" + + + + + +void main() { + gl_Position = vec4(vert_position, 0.0, 1.0); + frag_tex_coord = vert_tex_coord; + SMAABlendingWeightCalculationVS(vert_tex_coord, pixcoord, offset); +} \ No newline at end of file diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.frag b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.frag new file mode 100644 index 000000000..cc1de4e06 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.frag @@ -0,0 +1,28 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Neighborhood Blending Shader (Third Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 + +layout(location = 0) in vec2 frag_tex_coord; +layout(location = 1) in vec4 offset; +layout(location = 0) out vec4 color; +layout(binding = 0) uniform sampler2D color_texture; +uniform sampler2D SMAA_Input; + +#define SMAA_INCLUDE_VS 0 +//#include "SMAA.hlsl" + + + + + + + + +void main() { + color = SMAANeighborhoodBlendingPS(frag_tex_coord, offset, SMAA_Input, color_texture); +} diff --git a/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.vert b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.vert new file mode 100644 index 000000000..dfaf4b412 --- /dev/null +++ b/src/video_core/host_shaders/antialiasing/opengl_smaa_pass2.vert @@ -0,0 +1,29 @@ +//? #version 450 +// SPDX-License-Identifier: Unlicense +//----------------------------------------------------------------------------- +// Neighborhood Blending Shader (Third Pass) + +uniform vec4 i_resolution; +#define SMAA_RT_METRICS vec4(i_resolution.z, i_resolution.w, i_resolution.x, i_resolution.y) +#define SMAA_GLSL_4 + +layout(location = 0) in vec2 vert_position; +layout(location = 1) in vec2 vert_tex_coord; +layout(location = 0) out vec2 frag_tex_coord; +layout(location = 1) out vec4 offset; + +#define SMAA_INCLUDE_PS 0 +//#include "SMAA.hlsl" + + + + + + + + +void main() { + gl_Position = vec4(vert_position, 0.0, 1.0); + frag_tex_coord = vert_tex_coord; + SMAANeighborhoodBlendingVS(vert_tex_coord, offset); +} \ No newline at end of file diff --git a/src/video_core/host_shaders/opengl_simple_present.frag b/src/video_core/host_shaders/opengl_simple_present.frag index 4e10dddde..268c7242f 100644 --- a/src/video_core/host_shaders/opengl_simple_present.frag +++ b/src/video_core/host_shaders/opengl_simple_present.frag @@ -6,7 +6,6 @@ layout(location = 0) in vec2 frag_tex_coord; layout(location = 0) out vec4 color; - layout(binding = 0) uniform sampler2D color_texture; uniform int convert_colors; diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp index def0e3274..5ca4bec76 100644 --- a/src/video_core/renderer_opengl/gl_resource_manager.cpp +++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp @@ -75,7 +75,7 @@ void OGLTexture::Allocate(GLenum target, GLsizei levels, GLenum internalformat, glTexStorage3D(target, levels, internalformat, width, height, depth); break; } - + glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index f61c0d1e7..0f3d3331c 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -9,6 +9,7 @@ #include "core/frontend/emu_window.h" #include "core/frontend/framebuffer_layout.h" #include "core/memory.h" +#include "gl_state.h" #include "video_core/pica/pica_core.h" #include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_state.h" @@ -21,7 +22,17 @@ #include "video_core/host_shaders/opengl_present_frag.h" #include "video_core/host_shaders/opengl_present_interlaced_frag.h" #include "video_core/host_shaders/opengl_present_vert.h" +#include "video_core/host_shaders/opengl_simple_present_frag.h" +#include "video_core/host_shaders/opengl_simple_present_vert.h" +#include "video_core/host_shaders/antialiasing/opengl_fxaa_frag.h" +#include "video_core/host_shaders/antialiasing/opengl_fxaa_vert.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass0_frag.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass0_vert.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass1_frag.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass1_vert.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass2_frag.h" +#include "video_core/host_shaders/antialiasing/opengl_smaa_pass2_vert.h" namespace OpenGL { MICROPROFILE_DEFINE(OpenGL_RenderFrame, "OpenGL", "Render Frame", MP_RGB(128, 128, 64)); @@ -320,7 +331,6 @@ void RendererOpenGL::LoadFBToScreenInfo(const Pica::FramebufferConfig& framebuff * Initializes the OpenGL state and creates persistent objects. */ void RendererOpenGL::InitOpenGLObjects() { - renderFramebuffer.Create(); glClearColor(Settings::values.bg_red.GetValue(), Settings::values.bg_green.GetValue(), Settings::values.bg_blue.GetValue(), 1.0f); @@ -415,6 +425,31 @@ void RendererOpenGL::ReloadShader(Settings::StereoRenderOption render_3d) { } shader.Create(HostShaders::OPENGL_PRESENT_VERT, shader_data); state.draw.shader_program = shader.handle; + + // Setup FXAA, SMAA and Simple Present Shaders + std::string FXAA_shader_data = fragment_shader_precision_OES; + FXAA_shader_data += HostShaders::OPENGL_FXAA_FRAG; + FXAA_shader.Create(HostShaders::OPENGL_FXAA_VERT, FXAA_shader_data); + + std::string SimplePresent_shader_data = fragment_shader_precision_OES; + SimplePresent_shader_data += HostShaders::OPENGL_SIMPLE_PRESENT_FRAG; + SimplePresent_shader.Create(HostShaders::OPENGL_SIMPLE_PRESENT_VERT, SimplePresent_shader_data); + + // std::string SMAA_PASS_0_shader_data = fragment_shader_precision_OES; + // SMAA_PASS_0_shader_data += HostShaders::OPENGL_SMAA_PASS0_FRAG; + // SMAA_PASS_0_shader.Create(HostShaders::OPENGL_SMAA_PASS0_VERT, SMAA_PASS_0_shader_data); + + // std::string SMAA_PASS_1_shader_data = fragment_shader_precision_OES; + // SMAA_PASS_1_shader_data += HostShaders::OPENGL_SMAA_PASS1_FRAG; + // SMAA_PASS_1_shader.Create(HostShaders::OPENGL_SMAA_PASS1_VERT, SMAA_PASS_1_shader_data); + + // std::string SMAA_PASS_2_shader_data = fragment_shader_precision_OES; + // SMAA_PASS_2_shader_data += HostShaders::OPENGL_SMAA_PASS2_FRAG; + // SMAA_PASS_2_shader.Create(HostShaders::OPENGL_SMAA_PASS2_VERT, SMAA_PASS_2_shader_data); + + + // + state.Apply(); uniform_modelview_matrix = glGetUniformLocation(shader.handle, "modelview_matrix"); uniform_color_texture = glGetUniformLocation(shader.handle, "color_texture"); @@ -432,12 +467,6 @@ void RendererOpenGL::ReloadShader(Settings::StereoRenderOption render_3d) { else glUniform1i(uniform_reverse_interlaced, 0); } - uniform_i_resolution = glGetUniformLocation(shader.handle, "i_resolution"); - uniform_o_resolution = glGetUniformLocation(shader.handle, "o_resolution"); - uniform_convert_colors = glGetUniformLocation(shader.handle, "convert_colors"); - uniform_layer = glGetUniformLocation(shader.handle, "layer"); - attrib_position = glGetAttribLocation(shader.handle, "vert_position"); - attrib_tex_coord = glGetAttribLocation(shader.handle, "vert_tex_coord"); } void RendererOpenGL::ConfigureFramebufferTexture(TextureInfo& texture, @@ -508,6 +537,15 @@ void RendererOpenGL::ConfigureFramebufferTexture(TextureInfo& texture, state.Apply(); } +void RendererOpenGL::AttachUniforms(){ + uniform_i_resolution = glGetUniformLocation(state.draw.shader_program, "i_resolution"); + uniform_o_resolution = glGetUniformLocation(state.draw.shader_program, "o_resolution"); + uniform_convert_colors = glGetUniformLocation(state.draw.shader_program, "convert_colors"); + uniform_layer = glGetUniformLocation(state.draw.shader_program, "layer"); + attrib_position = glGetAttribLocation(state.draw.shader_program, "vert_position"); + attrib_tex_coord = glGetAttribLocation(state.draw.shader_program, "vert_tex_coord"); +} + /** * Draws a single texture to the emulator window, rotating the texture to correct for the 3DS's LCD * rotation. @@ -517,33 +555,24 @@ void RendererOpenGL::DrawSingleScreen(const ScreenInfo& screen_info, float scree const auto& texcoords = screen_info.display_texcoords; const u32 scale_factor = GetResolutionScaleFactor(); - // Texture Widthn and Height when correctly rotated to landscape + // Texture Width and Height when correctly rotated to landscape float textureWidth, textureHeight; textureWidth = static_cast(screen_info.texture.height * scale_factor); textureHeight = static_cast(screen_info.texture.width * scale_factor); - /* - Current Attempt Rendering - Pass 1: Rotate Texture to Landscape and convert to linear - Pass 2: Rotate Texture to orientation and convert to srgb - - Final Result Attempt - Pass 1: Rotate Texture to Landscape and convert to linear - ---Anti-aliasing pass - SMAA Pass 1 - Smaa Pass 2 - SMAA Pass 3 - or - FXAA Pass - --- - - Final Pass: Rotate Texture to Final orientation and convert to srgb (the present shader) - - */ - + bool isDownsampling = false; + if (orientation == Layout::DisplayOrientation::Landscape || orientation == Layout::DisplayOrientation::LandscapeFlipped) { + if (textureWidth > screenWidth){ + isDownsampling = true; + } + } else { + if (textureWidth > screenHeight){ + isDownsampling = true; + } + } // Rotate Internal Texture to Landscape (The 3DS stores images rotated 90° Counter Clockkwise internally) - std::array landscape_rotation_vertices; - landscape_rotation_vertices = {{ + std::array rotate_vertices; + rotate_vertices = {{ ScreenRectVertex(-1.f, 1.f, 1.f, 0.f), //Left, Top ScreenRectVertex(1.f, 1.f, 1.f, 1.f), //Right, Top ScreenRectVertex(-1.f, -1.f, 0.f, 0.f), //Left, Bottom @@ -611,56 +640,82 @@ void RendererOpenGL::DrawSingleScreen(const ScreenInfo& screen_info, float scree state.texture_units[0].sampler = sampler; state.Apply(); + /* TODO: - Implement Gamma Corrected Linear Filtering - use Simple Present Shader with landscape_rotation_vertices and convert_colors set to 1 - use Regular Present Shader with output_vertices and convert_colors set to 2 +-------Rewrite + General Idea: + Always rotate to landscape in a pass + Before Bilinear Scaling convert to linear in a pass + Before Presentation convert to sRGB + + The FXAA and SMAA shaders expect pass through vertices + + Implement Gamma Corrected Bilinear: + Rotate Texture and Convert to Linear + Bilinear downscale to Screen width/height and convert to srgb + Implement SMAA: + if (isDownsampling): + Rotate Texture and Convert to Linear + Bilinear downscale to Screen width/height + Run SMAA pass 1 + Run SMAA pass 2 + Run SMAA pass 3 + Convert to srgb and output + else: + Rotate Texture and Convert to Linear + Run SMAA pass 1 + Run SMAA pass 2 + Run SMAA pass 3 + Bilinear upscale to Screen width/height and convert to srgb Implement FXAA: - use Simple Present Shader with landscape_rotation_vertices and convert_colors set to 0 - use FXAA shader with pass_through_vertices - use Simple Present Shader with pass_through_vertices and convert colors set to 1 - use Regular Present Shader with output_vertices and convert_colors set to 2 - - Implement SMAA: - use Simple Present Shader with landscape_rotation_vertices and convert_colors set to 1 - use SMAA pass 1 with pass_through_vertices - use SMAA pass 2 with pass_through_vertices - use SMAA pass 3 with pass_through_vertices - use Regular Present Shader with output_vertices and convert_colors set to 2 + if (isDownsampling): + Rotate Texture and Convert to Linear + Bilinear downscale to Screen width/height and convert to srgb + Run FXAA pass + Output + else: + Rotate Texture + Run FXAA pass and convert to Linear + Bilinear upscale to Screen width/height and convert to srgb */ - GLuint old_read_fb = state.draw.read_framebuffer; - GLuint old_draw_fb = state.draw.draw_framebuffer; + GLuint originalReadFramebuffer = state.draw.read_framebuffer; + GLuint originalDrawFramebuffer = state.draw.draw_framebuffer; - // Draw to texture - OGLFramebuffer initFramebuffer; - initFramebuffer.Create(); - state.draw.draw_framebuffer = initFramebuffer.handle; - state.draw.read_framebuffer = initFramebuffer.handle; - OGLTexture initFramebufferTexture; - initFramebufferTexture.Create(); - initFramebufferTexture.Allocate(GL_TEXTURE_2D, 1, GL_RGBA8, screen_info.texture.height*scale_factor, screen_info.texture.width*scale_factor); - state.texture_units[1].texture_2d = initFramebufferTexture.handle; - state.texture_units[1].sampler = samplers[1].handle; + + // Create and bind Framebuffer + OGLFramebuffer postFBO; + postFBO.Create(); + state.draw.read_framebuffer = postFBO.handle; + state.draw.draw_framebuffer = postFBO.handle; state.Apply(); - glActiveTexture(GL_TEXTURE1); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, initFramebufferTexture.handle, 0); - + // Create Framebuffer Texture and bind to Framebuffer + OGLTexture postFBOTexture; + postFBOTexture.Create(); + postFBOTexture.Allocate(GL_TEXTURE_2D, 1, GL_RGBA8, textureWidth, textureHeight); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, postFBOTexture.handle, 0); + // Use the simple present shader handle + state.draw.shader_program = SimplePresent_shader.handle; + AttachUniforms(); + + state.Apply(); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(rotate_vertices), output_vertices.data()); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Draw to screen - state.draw.read_framebuffer = old_read_fb; - state.draw.draw_framebuffer = old_draw_fb; + state.draw.read_framebuffer = originalReadFramebuffer; + state.draw.draw_framebuffer = originalDrawFramebuffer; state.Apply(); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(output_vertices), output_vertices.data()); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); //Reset - initFramebuffer.Release(); + postFBO.Release(); state.texture_units[0].texture_2d = 0; state.texture_units[0].sampler = 0; state.Apply(); diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index 9ac9268dc..ba7172dbf 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -80,6 +80,7 @@ private: // Loads framebuffer from emulated memory into the display information structure void LoadFBToScreenInfo(const Pica::FramebufferConfig& framebuffer, ScreenInfo& screen_info, bool right_eye, const Pica::ColorFill& color_fill); + void AttachUniforms(); private: Pica::PicaCore& pica; @@ -91,6 +92,11 @@ private: OGLVertexArray vertex_array; OGLBuffer vertex_buffer; OGLProgram shader; + OGLProgram SimplePresent_shader; + OGLProgram FXAA_shader; + OGLProgram SMAA_PASS_0_shader; + OGLProgram SMAA_PASS_1_shader; + OGLProgram SMAA_PASS_2_shader; OGLFramebuffer screenshot_framebuffer; std::array samplers;