Guest User

1

a guest
Mar 29th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.95 KB | None | 0 0
  1. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. // ReShade 3.0 effect file
  3. // visit facebook.com/MartyMcModding for news/updates
  4. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5. // Ambient Obscurance with Indirect Lighting "MXAO" 3.4.3 by Marty McFly
  6. // CC BY-NC-ND 3.0 licensed.
  7. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8. #ifndef BUFFER_SHIT
  9. #define BUFFER_SHIT 0
  10. #endif
  11. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12. // Preprocessor Settings
  13. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14.  
  15. #ifndef MXAO_MIPLEVEL_AO
  16. #define MXAO_MIPLEVEL_AO 0 //[0 to 2] Miplevel of AO texture. 0 = fullscreen, 1 = 1/2 screen width/height, 2 = 1/4 screen width/height and so forth. Best results: IL MipLevel = AO MipLevel + 2
  17. #endif
  18.  
  19. #ifndef MXAO_MIPLEVEL_IL
  20. #define MXAO_MIPLEVEL_IL 2 //[0 to 4] Miplevel of IL texture. 0 = fullscreen, 1 = 1/2 screen width/height, 2 = 1/4 screen width/height and so forth.
  21. #endif
  22.  
  23. #ifndef MXAO_ENABLE_IL
  24. #define MXAO_ENABLE_IL 0 //[0 or 1] Enables Indirect Lighting calculation. Will cause a major fps hit.
  25. #endif
  26.  
  27. #ifndef MXAO_SMOOTHNORMALS
  28. #define MXAO_SMOOTHNORMALS 0 //[0 or 1] This feature makes low poly surfaces smoother, especially useful on older games.
  29. #endif
  30.  
  31. #ifndef MXAO_TWO_LAYER
  32. #define MXAO_TWO_LAYER 0 //[0 or 1] Splits MXAO into two separate layers that allow for both large and fine AO.
  33. #endif
  34.  
  35. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  36. // UI variables
  37. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  38.  
  39. uniform int MXAO_GLOBAL_SAMPLE_QUALITY_PRESET <
  40. ui_type = "combo";
  41. ui_label = "Sample Quality";
  42. ui_items = "Very Low\0Low\0Medium\0High\0Very High\0Ultra\0Maximum\0";
  43. ui_tooltip = "Global quality control, main performance knob. Higher radii might require higher quality.";
  44. > = 2;
  45.  
  46. uniform float MXAO_SAMPLE_RADIUS <
  47. ui_type = "drag";
  48. ui_min = 0.5; ui_max = 20.0;
  49. ui_label = "Sample Radius";
  50. ui_tooltip = "Sample radius of MXAO, higher means more large-scale occlusion with less fine-scale details.";
  51. > = 2.5;
  52.  
  53. uniform float MXAO_SAMPLE_NORMAL_BIAS <
  54. ui_type = "drag";
  55. ui_min = 0.0; ui_max = 0.8;
  56. ui_label = "Normal Bias";
  57. ui_tooltip = "Occlusion Cone bias to reduce self-occlusion of surfaces that have a low angle to each other.";
  58. > = 0.2;
  59.  
  60. uniform float MXAO_GLOBAL_RENDER_SCALE <
  61. ui_type = "drag";
  62. ui_label = "Render Size Scale";
  63. ui_min = 0.50; ui_max = 1.00;
  64. ui_tooltip = "Factor of MXAO resolution, lower values greatly reduce performance overhead but decrease quality.\n1.0 = MXAO is computed in original resolution\n0.5 = MXAO is computed in 1/2 width 1/2 height of original resolution\n...";
  65. > = 1.0;
  66.  
  67. uniform float MXAO_SSAO_AMOUNT <
  68. ui_type = "drag";
  69. ui_min = 0.00; ui_max = 4.00;
  70. ui_label = "Ambient Occlusion Amount";
  71. ui_tooltip = "Intensity of AO effect. Can cause pitch black clipping if set too high.";
  72. > = 1.00;
  73.  
  74. #if(MXAO_ENABLE_IL != 0)
  75. uniform float MXAO_SSIL_AMOUNT <
  76. ui_type = "drag";
  77. ui_min = 0.00; ui_max = 12.00;
  78. ui_label = "Indirect Lighting Amount";
  79. ui_tooltip = "Intensity of IL effect. Can cause overexposured white spots if set too high.";
  80. > = 4.00;
  81.  
  82. uniform float MXAO_SSIL_SATURATION <
  83. ui_type = "drag";
  84. ui_min = 0.00; ui_max = 3.00;
  85. ui_label = "Indirect Lighting Saturation";
  86. ui_tooltip = "Controls color saturation of IL effect.";
  87. > = 1.00;
  88. #endif
  89.  
  90. #if (MXAO_TWO_LAYER != 0)
  91. uniform float MXAO_SAMPLE_RADIUS_SECONDARY <
  92. ui_type = "drag";
  93. ui_min = 0.1; ui_max = 1.00;
  94. ui_label = "Fine AO Scale";
  95. ui_tooltip = "Multiplier of Sample Radius for fine geometry. A setting of 0.5 scans the geometry at half the radius of the main AO.";
  96. > = 0.2;
  97.  
  98. uniform float MXAO_AMOUNT_FINE <
  99. ui_type = "drag";
  100. ui_min = 0.00; ui_max = 1.00;
  101. ui_label = "Fine AO intensity multiplier";
  102. ui_tooltip = "Intensity of small scale AO / IL.";
  103. > = 1.0;
  104.  
  105. uniform float MXAO_AMOUNT_COARSE <
  106. ui_type = "drag";
  107. ui_min = 0.00; ui_max = 1.00;
  108. ui_label = "Coarse AO intensity multiplier";
  109. ui_tooltip = "Intensity of large scale AO / IL.";
  110. > = 1.0;
  111. #endif
  112.  
  113. uniform int MXAO_DEBUG_VIEW_ENABLE <
  114. ui_type = "combo";
  115. ui_label = "Enable Debug View";
  116. ui_items = "None\0AO/IL channel\0Culling Mask\0";
  117. ui_tooltip = "Different debug outputs";
  118. > = 0;
  119.  
  120. uniform int MXAO_BLEND_TYPE <
  121. ui_type = "drag";
  122. ui_min = 0; ui_max = 2;
  123. ui_label = "Blending Mode";
  124. ui_tooltip = "Different blending modes for merging AO/IL with original color.\0Blending mode 0 matches formula of MXAO 2.0 and older.";
  125. > = 0;
  126.  
  127. uniform float MXAO_FADE_DEPTH_START <
  128. ui_type = "drag";
  129. ui_label = "Fade Out Start";
  130. ui_min = 0.00; ui_max = 1.00;
  131. ui_tooltip = "Distance where MXAO starts to fade out. 0.0 = camera, 1.0 = sky. Must be less than Fade Out End.";
  132. > = 0.05;
  133.  
  134. uniform float MXAO_FADE_DEPTH_END <
  135. ui_type = "drag";
  136. ui_label = "Fade Out End";
  137. ui_min = 0.00; ui_max = 1.00;
  138. ui_tooltip = "Distance where MXAO completely fades out. 0.0 = camera, 1.0 = sky. Must be greater than Fade Out Start.";
  139. > = 0.4;
  140.  
  141. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  142. // Textures, Samplers
  143. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  144.  
  145. #include "ReShade.fxh"
  146.  
  147. texture2D MXAO_ColorTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 3+MXAO_MIPLEVEL_IL;};
  148. texture2D MXAO_DepthTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16F; MipLevels = 3+MXAO_MIPLEVEL_AO;};
  149. texture2D MXAO_NormalTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 3+MXAO_MIPLEVEL_IL;};
  150. texture2D MXAO_CullingTex { Width = BUFFER_WIDTH/8; Height = BUFFER_HEIGHT/8; Format = R8; };
  151. #if(BUFFER_SHIT != 0)
  152. texture2D MXAO_BlurTex1 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  153. texture2D MXAO_BlurTex2 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  154. sampler2D sMXAO_BlurTex1 { Texture = MXAO_BlurTex1; };
  155. sampler2D sMXAO_BlurTex2 { Texture = MXAO_BlurTex2; };
  156. #endif
  157. sampler2D sMXAO_ColorTex { Texture = MXAO_ColorTex; };
  158. sampler2D sMXAO_DepthTex { Texture = MXAO_DepthTex; };
  159. sampler2D sMXAO_NormalTex { Texture = MXAO_NormalTex; };
  160. sampler2D sMXAO_CullingTex { Texture = MXAO_CullingTex; };
  161.  
  162. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  163. // Vertex Shader
  164. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  165.  
  166. struct MXAO_VSOUT
  167. {
  168. float4 position : SV_Position;
  169. float2 texcoord : TEXCOORD0;
  170. float2 scaledcoord : TEXCOORD1;
  171. float samples : TEXCOORD2;
  172. float3 uvtoviewADD : TEXCOORD4;
  173. float3 uvtoviewMUL : TEXCOORD5;
  174. };
  175.  
  176. MXAO_VSOUT VS_MXAO(in uint id : SV_VertexID)
  177. {
  178. MXAO_VSOUT MXAO;
  179.  
  180. MXAO.texcoord.x = (id == 2) ? 2.0 : 0.0;
  181. MXAO.texcoord.y = (id == 1) ? 2.0 : 0.0;
  182. MXAO.scaledcoord.xy = MXAO.texcoord.xy / MXAO_GLOBAL_RENDER_SCALE;
  183. MXAO.position = float4(MXAO.texcoord.xy * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  184.  
  185. MXAO.samples = 8;
  186.  
  187. if( MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 0) { MXAO.samples = 4; }
  188. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 1) { MXAO.samples = 8; }
  189. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 2) { MXAO.samples = 16; }
  190. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 3) { MXAO.samples = 24; }
  191. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 4) { MXAO.samples = 32; }
  192. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 5) { MXAO.samples = 64; }
  193. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 6) { MXAO.samples = 255; }
  194.  
  195. MXAO.uvtoviewADD = float3(-1.0,-1.0,1.0);
  196. MXAO.uvtoviewMUL = float3(2.0,2.0,0.0);
  197. /* //uncomment to enable perspective-correct position recontruction. Minor difference for common FoV's
  198. static const float FOV = 70.0; //vertical FoV
  199.  
  200. MXAO.uvtoviewADD = float3(-tan(radians(FOV * 0.5)).xx,1.0);
  201. MXAO.uvtoviewADD.y *= BUFFER_WIDTH * BUFFER_RCP_HEIGHT;
  202. MXAO.uvtoviewMUL = float3(-2.0 * MXAO.uvtoviewADD.xy,0.0);
  203. */
  204. return MXAO;
  205. }
  206.  
  207. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  208. // Functions
  209. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  210.  
  211. float GetLinearDepth(in float2 coords)
  212. {
  213. return ReShade::GetLinearizedDepth(coords);
  214. }
  215.  
  216. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  217.  
  218. float3 GetPosition(in float2 coords, in MXAO_VSOUT MXAO)
  219. {
  220. return (coords.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * GetLinearDepth(coords.xy) * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  221. }
  222.  
  223. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  224.  
  225. float3 GetPositionLOD(in float2 coords, in MXAO_VSOUT MXAO, in int mipLevel)
  226. {
  227. return (coords.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * tex2Dlod(sMXAO_DepthTex, float4(coords.xy,0,mipLevel)).x;
  228. }
  229.  
  230. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  231.  
  232. void GetBlurWeight(in float4 tempKey, in float4 centerKey, in float surfacealignment, inout float weight)
  233. {
  234. float depthdiff = abs(tempKey.w - centerKey.w);
  235. float normaldiff = saturate(1.0 - dot(tempKey.xyz,centerKey.xyz));
  236.  
  237. weight = saturate(0.15 / surfacealignment - depthdiff) * saturate(0.65 - normaldiff);
  238. weight = saturate(weight * 4.0) * 2.0;
  239. }
  240.  
  241. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  242.  
  243. void GetBlurKeyAndSample(in float2 texcoord, in float inputscale, in sampler inputsampler, inout float4 tempsample, inout float4 key)
  244. {
  245. float4 lodcoord = float4(texcoord.xy,0,0);
  246. tempsample = tex2Dlod(inputsampler,lodcoord * inputscale);
  247. key = float4(tex2Dlod(sMXAO_NormalTex,lodcoord).xyz*2-1, tex2Dlod(sMXAO_DepthTex,lodcoord).x);
  248. }
  249.  
  250. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  251.  
  252. float4 BlurFilter(in MXAO_VSOUT MXAO, in sampler inputsampler, in float inputscale, in float radius, in int blursteps)
  253. {
  254. float4 tempsample;
  255. float4 centerkey, tempkey;
  256. float centerweight = 1.0, tempweight;
  257. float4 blurcoord = 0.0;
  258.  
  259. GetBlurKeyAndSample(MXAO.texcoord.xy,inputscale,inputsampler,tempsample,centerkey);
  260. float surfacealignment = saturate(-dot(centerkey.xyz,normalize(float3(MXAO.texcoord.xy*2.0-1.0,1.0)*centerkey.w)));
  261.  
  262. #if(MXAO_ENABLE_IL != 0)
  263. #define BLUR_COMP_SWIZZLE xyzw
  264. #else
  265. #define BLUR_COMP_SWIZZLE w
  266. #endif
  267.  
  268. float4 blurSum = tempsample.BLUR_COMP_SWIZZLE;
  269. float2 blurOffsets[8] = {float2(1.5,0.5),float2(-1.5,-0.5),float2(-0.5,1.5),float2(0.5,-1.5),float2(1.5,2.5),float2(-1.5,-2.5),float2(-2.5,1.5),float2(2.5,-1.5)};
  270.  
  271. [loop]
  272. for(int iStep = 0; iStep < blursteps; iStep++)
  273. {
  274. float2 sampleCoord = MXAO.texcoord.xy + blurOffsets[iStep] * ReShade::PixelSize * radius / inputscale;
  275.  
  276. GetBlurKeyAndSample(sampleCoord, inputscale, inputsampler, tempsample, tempkey);
  277. GetBlurWeight(tempkey, centerkey, surfacealignment, tempweight);
  278.  
  279. blurSum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  280. centerweight += tempweight;
  281. }
  282.  
  283. blurSum.BLUR_COMP_SWIZZLE /= centerweight;
  284.  
  285. #if(MXAO_ENABLE_IL == 0)
  286. blurSum.xyz = centerkey.xyz*0.5+0.5;
  287. #endif
  288.  
  289. return blurSum;
  290. }
  291.  
  292. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  293.  
  294. void SetupAOParameters(in MXAO_VSOUT MXAO, in float3 P, in float layerID, out float scaledRadius, out float falloffFactor)
  295. {
  296. scaledRadius = 0.25 * MXAO_SAMPLE_RADIUS / (MXAO.samples * (P.z + 2.0));
  297. falloffFactor = -1.0/(MXAO_SAMPLE_RADIUS * MXAO_SAMPLE_RADIUS);
  298.  
  299. #if(MXAO_TWO_LAYER != 0)
  300. scaledRadius *= lerp(1.0,MXAO_SAMPLE_RADIUS_SECONDARY,layerID);
  301. falloffFactor *= lerp(1.0,1.0/(MXAO_SAMPLE_RADIUS_SECONDARY*MXAO_SAMPLE_RADIUS_SECONDARY),layerID);
  302. #endif
  303. }
  304.  
  305. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  306.  
  307. void TesselateNormals(inout float3 N, in float3 P, in MXAO_VSOUT MXAO)
  308. {
  309. float2 searchRadiusScaled = 0.018 / P.z * float2(1.0,ReShade::AspectRatio);
  310. float3 likelyFace[4] = {N,N,N,N};
  311.  
  312. for(int iDirection=0; iDirection < 4; iDirection++)
  313. {
  314. float2 cdir;
  315. sincos(6.28318548 * 0.25 * iDirection,cdir.y,cdir.x);
  316. for(int i=1; i<=5; i++)
  317. {
  318. float cSearchRadius = exp2(i);
  319. float2 cOffset = MXAO.scaledcoord.xy + cdir * cSearchRadius * searchRadiusScaled;
  320.  
  321. float3 cN = tex2Dlod(sMXAO_NormalTex,float4(cOffset,0,0)).xyz * 2.0 - 1.0;
  322. float3 cP = GetPositionLOD(cOffset.xy,MXAO,0);
  323.  
  324. float3 cDelta = cP - P;
  325. float validWeightDistance = saturate(1.0 - dot(cDelta,cDelta) * 20.0 / cSearchRadius);
  326. float Angle = dot(N.xyz,cN.xyz);
  327. float validWeightAngle = smoothstep(0.3,0.98,Angle) * smoothstep(1.0,0.98,Angle); //only take normals into account that are NOT equal to the current normal.
  328.  
  329. float validWeight = saturate(3.0 * validWeightDistance * validWeightAngle / cSearchRadius);
  330.  
  331. likelyFace[iDirection] = lerp(likelyFace[iDirection],cN.xyz, validWeight);
  332. }
  333. }
  334.  
  335. N = normalize(likelyFace[0] + likelyFace[1] + likelyFace[2] + likelyFace[3]);
  336. }
  337.  
  338. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  339.  
  340. float GetCullingMask(in MXAO_VSOUT MXAO)
  341. {
  342. float4 cOffsets = float4(ReShade::PixelSize.xy,-ReShade::PixelSize.xy) * 8;
  343. float cullingArea = tex2D(sMXAO_CullingTex, MXAO.scaledcoord.xy + cOffsets.xy).x;
  344. cullingArea += tex2D(sMXAO_CullingTex, MXAO.scaledcoord.xy + cOffsets.zy).x;
  345. cullingArea += tex2D(sMXAO_CullingTex, MXAO.scaledcoord.xy + cOffsets.xw).x;
  346. cullingArea += tex2D(sMXAO_CullingTex, MXAO.scaledcoord.xy + cOffsets.zw).x;
  347. return cullingArea > 0.000001;
  348. }
  349.  
  350. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  351. // Pixel Shaders
  352. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  353.  
  354. void PS_InputBufferSetup(in MXAO_VSOUT MXAO, out float4 color : SV_Target0, out float4 depth : SV_Target1, out float4 normal : SV_Target2)
  355. {
  356. float3 offs = float3(ReShade::PixelSize.xy,0);
  357.  
  358. float3 f = GetPosition(MXAO.texcoord.xy, MXAO);
  359. float3 gradx1 = - f + GetPosition(MXAO.texcoord.xy + offs.xz, MXAO);
  360. float3 gradx2 = f - GetPosition(MXAO.texcoord.xy - offs.xz, MXAO);
  361. float3 grady1 = - f + GetPosition(MXAO.texcoord.xy + offs.zy, MXAO);
  362. float3 grady2 = f - GetPosition(MXAO.texcoord.xy - offs.zy, MXAO);
  363.  
  364. gradx1 = lerp(gradx1, gradx2, abs(gradx1.z) > abs(gradx2.z));
  365. grady1 = lerp(grady1, grady2, abs(grady1.z) > abs(grady2.z));
  366.  
  367. normal = float4(normalize(cross(grady1,gradx1)) * 0.5 + 0.5,0.0);
  368. color = tex2D(ReShade::BackBuffer, MXAO.texcoord.xy);
  369. depth = GetLinearDepth(MXAO.texcoord.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  370. }
  371.  
  372. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  373.  
  374. void PS_Culling(in MXAO_VSOUT MXAO, out float color : SV_Target0)
  375. {
  376. color = 0.0;
  377. MXAO.scaledcoord.xy = MXAO.texcoord.xy;
  378. MXAO.samples = clamp(MXAO.samples, 8, 32);
  379.  
  380. float3 P = GetPositionLOD(MXAO.scaledcoord.xy, MXAO, 0);
  381. float3 N = tex2D(sMXAO_NormalTex, MXAO.scaledcoord.xy).xyz * 2.0 - 1.0;
  382.  
  383. P += N * P.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  384.  
  385. float scaledRadius;
  386. float falloffFactor;
  387. SetupAOParameters(MXAO, P, 0, scaledRadius, falloffFactor);
  388.  
  389. float randStep = dot(floor(MXAO.position.xy % 4 + 0.1),int2(1,4)) + 1;
  390. randStep *= 0.0625;
  391.  
  392. float2 sampleUV, Dir;
  393. sincos(38.39941 * randStep, Dir.x, Dir.y);
  394.  
  395. Dir *= scaledRadius;
  396.  
  397. [loop]
  398. for(int iSample=0; iSample < MXAO.samples; iSample++)
  399. {
  400. sampleUV = MXAO.scaledcoord.xy + Dir.xy * float2(1.0, ReShade::AspectRatio) * (iSample + randStep);
  401. Dir.xy = mul(Dir.xy, float2x2(0.76465,-0.64444,0.64444,0.76465));
  402.  
  403. float sampleMIP = saturate(scaledRadius * iSample * 20.0) * 3.0;
  404.  
  405. float3 V = -P + GetPositionLOD(sampleUV, MXAO, sampleMIP + MXAO_MIPLEVEL_AO);
  406. float VdotV = dot(V, V);
  407. float VdotN = dot(V, N) * rsqrt(VdotV);
  408.  
  409. float fAO = saturate(1.0 + falloffFactor * VdotV) * saturate(VdotN - MXAO_SAMPLE_NORMAL_BIAS * 0.5);
  410. color += fAO;
  411. }
  412. }
  413.  
  414. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  415.  
  416. void PS_StencilSetup(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  417. {
  418. if( GetLinearDepth(MXAO.scaledcoord.xy) >= MXAO_FADE_DEPTH_END
  419. || 0.25 * 0.5 * MXAO_SAMPLE_RADIUS / (tex2D(sMXAO_DepthTex,MXAO.scaledcoord.xy).x + 2.0) * BUFFER_HEIGHT < 1.0
  420. || MXAO.scaledcoord.x > 1.0
  421. || MXAO.scaledcoord.y > 1.0
  422. || GetCullingMask(MXAO) < 0.5
  423. ) discard;
  424.  
  425. color = 1.0;
  426. }
  427.  
  428. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  429.  
  430. void PS_AmbientObscurance(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  431. {
  432. color = 0.0;
  433.  
  434. float3 P = GetPositionLOD(MXAO.scaledcoord.xy, MXAO, 0);
  435. float3 N = tex2D(sMXAO_NormalTex, MXAO.scaledcoord.xy).xyz * 2.0 - 1.0;
  436. float layerID = (MXAO.position.x + MXAO.position.y) % 2.0;
  437.  
  438. #if(MXAO_SMOOTHNORMALS != 0)
  439. TesselateNormals(N, P, MXAO);
  440. #endif
  441.  
  442. P += N * P.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  443.  
  444. float scaledRadius;
  445. float falloffFactor;
  446. SetupAOParameters(MXAO, P, layerID, scaledRadius, falloffFactor);
  447.  
  448. float randStep = dot(floor(MXAO.position.xy % 4 + 0.1),int2(1,4)) + 1;
  449. randStep *= 0.0625;
  450.  
  451. float2 sampleUV, Dir;
  452. sincos(38.39941 * randStep, Dir.x, Dir.y);
  453.  
  454. Dir *= scaledRadius;
  455.  
  456. [loop]
  457. for(int iSample=0; iSample < MXAO.samples; iSample++)
  458. {
  459. sampleUV = MXAO.scaledcoord.xy + Dir.xy * float2(1.0, ReShade::AspectRatio) * (iSample + randStep);
  460. Dir.xy = mul(Dir.xy, float2x2(0.76465,-0.64444,0.64444,0.76465));
  461.  
  462. float sampleMIP = saturate(scaledRadius * iSample * 20.0) * 3.0;
  463.  
  464. float3 V = -P + GetPositionLOD(sampleUV, MXAO, sampleMIP + MXAO_MIPLEVEL_AO);
  465. float VdotV = dot(V, V);
  466. float VdotN = dot(V, N) * rsqrt(VdotV);
  467.  
  468. float fAO = saturate(1.0 + falloffFactor * VdotV) * saturate(VdotN - MXAO_SAMPLE_NORMAL_BIAS);
  469.  
  470. #if(MXAO_ENABLE_IL != 0)
  471. if(fAO > 0.1)
  472. {
  473. float3 fIL = tex2Dlod(sMXAO_ColorTex, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz;
  474. float3 tN = tex2Dlod(sMXAO_NormalTex, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz * 2.0 - 1.0;
  475. fIL = fIL - fIL*saturate(dot(V,tN)*rsqrt(VdotV)*2.0);
  476. color += float4(fIL*fAO,fAO - fAO * dot(fIL,0.333));
  477. }
  478. #else
  479. color.w += fAO;
  480. #endif
  481. }
  482.  
  483. color = saturate(color/((1.0-MXAO_SAMPLE_NORMAL_BIAS)*MXAO.samples));
  484. color = sqrt(color); //AO denoise
  485.  
  486. #if(MXAO_TWO_LAYER != 0)
  487. color = pow(color,1.0 / lerp(MXAO_AMOUNT_COARSE, MXAO_AMOUNT_FINE, layerID));
  488. #endif
  489. }
  490.  
  491. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  492.  
  493. void PS_BlurX(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  494. {
  495. #if(BUFFER_SHIT != 0)
  496. color = BlurFilter(MXAO, sMXAO_BlurTex1, MXAO_GLOBAL_RENDER_SCALE, 1.0, 8);
  497. #else
  498. color = BlurFilter(MXAO, ReShade::BackBuffer, MXAO_GLOBAL_RENDER_SCALE, 1.0, 8);
  499. #endif
  500. }
  501.  
  502. void PS_BlurYandCombine(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  503. {
  504. #if(BUFFER_SHIT != 0)
  505. float4 aoil = BlurFilter(MXAO, sMXAO_BlurTex2, 1.0, 0.75/MXAO_GLOBAL_RENDER_SCALE, 4);
  506. #else
  507. float4 aoil = BlurFilter(MXAO, ReShade::BackBuffer, 1.0, 0.75/MXAO_GLOBAL_RENDER_SCALE, 4);
  508. #endif
  509. aoil *= aoil; //AO denoise
  510.  
  511. color = tex2D(sMXAO_ColorTex, MXAO.texcoord.xy);
  512.  
  513. float scenedepth = GetLinearDepth(MXAO.texcoord.xy);
  514. float3 lumcoeff = float3(0.2126, 0.7152, 0.0722);
  515. float colorgray = dot(color.rgb,lumcoeff);
  516. float blendfact = 1.0 - colorgray;
  517.  
  518. #if(MXAO_ENABLE_IL != 0)
  519. aoil.xyz = lerp(dot(aoil.xyz,lumcoeff),aoil.xyz, MXAO_SSIL_SATURATION) * MXAO_SSIL_AMOUNT * 4.0;
  520. #else
  521. aoil.xyz = 0.0;
  522. #endif
  523.  
  524. aoil.w = 1.0-pow(1.0-aoil.w, MXAO_SSAO_AMOUNT*4.0);
  525. aoil = lerp(aoil,0.0,smoothstep(MXAO_FADE_DEPTH_START, MXAO_FADE_DEPTH_END, scenedepth * float4(2.0,2.0,2.0,1.0)));
  526.  
  527. if(MXAO_BLEND_TYPE == 0)
  528. {
  529. color.rgb -= (aoil.www - aoil.xyz) * blendfact * color.rgb;
  530. }
  531. else if(MXAO_BLEND_TYPE == 1)
  532. {
  533. color.rgb = color.rgb * saturate(1.0 - aoil.www * blendfact * 1.2) + aoil.xyz * blendfact * colorgray * 2.0;
  534. }
  535. else if(MXAO_BLEND_TYPE == 2)
  536. {
  537. float colordiff = saturate(2.0 * distance(normalize(color.rgb + 1e-6),normalize(aoil.rgb + 1e-6)));
  538. color.rgb = color.rgb + aoil.rgb * lerp(color.rgb, dot(color.rgb, 0.3333), colordiff) * blendfact * blendfact * 4.0;
  539. color.rgb = color.rgb * (1.0 - aoil.www * (1.0 - dot(color.rgb, lumcoeff)));
  540. }
  541. else if(MXAO_BLEND_TYPE == 3)
  542. {
  543. color.rgb = pow(color.rgb,2.2);
  544. color.rgb -= (aoil.www - aoil.xyz) * color.rgb;
  545. color.rgb = pow(color.rgb,1.0/2.2);
  546. }
  547.  
  548. color.rgb = saturate(color.rgb);
  549.  
  550. if(MXAO_DEBUG_VIEW_ENABLE == 1) //can't move this into ternary as one is preprocessor def and the other is a uniform
  551. {
  552. color.rgb = max(0.0,1.0 - aoil.www + aoil.xyz);
  553. color.rgb *= (MXAO_ENABLE_IL != 0) ? 0.5 : 1.0;
  554. }
  555. else if(MXAO_DEBUG_VIEW_ENABLE == 2)
  556. {
  557. color.rgb = max(0.0,1.0 - aoil.www + aoil.xyz);
  558. color.rgb *= (MXAO_ENABLE_IL != 0) ? 0.5 : 1.0;
  559. color.rgb *= GetCullingMask(MXAO)*0.5+0.5;
  560. }
  561. else if(MXAO_DEBUG_VIEW_ENABLE == 3)
  562. {
  563. color.rgb = max(0.0,1.0 - aoil.www + aoil.xyz);
  564. color.rgb *= (MXAO_ENABLE_IL != 0) ? 0.5 : 1.0;
  565. }
  566.  
  567. color.a = 1.0;
  568. }
  569.  
  570. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  571. // Technique
  572. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  573.  
  574. technique MXAO
  575. {
  576.  
  577. pass
  578. {
  579. VertexShader = VS_MXAO;
  580. PixelShader = PS_InputBufferSetup;
  581. RenderTarget0 = MXAO_ColorTex;
  582. RenderTarget1 = MXAO_DepthTex;
  583. RenderTarget2 = MXAO_NormalTex;
  584. }
  585. pass
  586. {
  587. VertexShader = VS_MXAO;
  588. PixelShader = PS_Culling;
  589. RenderTarget = MXAO_CullingTex;
  590. }
  591. pass
  592. {
  593. VertexShader = VS_MXAO;
  594. PixelShader = PS_StencilSetup;
  595. /*Render Target is Backbuffer*/
  596. ClearRenderTargets = true;
  597. StencilEnable = true;
  598. StencilPass = REPLACE;
  599. StencilRef = 1;
  600. }
  601. pass
  602. {
  603. VertexShader = VS_MXAO;
  604. PixelShader = PS_AmbientObscurance;
  605. /*Render Target is Backbuffer*/
  606.  
  607. #if(BUFFER_SHIT != 0)
  608. RenderTarget = MXAO_BlurTex1;
  609. #endif
  610. ClearRenderTargets = true;
  611. StencilEnable = true;
  612. StencilPass = KEEP;
  613. StencilFunc = EQUAL;
  614. StencilRef = 1;
  615. }
  616. pass
  617. {
  618. VertexShader = VS_MXAO;
  619. PixelShader = PS_BlurX;
  620. #if(BUFFER_SHIT != 0)
  621. RenderTarget = MXAO_BlurTex2;
  622. #endif
  623. /*Render Target is Backbuffer*/
  624. }
  625. pass
  626. {
  627. VertexShader = VS_MXAO;
  628. PixelShader = PS_BlurYandCombine;
  629. /*Render Target is Backbuffer*/
  630. }
  631. }
Advertisement
Add Comment
Please, Sign In to add comment