Backlog/depot/content/_shaders/msl/ssao.frag.msl

125 lines
10 KiB
Plaintext

#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
template<typename T, size_t Num>
struct spvUnsafeArray
{
T elements[Num ? Num : 1];
thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}
device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}
constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}
threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};
struct type_Uniforms
{
float4x4 View;
float4x4 ViewProjection;
float4x4 Projection;
float2 extents;
float radius;
float bias0;
int numSamples;
};
constant spvUnsafeArray<float3, 64> _314 = spvUnsafeArray<float3, 64>({ float3(0.0), float3(0.00039307403494603931903839111328125, 0.00020744701032526791095733642578125, 0.02340200729668140411376953125), float3(-0.001318361726589500904083251953125, -0.001457856036722660064697265625, 0.01546739041805267333984375), float3(0.0003496186691336333751678466796875, 8.4181214333511888980865478515625e-05, 0.058608897030353546142578125), float3(-0.0005043491837568581104278564453125, -0.00383304059505462646484375, 0.02929248847067356109619140625), float3(-0.00241771643050014972686767578125, 0.0057135238312184810638427734375, 0.01733645610511302947998046875), float3(-0.0007101023220457136631011962890625, 0.000413290224969387054443359375, 0.0375200845301151275634765625), float3(1.5587833331665024161338806152344e-05, -0.00011572847870411351323127746582031, 0.073567919433116912841796875), float3(0.00334032182581722736358642578125, -0.0051653436385095119476318359375, 0.050431825220584869384765625), float3(-0.0063966461457312107086181640625, 0.01290071941912174224853515625, 0.00147703452967107295989990234375), float3(0.007907788269221782684326171875, -0.0049759554676711559295654296875, 0.001991991885006427764892578125), float3(-0.0003332807100377976894378662109375, -0.00100087863393127918243408203125, 0.001031001214869320392608642578125), float3(0.00011193146201549097895622253417969, 0.00036116360570304095745086669921875, 0.010905899107456207275390625), float3(-0.00385366589762270450592041015625, -0.004629385657608509063720703125, 0.027080766856670379638671875), float3(-0.0055218120105564594268798828125, 0.0163907893002033233642578125, 0.0033264369703829288482666015625), float3(-0.00297828204929828643798828125, -0.00340225151740014553070068359375, 0.009606515057384967803955078125), float3(0.0005643319454975426197052001953125, -0.00123721174895763397216796875, 0.01142332889139652252197265625), float3(0.0042646829970180988311767578125, 0.001690786913968622684478759765625, 0.00338359293527901172637939453125), float3(0.028113491833209991455078125, -0.01881640218198299407958984375, 0.0140878893435001373291015625), float3(-0.03022454865276813507080078125, -0.000894616474397480487823486328125, 0.0849911272525787353515625), float3(-0.01961914263665676116943359375, -0.0350324176251888275146484375, 0.014081305824220180511474609375), float3(0.015496986918151378631591796875, -0.010903127491474151611328125, 0.00180752319283783435821533203125), float3(0.0196535401046276092529296875, 0.01710924692451953887939453125, 0.0142847932875156402587890625), float3(-0.02892612479627132415771484375, 0.00322483875788748264312744140625, 0.06573773920536041259765625), float3(-0.0016983882524073123931884765625, 0.0004074592725373804569244384765625, 0.00372933782637119293212890625), float3(-0.0335367508232593536376953125, 0.0591063164174556732177734375, 0.01477129198610782623291015625), float3(0.01729742996394634246826171875, 0.02095410041511058807373046875, 0.061427496373653411865234375), float3(-0.0362440533936023712158203125, -0.01097334362566471099853515625, 0.052580632269382476806640625), float3(-0.010873113758862018585205078125, 0.071666888892650604248046875, 0.07090236246585845947265625), float3(0.0404303781688213348388671875, -0.0758908092975616455078125, 0.066407024860382080078125), float3(0.006424785591661930084228515625, -0.00888848863542079925537109375, 0.00571323931217193603515625), float3(-0.0424978695809841156005859375, 0.122786454856395721435546875, 0.03013108484447002410888671875), float3(-0.012614764273166656494140625, -0.01173587143421173095703125, 0.0161450766026973724365234375), float3(0.02246730588376522064208984375, -0.023659668862819671630859375, 0.01735904999077320098876953125), float3(0.0280994363129138946533203125, -0.0286351330578327178955078125, 0.0076164784841239452362060546875), float3(0.0701790750026702880859375, -0.1532168686389923095703125, 0.0887149870395660400390625), float3(0.0073889424093067646026611328125, 0.152280390262603759765625, 0.00664083100855350494384765625), float3(0.07831905782222747802734375, -0.072104983031749725341796875, 0.094614334404468536376953125), float3(0.03289596736431121826171875, -0.0408993400633335113525390625, 0.040609411895275115966796875), float3(-0.141404449939727783203125, 0.17826557159423828125, 0.067046172916889190673828125), float3(0.108955644071102142333984375, 0.1114947497844696044921875, 0.21018303930759429931640625), float3(0.02759261615574359893798828125, 0.0139005817472934722900390625, 0.036677159368991851806640625), float3(0.05564410984516143798828125, -0.099730752408504486083984375, 0.113144241273403167724609375), float3(-0.0315654389560222625732421875, -0.22426509857177734375, 0.23739193379878997802734375), float3(-0.0474216155707836151123046875, 0.00566783733665943145751953125, 0.1736856400966644287109375), float3(0.088452465832233428955078125, 0.096331931650638580322265625, 0.083578832447528839111328125), float3(-0.0036428333260118961334228515625, 0.010955554433166980743408203125, 0.00330849713645875453948974609375), float3(-0.1911892592906951904296875, -0.18971312046051025390625, 0.1564487516880035400390625), float3(0.034361146390438079833984375, 0.0655452907085418701171875, 0.0308254323899745941162109375), float3(0.371889293193817138671875, 0.0229042805731296539306640625, 0.2982411682605743408203125), float3(-0.014679887332022190093994140625, 0.01349230110645294189453125, 0.00740495882928371429443359375), float3(-0.00653826631605625152587890625, -0.0058869807980954647064208984375, 0.005725885741412639617919921875), float3(-0.2333120405673980712890625, -0.35499393939971923828125, 0.03432808816432952880859375), float3(0.0411357469856739044189453125, -0.023320116102695465087890625, 0.01891444809734821319580078125), float3(-0.0945928990840911865234375, 0.02529100514948368072509765625, 0.092956520617008209228515625), float3(-0.0317381806671619415283203125, 0.226026356220245361328125, 0.2367977797985076904296875), float3(-0.3801217377185821533203125, 0.2481244504451751708984375, 0.47917068004608154296875), float3(0.13565014302730560302734375, 0.13791494071483612060546875, 0.4077457487583160400390625), float3(0.523879945278167724609375, -0.388758122920989990234375, 0.24387760460376739501953125), float3(-0.0520826168358325958251953125, -0.118409633636474609375, 0.095709107816219329833984375), float3(0.122609488666057586669921875, -0.4220634996891021728515625, 0.178109467029571533203125), float3(-0.2801954448223114013671875, -0.520291268825531005859375, 0.26809024810791015625), float3(-0.3271856009960174560546875, 0.23393590748310089111328125, 0.098123751580715179443359375), float3(0.3826406896114349365234375, 0.08367092907428741455078125, 0.257561206817626953125) });
struct main0_out
{
float4 out_var_SV_Target0 [[color(0)]];
};
struct main0_in
{
float2 in_var_TEXCOORD0 [[user(locn0)]];
};
fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d<float> PositionTexture [[texture(0)]], texture2d<float> NormalTexture [[texture(1)]], texture2d<float> NoiseTexture [[texture(2)]], sampler Sampler0 [[sampler(0)]], sampler Sampler1 [[sampler(1)]], sampler Sampler2 [[sampler(2)]])
{
main0_out out = {};
float2 _318 = in.in_var_TEXCOORD0;
float4 _425;
do
{
_318.y = (_318.y * (-1.0)) + 1.0;
float4 _330 = PositionTexture.sample(Sampler0, _318);
float4 _331 = Uniforms.View * _330;
float3 _332 = _331.xyz;
float _333 = _331.z;
if (_333 < 0.00999999977648258209228515625)
{
_425 = float4(1.0);
break;
}
float4 _340 = NormalTexture.sample(Sampler1, _318);
float3 _341 = _340.xyz;
float4 _346 = NoiseTexture.sample(Sampler2, (_318 * float2(320.0, 180.0)));
float3 _348 = fast::normalize(_346.xyz);
float3 _352 = fast::normalize(_348 - (_341 * dot(_348, _341)));
float3x3 _355 = transpose(float3x3(_352, cross(_341, _352), _341));
float _357;
_357 = 0.0;
float _358;
for (int _360 = 0; _360 < Uniforms.numSamples; _357 = _358, _360++)
{
float3 _374 = _332 + (((_314[_360] + float3(0.0, 0.0, 0.0500000007450580596923828125)) * _355) * Uniforms.radius);
float _377 = _374.z;
float4 _381 = Uniforms.Projection * float4(_374.xy, _377, 1.0);
float3 _388 = ((_381.xyz / float3(_381.w)).xyz * 0.5) + float3(0.5);
float4 _389 = float4(_388.x, _388.y, _388.z, _381.w);
_389.y = (_388.y * (-1.0)) + 1.0;
float4 _396 = PositionTexture.sample(Sampler0, _389.xy);
float4 _397 = Uniforms.View * _396;
float _398 = _397.z;
float _409 = _357 + (float(_398 <= (_377 - Uniforms.bias0)) * smoothstep(0.0, 1.0, Uniforms.radius / abs(_333 - _398)));
if (_377 > 30.0)
{
_358 = _409 * precise::max(0.0, 1.0 - ((_377 - 30.0) * 0.0033333334140479564666748046875));
}
else
{
_358 = _409;
}
}
float _420 = 1.0 - (_357 / float(Uniforms.numSamples));
_425 = float4(_420) + float4(_420 * 9.9999997473787516355514526367188e-06);
break;
} while(false);
out.out_var_SV_Target0 = _425;
return out;
}