TECHARTNOMAD | TECHARTFLOWIO.COM

UNITY3D

RCAS ( Robust Contrast-Adaptive Sharpening )

jplee 2024. 9. 20. 20:48

AMD FSR 의 RCAS 를 셰이더토이에서 구현한 내용이 있어 공유 해 봅니다.

FSR 은 RASU ( Edge-Adaptive Spatial Upsamping ) 후 RCAS ( Robust Contrast-Adaptive Sharpening ) 을 적용하고 한 후 업스케일타겟으로 결과를 넘긴 후 finalpass 완료 단계로 향하게 됩니다.

 

Shadertoy

0.00 00.0 fps 0 x 0

www.shadertoy.com

/*
A poor approximation of AMD FidelityFX FSR
Potentially useful to upscale under-resolution renders.

Currently only implements RCAS part.

Best viewed in fullscreen.
Drag left right to change switchover position. RCAS on left.
Drag up and down to set CAS level.
*/

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    vec2 muv;
    if (iMouse.z > 0.) {
        muv = iMouse.xy/iResolution.xy;
    } else {
        muv = vec2(.5,.5);
    }
    
    // CAS algorithm
    vec4 uvoff = vec4(1,0,1,-1)/iChannelResolution[0].xxyy;

    float sharpness = 1.-muv.y;

    vec3 c = texture(iChannel0, uv).xyz;
    vec3 c_n = texture(iChannel0, uv+uvoff.yz).xyz;
    vec3 c_s = texture(iChannel0, uv+uvoff.yw).xyz;
    vec3 c_e = texture(iChannel0, uv-uvoff.xy).xyz;
    vec3 c_w = texture(iChannel0, uv+uvoff.xy).xyz;

    // Calculate sharpening value based on local edges
    vec3 max_edges = max(max(c_n, c_s), max(c_e, c_w));
    vec3 min_edges = min(min(c_n, c_s), min(c_e, c_w));
    vec3 sum_edges = c_n + c_e + c_s + c_w;
    vec3 edge = -.25 * min(min_edges / max_edges, (1.-max_edges) / (1.-min_edges));
    float edges = max(-.1875, min(0.,max(edge.r, max(edge.g, edge.b))));
    float w = edges * exp(-sharpness);

    // Apply sharpening
    vec3 col = (c + sum_edges * w) / (w*4. + 1.);
    
    // Output to screen
    if (uv.x > (muv.x-.002)) {
        if (uv.x > (muv.x+.002)) {
            col = c;
        } else {
            col = vec3(0);
        }
    }
    fragColor = vec4(col,1);
}