Guest User

Updated SMAA

a guest
Mar 25th, 2019
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * _______ ___ ___ ___ ___
  3. * / || \/ | / \ / \
  4. * | (---- | \ / | / ^ \ / ^ \
  5. * \ \ | |\/| | / /_\ \ / /_\ \
  6. * ----) | | | | | / _____ \ / _____ \
  7. * |_______/ |__| |__| /__/ \__\ /__/ \__\
  8. *
  9. * E N H A N C E D
  10. * S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
  11. *
  12. * for ReShade 3.0+
  13. */
  14.  
  15. //------------------- Preprocessor Settings -------------------
  16.  
  17. #ifndef SMAA_PREDICATION
  18. #define SMAA_PREDICATION 0 // Disable predication by default
  19. #endif
  20.  
  21. #if !defined(SMAA_PRESET_LOW) && !defined(SMAA_PRESET_MEDIUM) && !defined(SMAA_PRESET_HIGH) && !defined(SMAA_PRESET_ULTRA)
  22. #define SMAA_PRESET_CUSTOM // Do not use a quality preset by default
  23. #endif
  24.  
  25. //----------------------- UI Variables ------------------------
  26.  
  27. #include "ReShadeUI.fxh"
  28.  
  29. uniform int EdgeDetectionType <
  30. ui_type = "combo";
  31. ui_items = "Luminance edge detection\0Color edge detection\0Depth edge detection\0";
  32. ui_label = "Edge Detection Type";
  33. > = 1;
  34.  
  35. #ifdef SMAA_PRESET_CUSTOM
  36. uniform float EdgeDetectionThreshold <
  37. ui_type = "drag";
  38. ui_min = 0.05; ui_max = 0.20; ui_step = 0.01;
  39. ui_tooltip = "Edge detection threshold. If SMAA misses some edges try lowering this slightly.";
  40. ui_label = "Edge Detection Threshold";
  41. > = 0.10;
  42.  
  43. uniform int MaxSearchSteps < __UNIFORM_SLIDER_INT1
  44. ui_min = 0; ui_max = 112;
  45. ui_label = "Max Search Steps";
  46. ui_tooltip = "Determines the radius SMAA will search for aliased edges.";
  47. > = 32;
  48.  
  49. uniform int MaxSearchStepsDiagonal < __UNIFORM_SLIDER_INT1
  50. ui_min = 0; ui_max = 20;
  51. ui_label = "Max Search Steps Diagonal";
  52. ui_tooltip = "Determines the radius SMAA will search for diagonal aliased edges";
  53. > = 16;
  54.  
  55. uniform int CornerRounding < __UNIFORM_SLIDER_INT1
  56. ui_min = 0; ui_max = 100;
  57. ui_label = "Corner Rounding";
  58. ui_tooltip = "Determines the percent of anti-aliasing to apply to corners.";
  59. > = 25;
  60.  
  61. #if SMAA_PREDICATION
  62. uniform float PredicationThreshold <
  63. ui_type = "drag";
  64. ui_min = 0.005; ui_max = 1.00; ui_step = 0.01;
  65. ui_tooltip = "Threshold to be used in the additional predication buffer.";
  66. ui_label = "Predication Threshold";
  67. > = 0.01;
  68.  
  69. uniform float PredicationScale < __UNIFORM_SLIDER_FLOAT1
  70. ui_min = 1; ui_max = 5;
  71. ui_tooltip = "How much to scale the global threshold used for luma or color edge.";
  72. ui_label = "Predication Scale";
  73. > = 2.0;
  74.  
  75. uniform float PredicationStrength < __UNIFORM_SLIDER_FLOAT1
  76. ui_min = 0; ui_max = 1;
  77. ui_tooltip = "How much to locally decrease the threshold.";
  78. ui_label = "Predication Strength";
  79. > = 0.4;
  80. #endif
  81. #endif
  82.  
  83. uniform int DebugOutput <
  84. ui_type = "combo";
  85. ui_items = "None\0View edges\0View weights\0";
  86. ui_label = "Debug Output";
  87. > = false;
  88.  
  89. #ifdef SMAA_PRESET_CUSTOM
  90. #define SMAA_THRESHOLD EdgeDetectionThreshold
  91. #define SMAA_MAX_SEARCH_STEPS MaxSearchSteps
  92. #define SMAA_MAX_SEARCH_STEPS_DIAG MaxSearchStepsDiagonal
  93. #define SMAA_CORNER_ROUNDING CornerRounding
  94. #if SMAA_PREDICATION
  95. #define SMAA_PREDICATION_THRESHOLD PredicationThreshold
  96. #define SMAA_PREDICATION_SCALE PredicationScale
  97. #define SMAA_PREDICATION_STRENGTH PredicationStrength
  98. #endif
  99. #endif
  100.  
  101. #define SMAA_RT_METRICS float4(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT, BUFFER_WIDTH, BUFFER_HEIGHT)
  102. #define SMAA_CUSTOM_SL 1
  103.  
  104. #define predicationSampler depthLinearSampler
  105.  
  106. #define SMAATexture2D(tex) sampler tex
  107. #define SMAATexturePass2D(tex) tex
  108. #define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, coord))
  109. #define SMAASampleLevelZeroPoint(tex, coord) SMAASampleLevelZero(tex, coord)
  110. #define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlodoffset(tex, float4(coord, coord), offset)
  111. #define SMAASample(tex, coord) tex2D(tex, coord)
  112. #define SMAASamplePoint(tex, coord) SMAASample(tex, coord)
  113. #define SMAASampleOffset(tex, coord, offset) tex2Doffset(tex, coord, offset)
  114. #define SMAA_BRANCH [branch]
  115. #define SMAA_FLATTEN [flatten]
  116.  
  117. #if (__RENDERER__ == 0xb000 || __RENDERER__ == 0xb100)
  118. #define SMAAGather(tex, coord) tex2Dgather(tex, coord, 0)
  119. #endif
  120.  
  121. #include "SMAA.fxh"
  122. #include "ReShade.fxh"
  123.  
  124. // Textures
  125.  
  126. texture depthTex
  127. {
  128. Width = BUFFER_WIDTH;
  129. Height = BUFFER_HEIGHT;
  130. Format = R16F;
  131. };
  132.  
  133. texture edgesTex
  134. {
  135. Width = BUFFER_WIDTH;
  136. Height = BUFFER_HEIGHT;
  137. Format = RG8;
  138. };
  139. texture blendTex
  140. {
  141. Width = BUFFER_WIDTH;
  142. Height = BUFFER_HEIGHT;
  143. Format = RGBA8;
  144. };
  145.  
  146. texture areaTex < source = "AreaTex.dds"; >
  147. {
  148. Width = 160;
  149. Height = 560;
  150. Format = RG8;
  151. };
  152. texture searchTex < source = "SearchTex.dds"; >
  153. {
  154. Width = 64;
  155. Height = 16;
  156. Format = R8;
  157. };
  158.  
  159. // Samplers
  160.  
  161. sampler depthLinearSampler
  162. {
  163. Texture = depthTex;
  164. AddressU = Clamp; AddressV = Clamp;
  165. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  166. SRGBTexture = false;
  167. };
  168.  
  169. sampler colorGammaSampler
  170. {
  171. Texture = ReShade::BackBufferTex;
  172. AddressU = Clamp; AddressV = Clamp;
  173. MipFilter = Point; MinFilter = Linear; MagFilter = Linear;
  174. SRGBTexture = false;
  175. };
  176. sampler colorLinearSampler
  177. {
  178. Texture = ReShade::BackBufferTex;
  179. AddressU = Clamp; AddressV = Clamp;
  180. MipFilter = Point; MinFilter = Linear; MagFilter = Linear;
  181. SRGBTexture = true;
  182. };
  183. sampler edgesSampler
  184. {
  185. Texture = edgesTex;
  186. AddressU = Clamp; AddressV = Clamp;
  187. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  188. SRGBTexture = false;
  189. };
  190. sampler blendSampler
  191. {
  192. Texture = blendTex;
  193. AddressU = Clamp; AddressV = Clamp;
  194. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  195. SRGBTexture = false;
  196. };
  197. sampler areaSampler
  198. {
  199. Texture = areaTex;
  200. AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
  201. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  202. SRGBTexture = false;
  203. };
  204. sampler searchSampler
  205. {
  206. Texture = searchTex;
  207. AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
  208. MipFilter = Point; MinFilter = Point; MagFilter = Point;
  209. SRGBTexture = false;
  210. };
  211.  
  212. // Vertex shaders
  213.  
  214. void SMAAEdgeDetectionWrapVS(
  215. in uint id : SV_VertexID,
  216. out float4 position : SV_Position,
  217. out float2 texcoord : TEXCOORD0,
  218. out float4 offset[3] : TEXCOORD1)
  219. {
  220. PostProcessVS(id, position, texcoord);
  221. SMAAEdgeDetectionVS(texcoord, offset);
  222. }
  223. void SMAABlendingWeightCalculationWrapVS(
  224. in uint id : SV_VertexID,
  225. out float4 position : SV_Position,
  226. out float2 texcoord : TEXCOORD0,
  227. out float2 pixcoord : TEXCOORD1,
  228. out float4 offset[3] : TEXCOORD2)
  229. {
  230. PostProcessVS(id, position, texcoord);
  231. SMAABlendingWeightCalculationVS(texcoord, pixcoord, offset);
  232. }
  233. void SMAANeighborhoodBlendingWrapVS(
  234. in uint id : SV_VertexID,
  235. out float4 position : SV_Position,
  236. out float2 texcoord : TEXCOORD0,
  237. out float4 offset : TEXCOORD1)
  238. {
  239. PostProcessVS(id, position, texcoord);
  240. SMAANeighborhoodBlendingVS(texcoord, offset);
  241. }
  242.  
  243. // Pixel shaders
  244.  
  245. float SMAADepthLinearizationPS(
  246. float4 position : SV_Position,
  247. float2 texcoord : TEXCOORD0) : SV_Target
  248. {
  249. return ReShade::GetLinearizedDepth(texcoord.xy);
  250. }
  251.  
  252. float2 SMAAEdgeDetectionWrapPS(
  253. float4 position : SV_Position,
  254. float2 texcoord : TEXCOORD0,
  255. float4 offset[3] : TEXCOORD1) : SV_Target
  256. {
  257. if (EdgeDetectionType == 0)
  258. return SMAALumaEdgeDetectionPS(texcoord, offset, colorGammaSampler
  259. #if SMAA_PREDICATION
  260. , predicationSampler
  261. #endif
  262. );
  263. if (EdgeDetectionType == 2)
  264. return SMAADepthEdgeDetectionPS(texcoord, offset, depthLinearSampler);
  265.  
  266. return SMAAColorEdgeDetectionPS(texcoord, offset, colorGammaSampler
  267. #if SMAA_PREDICATION
  268. , predicationSampler
  269. #endif
  270. );
  271. }
  272. float4 SMAABlendingWeightCalculationWrapPS(
  273. float4 position : SV_Position,
  274. float2 texcoord : TEXCOORD0,
  275. float2 pixcoord : TEXCOORD1,
  276. float4 offset[3] : TEXCOORD2) : SV_Target
  277. {
  278. return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesSampler, areaSampler, searchSampler, 0.0);
  279. }
  280.  
  281. float3 SMAANeighborhoodBlendingWrapPS(
  282. float4 position : SV_Position,
  283. float2 texcoord : TEXCOORD0,
  284. float4 offset : TEXCOORD1) : SV_Target
  285. {
  286. if (DebugOutput == 1)
  287. return tex2D(edgesSampler, texcoord).rgb;
  288. if (DebugOutput == 2)
  289. return tex2D(blendSampler, texcoord).rgb;
  290.  
  291. return SMAANeighborhoodBlendingPS(texcoord, offset, colorLinearSampler, blendSampler).rgb;
  292. }
  293.  
  294. // Rendering passes
  295.  
  296. technique SMAA
  297. {
  298. pass LinearizeDepthPass
  299. {
  300. VertexShader = PostProcessVS;
  301. PixelShader = SMAADepthLinearizationPS;
  302. RenderTarget = depthTex;
  303. }
  304. pass EdgeDetectionPass
  305. {
  306. VertexShader = SMAAEdgeDetectionWrapVS;
  307. PixelShader = SMAAEdgeDetectionWrapPS;
  308. RenderTarget = edgesTex;
  309. ClearRenderTargets = true;
  310. StencilEnable = true;
  311. StencilPass = REPLACE;
  312. StencilRef = 1;
  313. }
  314. pass BlendWeightCalculationPass
  315. {
  316. VertexShader = SMAABlendingWeightCalculationWrapVS;
  317. PixelShader = SMAABlendingWeightCalculationWrapPS;
  318. RenderTarget = blendTex;
  319. ClearRenderTargets = true;
  320. StencilEnable = true;
  321. StencilPass = KEEP;
  322. StencilFunc = EQUAL;
  323. StencilRef = 1;
  324. }
  325. pass NeighborhoodBlendingPass
  326. {
  327. VertexShader = SMAANeighborhoodBlendingWrapVS;
  328. PixelShader = SMAANeighborhoodBlendingWrapPS;
  329. StencilEnable = false;
  330. SRGBWriteEnable = true;
  331. }
  332. }
RAW Paste Data