Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2018 Jacob Maximilian Fober
- This work is licensed under the Creative Commons
- Attribution-ShareAlike 4.0 International License.
- To view a copy of this license, visit
- http://creativecommons.org/licenses/by-sa/4.0/.
- */
- // Perfect Perspective PS ver. 2.3.3+MOD
- // Modifications by u/carlo_ren79:
- //
- // - Added supersampling (up to 8x8) to remove aliasing.
- // - Speedup by removing border blend options (always black color) and using BORDER texture addressing.
- // - Minor speedup by moving initialization of SqrTanFOVq to VS.
- ////////////////////
- /////// MENU ///////
- ////////////////////
- #ifndef ShaderAnalyzer
- uniform int FOV <
- ui_label = "Field of View";
- ui_tooltip = "Match in-game Field of View";
- ui_type = "drag";
- ui_min = 45; ui_max = 120;
- ui_category = "Distortion";
- > = 90;
- uniform float Vertical <
- ui_label = "Vertical Amount";
- ui_tooltip = "0.0 - cylindrical projection \n"
- "1.0 - spherical projection";
- ui_type = "drag";
- ui_min = 0.0; ui_max = 1.0;
- ui_category = "Distortion";
- > = 0.618;
- uniform int Type <
- ui_label = "Type of FOV";
- ui_tooltip = "If the image bulges in movement (too high FOV), change it to 'Diagonal' \n"
- "When proportions are distorted at the periphery (too low FOV), choose 'Vertical'";
- ui_type = "combo";
- ui_items = "Horizontal FOV\0Diagonal FOV\0Vertical FOV\0";
- ui_category = "Distortion";
- > = 0;
- uniform float Zooming <
- ui_label = "Border Scale";
- ui_type = "drag";
- ui_min = 0.0; ui_max = 3.0; ui_step = 0.001;
- ui_category = "Borders";
- > = 1.0;
- uniform bool Debug <
- ui_label = "Display Resolution Map";
- ui_tooltip = "Color map of the Resolution Scale \n"
- " Red - Undersampling \n"
- " Green - Supersampling \n"
- " Blue - Neutral sampling";
- ui_category = "Debug Tools";
- > = false;
- uniform float ResScale <
- ui_label = "DSR scale factor";
- ui_tooltip = "(DSR) Dynamic Super Resolution... \n"
- "Simulate application running beyond-native screen resolution";
- ui_type = "drag";
- ui_min = 1.0; ui_max = 8.0; ui_step = 0.02;
- ui_category = "Debug Tools";
- > = 1.0;
- uniform int SupersamplingChoice <
- ui_label = "Supersampling level";
- ui_category = "Quality";
- ui_type = "combo";
- ui_items = " 1x1\0 2x2\0 3x3\0 4x4\0 5x5\0 6x6\0 7x7\0 8x8\0";
- > = 0;
- #endif
- //////////////////////
- /////// SHADER ///////
- //////////////////////
- #include "ReShade.fxh"
- sampler SamplerColor
- {
- Texture = ReShade::BackBufferTex;
- SRGBTexture = true;
- AddressU = BORDER;
- AddressV = BORDER;
- };
- // Stereographic-Gnomonic lookup function by Jacob Max Fober
- // Input data:
- // FOV >> Camera Field of View in degrees
- // Coordinates >> UV coordinates (from -1, to 1), where (0,0) is at the center of the screen
- float Formula(float2 Coordinates, float SqrTanFOVq)
- {
- return (1.0 - SqrTanFOVq) / (1.0 - SqrTanFOVq * dot(Coordinates, Coordinates));
- }
- // Shader pass
- float3 PerfectPerspectivePS(float4 vois : SV_Position, float4 texcoord : TexCoord) : SV_Target
- {
- // Get Aspect Ratio
- float AspectR = BUFFER_HEIGHT * BUFFER_RCP_WIDTH;
- // Convert FOV type..
- float FovType = (Type == 1) ? sqrt(AspectR * AspectR + 1.0) : Type == 2 ? AspectR : 1.0;
- const float Supersampling = SupersamplingChoice + 1.0;
- const float dtx = BUFFER_RCP_WIDTH / Supersampling;
- const float dty = BUFFER_RCP_HEIGHT / Supersampling;
- // Sample display image
- float3 Display = float3(0.0, 0.0, 0.0);
- float2 SphCoord;
- for(float y = 0; y < Supersampling; ++y)
- {
- for(float x = 0; x < Supersampling; ++x)
- {
- float2 offset = float2(x * dtx, y * dty);
- // Convert UV to Radial Coordinates
- SphCoord = (texcoord.xy + offset) * 2.0 - 1.0;
- // Aspect Ratio correction
- SphCoord.y *= AspectR;
- // Zoom in image and adjust FOV type (pass 1 of 2)
- SphCoord *= Zooming / FovType;
- // Stereographic-Gnomonic lookup, vertical distortion amount and FOV type (pass 2 of 2)
- SphCoord *= Formula(float2(SphCoord.x, sqrt(Vertical) * SphCoord.y), texcoord.z) * FovType;
- // Aspect Ratio back to square
- SphCoord.y /= AspectR;
- // Back to UV Coordinates
- SphCoord = SphCoord * 0.5 + 0.5;
- Display += tex2D(SamplerColor, SphCoord).rgb;
- }
- }
- Display /= Supersampling * Supersampling;
- // Output type choice
- if (Debug)
- {
- // Calculate radial screen coordinates before and after perspective transformation
- float4 RadialCoord = float4(texcoord.xy, SphCoord) * 2 - 1;
- // Correct vertical aspect ratio
- RadialCoord.yw *= AspectR;
- // Define Mapping color
- float3 UnderSmpl = float3(1, 0, 0.2); // Red
- float3 SuperSmpl = float3(0, 1, 0.5); // Green
- float3 NeutralSmpl = float3(0, 0.5, 1); // Blue
- // Calculate Pixel Size difference...
- float PixelScale = fwidth( length(RadialCoord.xy) );
- // ...and simulate Dynamic Super Resolution (DSR) scalar
- PixelScale /= ResScale * fwidth( length(RadialCoord.zw) );
- PixelScale -= 1;
- // Generate supersampled-undersampled color map
- float3 ResMap = lerp(
- SuperSmpl,
- UnderSmpl,
- saturate(ceil(PixelScale))
- );
- // Create black-white gradient mask of scale-neutral pixels
- PixelScale = 1 - abs(PixelScale);
- PixelScale = saturate(PixelScale * 4 - 3); // Clamp to more representative values
- // Color neutral scale pixels
- ResMap = lerp(ResMap, NeutralSmpl, PixelScale);
- // Blend color map with display image
- Display = normalize(ResMap) * (0.8 * max( max(Display.r, Display.g), Display.b ) + 0.2);
- }
- return Display;
- }
- // Vertex shader generating a triangle covering the entire screen
- void PerfectPerspectiveVS(in uint id : SV_VertexID, out float4 position : SV_Position, out float3 texcoord : TEXCOORD)
- {
- texcoord.x = (id == 2) ? 2.0 : 0.0;
- texcoord.y = (id == 1) ? 2.0 : 0.0;
- // Convert 1/4 FOV to radians and calc tangent squared
- texcoord.z = tan(radians(float(FOV) * 0.25));
- texcoord.z *= texcoord.z;
- position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
- }
- technique PerfectPerspective
- {
- pass
- {
- VertexShader = PerfectPerspectiveVS;
- PixelShader = PerfectPerspectivePS;
- SRGBWriteEnable = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement