Advertisement
Guest User

MXAO 3.0.3 - custom blending modes, fix bugs

a guest
Oct 18th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.78 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.3 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 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.2;
  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. uniform float MXAO_FRAME_COUNT < source = "framecount"; >;
  148. uniform float MXAO_FRAME_TIME < source = "frametime"; >;
  149.  
  150. #if(MXAO_ENABLE_TSS != 0)
  151. texture2D texSSAOTSS { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  152. sampler2D SamplerSSAOTSS { Texture = texSSAOTSS; };
  153. #endif
  154.  
  155. texture2D texColorBypass { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+MXAO_MIPLEVEL_IL;};
  156. texture2D texDistance { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16F; MipLevels = 5+MXAO_MIPLEVEL_AO;};
  157. texture2D texSurfaceNormal { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+MXAO_MIPLEVEL_IL;};
  158.  
  159. sampler2D SamplerColorBypass { Texture = texColorBypass; };
  160. sampler2D SamplerDistance { Texture = texDistance; };
  161. sampler2D SamplerSurfaceNormal { Texture = texSurfaceNormal; };
  162.  
  163. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  164. // Vertex Shader
  165. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  166.  
  167. struct MXAO_VSOUT
  168. {
  169. float4 position : SV_Position;
  170. float2 texcoord : TEXCOORD0;
  171. float2 scaledcoord : TEXCOORD1;
  172. int samples : TEXCOORD2;
  173. int blursamples : TEXCOORD3;
  174. float3 uvtoviewADD : TEXCOORD4;
  175. float3 uvtoviewMUL : TEXCOORD5;
  176. };
  177.  
  178. MXAO_VSOUT VS_MXAO(in uint id : SV_VertexID)
  179. {
  180. MXAO_VSOUT MXAO;
  181.  
  182. MXAO.texcoord.x = (id == 2) ? 2.0 : 0.0;
  183. MXAO.texcoord.y = (id == 1) ? 2.0 : 0.0;
  184. MXAO.scaledcoord.xy = MXAO.texcoord.xy / MXAO_GLOBAL_RENDER_SCALE;
  185. MXAO.position = float4(MXAO.texcoord.xy * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  186.  
  187. MXAO.samples = 8;
  188. MXAO.blursamples = 3;
  189.  
  190. int blurqualityTweak = (MXAO_GLOBAL_RENDER_SCALE < 0.707) + MXAO_TWO_LAYER - MXAO_ENABLE_TSS;
  191. int samplequalityTweak = -MXAO_GLOBAL_SAMPLE_QUALITY_PRESET * 2 * MXAO_ENABLE_TSS;
  192.  
  193. if( MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 0) { MXAO.samples = 8 + samplequalityTweak; MXAO.blursamples = 3 + blurqualityTweak;}
  194. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 1) { MXAO.samples = 16 + samplequalityTweak; MXAO.blursamples = 3 + blurqualityTweak;}
  195. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 2) { MXAO.samples = 24 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  196. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 3) { MXAO.samples = 32 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  197. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 4) { MXAO.samples = 64 + samplequalityTweak; MXAO.blursamples = 2 + blurqualityTweak;}
  198. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 5) { MXAO.samples = 96 + samplequalityTweak; MXAO.blursamples = 1 + blurqualityTweak;}
  199. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 6) { MXAO.samples = 255 + samplequalityTweak; MXAO.blursamples = 0 + blurqualityTweak;}
  200.  
  201. MXAO.blursamples = max(MXAO.blursamples,0);
  202.  
  203. MXAO.uvtoviewADD = float3(-1.0,-1.0,1.0);
  204. MXAO.uvtoviewMUL = float3(2.0,2.0,0.0);
  205.  
  206. //static const float FOV = 70.0;
  207.  
  208. //MXAO.uvtoviewADD = float3(-tan(radians(FOV * 0.5)).xx,1.0);
  209. //MXAO.uvtoviewADD.y *= BUFFER_WIDTH * BUFFER_RCP_HEIGHT;
  210. //MXAO.uvtoviewMUL = float3(-2.0 * MXAO.uvtoviewADD.xy,0.0);
  211.  
  212. return MXAO;
  213. }
  214.  
  215. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  216. // Functions
  217. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  218.  
  219. float GetLinearDepth(float2 coords)
  220. {
  221. return ReShade::GetLinearizedDepth(coords);
  222. }
  223.  
  224. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  225.  
  226. float3 GetPosition(float2 coords, MXAO_VSOUT MXAO)
  227. {
  228. return (coords.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * GetLinearDepth(coords.xy) * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  229. }
  230.  
  231. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  232.  
  233. float3 GetPositionLOD(float2 coords, MXAO_VSOUT MXAO, int mipLevel)
  234. {
  235. return (coords.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * tex2Dlod(SamplerDistance, float4(coords.xy,0,mipLevel)).x;
  236. }
  237.  
  238. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  239.  
  240. void GetBlurWeight(in float4 tempKey, in float4 centerKey, in float surfacealignment, inout float weight)
  241. {
  242. float depthdiff = abs(tempKey.w - centerKey.w);
  243. float normaldiff = saturate(1.0 - dot(tempKey.xyz,centerKey.xyz));
  244.  
  245. float biggestdiff = 1e-6 + saturate(2.0 - centerKey.w / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE) * max(depthdiff*surfacealignment,normaldiff*2.0);
  246. weight = saturate(0.2 / biggestdiff) * 2.0;
  247. }
  248.  
  249. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  250.  
  251. void GetBlurKeyAndSample(in float2 texcoord, in float inputscale, in sampler inputsampler, inout float4 tempsample, inout float4 key)
  252. {
  253. float4 lodcoord = float4(texcoord.xy,0,0);
  254. tempsample = tex2Dlod(inputsampler,lodcoord * inputscale);
  255. #if(MXAO_ENABLE_IL != 0)
  256. key = float4(tex2Dlod(SamplerSurfaceNormal,lodcoord).xyz*2-1, tex2Dlod(SamplerDistance,lodcoord).x);
  257. #else
  258. key = float4(tempsample.xyz *2-1, tex2Dlod(SamplerDistance,lodcoord).x);
  259. #endif
  260. }
  261.  
  262. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  263.  
  264. float4 GetBlurredAO(MXAO_VSOUT MXAO, sampler inputsampler, float2 axisscaled, float inputscale)
  265. {
  266. float4 tempsample;
  267. float4 centerkey, tempkey;
  268. float centerweight = 1.0, tempweight;
  269. float4 blurcoord = 0.0;
  270.  
  271. GetBlurKeyAndSample(MXAO.texcoord.xy,inputscale,inputsampler,tempsample,centerkey);
  272. float surfacealignment = saturate(-dot(centerkey.xyz,normalize(float3(MXAO.texcoord.xy*2.0-1.0,1.0)*centerkey.w)));
  273.  
  274. #if(MXAO_ENABLE_IL != 0)
  275. #define BLUR_COMP_SWIZZLE xyzw
  276. #else
  277. #define BLUR_COMP_SWIZZLE w
  278. #endif
  279.  
  280. float4 blurSum = tempsample.BLUR_COMP_SWIZZLE;
  281.  
  282. [loop]
  283. for(int iStep = 1; iStep <= MXAO.blursamples; iStep++)
  284. {
  285. float currentLinearstep = iStep * 2.0 - 0.5;
  286.  
  287. GetBlurKeyAndSample(MXAO.texcoord.xy + currentLinearstep * axisscaled, inputscale, inputsampler, tempsample, tempkey);
  288. GetBlurWeight(tempkey, centerkey, surfacealignment, tempweight);
  289.  
  290. blurSum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  291. centerweight += tempweight;
  292.  
  293. GetBlurKeyAndSample(MXAO.texcoord.xy - currentLinearstep * axisscaled, inputscale, inputsampler, tempsample, tempkey);
  294. GetBlurWeight(tempkey, centerkey, surfacealignment, tempweight);
  295.  
  296. blurSum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  297. centerweight += tempweight;
  298. }
  299.  
  300. blurSum.BLUR_COMP_SWIZZLE /= centerweight;
  301.  
  302. #if(MXAO_ENABLE_IL == 0)
  303. blurSum.xyz = centerkey.xyz*0.5+0.5;
  304. #endif
  305.  
  306. return blurSum;
  307. }
  308.  
  309. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  310.  
  311. float2 GetSampleJitter(float2 pixelpos, int maxLevel)
  312. {
  313. float finalBayer = 0.0;
  314.  
  315. for(float i = 1-maxLevel; i<= 0; i++)
  316. {
  317. float2 bayerCoord = floor(pixelpos * exp2(i)) % 2.0;
  318. float bayer = 2.0 * bayerCoord.x - 4.0 * bayerCoord.x * bayerCoord.y + 3.0 * bayerCoord.y;
  319. finalBayer += exp2(2.0*(i+maxLevel))* bayer;
  320. }
  321.  
  322. finalBayer /= 4.0 * exp2(2.0 * maxLevel)- 4.0;
  323. float2 sampleJitter = finalBayer;
  324.  
  325. #if(MXAO_ENABLE_TSS != 0)
  326. sampleJitter = frac(sampleJitter + MXAO_FRAME_COUNT * float2(1,-1) * 10.0 / 7.0);
  327. #endif
  328.  
  329. return sampleJitter;
  330. }
  331.  
  332. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  333.  
  334. void SetupAOParameters(in MXAO_VSOUT MXAO, in float3 P, in float layerID, out float scaledRadius, out float falloffFactor)
  335. {
  336. scaledRadius = 0.25 * MXAO_SAMPLE_RADIUS / (MXAO.samples * (P.z + 2.0));
  337. falloffFactor = -1.0/(MXAO_SAMPLE_RADIUS * MXAO_SAMPLE_RADIUS);
  338.  
  339. #if(MXAO_TWO_LAYER != 0)
  340. scaledRadius *= lerp(1.0,MXAO_SAMPLE_RADIUS_SECONDARY,layerID);
  341. falloffFactor *= lerp(1.0,1.0/(MXAO_SAMPLE_RADIUS_SECONDARY*MXAO_SAMPLE_RADIUS_SECONDARY),layerID);
  342. #endif
  343. }
  344.  
  345. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  346.  
  347. void TesselateNormals(inout float3 N, in float3 P, MXAO_VSOUT MXAO)
  348. {
  349. float2 searchRadiusScaled = 0.018 / P.z * float2(1.0,ReShade::AspectRatio);
  350. float3 likelyFace[4] = {N,N,N,N};
  351.  
  352. for(int iDirection=0; iDirection < 4; iDirection++)
  353. {
  354. float2 cdir;
  355. sincos(6.28318548 * 0.25 * iDirection,cdir.y,cdir.x);
  356. for(int i=1; i<=5; i++)
  357. {
  358. float cSearchRadius = exp2(i);
  359. float2 cOffset = MXAO.scaledcoord.xy + cdir * cSearchRadius * searchRadiusScaled;
  360.  
  361. float3 cN = tex2Dlod(SamplerSurfaceNormal,float4(cOffset,0,0)).xyz * 2.0 - 1.0;
  362. float3 cP = GetPositionLOD(cOffset.xy,MXAO,0);
  363.  
  364. float3 cDelta = cP - P;
  365. float validWeightDistance = saturate(1.0 - dot(cDelta,cDelta) * 20.0 / cSearchRadius);
  366. float Angle = dot(N.xyz,cN.xyz);
  367. 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.
  368.  
  369. float validWeight = saturate(3.0 * validWeightDistance * validWeightAngle / cSearchRadius);
  370.  
  371. likelyFace[iDirection] = lerp(likelyFace[iDirection],cN.xyz, validWeight);
  372. }
  373. }
  374.  
  375. N = normalize(likelyFace[0] + likelyFace[1] + likelyFace[2] + likelyFace[3]);
  376. }
  377.  
  378. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  379. // Pixel Shaders
  380. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  381.  
  382. void PS_InputBufferSetup(MXAO_VSOUT MXAO, out float4 color : SV_Target0, out float4 depth : SV_Target1, out float4 normal : SV_Target2)
  383. {
  384. float3 offs = float3(ReShade::PixelSize.xy,0);
  385.  
  386. float3 f = GetPosition(MXAO.texcoord.xy, MXAO);
  387. float3 gradx1 = - f + GetPosition(MXAO.texcoord.xy + offs.xz, MXAO);
  388. float3 gradx2 = f - GetPosition(MXAO.texcoord.xy - offs.xz, MXAO);
  389. float3 grady1 = - f + GetPosition(MXAO.texcoord.xy + offs.zy, MXAO);
  390. float3 grady2 = f - GetPosition(MXAO.texcoord.xy - offs.zy, MXAO);
  391.  
  392. gradx1 = lerp(gradx1, gradx2, abs(gradx1.z) > abs(gradx2.z));
  393. grady1 = lerp(grady1, grady2, abs(grady1.z) > abs(grady2.z));
  394.  
  395. normal = float4(normalize(cross(grady1,gradx1)) * 0.5 + 0.5,0.0);
  396. color = tex2D(ReShade::BackBuffer, MXAO.texcoord.xy);
  397. depth = GetLinearDepth(MXAO.texcoord.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  398. }
  399.  
  400. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  401.  
  402. void PS_StencilSetup(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  403. {
  404. if( GetLinearDepth(MXAO.scaledcoord.xy) >= MXAO_FADE_DEPTH_END
  405. || 0.25 * 0.5 * MXAO_SAMPLE_RADIUS / (tex2D(SamplerDistance,MXAO.scaledcoord.xy).x + 2.0) * BUFFER_HEIGHT < 1.0
  406. || MXAO.scaledcoord.x > 1.0
  407. || MXAO.scaledcoord.y > 1.0) discard;
  408.  
  409. color = 1.0;
  410. }
  411.  
  412. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  413.  
  414. void PS_AmbientObscurance(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  415. {
  416. color = 0.0;
  417.  
  418. float2 sampleJitter = GetSampleJitter(floor(MXAO.position.xy),4);
  419. float3 P = GetPositionLOD(MXAO.scaledcoord.xy, MXAO, 0);
  420. float3 N = tex2D(SamplerSurfaceNormal, MXAO.scaledcoord.xy).xyz * 2.0 - 1.0;
  421. float layerID = (MXAO.position.x + MXAO.position.y) % 2.0;
  422.  
  423. #if(MXAO_SMOOTHNORMALS != 0)
  424. TesselateNormals(N, P, MXAO);
  425. #endif
  426.  
  427. P += N * P.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  428.  
  429. float scaledRadius;
  430. float falloffFactor;
  431. SetupAOParameters(MXAO, P, layerID, scaledRadius, falloffFactor);
  432.  
  433. float2 sampleUV, Dir;
  434. sincos(6.28318548*sampleJitter.y, Dir.y, Dir.x);
  435. Dir *= scaledRadius;
  436.  
  437. [loop]
  438. for(int iSample=0; iSample < MXAO.samples; iSample++)
  439. {
  440. Dir.xy = mul(Dir.xy, float2x2(0.575,0.81815,-0.81815,0.575));
  441. sampleUV = MXAO.scaledcoord.xy + Dir.xy * float2(1.0,ReShade::AspectRatio) * (iSample + sampleJitter.x);
  442.  
  443. float sampleMIP = saturate(scaledRadius * iSample * 20.0)*5.0;
  444.  
  445. float3 V = -P + GetPositionLOD(sampleUV, MXAO, sampleMIP);
  446. float VdotV = dot(V, V);
  447. float VdotN = dot(V, N) * rsqrt(VdotV);
  448.  
  449. float fAO = saturate(1.0 + falloffFactor * VdotV) * saturate(VdotN - MXAO_SAMPLE_NORMAL_BIAS);
  450.  
  451. #if(MXAO_ENABLE_IL != 0)
  452. if(fAO > 0.1)
  453. {
  454. float3 fIL = tex2Dlod(SamplerColorBypass, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz;
  455. float3 tN = tex2Dlod(SamplerSurfaceNormal, float4(sampleUV,0,sampleMIP + MXAO_MIPLEVEL_IL)).xyz * 2.0 - 1.0;
  456. fIL = fIL - fIL*saturate(dot(V,tN)*rsqrt(VdotV)*2.0);
  457. color += float4(fIL*fAO,fAO - fAO * dot(fIL,0.333));
  458. }
  459. #else
  460. color.w += fAO;
  461. #endif
  462. }
  463.  
  464. color = saturate(color/((1.0-MXAO_SAMPLE_NORMAL_BIAS)*MXAO.samples));
  465. color = sqrt(color); //AO denoise
  466.  
  467. #if(MXAO_TWO_LAYER != 0)
  468. color = pow(color,1.0 / lerp(MXAO_AMOUNT_COARSE, MXAO_AMOUNT_FINE, layerID));
  469. #endif
  470.  
  471. #if(MXAO_ENABLE_IL == 0)
  472. color.xyz = N * 0.5 + 0.5;
  473. #endif
  474.  
  475. #if(MXAO_ENABLE_TSS != 0)
  476. color = lerp(tex2D(SamplerSSAOTSS,MXAO.scaledcoord.xy),color,0.2);
  477. #endif
  478. }
  479.  
  480. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  481.  
  482. void PS_BlurX(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  483. {
  484. color = GetBlurredAO(MXAO, ReShade::BackBuffer, float2(ReShade::PixelSize.x,0.0), MXAO_GLOBAL_RENDER_SCALE);
  485. }
  486.  
  487. void PS_BlurYandCombine(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  488. {
  489. #if(MXAO_ENABLE_TSS != 0)
  490. float4 aoil = GetBlurredAO(MXAO, SamplerSSAOTSS , float2(0.0,ReShade::PixelSize.y), 1.0);
  491. #else
  492. float4 aoil = GetBlurredAO(MXAO, ReShade::BackBuffer, float2(0.0,ReShade::PixelSize.y), 1.0);
  493. #endif
  494.  
  495. #if(MXAO_ENABLE_IL == 0)
  496. aoil.rgb = 0;
  497. #endif
  498.  
  499. aoil *= aoil; //AO denoise
  500. color = tex2D(SamplerColorBypass, MXAO.texcoord.xy);
  501.  
  502. float scenedepth = GetLinearDepth(MXAO.texcoord.xy);
  503. float3 lumcoeff = float3(0.2126, 0.7152, 0.0722);
  504. float colorgray = dot(color.rgb,lumcoeff);
  505. float blendfact = 1.0 - colorgray;
  506.  
  507. #if(MXAO_ENABLE_IL != 0)
  508. aoil.xyz = lerp(dot(aoil.xyz,lumcoeff),aoil.xyz, MXAO_SSIL_SATURATION) * MXAO_SSIL_AMOUNT * 4.0;
  509. #endif
  510. aoil.w = 1-pow(1.0-aoil.w, MXAO_SSAO_AMOUNT*4.0);
  511.  
  512. //aoil = (MXAO_DEBUG_VIEW_ENABLE) ? aoil : lerp(aoil, 0.0, colorgray);
  513. aoil = lerp(aoil,0.0,smoothstep(MXAO_FADE_DEPTH_START, MXAO_FADE_DEPTH_END, scenedepth * float4(2.0,2.0,2.0,1.0)));
  514.  
  515. switch(MXAO_BLEND_TYPE)
  516. {
  517. case 0:
  518. {
  519. //color.rgb *= max(0.0,1.0 - (aoil.www - aoil.xyz) * blendfact);
  520. color.rgb -= (aoil.www - aoil.xyz) * blendfact * color.rgb;
  521. break;
  522. }
  523. case 1:
  524. {
  525. color.rgb = color.rgb * saturate(1-aoil.www * blendfact * 1.2) + aoil.xyz * blendfact * colorgray * 2.0;
  526. break;
  527. }
  528. case 2:
  529. {
  530. float colordiff = saturate(2.0 * distance(normalize(color.rgb+1e-6),normalize(aoil.rgb+1e-6)));
  531. color.rgb = color.rgb + aoil.rgb * lerp(color.rgb,dot(color.rgb,0.3333),colordiff) * blendfact * blendfact * 4.0;
  532. color.rgb = color.rgb * (1.0 - aoil.www * (1-dot(color.rgb,lumcoeff)));
  533. break;
  534. }
  535. }
  536.  
  537. color.rgb = saturate(color.rgb);
  538.  
  539. if(MXAO_DEBUG_VIEW_ENABLE) //can't move this into ternary as one is preprocessor def and the other is a uniform
  540. {
  541. color.rgb = max(0.0,1.0 - aoil.www + aoil.xyz);
  542. color.rgb *= (MXAO_ENABLE_IL != 0) ? 0.5 : 1.0;
  543. }
  544.  
  545. color.a = 1.0;
  546. }
  547.  
  548. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  549. // Technique
  550. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  551.  
  552. technique MXAO
  553. {
  554.  
  555. pass
  556. {
  557. VertexShader = VS_MXAO;
  558. PixelShader = PS_InputBufferSetup;
  559. RenderTarget0 = texColorBypass;
  560. RenderTarget1 = texDistance;
  561. RenderTarget2 = texSurfaceNormal;
  562. }
  563. pass
  564. {
  565. VertexShader = VS_MXAO;
  566. PixelShader = PS_StencilSetup;
  567. /*Render Target is Backbuffer*/
  568. ClearRenderTargets = true;
  569. StencilEnable = true;
  570. StencilPass = REPLACE;
  571. StencilRef = 1;
  572. }
  573. pass
  574. {
  575. VertexShader = VS_MXAO;
  576. PixelShader = PS_AmbientObscurance;
  577. /*Render Target is Backbuffer*/
  578. ClearRenderTargets = true;
  579. StencilEnable = true;
  580. StencilPass = KEEP;
  581. StencilFunc = EQUAL;
  582. StencilRef = 1;
  583. }
  584. pass
  585. {
  586. VertexShader = VS_MXAO;
  587. PixelShader = PS_BlurX;
  588. #if(MXAO_ENABLE_TSS != 0)
  589. RenderTarget = texSSAOTSS;
  590. #endif
  591. }
  592. pass
  593. {
  594. VertexShader = VS_MXAO;
  595. PixelShader = PS_BlurYandCombine;
  596. /*Render Target is Backbuffer*/
  597. }
  598. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement