Advertisement
Guest User

MXAO for Framework

a guest
Aug 16th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.71 KB | None | 0 0
  1. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. //LICENSE AGREEMENT AND DISTRIBUTION RULES:
  3. //1 Copyrights of the Master Effect exclusively belongs to author - Gilcher Pascal aka Marty McFly.
  4. //2 Master Effect (the SOFTWARE) is DonateWare application, which means you may or may not pay for this software to the author as donation.
  5. //3 If included in ENB presets, credit the author (Gilcher Pascal aka Marty McFly).
  6. //4 Software provided "AS IS", without warranty of any kind, use it on your own risk.
  7. //5 You may use and distribute software in commercial or non-commercial uses. For commercial use it is required to warn about using this software (in credits, on the box or other places). Commercial distribution of software as part of the games without author permission prohibited.
  8. //6 Author can change license agreement for new versions of the software.
  9. //7 All the rights, not described in this license agreement belongs to author.
  10. //8 Using the Master Effect means that user accept the terms of use, described by this license agreement.
  11. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. //For more information about license agreement contact me:
  14. //https://www.facebook.com/MartyMcModding
  15. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  16. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  17. //Copyright (c) 2009-2016 Gilcher Pascal aka Marty McFly
  18. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  19.  
  20. #pragma message " MXAO Ambient Occlusion with Indirect Lighting v1.2.Framework\n visit site for news/updates\n fb.com/martymcmodding\n Copyright (c) 2008-2016 Marty McFly\n"
  21.  
  22. #include EFFECT_CONFIG(MartyMcFly)
  23. #include "Common.fx"
  24.  
  25. #if USE_MXAO
  26.  
  27. namespace MartyMcFly
  28. {
  29.  
  30. texture2D texSSAO { Width = BUFFER_WIDTH*fMXAOSizeScale; Height = BUFFER_HEIGHT*fMXAOSizeScale; Format = RGBA8; };
  31. texture2D texDither <string source = "bayer16x16.png";> { Width = 16;Height = 16;Format = R8;};
  32. texture2D texLOD { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+fMXAOMipLevelIL;};
  33. #if(bMXAOLowPrecisionEnable != 0)
  34. texture2D texDepthLOD { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16F; MipLevels = 5+fMXAOMipLevelAO;};
  35. #else
  36. texture2D texDepthLOD { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R32F; MipLevels = 5+fMXAOMipLevelAO;};
  37. #endif
  38. #if(bMXAOBackfaceCheckEnable != 0)
  39. texture2D texNormal { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; MipLevels = 5+fMXAOMipLevelIL;};
  40. #else
  41. texture2D texNormal { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
  42. #endif
  43.  
  44. sampler2D SamplerLOD
  45. {
  46. Texture = texLOD;
  47. MinFilter = LINEAR;
  48. MagFilter = LINEAR;
  49. MipFilter = LINEAR;
  50. AddressU = Clamp;
  51. AddressV = Clamp;
  52. };
  53.  
  54. sampler2D SamplerDepthLOD
  55. {
  56. Texture = texDepthLOD;
  57. MinFilter = LINEAR;
  58. MagFilter = LINEAR;
  59. MipFilter = LINEAR;
  60. AddressU = Clamp;
  61. AddressV = Clamp;
  62. };
  63.  
  64. sampler2D SamplerNormal
  65. {
  66. Texture = texNormal;
  67. MinFilter = LINEAR;
  68. MagFilter = LINEAR;
  69. MipFilter = LINEAR;
  70. AddressU = Clamp;
  71. AddressV = Clamp;
  72. };
  73.  
  74. sampler2D SamplerSSAO
  75. {
  76. Texture = texSSAO;
  77. MinFilter = LINEAR;
  78. MagFilter = LINEAR;
  79. MipFilter = LINEAR;
  80. AddressU = Clamp;
  81. AddressV = Clamp;
  82. };
  83.  
  84. sampler2D SamplerDither
  85. {
  86. Texture = texDither;
  87. MinFilter = POINT;
  88. MagFilter = POINT;
  89. MipFilter = POINT;
  90. AddressU = Wrap;
  91. AddressV = Wrap;
  92. };
  93.  
  94. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  95. //
  96. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  97.  
  98. float MXAOGetLinearDepth(float2 coords)
  99. {
  100. float depth = tex2Dlod(ReShade::LinearizedDepth, float4(coords.xy,0,0)).x;
  101. return depth;
  102. }
  103.  
  104. float3 MXAOGetPosition(float2 coords)
  105. {
  106. float EyeDepth = MXAOGetLinearDepth(coords.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  107. return float3((coords.xy * 2.0 - 1.0)*EyeDepth,EyeDepth);
  108. }
  109.  
  110. float3 MXAOGetPositionLOD(float2 coords, int mipLevel)
  111. {
  112. float EyeDepth = tex2Dlod(SamplerDepthLOD, float4(coords.xy,0,mipLevel)).x;
  113. return float3((coords.xy * 2.0 - 1.0)*EyeDepth,EyeDepth);
  114. }
  115.  
  116. float3 MXAOGetNormalFromDepth(float2 coords)
  117. {
  118. float3 centerPos = MXAOGetPosition(coords.xy);
  119. float2 offs = ReShade::PixelSize.xy*1.0;
  120. float3 ddx1 = MXAOGetPosition(coords.xy + float2(offs.x, 0)) - centerPos;
  121. float3 ddx2 = centerPos - MXAOGetPosition(coords.xy + float2(-offs.x, 0));
  122.  
  123. float3 ddy1 = MXAOGetPosition(coords.xy + float2(0, offs.y)) - centerPos;
  124. float3 ddy2 = centerPos - MXAOGetPosition(coords.xy + float2(0, -offs.y));
  125.  
  126. ddx1 = lerp(ddx1, ddx2, abs(ddx1.z) > abs(ddx2.z));
  127. ddy1 = lerp(ddy1, ddy2, abs(ddy1.z) > abs(ddy2.z));
  128.  
  129. float3 normal = cross(ddy1, ddx1);
  130.  
  131. return normalize(normal);
  132. }
  133.  
  134. float4 MXAOGetBlurFactors(float2 coords)
  135. {
  136. return float4(tex2Dlod(SamplerNormal, float4(coords.xy,0,0)).xyz*2.0-1.0,MXAOGetLinearDepth(coords.xy));
  137. }
  138.  
  139. float MXAOGetBlurWeight(float r, float4 z, float4 z0)
  140. {
  141. float normaldiff = distance(z.xyz,z0.xyz);
  142. float depthdiff = abs(z.w-z0.w);
  143.  
  144. float depthfalloff = pow(saturate(1.0 - z0.w),3.0);
  145. float fresnelfactor = saturate(min(-z0.z,-z.z));
  146.  
  147. float normalweight = saturate(1.0-normaldiff * fMXAOBlurSharpness);
  148. float depthweight = saturate(1.0-depthdiff * RESHADE_DEPTH_LINEARIZATION_FAR_PLANE * fMXAOBlurSharpness * fresnelfactor * depthfalloff * 0.5);
  149.  
  150. return min(depthweight,normalweight);
  151. }
  152.  
  153. float2 MXAOGetRandom2FromCoord(float2 coords)
  154. {
  155. coords *= 1000.0;
  156. float3 coords3 = frac(float3(coords.xyx) * 0.1031);
  157. coords3 += dot(coords3.xyz, coords3.yzx+19.19);
  158. return frac(float2((coords3.x + coords3.y)*coords3.z, (coords3.x+coords3.z)*coords3.y));
  159. }
  160.  
  161. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  162. //
  163. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  164.  
  165. void PS_AO_Pre(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 color : SV_Target0, out float4 depth : SV_Target1, out float4 normal : SV_Target2)
  166. {
  167. color = tex2D(ReShade::BackBuffer, texcoord.xy);
  168. depth = MXAOGetLinearDepth(texcoord.xy)*RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  169. normal = MXAOGetNormalFromDepth(texcoord.xy).xyzz*0.5+0.5; // * 0.5 + 0.5; //packing into 2 components not possible.
  170. }
  171.  
  172. void PS_AO_Gen(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 res : SV_Target0)
  173. {
  174. float3 ScreenSpaceNormals = MXAOGetNormalFromDepth(texcoord.xy); //tex2D(SamplerNormal, texcoord.xy).xyz * 2.0 - 1.0; //better to use best possible data than rounded texture values
  175. float3 ScreenSpacePosition = MXAOGetPositionLOD(texcoord.xy, 0);
  176.  
  177. float scenedepth = ScreenSpacePosition.z / RESHADE_DEPTH_LINEARIZATION_FAR_PLANE;
  178. ScreenSpacePosition += ScreenSpaceNormals * scenedepth;
  179.  
  180. float numSamples = lerp(iMXAOSampleCount,12,scenedepth / AO_FADE____END); //AO FADEOUT //12 is minimum acceptable sampling count and max(12,...) makes falloff ineffective for small sample counts.
  181. float2 SampleRadiusScaled = fMXAOSampleRadius / (numSamples * ScreenSpacePosition.z * float2(1.0, ReShade::PixelSize.x/ ReShade::PixelSize.y) * 0.6);
  182. float radialJitter = (MXAOGetRandom2FromCoord(texcoord.xy).x-0.5) * fMXAOSampleRandomization;
  183.  
  184. float rotAngle = tex2D(SamplerDither, texcoord.xy * float2(BUFFER_WIDTH,BUFFER_HEIGHT) * fMXAOSizeScale * 0.0625).x;
  185. float mipFactor = SampleRadiusScaled.x*numSamples*19.0;
  186.  
  187. float4 AOandGI = 0.0;
  188.  
  189. float2x2 radialMatrix = float2x2(0.575,0.81815,-0.81815,0.575); //E.F
  190. float2 currentVector = float2(cos(rotAngle*6.283), sin(rotAngle*6.283));
  191.  
  192. float fNegInvR2 = -1.0/(fMXAOSampleRadius*fMXAOSampleRadius);
  193.  
  194. [loop]
  195. for (float i=1.0; i <= numSamples; i++)
  196. {
  197. currentVector = mul(currentVector.xy, radialMatrix);
  198. float2 currentOffset = texcoord.xy + currentVector.xy * SampleRadiusScaled.xy * (i+radialJitter);
  199.  
  200. #if(bMXAOBoundaryCheckEnable != 0)
  201. [branch]
  202. if(currentOffset.x < 1.0 && currentOffset.y < 1.0 && currentOffset.x > 0.0 && currentOffset.y > 0.0)
  203. {
  204. #endif
  205. float mipLevel = clamp((int)floor(log2(mipFactor*i)) - 3, fMXAOMipLevelAO, 5); //AO must not go beyond 5
  206.  
  207. float3 occlVec = MXAOGetPositionLOD(currentOffset.xy, mipLevel) - ScreenSpacePosition;
  208. float occlDistance = length(occlVec);
  209. float SurfaceAngle = dot(occlVec/occlDistance, ScreenSpaceNormals);
  210.  
  211. float fAO = saturate(occlDistance * fNegInvR2 + 1.0) * saturate(SurfaceAngle - fMXAONormalBias);
  212.  
  213. #if(bMXAOIndirectLightingEnable != 0)
  214. float3 fIL = tex2Dlod(SamplerLOD, float4(currentOffset,0,mipLevel + fMXAOMipLevelIL)).xyz;
  215. #if(bMXAOBackfaceCheckEnable != 0)
  216. float3 offsetNormals = tex2Dlod(SamplerNormal, float4(currentOffset,0,mipLevel + fMXAOMipLevelIL)).xyz * 2.0 - 1.0;
  217. float facingtoSource = dot(-normalize(occlVec),offsetNormals);
  218. facingtoSource = smoothstep(-0.5,0.0,facingtoSource);
  219. fIL *= facingtoSource;
  220. #endif
  221. AOandGI.w += fAO*saturate(1-dot(fIL,float3(0.299,0.587,0.114)));
  222. AOandGI.xyz += fIL*fAO;
  223. #else
  224. AOandGI.w += fAO;
  225. #endif
  226.  
  227. #if(bMXAOBoundaryCheckEnable != 0)
  228. }
  229. #endif
  230. }
  231. #define noinfomessages showfps
  232. AOandGI *= 20.0 / ((1.0-fMXAONormalBias)*numSamples*fMXAOSampleRadius);
  233. res = lerp(AOandGI,float4(0.0.xxx,0.0), AO_FADE____END < scenedepth); //AO FADEOUT
  234. }
  235.  
  236. void PS_AO_Blur1(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 res : SV_Target0)
  237. {
  238. float4 center_factor = MXAOGetBlurFactors(texcoord.xy);
  239. float4 temp_factor = 0.0;
  240.  
  241. float totalweight = 1.0;
  242. float tempweight = 0.0;
  243.  
  244. float4 total_ao = tex2Dlod(SamplerSSAO, float4(texcoord.xy,0,0));
  245. float4 temp_ao = 0.0;
  246.  
  247. [loop]
  248. for(float r = 1.0; r <= fMXAOBlurSteps; r += 1.0)
  249. {
  250. float2 axis = float2(-r,r)/fMXAOSizeScale*1.25;
  251.  
  252. temp_factor = MXAOGetBlurFactors(texcoord.xy + axis * ReShade::PixelSize.xy);
  253. temp_ao = tex2Dlod(SamplerSSAO, float4(texcoord.xy + axis * ReShade::PixelSize.xy,0,0));
  254. tempweight = MXAOGetBlurWeight(r, temp_factor, center_factor);
  255.  
  256. total_ao += temp_ao * tempweight;
  257. totalweight += tempweight;
  258.  
  259. temp_factor = MXAOGetBlurFactors(texcoord.xy - axis * ReShade::PixelSize.xy);
  260. temp_ao = tex2Dlod(SamplerSSAO, float4(texcoord.xy - axis * ReShade::PixelSize.xy,0,0));
  261. tempweight = MXAOGetBlurWeight(r, temp_factor, center_factor);
  262.  
  263. total_ao += temp_ao * tempweight;
  264. totalweight += tempweight;
  265. }
  266.  
  267. total_ao /= totalweight;
  268. res = total_ao;
  269. }
  270.  
  271. void PS_AO_Blur2(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 res : SV_Target0)
  272. {
  273. float4 center_factor = MXAOGetBlurFactors(texcoord.xy);
  274. float4 temp_factor = 0.0;
  275.  
  276. float totalweight = 1.0;
  277. float tempweight = 0.0;
  278.  
  279. float4 total_ao = tex2Dlod(ReShade::BackBuffer, float4(texcoord.xy,0,0));
  280. float4 temp_ao = 0.0;
  281.  
  282. [loop]
  283. for(float r = 1.0; r <= fMXAOBlurSteps; r += 1.0)
  284. {
  285. float2 axis = float2(r,r)/fMXAOSizeScale*1.25;
  286.  
  287. temp_factor = MXAOGetBlurFactors(texcoord.xy + axis * ReShade::PixelSize.xy);
  288. temp_ao = tex2Dlod(ReShade::BackBuffer, float4(texcoord.xy + axis * ReShade::PixelSize.xy,0,0));
  289. tempweight = MXAOGetBlurWeight(r, temp_factor, center_factor);
  290.  
  291. total_ao += temp_ao * tempweight;
  292. totalweight += tempweight;
  293.  
  294. temp_factor = MXAOGetBlurFactors(texcoord.xy - axis * ReShade::PixelSize.xy);
  295. temp_ao = tex2Dlod(ReShade::BackBuffer, float4(texcoord.xy - axis * ReShade::PixelSize.xy,0,0));
  296. tempweight = MXAOGetBlurWeight(r, temp_factor, center_factor);
  297.  
  298. total_ao += temp_ao * tempweight;
  299. totalweight += tempweight;
  300. }
  301.  
  302. total_ao /= totalweight;
  303. float4 mxao = saturate(total_ao);
  304.  
  305. float scenedepth = MXAOGetLinearDepth(texcoord.xy); //might change center_factor so better fetch depth directly here.
  306. float4 color = max(0.0,tex2D(SamplerLOD, texcoord.xy));
  307. float colorgray = dot(color.xyz,float3(0.299,0.587,0.114));
  308.  
  309. mxao.xyz = lerp(dot(mxao.xyz,float3(0.299,0.587,0.114)),mxao.xyz,fMXAOIndirectLightingSaturation) * fMXAOIndirectLightingAmount;
  310. mxao.w = 1.0-pow(1.0-mxao.w, fMXAOAmbientOcclusionAmount * 2.0);
  311.  
  312. #if(bMXAODebugViewEnable == 0)
  313. mxao = lerp(mxao, 0.0, pow(colorgray,2.0));
  314. #endif
  315. mxao.w = lerp(mxao.w, 0.0,smoothstep(AO_FADE____START, AO_FADE____END, scenedepth)); //AO FADEOUT
  316. mxao.xyz = lerp(mxao.xyz,0.0,smoothstep(AO_FADE____START*0.5, AO_FADE____END*0.5, scenedepth)); //AO FADEOUT //IL can look really bad on far objects.
  317.  
  318. float3 GI = mxao.w - mxao.xyz;
  319. GI = max(0.0,1-GI);
  320. color.xyz *= GI;
  321.  
  322. #if(bMXAODebugViewEnable != 0)
  323. #if(bMXAOIndirectLightingEnable != 0)
  324. color.xyz = (texcoord.x > 0.5) ? mxao.xyz : 1-mxao.w;
  325. #else
  326. color.xyz = 1-mxao.w;
  327. #endif
  328. #endif
  329.  
  330. res = color;
  331. }
  332.  
  333. technique MXAO_Tech <bool enabled = RESHADE_START_ENABLED; int toggle = MXAO_ToggleKey; >
  334. {
  335. pass P0
  336. {
  337. VertexShader = ReShade::VS_PostProcess;
  338. PixelShader = PS_AO_Pre;
  339. RenderTarget0 = texLOD;
  340. RenderTarget1 = texDepthLOD;
  341. RenderTarget2 = texNormal;
  342. }
  343. pass P1
  344. {
  345. VertexShader = ReShade::VS_PostProcess;
  346. PixelShader = PS_AO_Gen;
  347. RenderTarget = texSSAO;
  348. }
  349. pass P2_0
  350. {
  351. VertexShader = ReShade::VS_PostProcess;
  352. PixelShader = PS_AO_Blur1;
  353. }
  354. pass P2
  355. {
  356. VertexShader = ReShade::VS_PostProcess;
  357. PixelShader = PS_AO_Blur2;
  358. }
  359. }
  360.  
  361. }
  362. #endif
  363.  
  364. #include EFFECT_CONFIG_UNDEF(MartyMcFly)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement