Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / games / voxels / src / main / rust / shaders / composite.frag
composite.frag39 lines · 1.8 KiBRaw
| 1 | #version 450 |
| 2 | // Composite: FXAA on the HDR image, add bloom, tonemap+gamma to the swapchain. |
| 3 | // pc.param.xy = texel size (1/width, 1/height), pc.param.z = bloom intensity. |
| 4 | layout(location=0) in vec2 vUv; |
| 5 | layout(binding=0) uniform sampler2D hdrTex; |
| 6 | layout(binding=1) uniform sampler2D bloomTex; |
| 7 | layout(push_constant) uniform PC { vec4 param; } pc; |
| 8 | layout(location=0) out vec4 outColor; |
| 9 | |
| 10 | float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); } |
| 11 | |
| 12 | void main() { |
| 13 | vec2 texel = pc.param.xy; |
| 14 | vec3 c = texture(hdrTex, vUv).rgb; |
| 15 | vec3 nw = texture(hdrTex, vUv + vec2(-1.0, -1.0) * texel).rgb; |
| 16 | vec3 ne = texture(hdrTex, vUv + vec2( 1.0, -1.0) * texel).rgb; |
| 17 | vec3 sw = texture(hdrTex, vUv + vec2(-1.0, 1.0) * texel).rgb; |
| 18 | vec3 se = texture(hdrTex, vUv + vec2( 1.0, 1.0) * texel).rgb; |
| 19 | float lM = luma(c), lNW = luma(nw), lNE = luma(ne), lSW = luma(sw), lSE = luma(se); |
| 20 | float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE))); |
| 21 | float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE))); |
| 22 | |
| 23 | vec3 aa = c; |
| 24 | if (lMax - lMin > 0.05 * lMax + 0.02) { // only on edges |
| 25 | vec2 dir = vec2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE))); |
| 26 | float reduce = max((lNW + lNE + lSW + lSE) * 0.25 * 0.125, 1.0 / 128.0); |
| 27 | float rcp = 1.0 / (min(abs(dir.x), abs(dir.y)) + reduce); |
| 28 | dir = clamp(dir * rcp, -8.0, 8.0) * texel; |
| 29 | vec3 rA = 0.5 * (texture(hdrTex, vUv + dir * (1.0/3.0 - 0.5)).rgb + texture(hdrTex, vUv + dir * (2.0/3.0 - 0.5)).rgb); |
| 30 | vec3 rB = rA * 0.5 + 0.25 * (texture(hdrTex, vUv + dir * -0.5).rgb + texture(hdrTex, vUv + dir * 0.5).rgb); |
| 31 | float lB = luma(rB); |
| 32 | aa = (lB < lMin || lB > lMax) ? rA : rB; |
| 33 | } |
| 34 | |
| 35 | vec3 col = aa + texture(bloomTex, vUv).rgb * pc.param.z; |
| 36 | col = col / (col + vec3(0.55)); // tonemap |
| 37 | col = pow(col, vec3(0.85)); // gamma lift |
| 38 | outColor = vec4(col, 1.0); |
| 39 | } |