NFAA.fx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ////-------------//
  2. ///**NFAA Fast**///
  3. //-------------////
  4. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. //* Normal Filter Anti Aliasing.
  6. //* For ReShade 3.0+ & Freestyle
  7. //* ---------------------------------
  8. //* NFAA
  9. //* Due Diligence
  10. //* Based on port by b34r
  11. //* https://www.gamedev.net/forums/topic/580517-nfaa---a-post-process-anti-aliasing-filter-results-implementation-details/?page=2
  12. //* If I missed any please tell me.
  13. //*
  14. //* LICENSE
  15. //* ============
  16. //* Normal Filter Anti Aliasing is licenses under: Attribution-NoDerivatives 4.0 International
  17. //*
  18. //* You are free to:
  19. //* Share - copy and redistribute the material in any medium or format
  20. //* for any purpose, even commercially.
  21. //* The licensor cannot revoke these freedoms as long as you follow the license terms.
  22. //* Under the following terms:
  23. //* Attribution - You must give appropriate credit, provide a link to the license, and indicate if changes were made.
  24. //* You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  25. //*
  26. //* NoDerivatives - If you remix, transform, or build upon the material, you may not distribute the modified material.
  27. //*
  28. //* No additional restrictions - You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
  29. //*
  30. //* https://creativecommons.org/licenses/by-nd/4.0/
  31. //*
  32. //* Have fun,
  33. //* Jose Negrete AKA BlueSkyDefender
  34. //*
  35. //* https://github.com/BlueSkyDefender/Depth3D
  36. //*
  37. //* Have fun,
  38. //* Jose Negrete AKA BlueSkyDefender
  39. //*
  40. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. uniform int AA_Adjust <
  42. ui_type = "drag";
  43. ui_min = 1; ui_max = 128;
  44. ui_label = "AA Power";
  45. ui_tooltip = "Use this to adjust the AA power.\n"
  46. "Default is 16";
  47. ui_category = "NFAA";
  48. > = 16;
  49. uniform float Mask_Adjust <
  50. ui_type = "drag";
  51. ui_min = 0.0; ui_max = 4.0;
  52. ui_label = "Mask Adjustment";
  53. ui_tooltip = "Use this to adjust the Mask.\n"
  54. "Default is 1.00";
  55. ui_category = "NFAA";
  56. > = 1.00;
  57. uniform int View_Mode <
  58. ui_type = "combo";
  59. ui_items = "NFAA\0Mask View\0Normals\0DLSS\0";
  60. ui_label = "View Mode";
  61. ui_tooltip = "This is used to select the normal view output or debug view.\n"
  62. "NFAA Masked gives you a sharper image with applyed Normals AA.\n"
  63. "Masked View gives you a view of the edge detection.\n"
  64. "Normals gives you an view of the normals created.\n"
  65. "DLSS is NV_AI_DLSS Parody experiance..........\n"
  66. "Default is NFAA.";
  67. ui_category = "NFAA";
  68. > = 0;
  69. uniform bool HFR_AA <
  70. ui_label = "HFR AA";
  71. ui_tooltip = "This allows most monitors to assist in AA if your FPS is 60 or above and Locked to your monitors refresh-rate.";
  72. ui_category = "HFRAA";
  73. > = false;
  74. uniform float HFR_Adjust <
  75. ui_type = "drag";
  76. ui_min = 0.0; ui_max = 1.0;
  77. ui_label = "HFR AA Adjustment";
  78. ui_tooltip = "Use this to adjust HFR AA.\n"
  79. "Default is 1.00";
  80. ui_category = "HFRAA";
  81. > = 0.5;
  82. //Total amount of frames since the game started.
  83. uniform uint framecount < source = "framecount"; >;
  84. uniform float timer < source = "timer"; >;
  85. ////////////////////////////////////////////////////////////NFAA////////////////////////////////////////////////////////////////////
  86. #define Alternate framecount % 2 == 0
  87. #define pix float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT)
  88. texture BackBufferTex : COLOR;
  89. sampler BackBuffer
  90. {
  91. Texture = BackBufferTex;
  92. };
  93. void A0(float2 texcoord,float PosX,float PosY,inout float D, inout float E, inout float P, inout float T, inout float H, inout float III, inout float DD )
  94. {
  95. float PosXD = -0.035+PosX, offsetD = 0.001;D = all( abs(float2( texcoord.x -PosXD, texcoord.y-PosY)) < float2(0.0025,0.009));D -= all( abs(float2( texcoord.x -PosXD-offsetD, texcoord.y-PosY)) < float2(0.0025,0.007));
  96. float PosXE = -0.028+PosX, offsetE = 0.0005;E = all( abs(float2( texcoord.x -PosXE, texcoord.y-PosY)) < float2(0.003,0.009));E -= all( abs(float2( texcoord.x -PosXE-offsetE, texcoord.y-PosY)) < float2(0.0025,0.007));E += all( abs(float2( texcoord.x -PosXE, texcoord.y-PosY)) < float2(0.003,0.001));
  97. float PosXP = -0.0215+PosX, PosYP = -0.0025+PosY, offsetP = 0.001, offsetP1 = 0.002;P = all( abs(float2( texcoord.x -PosXP, texcoord.y-PosYP)) < float2(0.0025,0.009*0.775));P -= all( abs(float2( texcoord.x -PosXP-offsetP, texcoord.y-PosYP)) < float2(0.0025,0.007*0.680));float C[1];P += all( abs(float2( texcoord.x -PosXP+offsetP1, texcoord.y-PosY)) < float2(0.0005,0.009));
  98. float PosXT = -0.014+PosX, PosYT = -0.008+PosY;T = all( abs(float2( texcoord.x -PosXT, texcoord.y-PosYT)) < float2(0.003,0.001));T += all( abs(float2( texcoord.x -PosXT, texcoord.y-PosY)) < float2(0.000625,0.009));
  99. float PosXH = -0.0072+PosX;H = all( abs(float2( texcoord.x -PosXH, texcoord.y-PosY)) < float2(0.002,0.001));H -= all( abs(float2( texcoord.x -PosXH, texcoord.y-PosY)) < float2(0.002,0.009));H += all( abs(float2( texcoord.x -PosXH, texcoord.y-PosY)) < float2(0.00325,0.009));
  100. float offsetFive = 0.001, PosX3 = -0.001+PosX;III = all( abs(float2( texcoord.x -PosX3, texcoord.y-PosY)) < float2(0.002,0.009));III -= all( abs(float2( texcoord.x -PosX3 - offsetFive, texcoord.y-PosY)) < float2(0.003,0.007));III += all( abs(float2( texcoord.x -PosX3, texcoord.y-PosY)) < float2(0.002,0.001));
  101. float PosXDD = 0.006+PosX, offsetDD = 0.001;DD = all( abs(float2( texcoord.x -PosXDD, texcoord.y-PosY)) < float2(0.0025,0.009));DD -= all( abs(float2( texcoord.x -PosXDD-offsetDD, texcoord.y-PosY)) < float2(0.0025,0.007));
  102. }
  103. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. //SD video
  105. float LI(in float3 value)
  106. {
  107. return dot(value.rgb,float3(0.299, 0.587, 0.114));
  108. }
  109. float4 GetBB(float2 texcoord : TEXCOORD)
  110. {
  111. return tex2D(BackBuffer, texcoord);
  112. }
  113. float4 NFAA(float2 texcoord)
  114. { float t, l, r, s, MA = Mask_Adjust;
  115. if(View_Mode == 3 )
  116. MA = 5.0;
  117. float2 UV = texcoord.xy, SW = pix * MA, n; // But, I don't think it's really needed.
  118. float4 NFAA; // The Edge Seeking code can be adjusted to look for longer edges.
  119. // Find Edges
  120. t = LI(GetBB( float2( UV.x , UV.y - SW.y ) ).rgb);
  121. s = LI(GetBB( float2( UV.x , UV.y + SW.y ) ).rgb);
  122. l = LI(GetBB( float2( UV.x - SW.x , UV.y ) ).rgb);
  123. r = LI(GetBB( float2( UV.x + SW.x , UV.y ) ).rgb);
  124. n = float2(t - s,-(r - l));
  125. // I should have made rep adjustable. But, I didn't see the need.
  126. // Since my goal was to make this AA fast cheap and simple.
  127. float nl = length(n), Rep = rcp(AA_Adjust);
  128. if(View_Mode == 3 || View_Mode == 4)
  129. Rep = rcp(128);
  130. // Seek aliasing and apply AA. Think of this as basically blur control.
  131. if (nl < Rep)
  132. {
  133. NFAA = GetBB(UV);
  134. }
  135. else
  136. {
  137. n *= pix / (nl * (View_Mode == 3 ? 0.5 : 1.0));
  138. float4 o = GetBB( UV ),
  139. t0 = GetBB( UV + float2(n.x, -n.y) * 0.5) * 0.9,
  140. t1 = GetBB( UV - float2(n.x, -n.y) * 0.5) * 0.9,
  141. t2 = GetBB( UV + n * 0.9) * 0.75,
  142. t3 = GetBB( UV - n * 0.9) * 0.75;
  143. NFAA = (o + t0 + t1 + t2 + t3) / 4.3;
  144. }
  145. // Lets make that mask for a sharper image.
  146. float Mask = nl * 5.0;
  147. if (Mask > 0.025)
  148. Mask = 1-Mask;
  149. else
  150. Mask = 1;
  151. // Super Evil Magic Number.
  152. Mask = saturate(lerp(Mask,1,-1));
  153. // Final color
  154. if(View_Mode == 0)
  155. {
  156. NFAA = lerp(NFAA,GetBB( texcoord.xy), Mask );
  157. }
  158. else if(View_Mode == 1)
  159. {
  160. NFAA = Mask;
  161. }
  162. else if (View_Mode == 2)
  163. {
  164. NFAA = float3(-float2(-(r - l),-(t - s)) * 0.5 + 0.5,1);
  165. }
  166. return NFAA;
  167. }
  168. void A1(float2 texcoord,float PosX,float PosY,inout float I, inout float N, inout float F, inout float O)
  169. {
  170. float PosXI = 0.0155+PosX, PosYI = 0.004+PosY, PosYII = 0.008+PosY;I = all( abs(float2( texcoord.x - PosXI, texcoord.y - PosY)) < float2(0.003,0.001));I += all( abs(float2( texcoord.x - PosXI, texcoord.y - PosYI)) < float2(0.000625,0.005));I += all( abs(float2( texcoord.x - PosXI, texcoord.y - PosYII)) < float2(0.003,0.001));
  171. float PosXN = 0.0225+PosX, PosYN = 0.005+PosY,offsetN = -0.001;N = all( abs(float2( texcoord.x - PosXN, texcoord.y - PosYN)) < float2(0.002,0.004));N -= all( abs(float2( texcoord.x - PosXN, texcoord.y - PosYN - offsetN)) < float2(0.003,0.005));
  172. float PosXF = 0.029+PosX, PosYF = 0.004+PosY, offsetF = 0.0005, offsetF1 = 0.001;F = all( abs(float2( texcoord.x -PosXF-offsetF, texcoord.y-PosYF-offsetF1)) < float2(0.002,0.004));F -= all( abs(float2( texcoord.x -PosXF, texcoord.y-PosYF)) < float2(0.0025,0.005));F += all( abs(float2( texcoord.x -PosXF, texcoord.y-PosYF)) < float2(0.0015,0.00075));
  173. float PosXO = 0.035+PosX, PosYO = 0.004+PosY;O = all( abs(float2( texcoord.x -PosXO, texcoord.y-PosYO)) < float2(0.003,0.005));O -= all( abs(float2( texcoord.x -PosXO, texcoord.y-PosYO)) < float2(0.002,0.003));
  174. }
  175. float4 Out(float4 position : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
  176. { float3 Color = NFAA(texcoord).rgb;
  177. float PosX = 0.9525f*BUFFER_WIDTH*pix.x,PosY = 0.975f*BUFFER_HEIGHT*pix.y,A,B,C,D,E,F,G,H,I,J,K,L,PosXDot = 0.011+PosX, PosYDot = 0.008+PosY;L = all( abs(float2( texcoord.x -PosXDot, texcoord.y-PosYDot)) < float2(0.00075,0.0015));A0(texcoord,PosX,PosY,A,B,C,D,E,F,G );A1(texcoord,PosX,PosY,H,I,J,K);
  178. return timer <= 12500 ? A+B+C+D+E+F+G+H+I+J+K+L ? 1-texcoord.y*50.0+48.35f : float4(Color,1.) : float4(Color,1.);
  179. }
  180. float4 PostFilter(float4 position : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
  181. { float Shift;
  182. if(Alternate && HFR_AA)
  183. Shift = pix.x;
  184. return tex2D(BackBuffer, texcoord + float2(Shift * saturate(HFR_Adjust),0.0));
  185. }
  186. ///////////////////////////////////////////////////////////ReShade.fxh/////////////////////////////////////////////////////////////
  187. // Vertex shader generating a triangle covering the entire screen
  188. void PostProcessVS(in uint id : SV_VertexID, out float4 position : SV_Position, out float2 texcoord : TEXCOORD)
  189. {
  190. texcoord.x = (id == 2) ? 2.0 : 0.0;
  191. texcoord.y = (id == 1) ? 2.0 : 0.0;
  192. position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  193. }
  194. //*Rendering passes*//
  195. technique Normal_Filter_Anti_Aliasing
  196. {
  197. pass NFAA_Fast
  198. {
  199. VertexShader = PostProcessVS;
  200. PixelShader = Out;
  201. }
  202. pass HFR_AA
  203. {
  204. VertexShader = PostProcessVS;
  205. PixelShader = PostFilter;
  206. }
  207. }