From d2f7ecf35207c5fbbd2ff854cb88e0a89efa8d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 16 Jun 2026 21:46:03 -0700 Subject: [PATCH] Metal: Fixed 36-sky shaders. (#3765) --- examples/36-sky/fs_sky_color_banding_fix.sc | 27 ++++++---------- examples/36-sky/fs_sky_landscape.sc | 29 +++++++----------- .../metal/fs_sky_color_banding_fix.bin | Bin 1539 -> 1216 bytes .../shaders/metal/fs_sky_landscape.bin | Bin 2015 -> 1704 bytes 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/examples/36-sky/fs_sky_color_banding_fix.sc b/examples/36-sky/fs_sky_color_banding_fix.sc index de96f2180..67829112a 100644 --- a/examples/36-sky/fs_sky_color_banding_fix.sc +++ b/examples/36-sky/fs_sky_color_banding_fix.sc @@ -5,28 +5,19 @@ $input v_skyColor, v_screenPos, v_viewDir * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE */ - uniform vec4 u_parameters; // x - sun size, y - sun bloom, z - exposition, w - time uniform vec4 u_sunDirection; uniform vec4 u_sunLuminance; #include "../common/common.sh" -// https://www.shadertoy.com/view/4ssXRX -// http://www.loopit.dk/banding_in_games.pdf -// http://www.dspguide.com/ch2/6.htm - -//uniformly distributed, normalized rand, [0, 1) -float nrand(in vec2 n) +// Interleaved gradient noise (better for Metal) +// Reference: http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare +float interleavedGradientNoise(vec2 screenPos, float temporalFactor) { - return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453); -} - -float n4rand_ss(in vec2 n) -{ - float nrnd0 = nrand( n + 0.07*fract( u_parameters.w ) ); - float nrnd1 = nrand( n + 0.11*fract( u_parameters.w + 0.573953 ) ); - return 0.23*sqrt(-log(nrnd0+0.00001))*cos(2.0*3.141592*nrnd1)+0.5; + vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189); + vec2 coord = screenPos + fract(temporalFactor) * vec2_splat(13.0); + return fract(magic.z * fract(dot(coord, magic.xy))); } void main() @@ -39,8 +30,10 @@ void main() float sun2 = min(sun * sun, 1.0); vec3 color = v_skyColor + sun2; color = toGamma(color); - float r = n4rand_ss(v_screenPos); - color += vec3(r, r, r) / 40.0; + + float dither = interleavedGradientNoise(gl_FragCoord.xy, u_parameters.w); + dither = (dither - 0.5) / 255.0; + color += vec3_splat(dither); gl_FragColor = vec4(color, 1.0); } diff --git a/examples/36-sky/fs_sky_landscape.sc b/examples/36-sky/fs_sky_landscape.sc index df59dcd9d..2de3f351b 100644 --- a/examples/36-sky/fs_sky_landscape.sc +++ b/examples/36-sky/fs_sky_landscape.sc @@ -13,21 +13,13 @@ uniform vec4 u_sunLuminance; uniform vec4 u_skyLuminance; uniform vec4 u_parameters; -// https://www.shadertoy.com/view/4ssXRX -// http://www.loopit.dk/banding_in_games.pdf -// http://www.dspguide.com/ch2/6.htm - -//uniformly distributed, normalized rand, [0, 1) -float nrand(in vec2 n) +// Interleaved gradient noise (better for Metal) +// Reference: http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare +float interleavedGradientNoise(vec2 screenPos, float temporalFactor) { - return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453); -} - -float n4rand_ss(in vec2 n) -{ - float nrnd0 = nrand( n + 0.07*fract( u_parameters.w ) ); - float nrnd1 = nrand( n + 0.11*fract( u_parameters.w + 0.573953 ) ); - return 0.23*sqrt(-log(nrnd0+0.00001))*cos(2.0*3.141592*nrnd1)+0.5; + vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189); + vec2 coord = screenPos + fract(temporalFactor) * vec2_splat(13.0); + return fract(magic.z * fract(dot(coord, magic.xy))); } float toLinear(float _rgb) @@ -49,10 +41,11 @@ void main() vec3 color = diffuseSun * u_sunLuminance.rgb + (diffuseSky * u_skyLuminance.rgb + 0.01) * occulsion; color *= 0.5; - //color = mix(color, (u_skyLuminance + u_sunLuminance)*0.3, v_fogFactor); - gl_FragColor.xyz = color * u_parameters.z; gl_FragColor.w = 1.0; - float r = n4rand_ss(gl_FragCoord.xy) / 40.0; - gl_FragColor.xyz = toGamma(gl_FragColor.xyz) + vec3(r, r, r); + + float dither = interleavedGradientNoise(gl_FragCoord.xy, u_parameters.w); + dither = (dither - 0.5) / 255.0; + + gl_FragColor.xyz = toGamma(gl_FragColor.xyz) + vec3_splat(dither); } diff --git a/examples/runtime/shaders/metal/fs_sky_color_banding_fix.bin b/examples/runtime/shaders/metal/fs_sky_color_banding_fix.bin index 85e5b8ed460cef382b2f186a0858dbeed5de8c1f..d291ea4aee209f1827cdf9eeb9bbd5b6c62ee0cd 100644 GIT binary patch delta 374 zcmYjM%Sr=55R8*}$RY?H^dLx|WLDQ@rsqwF2!cobfQVr+n-B=uu$e#<#Dji-GN0lP z_*r^$HhQoQeWZZ06@B4=sH zwvV3E^zR>}EN9wq&Xi_WNXd+4S~xAO6h>tbWu~k!Tsp@sGfo(d$P8<8(+D_=9+k!l uja4}0N?WU((6JwCXN8uijBV^0@YtZ296&@}`c*xLYUO*$Q3sW%VNiiur=08RCIj}lb)vp_^^$b0#eWk}}pe)s4J}HJ@e#=G8kf~G$ zs%fhmEvRgD%cCC1ymhuMG~Fz4{Mwj`Wjkf1-#1|(!l4nrVE{fp-Ns@3B~ z{Y_u3F6XUou9y02-RkS5`9I5n>IqTkAZ+LWQv@MIioqu)i=BfFOCAo?+M-EOcEAZ3 zJrG;=$Rib^coI`KfZ&~xQqu0L?iLL@A7vt8lS%yvE>Mt}^~B89F?#aB##oKPZ=TNU zMtg%%gdB*DC4VX#eZ;p0v#^m%4oLDaBRUUBOy!-y-dYTS;UEyEfHB78JQE>w$;)!| m+R*!AOA)5rO6AD#sc)SEihNhS^EO!rQfQg)2kIGe#+?8F diff --git a/examples/runtime/shaders/metal/fs_sky_landscape.bin b/examples/runtime/shaders/metal/fs_sky_landscape.bin index ca6046857fe4f7cb677a76c2173e9d90e8c85e55..fc0326577e8bb817ad5a2f6f05ca2befb5e1dd6a 100644 GIT binary patch delta 396 zcmYk1y-EW?6oq5fsH}>TRxnfUY8+4(#7&#@Ptw$O@MMY(QP3NtNyY*Sn1uP$+J633*}Nl|NJS|Tl4#(ZC`}|7 z$u$Qh>d47NXi$_JLp4zblw^!3QUr!>(2?_q&}A1^FpLVRwJ;!EF(!=$$(c~Pus3|< zW?q9M_Yg(Vak2G6C$N?g3&nP`vAsg+IBWQ~zF#V} FO1~bjV;BGc delta 634 zcma)(v2N5r5Qe2YE+8a?LKCRiB843;tJ#^I*5>b!qa-xf(9i%U z7rX)$5^oXm5_uKI_9+Sy1=H;M+x`8|{QKj{?bC1jtM6N@{Wq(h&qtdNe_ekWjR~YZ z7~7F>`+2Rf;=@Ls$u_(4;gH1p!zo)>3jRi4b6SykH$ zZ44|2)nE!3V@w(;5kfb3o=?r%;osvg|95^(q&Mn<(X5e&uqmMWc9DB=$$8L3^Vg4! tR`2Ug;W_GycMm=A4%o{Mk+>aud5>PLslB=fKdxbWy76+vvHiO7;tx66mI43(