dxgi.fx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Copyright (C) 2011 Jorge Jimenez (jorge@iryoku.com)
  3. * Copyright (C) 2011 Belen Masia (bmasia@unizar.es)
  4. * Copyright (C) 2011 Jose I. Echevarria (joseignacioechevarria@gmail.com)
  5. * Copyright (C) 2011 Fernando Navarro (fernandn@microsoft.com)
  6. * Copyright (C) 2011 Diego Gutierrez (diegog@unizar.es)
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution:
  18. *
  19. * "Uses SMAA. Copyright (C) 2011 by Jorge Jimenez, Jose I. Echevarria,
  20. * Belen Masia, Fernando Navarro and Diego Gutierrez."
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  23. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  26. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * The views and conclusions contained in the software and documentation are
  35. * those of the authors and should not be interpreted as representing official
  36. * policies, either expressed or implied, of the copyright holders.
  37. */
  38. //Include SweetFX settings
  39. #include "SweetFX_preset.txt"
  40. /**
  41. * Setup mandatory defines. Use a real macro here for maximum performance!
  42. */
  43. #ifndef SMAA_PIXEL_SIZE // It's actually set on runtime, this is for compilation time syntax checking.
  44. #define SMAA_PIXEL_SIZE float2(1.0 / 1280.0, 1.0 / 720.0)
  45. #endif
  46. /**
  47. * This is only required for temporal modes (SMAA T2x).
  48. */
  49. int4 subsampleIndices;
  50. /*
  51. #define SMAA_PRESET_CUSTOM
  52. #ifdef SMAA_PRESET_CUSTOM
  53. #define SMAA_THRESHOLD threshld
  54. #define SMAA_MAX_SEARCH_STEPS maxSearchSteps
  55. #define SMAA_MAX_SEARCH_STEPS_DIAG maxSearchStepsDiag
  56. #define SMAA_CORNER_ROUNDING cornerRounding
  57. #define SMAA_FORCE_DIAGONAL_DETECTION 1
  58. #define SMAA_FORCE_CORNER_DETECTION 1
  59. #endif
  60. */
  61. // Set the HLSL version:
  62. #ifndef SMAA_HLSL_4_1
  63. #define SMAA_HLSL_4 1
  64. #endif
  65. /*
  66. #ifdef SMAA_PRESET_CUSTOM
  67. #include "SMAA_settings.h" //include custom settings
  68. #endif
  69. */
  70. // And include our header!
  71. #include "SweetFX\Shaders\SMAA.h"
  72. // Set pixel shader version accordingly:
  73. #if SMAA_HLSL_4_1 == 1
  74. #define PS_VERSION ps_4_1
  75. #else
  76. #define PS_VERSION ps_4_0
  77. #endif
  78. /**
  79. * DepthStencilState's and company
  80. */
  81. DepthStencilState DisableDepthStencil {
  82. DepthEnable = FALSE;
  83. StencilEnable = FALSE;
  84. };
  85. DepthStencilState DisableDepthReplaceStencil {
  86. DepthEnable = FALSE;
  87. StencilEnable = TRUE;
  88. FrontFaceStencilPass = REPLACE;
  89. };
  90. DepthStencilState DisableDepthUseStencil {
  91. DepthEnable = FALSE;
  92. StencilEnable = TRUE;
  93. FrontFaceStencilFunc = EQUAL;
  94. };
  95. BlendState NoBlending {
  96. AlphaToCoverageEnable = FALSE;
  97. BlendEnable[0] = FALSE;
  98. };
  99. /**
  100. * Input textures
  101. */
  102. Texture2D colorTexPrev;
  103. Texture2D colorTex;
  104. Texture2D colorTexGamma;
  105. Texture2D depthTex;
  106. Texture2D velocityTex;
  107. /**
  108. * Temporal textures
  109. */
  110. Texture2D edgesTex;
  111. Texture2D blendTex;
  112. /**
  113. * Pre-computed area and search textures
  114. */
  115. Texture2D areaTex;
  116. Texture2D searchTex;
  117. #include "SweetFX/Shaders/Main.h"
  118. /**
  119. * Function wrappers
  120. */
  121. void DX10_SMAAEdgeDetectionVS(float4 position : POSITION,
  122. out float4 svPosition : SV_POSITION,
  123. inout float2 texcoord : TEXCOORD0,
  124. out float4 offset[3] : TEXCOORD1) {
  125. SMAAEdgeDetectionVS(position, svPosition, texcoord, offset);
  126. }
  127. void DX10_SMAABlendWeightCalculationVS(float4 position : POSITION,
  128. out float4 svPosition : SV_POSITION,
  129. inout float2 texcoord : TEXCOORD0,
  130. out float2 pixcoord : TEXCOORD1,
  131. out float4 offset[3] : TEXCOORD2) {
  132. SMAABlendWeightCalculationVS(position, svPosition, texcoord, pixcoord, offset);
  133. }
  134. void DX10_SMAANeighborhoodBlendingVS(float4 position : POSITION,
  135. out float4 svPosition : SV_POSITION,
  136. inout float2 texcoord : TEXCOORD0,
  137. out float4 offset[2] : TEXCOORD1) {
  138. SMAANeighborhoodBlendingVS(position, svPosition, texcoord, offset);
  139. }
  140. void DX10_SMAAResolveVS(float4 position : POSITION,
  141. out float4 svPosition : SV_POSITION,
  142. inout float2 texcoord : TEXCOORD0) {
  143. SMAAResolveVS(position, svPosition, texcoord);
  144. }
  145. [earlydepthstencil]
  146. float4 DX10_SMAALumaEdgeDetectionPS(float4 position : SV_POSITION,
  147. float2 texcoord : TEXCOORD0,
  148. float4 offset[3] : TEXCOORD1,
  149. uniform SMAATexture2D colorTexGamma) : SV_TARGET {
  150. #if SMAA_PREDICATION == 1
  151. return SMAALumaEdgeDetectionPS(texcoord, offset, colorTexGamma, depthTex);
  152. #else
  153. return SMAALumaEdgeDetectionPS(texcoord, offset, colorTexGamma);
  154. #endif
  155. }
  156. [earlydepthstencil]
  157. float4 DX10_SMAAColorEdgeDetectionPS(float4 position : SV_POSITION,
  158. float2 texcoord : TEXCOORD0,
  159. float4 offset[3] : TEXCOORD1,
  160. uniform SMAATexture2D colorTexGamma) : SV_TARGET {
  161. #if SMAA_PREDICATION == 1
  162. return SMAAColorEdgeDetectionPS(texcoord, offset, colorTexGamma, depthTex);
  163. #else
  164. return SMAAColorEdgeDetectionPS(texcoord, offset, colorTexGamma);
  165. #endif
  166. }
  167. [earlydepthstencil]
  168. float4 DX10_SMAADepthEdgeDetectionPS(float4 position : SV_POSITION,
  169. float2 texcoord : TEXCOORD0,
  170. float4 offset[3] : TEXCOORD1,
  171. uniform SMAATexture2D depthTex) : SV_TARGET {
  172. return SMAADepthEdgeDetectionPS(texcoord, offset, depthTex);
  173. }
  174. [earlydepthstencil]
  175. float4 DX10_SMAABlendingWeightCalculationPS(float4 position : SV_POSITION,
  176. float2 texcoord : TEXCOORD0,
  177. float2 pixcoord : TEXCOORD1,
  178. float4 offset[3] : TEXCOORD2,
  179. uniform SMAATexture2D edgesTex,
  180. uniform SMAATexture2D areaTex,
  181. uniform SMAATexture2D searchTex) : SV_TARGET {
  182. return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex, subsampleIndices);
  183. }
  184. [earlydepthstencil]
  185. float4 DX10_SMAANeighborhoodBlendingPS(float4 position : SV_POSITION,
  186. float2 texcoord : TEXCOORD0,
  187. float4 offset[2] : TEXCOORD1,
  188. uniform SMAATexture2D colorTex,
  189. uniform SMAATexture2D blendTex) : SV_TARGET {
  190. float4 SMAAoutput = SMAANeighborhoodBlendingPS(texcoord, offset, colorTex, blendTex);
  191. SMAAoutput = SweetFX_main(texcoord,SMAAoutput); // Add the other effects
  192. return SMAAoutput; //Returning the pixel
  193. }
  194. [earlydepthstencil]
  195. float4 DX10_SMAAResolvePS(float4 position : SV_POSITION,
  196. float2 texcoord : TEXCOORD0,
  197. uniform SMAATexture2D colorTex,
  198. uniform SMAATexture2D colorTexPrev,
  199. uniform SMAATexture2D velocityTex) : SV_TARGET {
  200. #if SMAA_REPROJECTION == 1
  201. return SMAAResolvePS(texcoord, colorTex, colorTexPrev, velocityTex);
  202. #else
  203. return SMAAResolvePS(texcoord, colorTex, colorTexPrev);
  204. #endif
  205. }
  206. #if (USE_SMAA_ANTIALIASING == 1)
  207. /**
  208. * Edge detection techniques
  209. */
  210. technique10 LumaEdgeDetection {
  211. pass LumaEdgeDetection {
  212. SetVertexShader(CompileShader(vs_4_0, DX10_SMAAEdgeDetectionVS()));
  213. SetGeometryShader(NULL);
  214. #if COLOR_EDGE_DETECTION == 1
  215. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAAColorEdgeDetectionPS(colorTexGamma)));
  216. #else
  217. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAALumaEdgeDetectionPS(colorTexGamma)));
  218. #endif
  219. SetDepthStencilState(DisableDepthReplaceStencil, 1);
  220. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  221. }
  222. }
  223. technique10 ColorEdgeDetection {
  224. pass ColorEdgeDetection {
  225. SetVertexShader(CompileShader(vs_4_0, DX10_SMAAEdgeDetectionVS()));
  226. SetGeometryShader(NULL);
  227. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAAColorEdgeDetectionPS(colorTexGamma)));
  228. SetDepthStencilState(DisableDepthReplaceStencil, 1);
  229. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  230. }
  231. }
  232. technique10 DepthEdgeDetection {
  233. pass DepthEdgeDetection {
  234. SetVertexShader(CompileShader(vs_4_0, DX10_SMAAEdgeDetectionVS()));
  235. SetGeometryShader(NULL);
  236. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAADepthEdgeDetectionPS(depthTex)));
  237. SetDepthStencilState(DisableDepthReplaceStencil, 1);
  238. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  239. }
  240. }
  241. /**
  242. * Blending weight calculation technique
  243. */
  244. technique10 BlendingWeightCalculation {
  245. pass BlendingWeightCalculation {
  246. SetVertexShader(CompileShader(vs_4_0, DX10_SMAABlendWeightCalculationVS()));
  247. SetGeometryShader(NULL);
  248. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAABlendingWeightCalculationPS(edgesTex, areaTex, searchTex)));
  249. SetDepthStencilState(DisableDepthUseStencil, 1);
  250. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  251. }
  252. }
  253. /**
  254. * Neighborhood blending technique
  255. */
  256. technique10 NeighborhoodBlending {
  257. pass NeighborhoodBlending {
  258. SetVertexShader(CompileShader(vs_4_0, DX10_SMAANeighborhoodBlendingVS()));
  259. SetGeometryShader(NULL);
  260. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAANeighborhoodBlendingPS(colorTex, blendTex)));
  261. SetDepthStencilState(DisableDepthStencil, 0);
  262. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  263. }
  264. }
  265. /**
  266. * Temporal resolve technique
  267. */
  268. technique10 Resolve {
  269. pass Resolve {
  270. SetVertexShader(CompileShader(vs_4_0, DX10_SMAAResolveVS()));
  271. SetGeometryShader(NULL);
  272. SetPixelShader(CompileShader(PS_VERSION, DX10_SMAAResolvePS(colorTex, colorTexPrev, velocityTex)));
  273. SetDepthStencilState(DisableDepthStencil, 0);
  274. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  275. }
  276. }
  277. #else //SMAA off
  278. /*------------------------.
  279. | :: SweetFX - structs :: |
  280. '------------------------*/
  281. struct Output
  282. {
  283. float4 position_cs : SV_POSITION;
  284. float2 texcoord : TEXCOORD;
  285. };
  286. /*---------------------------------------.
  287. | :: SweetFX - SMAA_off vertex shader :: |
  288. '---------------------------------------*/
  289. Output DX10_SMAA_off_VS(uint id: SV_VertexID) {
  290. Output output;
  291. output.texcoord = float2((id << 1) & 2, id & 2);
  292. output.position_cs = float4(output.texcoord * float2(2, -2) + float2(-1, 1), 0, 1);
  293. return output;
  294. }
  295. /*--------------------------------------.
  296. | :: SweetFX - SMAA_off pixel shader :: |
  297. '--------------------------------------*/
  298. [earlydepthstencil]
  299. float4 main(float4 position : SV_POSITION,
  300. float2 texcoord : TEXCOORD0,
  301. /*float4 offset[2] : TEXCOORD1,*/
  302. uniform SMAATexture2D colorTexGamma) : SV_TARGET {
  303. float4 SMAAoutput = myTex2D(colorTexGamma, texcoord);
  304. SMAAoutput = SweetFX_main(texcoord,SMAAoutput); // Add the other effects
  305. return SMAAoutput; //Returning the pixel
  306. }
  307. /*--------------------------.
  308. | :: SweetFX - Technique :: |
  309. '--------------------------*/
  310. /**
  311. * SMAA_off technique
  312. */
  313. technique10 LumaEdgeDetection {
  314. pass LumaEdgeDetection {
  315. SetVertexShader(CompileShader(vs_4_0, DX10_SMAA_off_VS()));
  316. SetGeometryShader(NULL);
  317. SetPixelShader(CompileShader(PS_VERSION, main(colorTexGamma)));
  318. SetDepthStencilState(DisableDepthStencil, 0); //SetDepthStencilState(DisableDepthReplaceStencil, 1);
  319. SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  320. }
  321. }
  322. #endif