Advertisement
Guest User

MXAO 3.4.59

a guest
Apr 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.76 KB | None | 0 0
  1. /*=============================================================================
  2.  
  3. ReShade 3.0 effect file
  4. visit facebook.com/MartyMcModding for news/updates
  5.  
  6. Ambient Obscurance with Indirect Lighting "MXAO" 3.4.59 by Marty McFly
  7.  
  8. CC BY-NC-ND 3.0 licensed.
  9.  
  10. =============================================================================*/
  11.  
  12. /*=============================================================================
  13. Preprocessor settings
  14. =============================================================================*/
  15.  
  16. #ifndef MXAO_MIPLEVEL_AO
  17. #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
  18. #endif
  19.  
  20. #ifndef MXAO_MIPLEVEL_IL
  21. #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.
  22. #endif
  23.  
  24. #ifndef MXAO_ENABLE_IL
  25. #define MXAO_ENABLE_IL 0 //[0 or 1] Enables Indirect Lighting calculation. Will cause a major fps hit.
  26. #endif
  27.  
  28. #ifndef MXAO_SMOOTHNORMALS
  29. #define MXAO_SMOOTHNORMALS 0 //[0 or 1] This feature makes low poly surfaces smoother, especially useful on older games.
  30. #endif
  31.  
  32. #ifndef MXAO_TWO_LAYER
  33. #define MXAO_TWO_LAYER 0 //[0 or 1] Splits MXAO into two separate layers that allow for both large and fine AO.
  34. #endif
  35.  
  36. /*=============================================================================
  37. UI Uniforms
  38. =============================================================================*/
  39.  
  40. uniform int MXAO_GLOBAL_SAMPLE_QUALITY_PRESET <
  41. ui_type = "combo";
  42. ui_label = "Sample Quality";
  43. ui_items = "Very Low\0Low\0Medium\0High\0Very High\0Ultra\0Maximum\0";
  44. ui_tooltip = "Global quality control, main performance knob. Higher radii might require higher quality.";
  45. ui_category = "Global Settings";
  46. > = 2;
  47.  
  48. uniform float MXAO_SAMPLE_RADIUS <
  49. ui_type = "drag";
  50. ui_min = 0.5; 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. ui_category = "Global Settings";
  54. > = 2.5;
  55.  
  56. uniform float MXAO_SAMPLE_NORMAL_BIAS <
  57. ui_type = "drag";
  58. ui_min = 0.0; ui_max = 0.8;
  59. ui_label = "Normal Bias";
  60. ui_tooltip = "Occlusion Cone bias to reduce self-occlusion of surfaces that have a low angle to each other.";
  61. ui_category = "Global Settings";
  62. > = 0.2;
  63.  
  64. uniform float MXAO_GLOBAL_RENDER_SCALE <
  65. ui_type = "drag";
  66. ui_label = "Render Size Scale";
  67. ui_min = 0.50; ui_max = 1.00;
  68. 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...";
  69. ui_category = "Global Settings";
  70. > = 1.0;
  71.  
  72. uniform float MXAO_SSAO_AMOUNT <
  73. ui_type = "drag";
  74. ui_min = 0.00; ui_max = 4.00;
  75. ui_label = "Ambient Occlusion Amount";
  76. ui_tooltip = "Intensity of AO effect. Can cause pitch black clipping if set too high.";
  77. ui_category = "Ambient Occlusion Settings";
  78. > = 1.00;
  79.  
  80. #if(MXAO_ENABLE_IL != 0)
  81. uniform float MXAO_SSIL_AMOUNT <
  82. ui_type = "drag";
  83. ui_min = 0.00; ui_max = 12.00;
  84. ui_label = "Indirect Lighting Amount";
  85. ui_tooltip = "Intensity of IL effect. Can cause overexposured white spots if set too high.";
  86. ui_category = "Indirect Lighting Settings";
  87. > = 4.00;
  88.  
  89. uniform float MXAO_SSIL_SATURATION <
  90. ui_type = "drag";
  91. ui_min = 0.00; ui_max = 3.00;
  92. ui_label = "Indirect Lighting Saturation";
  93. ui_tooltip = "Controls color saturation of IL effect.";
  94. ui_category = "Indirect Lighting Settings";
  95. > = 1.00;
  96. #endif
  97.  
  98. #if (MXAO_TWO_LAYER != 0)
  99. uniform float MXAO_SAMPLE_RADIUS_SECONDARY <
  100. ui_type = "drag";
  101. ui_min = 0.1; ui_max = 1.00;
  102. ui_label = "Fine AO Scale";
  103. 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.";
  104. ui_category = "Double Layer Settings";
  105. > = 0.2;
  106.  
  107. uniform float MXAO_AMOUNT_FINE <
  108. ui_type = "drag";
  109. ui_min = 0.00; ui_max = 1.00;
  110. ui_label = "Fine AO intensity multiplier";
  111. ui_tooltip = "Intensity of small scale AO / IL.";
  112. ui_category = "Double Layer Settings";
  113. > = 1.0;
  114.  
  115. uniform float MXAO_AMOUNT_COARSE <
  116. ui_type = "drag";
  117. ui_min = 0.00; ui_max = 1.00;
  118. ui_label = "Coarse AO intensity multiplier";
  119. ui_tooltip = "Intensity of large scale AO / IL.";
  120. ui_category = "Double Layer Settings";
  121. > = 1.0;
  122. #endif
  123.  
  124. uniform int MXAO_BLEND_TYPE <
  125. ui_type = "drag";
  126. ui_min = 0; ui_max = 4;
  127. ui_label = "Blending Mode";
  128. ui_tooltip = "Different blending modes for merging AO/IL with original color.\0Blending mode 0 matches formula of MXAO 2.0 and older.";
  129. ui_category = "Blending Settings";
  130. > = 0;
  131.  
  132. uniform float MXAO_FADE_DEPTH_START <
  133. ui_type = "drag";
  134. ui_label = "Fade Out Start";
  135. ui_min = 0.00; ui_max = 1.00;
  136. ui_tooltip = "Distance where MXAO starts to fade out. 0.0 = camera, 1.0 = sky. Must be less than Fade Out End.";
  137. ui_category = "Blending Settings";
  138. > = 0.05;
  139.  
  140. uniform float MXAO_FADE_DEPTH_END <
  141. ui_type = "drag";
  142. ui_label = "Fade Out End";
  143. ui_min = 0.00; ui_max = 1.00;
  144. ui_tooltip = "Distance where MXAO completely fades out. 0.0 = camera, 1.0 = sky. Must be greater than Fade Out Start.";
  145. ui_category = "Blending Settings";
  146. > = 0.4;
  147.  
  148. uniform int MXAO_DEBUG_VIEW_ENABLE <
  149. ui_type = "combo";
  150. ui_label = "Enable Debug View";
  151. ui_items = "None\0AO/IL channel\0Normal vectors\0";
  152. ui_tooltip = "Different debug outputs";
  153. ui_category = "Debug";
  154. > = 0;
  155.  
  156. /*=============================================================================
  157. Textures, Samplers, Globals
  158. =============================================================================*/
  159.  
  160. #include "ReShade.fxh"
  161.  
  162. texture2D MXAO_ColorTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 3+MXAO_MIPLEVEL_IL;};
  163. texture2D MXAO_DepthTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16F; MipLevels = 3+MXAO_MIPLEVEL_AO;};
  164. texture2D MXAO_NormalTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 3+MXAO_MIPLEVEL_IL;};
  165. //need this - backbuffer isn't guaranteed to be RGBA8 and bit ops for blur are too expensive
  166. texture2D MXAO_BlurTex1 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  167. texture2D MXAO_BlurTex2 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  168.  
  169. sampler2D sMXAO_ColorTex { Texture = MXAO_ColorTex; };
  170. sampler2D sMXAO_DepthTex { Texture = MXAO_DepthTex; };
  171. sampler2D sMXAO_NormalTex { Texture = MXAO_NormalTex; };
  172. sampler2D sMXAO_BlurTex1 { Texture = MXAO_BlurTex1; };
  173. sampler2D sMXAO_BlurTex2 { Texture = MXAO_BlurTex2; };
  174.  
  175. #if(MXAO_ENABLE_IL != 0)
  176. #define BLUR_COMP_SWIZZLE xyzw
  177. #else
  178. #define BLUR_COMP_SWIZZLE w
  179. #endif
  180.  
  181. /*=============================================================================
  182. Vertex Shader
  183. =============================================================================*/
  184.  
  185. struct MXAO_VSOUT
  186. {
  187. float4 vpos : SV_Position;
  188. float4 uv : TEXCOORD0;
  189. float samples : TEXCOORD1;
  190. float3 uvtoviewADD : TEXCOORD4;
  191. float3 uvtoviewMUL : TEXCOORD5;
  192. };
  193.  
  194. MXAO_VSOUT VS_MXAO(in uint id : SV_VertexID)
  195. {
  196. MXAO_VSOUT MXAO;
  197.  
  198. MXAO.uv.x = (id == 2) ? 2.0 : 0.0;
  199. MXAO.uv.y = (id == 1) ? 2.0 : 0.0;
  200. MXAO.uv.zw = MXAO.uv.xy / MXAO_GLOBAL_RENDER_SCALE;
  201. MXAO.vpos = float4(MXAO.uv.xy * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  202.  
  203. MXAO.samples = 8;
  204.  
  205. if( MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 0) { MXAO.samples = 4; }
  206. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 1) { MXAO.samples = 8; }
  207. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 2) { MXAO.samples = 16; }
  208. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 3) { MXAO.samples = 24; }
  209. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 4) { MXAO.samples = 32; }
  210. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 5) { MXAO.samples = 64; }
  211. else if(MXAO_GLOBAL_SAMPLE_QUALITY_PRESET == 6) { MXAO.samples = 255; }
  212.  
  213. MXAO.uvtoviewADD = float3(-1.0,-1.0,1.0);
  214. MXAO.uvtoviewMUL = float3(2.0,2.0,0.0);
  215.  
  216. /* //uncomment to enable perspective-correct position recontruction. Minor difference for common FoV's
  217. static const float FOV = 70.0; //vertical FoV
  218.  
  219. MXAO.uvtoviewADD = float3(-tan(radians(FOV * 0.5)).xx,1.0);
  220. MXAO.uvtoviewADD.y *= ReShade::AspectRatio;
  221. MXAO.uvtoviewMUL = float3(-2.0 * MXAO.uvtoviewADD.xy,0.0);
  222. */
  223. return MXAO;
  224. }
  225.  
  226. /*=============================================================================
  227. Functions
  228. =============================================================================*/
  229.  
  230. float3 get_position_from_uv(in float2 uv, in MXAO_VSOUT MXAO)
  231. {
  232. return (uv.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * ReShade::GetLinearizedDepth(uv.xy) * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  233. }
  234.  
  235. float3 get_position_from_uv_mipmapped(in float2 uv, in MXAO_VSOUT MXAO, in int miplevel)
  236. {
  237. return (uv.xyx * MXAO.uvtoviewMUL + MXAO.uvtoviewADD) * tex2Dlod(sMXAO_DepthTex, float4(uv.xyx, miplevel)).x;
  238. }
  239.  
  240. void spatial_sample_weight(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. weight = saturate(0.2 / surfacealignment - depthdiff) * saturate(0.75 - normaldiff);
  246. weight = saturate(weight * 4.0) * 2.0;
  247. }
  248.  
  249. void spatial_sample_key(in float2 uv, in float inputscale, in sampler inputsampler, inout float4 tempsample, inout float4 key)
  250. {
  251. float4 lodcoord = float4(uv.xy, 0, 0);
  252. tempsample = tex2Dlod(inputsampler, lodcoord * inputscale);
  253. key = float4(tex2Dlod(sMXAO_NormalTex, lodcoord).xyz * 2.0 - 1.0, tex2Dlod(sMXAO_DepthTex, lodcoord).x);
  254. }
  255.  
  256. float4 blur_filter(in MXAO_VSOUT MXAO, in sampler inputsampler, in float inputscale, in float radius, in int blursteps, in int passid)
  257. {
  258. float4 tempsample;
  259. float4 centerkey, tempkey;
  260. float centerweight = 1.0, tempweight;
  261. float4 blurcoord = 0.0;
  262.  
  263. spatial_sample_key(MXAO.uv.xy, inputscale, inputsampler, tempsample, centerkey);
  264. float surfacealignment = saturate(-dot(centerkey.xyz, normalize(float3(MXAO.uv.xy * 2.0 - 1.0, 1.0) * centerkey.w)));
  265.  
  266. float4 samplesum = tempsample.BLUR_COMP_SWIZZLE;
  267. float2 bluroffsets[8] = {float2(1.5,0.5),float2(-1.5,-0.5),float2(-0.5,1.5),float2(0.5,-1.5),
  268. float2(1.5,2.5),float2(-1.5,-2.5),float2(-2.5,1.5),float2(2.5,-1.5)};
  269.  
  270. float2 scaled_radius = ReShade::PixelSize * radius / inputscale;
  271.  
  272. [unroll]
  273. for(int i = 0; i < blursteps; i++)
  274. {
  275. float2 sampleCoord = MXAO.uv.xy + bluroffsets[i] * scaled_radius;
  276.  
  277. spatial_sample_key(sampleCoord, inputscale, inputsampler, tempsample, tempkey);
  278. spatial_sample_weight(tempkey, centerkey, surfacealignment, tempweight);
  279.  
  280. samplesum += tempsample.BLUR_COMP_SWIZZLE * tempweight;
  281. centerweight += tempweight;
  282. }
  283.  
  284. return samplesum.BLUR_COMP_SWIZZLE / centerweight;
  285. }
  286.  
  287. void sample_parameter_setup(in MXAO_VSOUT MXAO, in float scaled_depth, in float layer_id, out float scaled_radius, out float falloff_factor)
  288. {
  289. scaled_radius = 0.25 * MXAO_SAMPLE_RADIUS / (MXAO.samples * (scaled_depth + 2.0));
  290. falloff_factor = -1.0/(MXAO_SAMPLE_RADIUS * MXAO_SAMPLE_RADIUS);
  291.  
  292. #if(MXAO_TWO_LAYER != 0)
  293. scaled_radius *= lerp(1.0, MXAO_SAMPLE_RADIUS_SECONDARY + 1e-6, layer_id);
  294. falloff_factor *= lerp(1.0, 1.0 / (MXAO_SAMPLE_RADIUS_SECONDARY * MXAO_SAMPLE_RADIUS_SECONDARY + 1e-6), layer_id);
  295. #endif
  296. }
  297.  
  298. void smooth_normals(inout float3 normal, in float3 position, in MXAO_VSOUT MXAO)
  299. {
  300. float2 scaled_radius = 0.018 / position.z * float2(1.0, ReShade::AspectRatio);
  301. float3 neighbour_normal[4] = {normal, normal, normal, normal};
  302.  
  303. [unroll]
  304. for(int i = 0; i < 4; i++)
  305. {
  306. float2 direction;
  307. sincos(6.28318548 * 0.25 * i, direction.y, direction.x);
  308.  
  309. [unroll]
  310. for(int direction_step = 1; direction_step <= 5; direction_step++)
  311. {
  312. float search_radius = exp2(direction_step);
  313. float2 sample_uv = MXAO.uv.zw + direction * search_radius * scaled_radius;
  314.  
  315. float3 temp_normal = tex2Dlod(sMXAO_NormalTex, float4(sample_uv, 0, 0)).xyz * 2.0 - 1.0;
  316. float3 temp_position = get_position_from_uv_mipmapped(sample_uv, MXAO, 0);
  317.  
  318. float3 position_delta = temp_position - position;
  319. float distance_weight = saturate(1.0 - dot(position_delta, position_delta) * 20.0 / search_radius);
  320. float normal_angle = dot(normal, temp_normal);
  321. float angle_weight = smoothstep(0.3, 0.98, normal_angle) * smoothstep(1.0, 0.98, normal_angle); //only take normals into account that are NOT equal to the current normal.
  322.  
  323. float total_weight = saturate(3.0 * distance_weight * angle_weight / search_radius);
  324.  
  325. neighbour_normal[i] = lerp(neighbour_normal[i], temp_normal, total_weight);
  326. }
  327. }
  328.  
  329. normal = normalize(neighbour_normal[0] + neighbour_normal[1] + neighbour_normal[2] + neighbour_normal[3]);
  330. }
  331.  
  332. /*=============================================================================
  333. Pixel Shaders
  334. =============================================================================*/
  335.  
  336. void PS_InputBufferSetup(in MXAO_VSOUT MXAO, out float4 color : SV_Target0, out float4 depth : SV_Target1, out float4 normal : SV_Target2)
  337. {
  338. float3 single_pixel_offset = float3(ReShade::PixelSize.xy, 0);
  339.  
  340. float3 position = get_position_from_uv(MXAO.uv.xy, MXAO);
  341. float3 position_delta_x1 = - position + get_position_from_uv(MXAO.uv.xy + single_pixel_offset.xz, MXAO);
  342. float3 position_delta_x2 = position - get_position_from_uv(MXAO.uv.xy - single_pixel_offset.xz, MXAO);
  343. float3 position_delta_y1 = - position + get_position_from_uv(MXAO.uv.xy + single_pixel_offset.zy, MXAO);
  344. float3 position_delta_y2 = position - get_position_from_uv(MXAO.uv.xy - single_pixel_offset.zy, MXAO);
  345.  
  346. position_delta_x1 = lerp(position_delta_x1, position_delta_x2, abs(position_delta_x1.z) > abs(position_delta_x2.z));
  347. position_delta_y1 = lerp(position_delta_y1, position_delta_y2, abs(position_delta_y1.z) > abs(position_delta_y2.z));
  348.  
  349. float sample_jitter = dot(floor(MXAO.vpos.xy % 4 + 0.1), int2(1, 4)) + 1;
  350. sample_jitter *= 0.0625;
  351.  
  352. normal = float4(normalize(cross(position_delta_y1, position_delta_x1)) * 0.5 + 0.5, sample_jitter);
  353. color = tex2D(ReShade::BackBuffer, MXAO.uv.xy);
  354. depth = ReShade::GetLinearizedDepth(MXAO.uv.xy) * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  355. }
  356.  
  357. void PS_StencilSetup(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  358. {
  359. if( ReShade::GetLinearizedDepth(MXAO.uv.zw) >= MXAO_FADE_DEPTH_END
  360. || 0.25 * 0.5 * MXAO_SAMPLE_RADIUS / (tex2D(sMXAO_DepthTex, MXAO.uv.zw).x + 2.0) * BUFFER_HEIGHT < 1.0
  361. || MXAO.uv.z > 1.0
  362. || MXAO.uv.w > 1.0
  363. ) discard;
  364.  
  365. color = 1.0;
  366. }
  367.  
  368. void PS_AmbientObscurance(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  369. {
  370. color = 0.0;
  371.  
  372. float3 position = get_position_from_uv_mipmapped(MXAO.uv.zw, MXAO, 0);
  373. float4 normal = tex2D(sMXAO_NormalTex, MXAO.uv.zw);
  374. normal.xyz = normal.xyz * 2.0 - 1.0;
  375.  
  376. float layer_id = (MXAO.vpos.x + MXAO.vpos.y) % 2.0;
  377.  
  378. #if(MXAO_SMOOTHNORMALS != 0)
  379. smooth_normals(normal.xyz, position, MXAO);
  380. #endif
  381.  
  382. position += normal.xyz * position.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  383.  
  384. float scaled_radius;
  385. float falloff_factor;
  386. sample_parameter_setup(MXAO, position.z, layer_id, scaled_radius, falloff_factor);
  387.  
  388. float2 sample_uv, sample_direction;
  389. sincos(38.39941 * normal.w, sample_direction.x, sample_direction.y); //2.3999632 * 16
  390. sample_direction *= scaled_radius;
  391.  
  392. [loop]
  393. for(int i = 0; i < MXAO.samples; i++)
  394. {
  395. sample_uv = MXAO.uv.zw + sample_direction.xy * float2(1.0, ReShade::AspectRatio) * (i + normal.w);
  396. sample_direction.xy = mul(sample_direction.xy, float2x2(0.76465,-0.64444,0.64444,0.76465)); //cos/sin 2.3999632 * 16
  397.  
  398. float sample_mip = saturate(scaled_radius * i * 20.0) * 3.0;
  399.  
  400. float3 occlusion_vector = -position + get_position_from_uv_mipmapped(sample_uv, MXAO, sample_mip + MXAO_MIPLEVEL_AO);
  401. float occlusion_distance_squared = dot(occlusion_vector, occlusion_vector);
  402. float occlusion_normal_angle = dot(occlusion_vector, normal.xyz) * rsqrt(occlusion_distance_squared);
  403.  
  404. float sample_occlusion = saturate(1.0 + falloff_factor * occlusion_distance_squared) * saturate(occlusion_normal_angle - MXAO_SAMPLE_NORMAL_BIAS);
  405. #if(MXAO_ENABLE_IL != 0)
  406. [branch]
  407. if(sample_occlusion > 0.1)
  408. {
  409. float3 sample_indirect_lighting = tex2Dlod(sMXAO_ColorTex, float4(sample_uv, 0, sample_mip + MXAO_MIPLEVEL_IL)).xyz;
  410. float3 sample_normal = tex2Dlod(sMXAO_NormalTex, float4(sample_uv, 0, sample_mip + MXAO_MIPLEVEL_IL)).xyz * 2.0 - 1.0;
  411. sample_indirect_lighting *= saturate(dot(-sample_normal, occlusion_vector) + rsqrt(occlusion_distance_squared) * 4.0) * saturate(1.0 + falloff_factor * occlusion_distance_squared * 0.25);
  412. color += float4(sample_indirect_lighting, sample_occlusion);
  413. }
  414. #else
  415. color.w += sample_occlusion;
  416. #endif
  417. }
  418.  
  419. color = saturate(color / ((1.0 - MXAO_SAMPLE_NORMAL_BIAS) * MXAO.samples) * 2.0);
  420. color = sqrt(color.BLUR_COMP_SWIZZLE); //AO denoise
  421.  
  422. #if(MXAO_TWO_LAYER != 0)
  423. color *= lerp(MXAO_AMOUNT_COARSE, MXAO_AMOUNT_FINE, layer_id);
  424. #endif
  425. }
  426.  
  427. void PS_SpatialFilter1(in MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  428. {
  429. color = blur_filter(MXAO, sMXAO_BlurTex1, MXAO_GLOBAL_RENDER_SCALE, 1, 8, 0);
  430. }
  431.  
  432. void PS_SpatialFilter2(MXAO_VSOUT MXAO, out float4 color : SV_Target0)
  433. {
  434. float4 ssil_ssao = blur_filter(MXAO, sMXAO_BlurTex2, 1, 0.75 / MXAO_GLOBAL_RENDER_SCALE, 4, 1);
  435. ssil_ssao *= ssil_ssao; //AO denoise
  436.  
  437. color = tex2D(sMXAO_ColorTex, MXAO.uv.xy);
  438.  
  439. static const float3 lumcoeff = float3(0.2126, 0.7152, 0.0722);
  440. float scenedepth = ReShade::GetLinearizedDepth(MXAO.uv.xy);
  441. float colorgray = dot(color.rgb, lumcoeff);
  442. float blendfact = 1.0 - colorgray;
  443.  
  444. #if(MXAO_ENABLE_IL != 0)
  445. ssil_ssao.xyz = lerp(dot(ssil_ssao.xyz, lumcoeff), ssil_ssao.xyz, MXAO_SSIL_SATURATION) * MXAO_SSIL_AMOUNT * 2.0;
  446. #else
  447. ssil_ssao.xyz = 0.0;
  448. #endif
  449.  
  450. ssil_ssao.w = 1.0 - pow(1.0 - ssil_ssao.w, MXAO_SSAO_AMOUNT * 4.0);
  451. ssil_ssao *= 1.0 - smoothstep(MXAO_FADE_DEPTH_START, MXAO_FADE_DEPTH_END, scenedepth * float4(2.0, 2.0, 2.0, 1.0));
  452.  
  453. if(MXAO_BLEND_TYPE == 0)
  454. {
  455. color.rgb -= (ssil_ssao.www - ssil_ssao.xyz) * blendfact * color.rgb;
  456. }
  457. else if(MXAO_BLEND_TYPE == 1)
  458. {
  459. color.rgb = color.rgb * saturate(1.0 - ssil_ssao.www * blendfact * 1.2) + ssil_ssao.xyz * blendfact * colorgray * 2.0;
  460. }
  461. else if(MXAO_BLEND_TYPE == 2)
  462. {
  463. float colordiff = saturate(2.0 * distance(normalize(color.rgb + 1e-6),normalize(ssil_ssao.rgb + 1e-6)));
  464. color.rgb = color.rgb + ssil_ssao.rgb * lerp(color.rgb, dot(color.rgb, 0.3333), colordiff) * blendfact * blendfact * 4.0;
  465. color.rgb = color.rgb * (1.0 - ssil_ssao.www * (1.0 - dot(color.rgb, lumcoeff)));
  466. }
  467. else if(MXAO_BLEND_TYPE == 3)
  468. {
  469. color.rgb *= color.rgb;
  470. color.rgb -= (ssil_ssao.www - ssil_ssao.xyz) * color.rgb;
  471. color.rgb = sqrt(color.rgb);
  472. }
  473. else if(MXAO_BLEND_TYPE == 4)
  474. {
  475. float3 ambientColor = tex2Dlod(sMXAO_ColorTex, float4(MXAO.uv.xy,0,5)).rgb;
  476. color.rgb -= ambientColor;
  477. ambientColor.rgb *= max(0.0, 1.0 - ssil_ssao.www + ssil_ssao.xyz);
  478. color.rgb += ambientColor;
  479. }
  480.  
  481. if(MXAO_DEBUG_VIEW_ENABLE == 1)
  482. {
  483. color.rgb = max(0.0, 1.0 - ssil_ssao.www + ssil_ssao.xyz);
  484. color.rgb *= (MXAO_ENABLE_IL != 0) ? 0.5 : 1.0;
  485. }
  486. else if(MXAO_DEBUG_VIEW_ENABLE == 2)
  487. {
  488. color.rgb = tex2D(sMXAO_NormalTex, MXAO.uv.xy).xyz;
  489. }
  490.  
  491. color.a = 1.0;
  492. }
  493.  
  494. /*=============================================================================
  495. Techniques
  496. =============================================================================*/
  497.  
  498. technique MXAO
  499. {
  500. pass
  501. {
  502. VertexShader = VS_MXAO;
  503. PixelShader = PS_InputBufferSetup;
  504. RenderTarget0 = MXAO_ColorTex;
  505. RenderTarget1 = MXAO_DepthTex;
  506. RenderTarget2 = MXAO_NormalTex;
  507. }
  508. pass
  509. {
  510. VertexShader = VS_MXAO;
  511. PixelShader = PS_StencilSetup;
  512. /*Render Target is Backbuffer*/
  513. ClearRenderTargets = true;
  514. StencilEnable = true;
  515. StencilPass = REPLACE;
  516. StencilRef = 1;
  517. }
  518. pass
  519. {
  520. VertexShader = VS_MXAO;
  521. PixelShader = PS_AmbientObscurance;
  522. RenderTarget = MXAO_BlurTex1;
  523. ClearRenderTargets = true;
  524. StencilEnable = true;
  525. StencilPass = KEEP;
  526. StencilFunc = EQUAL;
  527. StencilRef = 1;
  528. }
  529. pass
  530. {
  531. VertexShader = VS_MXAO;
  532. PixelShader = PS_SpatialFilter1;
  533. RenderTarget = MXAO_BlurTex2;
  534. }
  535. pass
  536. {
  537. VertexShader = VS_MXAO;
  538. PixelShader = PS_SpatialFilter2;
  539. /*Render Target is Backbuffer*/
  540. }
  541. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement