molstar
Version:
A comprehensive macromolecular library.
149 lines (129 loc) • 4.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cas_frag = void 0;
exports.cas_frag = `
precision mediump float;
precision mediump sampler2D;
uniform sampler2D tColor;
uniform vec2 uTexSizeInv;
uniform float uSharpness;
// adapted from https://www.shadertoy.com/view/stXSWB
/*
* FidelityFX Super Resolution scales up a low resolution
* image, while adding fine detail.
*
* MIT Open License
*
* https://gpuopen.com/fsr
*
* Left: FSR processed
* Right: Original texture, bilinear interpolation
*
* Mouse at top: Sharpness 0 stops (maximum)
* Mouse at bottom: Sharpness 2 stops (minimum)
*
* It works in two passes-
* EASU upsamples the image with a clamped Lanczos kernel.
* RCAS sharpens the image at the target resolution.
*
* I needed to make a few changes to improve readability and
* WebGL compatibility in an algorithm I don't fully understand.
* Expect bugs.
*
* Shader not currently running for WebGL1 targets (eg. mobile Safari)
*
* There is kind of no point to using FSR in Shadertoy, as it renders buffers
* at full target resolution. But this might be useful for WebGL based demos
* running smaller-than-target render buffers.
*
* For sharpening with a full resolution render buffer,
* FidelityFX CAS is a better option.
* https://www.shadertoy.com/view/ftsXzM
*
* For readability and compatibility, these optimisations have been removed:
* * Fast approximate inverse and inversesqrt
* * textureGather fetches (not WebGL compatible)
* * Multiplying by reciprocal instead of division
*
* Apologies to AMD for the numerous slowdowns and errors I have introduced.
*
*/
/***** RCAS *****/
// Input callback prototypes that need to be implemented by calling shader
vec4 FsrRcasLoadF(vec2 p);
//------------------------------------------------------------------------------------------------------------------------------
void FsrRcasCon(
out float con,
// The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
float sharpness
) {
// Transform from stops to linear value.
con = exp2(-sharpness);
}
vec3 FsrRcasF(
vec2 ip, // Integer pixel position in output.
float con
) {
// Constant generated by RcasSetup().
// Algorithm uses minimal 3x3 pixel neighborhood.
// b
// d e f
// h
vec2 sp = vec2(ip);
vec3 b = FsrRcasLoadF(sp + vec2( 0,-1)).rgb;
vec3 d = FsrRcasLoadF(sp + vec2(-1, 0)).rgb;
vec3 e = FsrRcasLoadF(sp).rgb;
vec3 f = FsrRcasLoadF(sp + vec2( 1, 0)).rgb;
vec3 h = FsrRcasLoadF(sp + vec2( 0, 1)).rgb;
// Luma times 2.
float bL = b.g + .5 * (b.b + b.r);
float dL = d.g + .5 * (d.b + d.r);
float eL = e.g + .5 * (e.b + e.r);
float fL = f.g + .5 * (f.b + f.r);
float hL = h.g + .5 * (h.b + h.r);
// Noise detection.
float nz = .25 * (bL + dL + fL + hL) - eL;
nz=clamp(
abs(nz)
/(
max(max(bL,dL),max(eL,max(fL,hL)))
-min(min(bL,dL),min(eL,min(fL,hL)))
),
0., 1.
);
nz=1.-.5*nz;
// Min and max of ring.
vec3 mn4 = min(b, min(f, h));
vec3 mx4 = max(b, max(f, h));
// Immediate constants for peak range.
vec2 peakC = vec2(1., -4.);
// Limiters, these need to be high precision RCPs.
vec3 hitMin = mn4 / (4. * mx4);
vec3 hitMax = (peakC.x - mx4) / (4.* mn4 + peakC.y);
vec3 lobeRGB = max(-hitMin, hitMax);
float lobe = max(
-FSR_RCAS_LIMIT,
min(max(lobeRGB.r, max(lobeRGB.g, lobeRGB.b)), 0.)
)*con;
// Apply noise removal.
lobe *= nz;
// Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
return (lobe * (b + d + h + f) + e) / (4. * lobe + 1.);
}
vec4 FsrRcasLoadF(vec2 p) {
return texture2D(tColor, p * uTexSizeInv);
}
void main() {
// Set up constants
float con;
FsrRcasCon(con, uSharpness);
// Perform RCAS pass
vec3 col = FsrRcasF(gl_FragCoord.xy, con);
gl_FragColor = vec4(col, FsrRcasLoadF(gl_FragCoord.xy).a);
}
`;