d3d9.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "SweetFX/SweetFX_compatibility_settings.txt"
  41. /**
  42. * Setup mandatory defines. Use a real macro here for maximum performance!
  43. */
  44. #ifndef SMAA_PIXEL_SIZE // It's actually set on runtime, this is for compilation time syntax checking.
  45. #define SMAA_PIXEL_SIZE float2(1.0 / 1920.0, 1.0 / 1080.0)
  46. #endif
  47. /**
  48. * This can be ignored; its purpose is to support interactive custom parameter
  49. * tweaking.
  50. */
  51. /*
  52. float threshold;
  53. float maxSearchSteps;
  54. float maxSearchStepsDiag;
  55. */
  56. /*
  57. #ifdef SMAA_PRESET_CUSTOM
  58. #define SMAA_THRESHOLD threshold
  59. #define SMAA_MAX_SEARCH_STEPS maxSearchSteps
  60. #define SMAA_MAX_SEARCH_STEPS_DIAG maxSearchStepsDiag
  61. #define SMAA_FORCE_DIAGONALS 1
  62. #endif
  63. */
  64. // Set the HLSL version:
  65. #define SMAA_HLSL_3 1
  66. // And include our header!
  67. #include "SweetFX\Shaders\SMAA.h"
  68. /**
  69. * Input vars and textures.
  70. */
  71. texture2D colorTex2D;
  72. /*texture2D depthTex2D;*/
  73. texture2D edgesTex2D;
  74. texture2D blendTex2D;
  75. texture2D areaTex2D;
  76. texture2D searchTex2D;
  77. /**
  78. * DX9 samplers.
  79. */
  80. sampler2D colorTex {
  81. Texture = <colorTex2D>;
  82. AddressU = Clamp; AddressV = Clamp;
  83. MipFilter = Point; MinFilter = Linear; MagFilter = Linear;
  84. SRGBTexture = true;
  85. };
  86. sampler2D colorTexG {
  87. Texture = <colorTex2D>;
  88. AddressU = Clamp; AddressV = Clamp;
  89. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  90. SRGBTexture = false;
  91. };
  92. /*
  93. sampler2D depthTex {
  94. Texture = <depthTex2D>;
  95. AddressU = Clamp; AddressV = Clamp;
  96. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  97. SRGBTexture = false;
  98. };
  99. */
  100. sampler2D edgesTex {
  101. Texture = <edgesTex2D>;
  102. AddressU = Clamp; AddressV = Clamp;
  103. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  104. SRGBTexture = false;
  105. };
  106. sampler2D blendTex {
  107. Texture = <blendTex2D>;
  108. AddressU = Clamp; AddressV = Clamp;
  109. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  110. SRGBTexture = false;
  111. };
  112. sampler2D areaTex {
  113. Texture = <areaTex2D>;
  114. AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
  115. MipFilter = Linear; MinFilter = Linear; MagFilter = Linear;
  116. SRGBTexture = false;
  117. };
  118. sampler2D searchTex {
  119. Texture = <searchTex2D>;
  120. AddressU = Clamp; AddressV = Clamp; AddressW = Clamp;
  121. MipFilter = Point; MinFilter = Point; MagFilter = Point;
  122. SRGBTexture = false;
  123. };
  124. //Include the main SweetFX control shader
  125. #include "SweetFX/Shaders/Main.h"
  126. #if (USE_SMAA_ANTIALIASING == 1)
  127. /**
  128. * Function wrappers
  129. */
  130. void DX9_SMAAEdgeDetectionVS(inout float4 position : POSITION,
  131. inout float2 texcoord : TEXCOORD0,
  132. out float4 offset[3] : TEXCOORD1) {
  133. SMAAEdgeDetectionVS(position, position, texcoord, offset);
  134. }
  135. void DX9_SMAABlendWeightCalculationVS(inout float4 position : POSITION,
  136. inout float2 texcoord : TEXCOORD0,
  137. out float2 pixcoord : TEXCOORD1,
  138. out float4 offset[3] : TEXCOORD2) {
  139. SMAABlendWeightCalculationVS(position, position, texcoord, pixcoord, offset);
  140. }
  141. void DX9_SMAANeighborhoodBlendingVS(inout float4 position : POSITION,
  142. inout float2 texcoord : TEXCOORD0,
  143. out float4 offset[2] : TEXCOORD1) {
  144. SMAANeighborhoodBlendingVS(position, position, texcoord, offset);
  145. }
  146. float4 DX9_SMAALumaEdgeDetectionPS(float4 position : SV_POSITION,
  147. float2 texcoord : TEXCOORD0,
  148. float4 offset[3] : TEXCOORD1,
  149. uniform SMAATexture2D colorGammaTex) : COLOR {
  150. return SMAALumaEdgeDetectionPS(texcoord, offset, colorGammaTex);
  151. }
  152. float4 DX9_SMAAColorEdgeDetectionPS(float4 position : SV_POSITION,
  153. float2 texcoord : TEXCOORD0,
  154. float4 offset[3] : TEXCOORD1,
  155. uniform SMAATexture2D colorGammaTex) : COLOR {
  156. return SMAAColorEdgeDetectionPS(texcoord, offset, colorGammaTex);
  157. }
  158. float4 DX9_SMAADepthEdgeDetectionPS(float4 position : SV_POSITION,
  159. float2 texcoord : TEXCOORD0,
  160. float4 offset[3] : TEXCOORD1,
  161. uniform SMAATexture2D depthTex) : COLOR {
  162. return SMAADepthEdgeDetectionPS(texcoord, offset, depthTex);
  163. }
  164. float4 DX9_SMAABlendingWeightCalculationPS(float4 position : SV_POSITION,
  165. float2 texcoord : TEXCOORD0,
  166. float2 pixcoord : TEXCOORD1,
  167. float4 offset[3] : TEXCOORD2,
  168. uniform SMAATexture2D edgesTex,
  169. uniform SMAATexture2D areaTex,
  170. uniform SMAATexture2D searchTex) : COLOR {
  171. return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex, 0);
  172. }
  173. float4 DX9_SMAANeighborhoodBlendingPS(float4 position : SV_POSITION,
  174. float2 texcoord : TEXCOORD0,
  175. float4 offset[2] : TEXCOORD1,
  176. uniform SMAATexture2D colorTex,
  177. uniform SMAATexture2D blendTex) : COLOR {
  178. float4 SMAAoutput = SMAANeighborhoodBlendingPS(texcoord, offset, colorTex, blendTex);
  179. SMAAoutput = SweetFX_main(texcoord,SMAAoutput); // Add the other effects
  180. return SMAAoutput; //Returning the pixel
  181. }
  182. /*----------------------------------------------------------*/
  183. /**
  184. * Time for some techniques!
  185. */
  186. technique LumaEdgeDetection { // Pass 1A
  187. pass LumaEdgeDetection {
  188. VertexShader = compile vs_3_0 DX9_SMAAEdgeDetectionVS();
  189. #if COLOR_EDGE_DETECTION == 1
  190. PixelShader = compile ps_3_0 DX9_SMAAColorEdgeDetectionPS(colorTexG);
  191. #else
  192. PixelShader = compile ps_3_0 DX9_SMAALumaEdgeDetectionPS(colorTexG);
  193. #endif
  194. ZEnable = false;
  195. SRGBWriteEnable = false;
  196. AlphaBlendEnable = false;
  197. // We will be creating the stencil buffer for later usage.
  198. StencilEnable = true;
  199. StencilPass = REPLACE;
  200. StencilRef = 1;
  201. }
  202. }
  203. /*
  204. technique ColorEdgeDetection { // Pass 1B
  205. pass ColorEdgeDetection {
  206. VertexShader = compile vs_3_0 DX9_SMAAEdgeDetectionVS();
  207. PixelShader = compile ps_3_0 DX9_SMAAColorEdgeDetectionPS(colorTexG);
  208. ZEnable = false;
  209. SRGBWriteEnable = false;
  210. AlphaBlendEnable = false;
  211. // We will be creating the stencil buffer for later usage.
  212. StencilEnable = true;
  213. StencilPass = REPLACE;
  214. StencilRef = 1;
  215. }
  216. }
  217. */
  218. /*
  219. technique DepthEdgeDetection { // Pass 1C
  220. pass DepthEdgeDetection {
  221. VertexShader = compile vs_3_0 DX9_SMAAEdgeDetectionVS();
  222. PixelShader = compile ps_3_0 DX9_SMAADepthEdgeDetectionPS(depthTex);
  223. ZEnable = false;
  224. SRGBWriteEnable = false;
  225. AlphaBlendEnable = false;
  226. // We will be creating the stencil buffer for later usage.
  227. StencilEnable = true;
  228. StencilPass = REPLACE;
  229. StencilRef = 1;
  230. }
  231. }
  232. */
  233. technique BlendWeightCalculation { // Pass 2
  234. pass BlendWeightCalculation {
  235. VertexShader = compile vs_3_0 DX9_SMAABlendWeightCalculationVS();
  236. PixelShader = compile ps_3_0 DX9_SMAABlendingWeightCalculationPS(edgesTex, areaTex, searchTex);
  237. ZEnable = false;
  238. SRGBWriteEnable = false;
  239. AlphaBlendEnable = false;
  240. // Here we want to process only marked pixels.
  241. StencilEnable = true;
  242. StencilPass = KEEP;
  243. StencilFunc = EQUAL;
  244. StencilRef = 1;
  245. }
  246. }
  247. technique NeighborhoodBlending { // Pass 3
  248. pass NeighborhoodBlending {
  249. VertexShader = compile vs_3_0 DX9_SMAANeighborhoodBlendingVS();
  250. PixelShader = compile ps_3_0 DX9_SMAANeighborhoodBlendingPS(colorTex, blendTex);
  251. ZEnable = false;
  252. #if (Need_sRGB == 1)
  253. SRGBWriteEnable = false;
  254. #else
  255. SRGBWriteEnable = true;
  256. #endif
  257. AlphaBlendEnable = false;
  258. // Here we want to process all the pixels.
  259. StencilEnable = false;
  260. }
  261. }
  262. #else // if SMAA is off
  263. /*---------------------------------------.
  264. | :: SweetFX - SMAA_off vertex shader :: |
  265. '---------------------------------------*/
  266. void DX9_SMAA_off_VS(inout float4 position : POSITION
  267. ,inout float2 texcoord : TEXCOORD0
  268. //,out float4 lumacoord[2] : TEXCOORD1
  269. ){
  270. //lumacoord[0] = pixel.xyxy * (float4(0.5, -0.5, -0.5, -0.5) * offset_bias) + texcoord.xyxy;
  271. //lumacoord[1] = pixel.xyxy * (float4(0.5, 0.5, -0.5, 0.5) * offset_bias) + texcoord.xyxy;
  272. }
  273. /*--------------------------------------.
  274. | :: SweetFX - SMAA_off pixel shader :: |
  275. '--------------------------------------*/
  276. float4 main( float2 texcoord : TEXCOORD0
  277. //, float4 lumacoord[2] : TEXCOORD1
  278. ) : COLOR {
  279. float4 SMAAoutput = tex2D(s0, texcoord);
  280. //SMAAoutput = SweetFX_main(SMAAoutput,texcoord,lumacoord); // Add the other effects
  281. SMAAoutput = SweetFX_main(texcoord,SMAAoutput); // Add the other effects
  282. return SMAAoutput; //Returning the pixel
  283. }
  284. /*--------------------------.
  285. | :: SweetFX - Technique :: |
  286. '--------------------------*/
  287. //What Shader Model should we use?
  288. #if (SweetFX_shader_model == 0)
  289. #define vertex_shader_model vs_2_0
  290. #define pixel_shader_model ps_2_0
  291. #elif (SweetFX_shader_model == 1)
  292. #define vertex_shader_model vs_2_0
  293. #define pixel_shader_model ps_2_a
  294. #elif (SweetFX_shader_model == 2)
  295. #define vertex_shader_model vs_2_0
  296. #define pixel_shader_model ps_2_b
  297. #else
  298. #define vertex_shader_model vs_3_0
  299. #define pixel_shader_model ps_3_0
  300. #endif
  301. technique SMAA_off { // Not doing SMAA.
  302. pass SMAA_off {
  303. VertexShader = compile vertex_shader_model DX9_SMAA_off_VS();
  304. PixelShader = compile pixel_shader_model main(); //Use this with GPU Shaderanalyzer
  305. ZEnable = false;
  306. SRGBWriteEnable = false;
  307. AlphaBlendEnable = false;
  308. // Here we want to process all the pixels.
  309. StencilEnable = false;
  310. }
  311. }
  312. #endif