New Contrast Adaptive Sharpening Shader In Reshade
Results 1 to 6 of 6

Thread: New Contrast Adaptive Sharpening Shader In Reshade

  1. #1

    New Contrast Adaptive Sharpening Shader In Reshade

    I've been using CAS in Reshade for a year now and have been pretty happy with the sharpening it provides without halos, or a big FPS hit. The original CAS shader was done with basically the same code that AMD had been using for sharpening, and Nvidia was also starting to add into their driver at the time. I recently noticed that a refinement of the CAS shader that provided more control over fine details was available, and I like it a little better than the previous version.

    Stock AnKor's Shaders on the left, and AnKor's with the New CAS shader in Reshade on the right.


    I think it makes the details a bit more distinct without looking too artificial.
    US Army, Major, Ret.

    Service To The Line,
    On The Line,
    On Time

    US Army Ordnance Corps.

  2. #2
    Here's the new code that you can paste into a text file I called NewCAS.fx.

    // LICENSE
    // =======
    // Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
    // -------
    // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
    // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
    // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
    // Software is furnished to do so, subject to the following conditions:
    // -------
    // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
    // Software.
    // -------
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE


    //Initial port to ReShade: SLSNe https://gist.github.com/SLSNe/bbaf2d...55df581b3cf00c
    //Optimizations by Marty McFly:
    // vectorized math, even with scalar gcn hardware this should work
    // out the same, order of operations has not changed
    // For some reason, it went from 64 to 48 instructions, a lot of MOV gone
    // Also modified the way the final window is calculated
    //
    // reordered min() and max() operations, from 11 down to 9 registers
    //
    // restructured final weighting, 49 -> 48 instructions
    //
    // delayed RCP to replace SQRT with RSQRT
    //
    // removed the saturate() from the control var as it is clamped
    // by UI manager already, 48 -> 47 instructions
    //
    // replaced tex2D with tex2Doffset intrinsic (address offset by immediate integer)
    // 47 -> 43 instructions
    // 9 -> 8 registers
    //Further modified by OopyDoopy and Lord of Lunacy:
    // Changed wording in the UI for the existing variable and added a new variable and relevant code to adjust sharpening strength.
    //Fix by Lord of Lunacy:
    // Made the shader use a linear colorspace rather than sRGB, as recommended by the original AMD documentation from FidelityFX.


    uniform float Contrast <
    ui_type = "drag";
    ui_label = "Contrast Adaptation";
    ui_tooltip = "Adjusts the range the shader adapts to high contrast (0 is not all the way off). Higher values = more high contrast sharpening.";
    ui_min = 0.0; ui_max = 1.0;
    > = 0.0;


    uniform float Sharpening <
    ui_type = "drag";
    ui_label = "Sharpening intensity";
    ui_tooltip = "Adjusts sharpening intensity by averaging the original pixels to the sharpened result. 1.0 is the unmodified default.";
    ui_min = 0.0; ui_max = 1.0;
    > = 1.0;


    #include "ReShade.fxh"
    texture TexColor : COLOR;
    sampler sTexColor {Texture = TexColor; SRGBTexture = true;};


    float3 CASPass(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
    {
    // fetch a 3x3 neighborhood around the pixel 'e',
    // a b c
    // d(e)f
    // g h i
    float3 a = tex2Doffset(sTexColor, texcoord, int2(-1, -1)).rgb;
    float3 b = tex2Doffset(sTexColor, texcoord, int2(0, -1)).rgb;
    float3 c = tex2Doffset(sTexColor, texcoord, int2(1, -1)).rgb;
    float3 d = tex2Doffset(sTexColor, texcoord, int2(-1, 0)).rgb;
    float3 e = tex2Doffset(sTexColor, texcoord, int2(0, 0)).rgb;
    float3 f = tex2Doffset(sTexColor, texcoord, int2(1, 0)).rgb;
    float3 g = tex2Doffset(sTexColor, texcoord, int2(-1, 1)).rgb;
    float3 h = tex2Doffset(sTexColor, texcoord, int2(0, 1)).rgb;
    float3 i = tex2Doffset(sTexColor, texcoord, int2(1, 1)).rgb;

    // Soft min and max.
    // a b c b
    // d e f * 0.5 + d e f * 0.5
    // g h i h
    // These are 2.0x bigger (factored out the extra multiply).
    float3 mnRGB = min(min(min(d, e), min(f, b)), h);
    float3 mnRGB2 = min(mnRGB, min(min(a, c), min(g, i)));
    mnRGB += mnRGB2;


    float3 mxRGB = max(max(max(d, e), max(f, b)), h);
    float3 mxRGB2 = max(mxRGB, max(max(a, c), max(g, i)));
    mxRGB += mxRGB2;


    // Smooth minimum distance to signal limit divided by smooth max.
    float3 rcpMRGB = rcp(mxRGB);
    float3 ampRGB = saturate(min(mnRGB, 2.0 - mxRGB) * rcpMRGB);

    // Shaping amount of sharpening.
    ampRGB = rsqrt(ampRGB);

    float peak = 8.0 - 3.0 * Contrast;
    float3 wRGB = -rcp(ampRGB * peak);


    float3 rcpWeightRGB = rcp(1.0 + 4.0 * wRGB);


    // 0 w 0
    // Filter shape: w 1 w
    // 0 w 0
    float3 window = (b + d) + (f + h);
    float3 outColor = saturate((window * wRGB + e) * rcpWeightRGB);
    return lerp(e, outColor, Sharpening);
    }


    technique NewCAS
    {
    pass
    {
    VertexShader = PostProcessVS;
    PixelShader = CASPass;
    SRGBWriteEnable = true;
    }
    }
    US Army, Major, Ret.

    Service To The Line,
    On The Line,
    On Time

    US Army Ordnance Corps.

  3. #3
    Reshade calls the shader in game through a file I named NewCAS.ini

    PreprocessorDefinitions=
    Techniques=Levels,NewCAS
    TechniqueSorting=AdaptiveSharpen,AmbientLight,Clar ity,Curves,DPX,HDR,HighPassSharp,Levels,LiftGammaG ain,LumaSharpen,Tonemap,Vibrance,LevelsPlus,CAS,Co ntrastAdaptiveSharpen,LocalContrast,Clarity2,NewCA S


    [Levels.fx]
    BlackPoint=10
    HighlightClipping=0
    WhitePoint=245


    [NewCAS.fx]
    Contrast=1.000000
    Sharpening=1.000000
    US Army, Major, Ret.

    Service To The Line,
    On The Line,
    On Time

    US Army Ordnance Corps.

  4. #4
    The code for Levels.fx is

    /**
    * Levels version 1.2
    * by Christian Cann Schuldt Jensen ~ CeeJay.dk
    *
    * Allows you to set a new black and a white level.
    * This increases contrast, but clips any colors outside the new range to either black or white
    * and so some details in the shadows or highlights can be lost.
    *
    * The shader is very useful for expanding the 16-235 TV range to 0-255 PC range.
    * You might need it if you're playing a game meant to display on a TV with an emulator that does not do this.
    * But it's also a quick and easy way to uniformly increase the contrast of an image.
    *
    * -- Version 1.0 --
    * First release
    * -- Version 1.1 --
    * Optimized to only use 1 instruction (down from 2 - a 100% performance increase )
    * -- Version 1.2 --
    * Added the ability to highlight clipping regions of the image with #define HighlightClipping 1
    */


    #include "ReShadeUI.fxh"


    uniform int BlackPoint < __UNIFORM_SLIDER_INT1
    ui_min = 0; ui_max = 255;
    ui_label = "Black Point";
    ui_tooltip = "The black point is the new black - literally. Everything darker than this will become completely black.";
    > = 16;


    uniform int WhitePoint < __UNIFORM_SLIDER_INT1
    ui_min = 0; ui_max = 255;
    ui_label = "White Point";
    ui_tooltip = "The new white point. Everything brighter than this becomes completely white";
    > = 235;


    uniform bool HighlightClipping <
    ui_label = "Highlight clipping pixels";
    ui_tooltip = "Colors between the two points will stretched, which increases contrast, but details above and below the points are lost (this is called clipping).\n"
    "This setting marks the pixels that clip.\n"
    "Red: Some detail is lost in the highlights\n"
    "Yellow: All detail is lost in the highlights\n"
    "Blue: Some detail is lost in the shadows\n"
    "Cyan: All detail is lost in the shadows.";
    > = false;


    #include "ReShade.fxh"


    float3 LevelsPass(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
    {
    float black_point_float = BlackPoint / 255.0;
    float white_point_float = WhitePoint == BlackPoint ? (255.0 / 0.00025) : (255.0 / (WhitePoint - BlackPoint)); // Avoid division by zero if the white and black point are the same


    float3 color = tex2D(ReShade::BackBuffer, texcoord).rgb;
    color = color * white_point_float - (black_point_float * white_point_float);


    if (HighlightClipping)
    {
    float3 clipped_colors;


    clipped_colors = any(color > saturate(color)) // any colors whiter than white?
    ? float3(1.0, 0.0, 0.0)
    : color;
    clipped_colors = all(color > saturate(color)) // all colors whiter than white?
    ? float3(1.0, 1.0, 0.0)
    : clipped_colors;
    clipped_colors = any(color < saturate(color)) // any colors blacker than black?
    ? float3(0.0, 0.0, 1.0)
    : clipped_colors;
    clipped_colors = all(color < saturate(color)) // all colors blacker than black?
    ? float3(0.0, 1.0, 1.0)
    : clipped_colors;


    color = clipped_colors;
    }


    return color;
    }


    technique Levels
    {
    pass
    {
    VertexShader = PostProcessVS;
    PixelShader = LevelsPass;
    }
    }
    US Army, Major, Ret.

    Service To The Line,
    On The Line,
    On Time

    US Army Ordnance Corps.

  5. #5
    SOH-CM-2023 mongoose's Avatar
    Join Date
    Jun 2005
    Location
    Navigator, where are we?
    Age
    79
    Posts
    3,554
    So to explain to a programming illiterate, are there 3 files involved which just go into the install, or area ll these related to an app called Reshade. which is what?

    Cato said "Carthaginem esse delendam"
    I say "Carthago iam diu deleta,sed enim Bellum Alium adhuc aedificandum est"

  6. #6
    This is entirely related to adding new functions into Reshade (in this case image sharpening) that can be added to CFS3 once you have AnKor's shaders running. Reshade is completely optional, and unrelated to AnKor's shaders other than Reshade needing the DX 9 functionality that he provides before it can do anything.

    You can think of Reshade as being the same thing you might do to a screenshot in post-process to make it look "better" than it did in game, but it does it in real time while you're playing.

    Here's a link to the Knowledge Base Entry For Reshade Installs
    http://www.sim-outhouse.com/sohforum...=1#post1218972
    US Army, Major, Ret.

    Service To The Line,
    On The Line,
    On Time

    US Army Ordnance Corps.

Members who have read this thread: 0

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •