Advertisement
Guest User

MXAO 3.0.1 - customized blending math

a guest
Oct 17th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.55 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.0.1 by Marty McFly
  6. // CC BY-NC-ND 3.0 licensed.
  7. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8.  
  9. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  10. // Preprocessor Settings
  11. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12.  
  13. #ifndef MXAO_MIPLEVEL_AO
  14. #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
  15. #endif
  16.  
  17. #ifndef MXAO_MIPLEVEL_IL
  18. #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.
  19. #endif
  20.  
  21. #ifndef MXAO_ENABLE_IL
  22. #define MXAO_ENABLE_IL 0 //[0 or 1] Enables Indirect Lighting calculation. Will cause a major fps hit.
  23. #endif
  24.  
  25. #ifndef MXAO_SMOOTHNORMALS
  26. #define MXAO_SMOOTHNORMALS 0 //[0 or 1] This feature makes low poly surfaces smoother, especially useful on older games.
  27. #endif
  28.  
  29. #ifndef MXAO_TWO_LAYER
  30. #define MXAO_TWO_LAYER 0 //[0 or 1] Splits MXAO into two separate layers that allow for both large and fine AO.
  31. #endif
  32.  
  33. #ifndef MXAO_ENABLE_TSS
  34. #define MXAO_ENABLE_TSS 0 //[0 or 1] Combines the current frame AO with older frames to improve quality a LOT, at the expense of some ghosting.
  35. #endif
  36.  
  37. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  38. // UI variables
  39. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  40.  
  41. uniform int MXAO_GLOBAL_SAMPLE_QUALITY_PRESET <
  42. ui_type = "combo";
  43. ui_label = "Sample Quality";
  44. ui_items = "Very Low\0Low\0Medium\0High\0Very High\0Ultra\0Maximum\0";
  45. ui_tooltip = "Global quality control, main performance knob. Higher radii might require higher quality.";
  46. > = 1;
  47.  
  48. uniform float MXAO_SAMPLE_RADIUS <
  49. ui_type = "drag";
  50. ui_min = 1.0; ui_max = 20.0;
  51. ui_label = "Sample Radius";
  52. ui_tooltip = "Sample radius of MXAO, higher means more large-scale occlusion with less fine-scale details.";
  53. > = 2.5;
  54.  
  55. uniform float MXAO_SAMPLE_NORMAL_BIAS <
  56. ui_type = "drag";
  57. ui_min = 0.0; ui_max = 0.8;
  58. ui_label = "Normal Bias";
  59. ui_tooltip = "Occlusion Cone bias to reduce self-occlusion of surfaces that have a low angle to each other.";
  60. > = 0.2;
  61.  
  62. uniform float MXAO_GLOBAL_RENDER_SCALE <
  63. ui_type = "drag";
  64. ui_label = "Render Size Scale";
  65. ui_min = 0.50; ui_max = 1.00;
  66. 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...";
  67. > = 1.0;
  68.  
  69. uniform float MXAO_SSAO_AMOUNT <
  70. ui_type = "drag";
  71. ui_min = 0.00; ui_max = 3.00;
  72. ui_label = "Ambient Occlusion Amount";
  73. ui_tooltip = "Intensity of AO effect. Can cause pitch black clipping if set too high.";
  74. > = 2.00;
  75.  
  76. #if(MXAO_ENABLE_IL != 0)
  77. uniform float MXAO_SSIL_AMOUNT <
  78. ui_type = "drag";
  79. ui_min = 0.00; ui_max = 12.00;
  80. ui_label = "Indirect Lighting Amount";
  81. ui_tooltip = "Intensity of IL effect. Can cause overexposured white spots if set too high.";
  82. > = 4.00;
  83.  
  84. uniform float MXAO_SSIL_SATURATION <
  85. ui_type = "drag";
  86. ui_min = 0.00; ui_max = 3.00;
  87. ui_label = "Indirect Lighting Saturation";
  88. ui_tooltip = "Controls color saturation of IL effect.";
  89. > = 1.00;
  90. #endif
  91.  
  92. #if (MXAO_TWO_LAYER != 0)
  93. uniform float MXAO_SAMPLE_RADIUS_SECONDARY <
  94. ui_type = "drag";
  95. ui_min = 0.1; ui_max = 1.00;
  96. ui_label = "Fine AO Scale";
  97. 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.";
  98. > = 0.2;
  99.  
  100. uniform float MXAO_AMOUNT_FINE <
  101. ui_type = "drag";
  102. ui_min = 0.00; ui_max = 1.00;
  103. ui_label = "Fine AO intensity multiplier";
  104. ui_tooltip = "Intensity of small scale AO / IL.";
  105. > = 1.0;
  106.  
  107. uniform float MXAO_AMOUNT_COARSE <
  108. ui_type = "drag";
  109. ui_min = 0.00; ui_max = 1.00;
  110. ui_label = "Coarse AO intensity multiplier";
  111. ui_tooltip = "Intensity of large scale AO / IL.";
  112. > = 1.0;
  113. #endif
  114.  
  115. uniform bool MXAO_DEBUG_VIEW_ENABLE <
  116. ui_label = "Enable Debug View";
  117. ui_tooltip = "Enables raw MXAO output for debugging and tuning purposes.";
  118. > = false;
  119.  
  120. uniform float MXAO_FADE_DEPTH_START <
  121. ui_type = "drag";
  122. ui_label = "Fade Out Start";
  123. ui_min = 0.00; ui_max = 1.00;
  124. ui_tooltip = "Distance where MXAO starts to fade out. 0.0 = camera, 1.0 = sky. Must be less than Fade Out End.";
  125. > = 0.2;
  126.  
  127. uniform float MXAO_FADE_DEPTH_END <
  128. ui_type = "drag";
  129. ui_label = "Fade Out End";
  130. ui_min = 0.00; ui_max = 1.00;
  131. ui_tooltip = "Distance where MXAO completely fades out. 0.0 = camera, 1.0 = sky. Must be greater than Fade Out Start.";
  132. > = 0.4;
  133.  
  134. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  135. // Textures, Samplers
  136. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  137.  
  138. #include "ReShade.fxh"
  139.  
  140. uniform float MXAO_FRAME_COUNT < source = "framecount"; >;
  141. uniform float MXAO_FRAME_TIME < source = "frametime"; >;
  142.  
  143. #if(MXAO_ENABLE_TSS != 0)
  144. texture2D texSSAOTSS { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  145. sampler2D SamplerSSAOTSS { Texture = texSSAOTSS; };
  146. #endif
  147.  
  148. texture2D texColorBypass { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+MXAO_MIPLEVEL_IL;};
  149. texture2D texDistance { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16F; MipLevels = 5+MXAO_MIPLEVEL_AO;};
  150. texture2D texSurfaceNormal { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+MXAO_MIPLEVEL_IL;};
  151.  
  152. sampler2D SamplerColorBypass { Texture = texColorBypass; };
  153. sampler2D SamplerDistance { Texture = texDistance; };
  154. sampler2D SamplerSurfaceNormal { Texture = texSurfaceNormal; };
  155.  
  156. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  157. // Vertex Shader
  158. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  159.  
  160. struct MXAO_VSOUT
  161. {
  162. float4 position : SV_Position;
  163. float2 texcoord : TEXCOORD0;
  164. float2 scaledcoord : TEXCOORD1;
  165. int samples : TEXCOORD2;
  166. int blursamples : TEXCOORD3;
  167. float3 uvtoviewADDMUL[2] : TEXCOORD4;
  168. };
  169.  
  170. MXAO_VSOUT VS_MXAO(in uint id : SV_VertexID)
  171. {
  172. MXAO_VSOUT MXAO;
  173.  
  174. MXAO.texcoord.x = (id == 2) ? 2.0 : 0.0;
  175. MXAO.texcoord.y = (id == 1) ? 2.0 : 0.0;
  176. MXAO.scaledcoord.xy = MXAO.texcoord.xy / MXAO_GLOBAL_RENDER_SCALE;
  177. MXAO.position = float4(MXAO.texcoord.xy * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  178.  
  179. MXAO.samples = 8;
  180. MXAO.blursamples = 3;
  181.  
  182. int blurqualityTweak = (MXAO_GLOBAL_RENDER_SCALE < 0.707) + MXAO_TWO_LAYER - MXAO_ENABLE_TSS;
  183. int samplequalityTweak = -MXAO_GLOBAL_SAMPLE_QUALITY_PRESET * 2 * MXAO_ENABLE_TSS;
  184.  
  185. if( MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 0) { MXAO.samples = 8 + samplequalityTweak; MXAO.blursamples = 3 + blurqualityTweak;}
  186. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 1) { MXAO.samples = 16 + samplequalityTweak; MXAO.blursamples = 3 + blurqualityTweak;}
  187. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 2) { MXAO.samples = 24 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  188. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 3) { MXAO.samples = 32 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  189. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 4) { MXAO.samples = 64 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  190. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 5) { MXAO.samples = 96 + samplequalityTweak; MXAO.blursamples = 1 + blurqualityTweak;}
  191. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 6) { MXAO.samples = 255 + samplequalityTweak; MXAO.blursamples = 0 + blurqualityTweak;}
  192.  
  193. MXAO.blursamples = max(MXAO.blursamples,0);
  194.  
  195. MXAO.uvtoviewADDMUL[0] = float3(-1.0,-1.0,1.0);
  196. MXAO.uvtoviewADDMUL[1] = float3(2.0,2.0,0.0);
  197.  
  198. //static const float FOV = 70.0;
  199.  
  200. //MXAO.uvtoviewADDMUL[0] = float3(-tan(radians(FOV * 0.5)).xx,1.0);
  201. //MXAO.uvtoviewADDMUL[0].y *= BUFFER_WIDTH * BUFFER_RCP_HEIGHT;
  202. //MXAO.uvtoviewADDMUL[1] = float3(-2.0 * MXAO.uvtoviewADDMUL[0].xy,0.0);
  203.  
  204. return MXAO;
  205. }
  206.  
  207. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  208. // Functions
  209. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  210.  
  211. float GetLinearDepth(float2 coords)
  212. {
  213. return ReShade::GetLinearizedDepth(coords);
  214. }
  215.  
  216. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  217.  
  218. float3 GetPosition(float2 coords, MXAO_VSOUT MXAO)
  219. {
  220. return (coords.xyx * MXAO.uvtoviewADDMUL[1] + MXAO.uvtoviewADDMUL[0]) * GetLinearDepth(coords.xy) * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  221. }
  222.  
  223. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  224.  
  225. float3 GetPositionLOD(float2 coords, MXAO_VSOUT MXAO, int mipLevel)
  226. {
  227. return (coords.xyx * MXAO.uvtoviewADDMUL[1] + MXAO.uvtoviewADDMUL[0]) * tex2Dlod(SamplerDistance, 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. float biggestdiff = 1e-6 + saturate(2.0 - centerKey.w / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE) * max(depthdiff*surfacealignment,normaldiff*2.0);
  238. weight = saturate(0.2 / biggestdiff) * 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. #if(MXAO_ENABLE_IL != 0)
  248. key = float4(tex2Dlod(SamplerSurfaceNormal,lodcoord).xyz*2-1, tex2Dlod(SamplerDistance,lodcoord).x);
  249. #else
  250. key = float4(tempsample.xyz *2-1, tex2Dlod(SamplerDistance,lodcoord).x);
  251. #endif
  252. }
  253.  
  254. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  255.  
  256. float4 GetBlurredAO(MXAO_VSOUT MXAO, sampler inputsampler, float2 axisscaled, float inputscale)
  257. {
  258. float4 tempsample;
  259. float4 centerkey, tempkey;
  260. float centerweight = 1.0, tempweight;
  261. float4 blurcoord = 0.0;
  262.  
  263. GetBlurKeyAndSample(MXAO.texcoord.xy,inputscale,inputsampler,tempsample,centerkey);
  264. float surfacealignment = saturate(-dot(centerkey.xyz,normalize(float3(MXAO.texcoord.xy*2.0-1.0,1.0)*centerkey.w)));
  265.  
  266. #if(MXAO_ENABLE_IL != 0)
  267. #define BLUR_COMP_SWIZZLE xyzw
  268. #else
  269. #define BLUR_COMP_SWIZZLE w
  270. #endif
  271.  
  272. float4 blurSum = tempsample.BLUR_COMP_SWIZZLE;
  273.  
  274. [loop]
  275. for(int iStep = 1; iStep <= MXAO.blursamples; iStep++)
  276. {
  277. float currentLinearstep = iStep * 2.0 - 0.5;
  278.  
  279. GetBlurKeyAndSample(MXAO.texcoord.xy + currentLinearstep * axisscaled, inputscale, inputsampler, tempsample, tempkey);
  280. GetBlurWeight(tempkey, centerkey, surfacealignment, tempweight);
  281.  
  282. blurSum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  283. centerweight += tempweight;
  284.  
  285. GetBlurKeyAndSample(MXAO.texcoord.xy - currentLinearstep * axisscaled, inputscale, inputsampler, tempsample, tempkey);
  286. GetBlurWeight(tempkey, centerkey, surfacealignment, tempweight);
  287.  
  288. blurSum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  289. centerweight += tempweight;
  290. }
  291.  
  292. blurSum.BLUR_COMP_SWIZZLE /= centerweight;
  293.  
  294. #if(MXAO_ENABLE_IL == 0)
  295. blurSum.xyz = centerkey.xyz*0.5+0.5;
  296. #endif
  297.  
  298. return blurSum;
  299. }
  300.  
  301. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  302.  
  303. float2 GetSampleJitter(float2 pixelpos, int maxLevel)
  304. {
  305. float finalBayer = 0.0;
  306.  
  307. for(float i = 1-maxLevel; i<= 0; i++)
  308. {
  309. float2 bayerCoord = floor(pixelpos * exp2(i)) % 2.0;
  310. float bayer = 2.0 * bayerCoord.x - 4.0 * bayerCoord.x * bayerCoord.y + 3.0 * bayerCoord.y;
  311. finalBayer += exp2(2.0*(i+maxLevel))* bayer;
  312. }
  313.  
  314. finalBayer /= 4.0 * exp2(2.0 * maxLevel)- 4.0;
  315. float2 sampleJitter = finalBayer;
  316.  
  317. #if(MXAO_ENABLE_TSS != 0)
  318. sampleJitter = frac(sampleJitter + MXAO_FRAME_COUNT * float2(1,-1) * 10.0 / 7.0);
  319. #endif
  320.  
  321. return sampleJitter;
  322. }
  323.  
  324. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  325.  
  326. void SetupAOParameters(in MXAO_VSOUT MXAO, in float3 P, in float layerID, out float scaledRadius, out float falloffFactor)
  327. {
  328. scaledRadius = 0.25 * MXAO_SAMPLE_RADIUS / (MXAO.samples * (P.z + 2.0));
  329. falloffFactor = -1.0/(MXAO_SAMPLE_RADIUS * MXAO_SAMPLE_RADIUS);
  330.  
  331. #if(MXAO_TWO_LAYER != 0)
  332. scaledRadius *= lerp(1.0,MXAO_SAMPLE_RADIUS_SECONDARY,layerID);
  333. falloffFactor *= lerp(1.0,1.0/(MXAO_SAMPLE_RADIUS_SECONDARY*MXAO_SAMPLE_RADIUS_SECONDARY),layerID);
  334. #endif
  335. }
  336.  
  337. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  338.  
  339. void TesselateNormals(inout float3 N, in float3 P, MXAO_VSOUT MXAO)
  340. {
  341. float2 searchRadiusScaled = 0.018 / P.z * float2(1.0,ReShade::AspectRatio);
  342. float3 likelyFace[4] = {N,N,N,N};
  343.  
  344. for(int iDirection=0; iDirection < 4; iDirection++)
  345. {
  346. float2 cdir;
  347. sincos(6.28318548 * 0.25 * iDirection,cdir.y,cdir.x);
  348. for(int i=1; i<=5; i++)
  349. {
  350. float cSearchRadius = exp2(i);
  351. float2 cOffset = MXAO.scaledcoord.xy + cdir * cSearchRadius * searchRadiusScaled;
  352.  
  353. float3 cN = tex2Dlod(SamplerSurfaceNormal,float4(cOffset,0,0)).xyz * 2.0 - 1.0;
  354. float3 cP = GetPositionLOD(cOffset.xy,MXAO,0);
  355.  
  356. float3 cDelta = cP - P;
  357. float validWeightDistance = saturate(1.0 - dot(cDelta,cDelta) * 20.0 / cSearchRadius);
  358. float Angle = dot(N.xyz,cN.xyz);
  359. 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.
  360.  
  361. float validWeight = saturate(3.0 * validWeightDistance * validWeightAngle / cSearchRadius);
  362.  
  363. likelyFace[iDirection] = lerp(likelyFace[iDirection],cN.xyz, validWeight);
  364. }
  365. }
  366.  
  367. N = normalize(likelyFace[0] + likelyFace[1] + likelyFace[2] + likelyFace[3]);
  368. }
  369.  
  370. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  371. // Pixel Shaders
  372. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  373.  
  374. void PS_InputBufferSetup(MXAO_VSOUT MXAO, out float4 color : SV_Target0, out float4 depth : SV_Target1, out float4 normal : SV_Target2)
  375. {
  376. float3 offs = float3(ReShade::PixelSize.xy,0);
  377.  
  378. float3 f = GetPosition(MXAO.texcoord.xy, MXAO);
  379. float3 gradx1 = - f + GetPosition(MXAO.texcoord.xy + offs.xz, MXAO);
  380. float3 gradx2 = f - GetPosition(MXAO.texcoord.xy - offs.xz, MXAO);
  381. float3 grady1 = - f + GetPosition(MXAO.texcoord.xy + offs.zy, MXAO);
  382. float3 grady2 = f - GetPosition(MXAO.texcoord.xy - offs.zy, MXAO);
  383.  
  384. gradx1 = lerp(gradx1, gradx2, abs(gradx1.z) > abs(gradx2.z));
  385. grady1 = lerp(grady1, grady2, abs(grady1.z) > abs(grady2.z));
  386.  
  387. normal = float4(normalize(cross(grady1,gradx1)) * 0.5 + 0.5,0.0);
  388. color = tex2D(ReShade::BackBuffer, MXAO.texcoord.xy);
  389. depth = GetLinearDepth(MXAO.texcoord.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  390. }
  391.  
  392. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  393.  
  394. void PS_StencilSetup(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  395. {
  396. if( GetLinearDepth(MXAO.scaledcoord.xy) >= MXAO_FADE_DEPTH_END
  397. || 0.25 * 0.5 * MXAO_SAMPLE_RADIUS / (tex2D(SamplerDistance,MXAO.scaledcoord.xy).x + 2.0) * BUFFER_HEIGHT < 1.0
  398. || MXAO.scaledcoord.x > 1.0
  399. || MXAO.scaledcoord.y > 1.0) discard;
  400.  
  401. color = 1.0;
  402. }
  403.  
  404. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  405.  
  406. void PS_AmbientObscurance(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  407. {
  408. color = 0.0;
  409.  
  410. float2 sampleJitter = GetSampleJitter(floor(MXAO.position.xy),4);
  411. float3 P = GetPositionLOD(MXAO.scaledcoord.xy, MXAO, 0);
  412. float3 N = tex2D(SamplerSurfaceNormal, MXAO.scaledcoord.xy).xyz * 2.0 - 1.0;
  413. float layerID = (MXAO.position.x + MXAO.position.y) % 2.0;
  414.  
  415. #if(MXAO_SMOOTHNORMALS != 0)
  416. TesselateNormals(N, P, MXAO);
  417. #endif
  418.  
  419. P += N * P.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  420.  
  421. float scaledRadius;
  422. float falloffFactor;
  423. SetupAOParameters(MXAO, P, layerID, scaledRadius, falloffFactor);
  424.  
  425. float2 sampleUV, Dir;
  426. sincos(6.28318548*sampleJitter.y, Dir.y, Dir.x);
  427. Dir *= scaledRadius;
  428.  
  429. [loop]
  430. for(int iSample=0; iSample < MXAO.samples; iSample++)
  431. {
  432. Dir.xy = mul(Dir.xy, float2x2(0.575,0.81815,-0.81815,0.575));
  433. sampleUV = MXAO.scaledcoord.xy + Dir.xy * float2(1.0,ReShade::AspectRatio) * (iSample + sampleJitter.x);
  434.  
  435. float sampleMIP = saturate(scaledRadius * iSample * 20.0)*5.0;
  436.  
  437. float3 V = -P + GetPositionLOD(sampleUV, MXAO, sampleMIP);
  438. float VdotV = dot(V, V);
  439. float VdotN = dot(V, N) * rsqrt(VdotV);
  440.  
  441. float fAO = saturate(1.0 + falloffFactor * VdotV) * saturate(VdotN - MXAO_SAMPLE_NORMAL_BIAS);
  442.  
  443. #if(MXAO_ENABLE_IL != 0)
  444. if(fAO > 0.1)
  445. {
  446. float3 fIL = tex2Dlod(SamplerColorBypass, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz;
  447. float3 tN = tex2Dlod(SamplerSurfaceNormal, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz * 2.0 - 1.0;
  448. fIL = fIL - fIL*saturate(dot(V,tN)*rsqrt(VdotV)*2.0);
  449. color += float4(fIL*fAO,fAO - fAO * dot(fIL,0.333));
  450. }
  451. #else
  452. color.w += fAO;
  453. #endif
  454. }
  455.  
  456. color = saturate(color/((1.0-MXAO_SAMPLE_NORMAL_BIAS)*MXAO.samples));
  457. color = sqrt(color); //AO denoise
  458.  
  459. #if(MXAO_TWO_LAYER != 0)
  460. color = pow(color,1.0 / lerp(MXAO_AMOUNT_COARSE, MXAO_AMOUNT_FINE, layerID));
  461. #endif
  462.  
  463. #if(MXAO_ENABLE_IL == 0)
  464. color.xyz = N * 0.5 + 0.5;
  465. #endif
  466.  
  467. #if(MXAO_ENABLE_TSS != 0)
  468. color = lerp(tex2D(SamplerSSAOTSS,MXAO.scaledcoord.xy),color,0.2);
  469. #endif
  470. }
  471.  
  472. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  473.  
  474. void PS_BlurX(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  475. {
  476. color = GetBlurredAO(MXAO, ReShade::BackBuffer, float2(ReShade::PixelSize.x,0.0), MXAO_GLOBAL_RENDER_SCALE);
  477. }
  478.  
  479. void PS_BlurYandCombine(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  480. {
  481. #if(MXAO_ENABLE_TSS != 0)
  482. float4 aoil = GetBlurredAO(MXAO, SamplerSSAOTSS , float2(0.0,ReShade::PixelSize.y), 1.0);
  483. #else
  484. float4 aoil = GetBlurredAO(MXAO, ReShade::BackBuffer, float2(0.0,ReShade::PixelSize.y), 1.0);
  485. #endif
  486.  
  487. #if(MXAO_ENABLE_IL == 0)
  488. aoil.rgb = 0;
  489. #endif
  490.  
  491. aoil *= aoil; //AO denoise
  492. color = tex2D(SamplerColorBypass, MXAO.texcoord.xy);
  493. float scenedepth = GetLinearDepth(MXAO.texcoord.xy);
  494. float lumcoeff = float3(0.2126, 0.7152, 0.0722);
  495. float colorgray = dot(color.rgb,lumcoeff);
  496.  
  497. #if(MXAO_ENABLE_IL != 0)
  498. aoil.xyz = lerp(dot(aoil.xyz,lumcoeff),aoil.xyz, MXAO_SSIL_SATURATION) * MXAO_SSIL_AMOUNT * 4.0;
  499. #endif
  500. aoil.w = 1-pow(1.0-aoil.w, MXAO_SSAO_AMOUNT*4.0);
  501.  
  502. aoil = (MXAO_DEBUG_VIEW_ENABLE) ? aoil : lerp(aoil, 0.0, colorgray);
  503. aoil = lerp(aoil,0.0,smoothstep(MXAO_FADE_DEPTH_START, MXAO_FADE_DEPTH_END, scenedepth * float4(2.0,2.0,2.0,1.0)));
  504.  
  505. //float3 GI = max(0.0,1.0 - aoil.www + aoil.xyz);
  506. //color.rgb *= GI;
  507.  
  508. color.rgb = color.rgb * (1-aoil.www) + aoil.xyz * (1.0-colorgray) * colorgray * 2.0;
  509. color.rgb = max(color.rgb,0);
  510.  
  511. if(MXAO_DEBUG_VIEW_ENABLE) //can't move this into ternary as one is preprocessor def and the other is a uniform
  512. {
  513. color.rgb = (MXAO_ENABLE_IL != 0) ? GI*0.5 : GI;
  514. }
  515.  
  516. color.a = 1.0;
  517. }
  518.  
  519. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  520. // Technique
  521. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  522.  
  523. technique MXAO
  524. {
  525.  
  526. pass
  527. {
  528. VertexShader = VS_MXAO;
  529. PixelShader = PS_InputBufferSetup;
  530. RenderTarget0 = texColorBypass;
  531. RenderTarget1 = texDistance;
  532. RenderTarget2 = texSurfaceNormal;
  533. }
  534. pass
  535. {
  536. VertexShader = VS_MXAO;
  537. PixelShader = PS_StencilSetup;
  538. /*Render Target is Backbuffer*/
  539. ClearRenderTargets = true;
  540. StencilEnable = true;
  541. StencilPass = REPLACE;
  542. StencilRef = 1;
  543. }
  544. pass
  545. {
  546. VertexShader = VS_MXAO;
  547. PixelShader = PS_AmbientObscurance;
  548. /*Render Target is Backbuffer*/
  549. ClearRenderTargets = true;
  550. StencilEnable = true;
  551. StencilPass = KEEP;
  552. StencilFunc = EQUAL;
  553. StencilRef = 1;
  554. }
  555. pass
  556. {
  557. VertexShader = VS_MXAO;
  558. PixelShader = PS_BlurX;
  559. #if(MXAO_ENABLE_TSS != 0)
  560. RenderTarget = texSSAOTSS;
  561. #endif
  562. }
  563. pass
  564. {
  565. VertexShader = VS_MXAO;
  566. PixelShader = PS_BlurYandCombine;
  567. /*Render Target is Backbuffer*/
  568. }
  569. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement