Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.91 KB | None | 0 0
  1. //enable blurring, useless, disabled at all
  2. //#define EBLURRING
  3.  
  4. //enable sharpening
  5. #define ESHARPENING
  6.  
  7. //if defined, color sharpen, otherwise sharp by gray
  8. #define ESHARPENINGCOLOR
  9.  
  10. //enable noise in dark areas
  11. //#define ENOISE
  12.  
  13.  
  14. float ShiftSamplingRange=0.0;
  15. float SamplingRange=1.00;
  16. float SharpeningAmount=2.0;
  17. float ScanLineAmount=0.0;
  18. float ScanLineRepeat=0.0;
  19. float NoiseAmount=0;
  20.  
  21.  
  22. //keyboard controled variables
  23. float tempF1;
  24. float tempF2;
  25. float tempF3;
  26. float tempF4;
  27. float tempF5;
  28. float tempF6;
  29. float tempF7;
  30. float tempF8;
  31. float tempF9;
  32. float tempF0;
  33.  
  34. //global variables, already set before executing this code
  35. float ScreenSize; //width of the display resolution (1920 f.e.)
  36. float ScreenScaleY; //screen proportions (1.333 for 1920/1080)
  37.  
  38. //textures
  39. texture2D texColor;
  40. texture2D texNoise;
  41.  
  42. sampler2D SamplerColor = sampler_state
  43. {
  44. Texture = <texColor>;
  45. MinFilter = LINEAR;
  46. MagFilter = LINEAR;
  47. MipFilter = NONE;//NONE;
  48. AddressU = Clamp;
  49. AddressV = Clamp;
  50. SRGBTexture=FALSE;
  51. MaxMipLevel=0;
  52. MipMapLodBias=0;
  53. };
  54.  
  55. sampler2D SamplerNoise = sampler_state
  56. {
  57. Texture = <texNoise>;
  58. MinFilter = POINT;
  59. MagFilter = POINT;
  60. MipFilter = NONE;//NONE;
  61. AddressU = Wrap;
  62. AddressV = Wrap;
  63. SRGBTexture=TRUE;
  64. MaxMipLevel=0;
  65. MipMapLodBias=0;
  66. };
  67.  
  68. struct VS_OUTPUT_POST {
  69. float4 vpos : POSITION;
  70. float2 txcoord : TEXCOORD0;
  71. };
  72.  
  73. struct VS_INPUT_POST {
  74. float3 pos : POSITION;
  75. float2 txcoord : TEXCOORD0;
  76. };
  77.  
  78.  
  79. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  80. //
  81. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  82. VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
  83. {
  84. VS_OUTPUT_POST OUT;
  85.  
  86. float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);
  87.  
  88. OUT.vpos=pos;
  89. OUT.txcoord.xy=IN.txcoord.xy;
  90.  
  91. return OUT;
  92. }
  93.  
  94.  
  95. /*============================================================================
  96. FXAA3 QUALITY - PC
  97. NVIDIA FXAA III.8 by TIMOTHY LOTTES
  98. ============================================================================*/
  99. #define FXAA_LINEAR 0
  100. #define FXAA_QUALITY__EDGE_THRESHOLD (1.0/32.0)
  101. #define FXAA_QUALITY__EDGE_THRESHOLD_MIN (1.0/16.0)
  102. #define FXAA_QUALITY__SUBPIX_CAP (7.0/8.0)
  103. #define FXAA_QUALITY__SUBPIX_TRIM (1.0/12.0)
  104. #define FXAA_QUALITY__SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_QUALITY__SUBPIX_TRIM))
  105. #define FXAA_SEARCH_STEPS 16
  106. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  107.  
  108. float4 FxaaPixelShader(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
  109. {
  110.  
  111. #define FxaaTexTop(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
  112. #define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))
  113.  
  114. float2 pos = IN.txcoord.xy;
  115.  
  116. float2 rcpFrame = float2(1/ScreenSize, ScreenScaleY/ScreenSize);
  117. float4 rcpFrameOpt = float4(2/ScreenSize, 2*ScreenScaleY/ScreenSize, 0.5/ScreenSize, 0.5*ScreenScaleY/ScreenSize);
  118.  
  119. float lumaN = dot(FxaaTexOff(SamplerColor, pos.xy, float2(0, -1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  120. float lumaW = dot(FxaaTexOff(SamplerColor, pos.xy, float2(-1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  121.  
  122.  
  123. float4 rgbyM;
  124. rgbyM.xyz = FxaaTexTop(SamplerColor, pos.xy).xyz;
  125. rgbyM.w = dot(rgbyM.xyz, float3(0.299, 0.587, 0.114));
  126. float lumaE = dot(FxaaTexOff(SamplerColor, pos.xy, float2( 1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  127. float lumaS = dot(FxaaTexOff(SamplerColor, pos.xy, float2( 0, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  128. float lumaM = rgbyM.w;
  129.  
  130.  
  131. float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  132. float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  133. float range = rangeMax - rangeMin;
  134.  
  135. if(range < max(FXAA_QUALITY__EDGE_THRESHOLD_MIN, rangeMax * FXAA_QUALITY__EDGE_THRESHOLD)) return rgbyM;
  136.  
  137.  
  138. float lumaNW = dot(FxaaTexOff(SamplerColor, pos.xy, float2(-1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  139. float lumaNE = dot(FxaaTexOff(SamplerColor, pos.xy, float2( 1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  140. float lumaSW = dot(FxaaTexOff(SamplerColor, pos.xy, float2(-1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  141. float lumaSE = dot(FxaaTexOff(SamplerColor, pos.xy, float2( 1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
  142.  
  143.  
  144.  
  145. float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
  146. float rangeL = abs(lumaL - lumaM);
  147. float blendL = saturate((rangeL / range) - FXAA_QUALITY__SUBPIX_TRIM) * FXAA_QUALITY__SUBPIX_TRIM_SCALE;
  148. blendL = min(FXAA_QUALITY__SUBPIX_CAP, blendL);
  149.  
  150. float edgeVert = abs(lumaNW + (-2.0 * lumaN) + lumaNE) + 2.0 * abs(lumaW + (-2.0 * lumaM) + lumaE ) + abs(lumaSW + (-2.0 * lumaS) + lumaSE);
  151. float edgeHorz = abs(lumaNW + (-2.0 * lumaW) + lumaSW) + 2.0 * abs(lumaN + (-2.0 * lumaM) + lumaS ) + abs(lumaNE + (-2.0 * lumaE) + lumaSE);
  152. bool horzSpan = edgeHorz >= edgeVert;
  153.  
  154. float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
  155. if(!horzSpan) lumaN = lumaW;
  156. if(!horzSpan) lumaS = lumaE;
  157. float gradientN = abs(lumaN - lumaM);
  158. float gradientS = abs(lumaS - lumaM);
  159. lumaN = (lumaN + lumaM) * 0.5;
  160. lumaS = (lumaS + lumaM) * 0.5;
  161.  
  162. bool pairN = gradientN >= gradientS;
  163. if(!pairN) lumaN = lumaS;
  164. if(!pairN) gradientN = gradientS;
  165. if(!pairN) lengthSign *= -1.0;
  166. float2 posN;
  167. posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
  168. posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
  169.  
  170.  
  171. gradientN *= FXAA_SEARCH_THRESHOLD;
  172.  
  173. float2 posP = posN;
  174. float2 offNP = horzSpan ?
  175. float2(rcpFrame.x, 0.0) :
  176. float2(0.0f, rcpFrame.y);
  177. float lumaEndN;
  178. float lumaEndP;
  179. bool doneN = false;
  180. bool doneP = false;
  181. posN += offNP * (-1.5);
  182. posP += offNP * ( 1.5);
  183. for(int i = 0; i < FXAA_SEARCH_STEPS; i++)
  184. {
  185. lumaEndN = dot(FxaaTexTop(SamplerColor, posN.xy).xyz, float3(0.299, 0.587, 0.114));
  186. lumaEndP = dot(FxaaTexTop(SamplerColor, posP.xy).xyz, float3(0.299, 0.587, 0.114));
  187. bool doneN2 = abs(lumaEndN - lumaN) >= gradientN;
  188. bool doneP2 = abs(lumaEndP - lumaN) >= gradientN;
  189. if(doneN2 && !doneN) posN += offNP;
  190. if(doneP2 && !doneP) posP -= offNP;
  191. if(doneN2 && doneP2) break;
  192. doneN = doneN2;
  193. doneP = doneP2;
  194. if(!doneN) posN -= offNP * 2.0;
  195. if(!doneP) posP += offNP * 2.0;
  196. }
  197.  
  198. float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
  199. float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;
  200.  
  201. bool directionN = dstN < dstP;
  202. lumaEndN = directionN ? lumaEndN : lumaEndP;
  203.  
  204. if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
  205. lengthSign = 0.0;
  206.  
  207. float spanLength = (dstP + dstN);
  208. dstN = directionN ? dstN : dstP;
  209. float subPixelOffset = 0.5 + (dstN * (-1.0/spanLength));
  210. subPixelOffset += blendL * (1.0/8.0);
  211. subPixelOffset *= lengthSign;
  212. float3 rgbF = FxaaTexTop(SamplerColor, float2(pos.x + (horzSpan ? 0.0 : subPixelOffset), pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
  213.  
  214. #if (FXAA_LINEAR == 1)
  215. lumaL *= lumaL;
  216. #endif
  217. float lumaF = dot(rgbF, float3(0.299, 0.587, 0.114)) + (1.0/(65536.0*256.0));
  218. float lumaB = lerp(lumaF, lumaL, blendL);
  219. float scale = min(4.0, lumaB/lumaF);
  220. rgbF *= scale;
  221.  
  222. return float4(rgbF, lumaM);
  223. }
  224.  
  225. float4 PS_PostProcess5(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
  226. {
  227. float4 res;
  228. float4 coord=0.0;
  229.  
  230. coord.xy=IN.txcoord.xy;
  231. float4 origcolor;
  232.  
  233. coord.w=0.0;
  234.  
  235.  
  236. origcolor=tex2Dlod(SamplerColor, coord);
  237.  
  238. // coord.x=IN.txcoord.x-(1.5/ScreenSize);
  239. // float4 lshift=tex2Dlod(SamplerColor, coord);
  240. // coord.x=IN.txcoord.x+(1.5/ScreenSize);
  241. // float4 rshift=tex2Dlod(SamplerColor, coord);
  242.  
  243.  
  244. float2 offset[8]=
  245. {
  246. float2(1.0, 1.0),
  247. float2(-1.0, -1.0),
  248. float2(-1.0, 1.0),
  249. float2(1.0, -1.0),
  250. float2(1.41, 0.0),
  251. float2(-1.41, 0.0),
  252. float2(0.0, 1.41),
  253. float2(0.0, -1.41)
  254. };
  255. int i=0;
  256.  
  257. float4 tcol=origcolor;
  258. float invscreensize=1.0/ScreenSize;
  259. //for (i=0; i<8; i++) //higher quality
  260. for (i=0; i<8; i++)
  261. {
  262. float2 tdir=offset[i].xy;
  263. coord.xy=IN.txcoord.xy+tdir.xy*invscreensize*SamplingRange;//*1.0;
  264. float4 ct=tex2Dlod(SamplerColor, coord);
  265.  
  266. tcol+=ct;
  267. }
  268. //tcol*=0.111; // 1.0/(4+1)
  269. tcol*=0.111; // 1.0/(8+1) //higher quality
  270.  
  271.  
  272. /*
  273. //not interesting
  274. #ifdef EBLURRING
  275. //blur
  276. res=tcol;
  277. #endif
  278. */
  279.  
  280. //sharp
  281. #ifdef ESHARPENING
  282.  
  283. #ifdef ESHARPENINGCOLOR
  284. //color
  285. res=origcolor*(1.0+((origcolor-tcol)*SharpeningAmount));
  286. #else
  287. //non color
  288. float difffact=dot((origcolor.xyz-tcol.xyz), 0.333);
  289. res=origcolor*(1.0+difffact*SharpeningAmount);
  290. #endif
  291.  
  292. //less sharpening for bright pixels
  293. float rgray=origcolor.z; //blue fit well
  294. //float rgray=max(origcolor.x, max(origcolor.y, origcolor.z));
  295. rgray=pow(rgray, 3.0);
  296. res=lerp(res, origcolor, saturate(rgray));
  297.  
  298. #endif
  299.  
  300.  
  301.  
  302.  
  303. //grain noise
  304. #ifdef ENOISE
  305. float origgray=max(res.x, res.y);//dot(res.xyz, 0.333);
  306. origgray=max(origgray, res.z);
  307. coord.xy=IN.txcoord.xy*16.0 + origgray;
  308. float4 cnoi=tex2Dlod(SamplerNoise, coord);
  309. res=lerp(res, (cnoi.x+1)*res, NoiseAmount*saturate(1.0-origgray*0.5));
  310. #endif
  311.  
  312.  
  313. res.w=1.0;
  314. return res;
  315. }
  316.  
  317. float4 PS_Process6(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
  318. {
  319. float4 res;
  320. float4 coord=0.0;
  321.  
  322. coord.xy=IN.txcoord.xy;
  323. float4 origcolor;
  324.  
  325. coord.w=0.0;
  326.  
  327.  
  328. origcolor=tex2Dlod(SamplerColor, coord);
  329.  
  330. // coord.x=IN.txcoord.x-(1.5/ScreenSize);
  331. // float4 lshift=tex2Dlod(SamplerColor, coord);
  332. // coord.x=IN.txcoord.x+(1.5/ScreenSize);
  333. // float4 rshift=tex2Dlod(SamplerColor, coord);
  334.  
  335.  
  336. float2 offset[8]=
  337. {
  338. float2(1.0, 1.0),
  339. float2(-1.0, -1.0),
  340. float2(-1.0, 1.0),
  341. float2(1.0, -1.0),
  342.  
  343. float2(1.41, 0.0),
  344. float2(-1.41, 0.0),
  345. float2(0.0, 1.41),
  346. float2(0.0, -1.41)
  347. };
  348. int i=0;
  349.  
  350. float4 tcol=origcolor;
  351. float2 invscreensize=1.0/ScreenSize;
  352. invscreensize.y=invscreensize.y/ScreenScaleY;
  353. //for (i=0; i<8; i++) //higher quality
  354. /* for (i=0; i<4; i++)
  355. {
  356. float2 tdir=offset[i].xy;
  357. coord.xy=IN.txcoord.xy+tdir.xy*invscreensize*SamplingRange;//*1.0;
  358. float4 ct=tex2Dlod(SamplerColor, coord);
  359.  
  360. tcol+=ct;
  361. }
  362. //tcol*=0.2; // 1.0/(4+1)
  363. tcol*=0.111; // 1.0/(8+1) //higher quality
  364. */
  365.  
  366. coord.xy=IN.txcoord.xy;
  367. origcolor=tex2Dlod(SamplerColor, coord);
  368. res.y=origcolor.y;
  369.  
  370. coord.xy=IN.txcoord.xy;
  371. coord.y-=invscreensize*ShiftSamplingRange;
  372. origcolor=tex2Dlod(SamplerColor, coord);
  373. res.x=origcolor.x;
  374.  
  375. coord.xy=IN.txcoord.xy;
  376. coord.y+=invscreensize*ShiftSamplingRange;
  377. origcolor=tex2Dlod(SamplerColor, coord);
  378. res.z=origcolor.z;
  379.  
  380.  
  381. res.w=1.0;
  382. return res;
  383. }
  384.  
  385.  
  386.  
  387.  
  388. technique PostProcess
  389. {
  390. pass P0
  391. {
  392. VertexShader = compile vs_3_0 VS_PostProcess();
  393. PixelShader = compile ps_3_0 FxaaPixelShader();
  394.  
  395. FogEnable=FALSE;
  396. ALPHATESTENABLE=FALSE;
  397. SEPARATEALPHABLENDENABLE=FALSE;
  398. AlphaBlendEnable=FALSE;
  399. FogEnable=FALSE;
  400. SRGBWRITEENABLE=FALSE;
  401. }
  402. }
  403.  
  404. technique PostProcess2
  405. {
  406. pass P0
  407. {
  408. VertexShader = compile vs_3_0 VS_PostProcess();
  409. PixelShader = compile ps_3_0 FxaaPixelShader();
  410.  
  411. FogEnable=FALSE;
  412. ALPHATESTENABLE=FALSE;
  413. SEPARATEALPHABLENDENABLE=FALSE;
  414. AlphaBlendEnable=FALSE;
  415. FogEnable=FALSE;
  416. SRGBWRITEENABLE=FALSE;
  417. }
  418. }
  419.  
  420. technique PostProcess3
  421. {
  422. pass P0
  423. {
  424. VertexShader = compile vs_3_0 VS_PostProcess();
  425. PixelShader = compile ps_3_0 FxaaPixelShader();
  426.  
  427. FogEnable=FALSE;
  428. ALPHATESTENABLE=FALSE;
  429. SEPARATEALPHABLENDENABLE=FALSE;
  430. AlphaBlendEnable=FALSE;
  431. FogEnable=FALSE;
  432. SRGBWRITEENABLE=FALSE;
  433. }
  434. }
  435.  
  436. technique PostProcess4
  437. {
  438. pass P0
  439. {
  440. VertexShader = compile vs_3_0 VS_PostProcess();
  441. PixelShader = compile ps_3_0 FxaaPixelShader();
  442.  
  443. FogEnable=FALSE;
  444. ALPHATESTENABLE=FALSE;
  445. SEPARATEALPHABLENDENABLE=FALSE;
  446. AlphaBlendEnable=FALSE;
  447. FogEnable=FALSE;
  448. SRGBWRITEENABLE=FALSE;
  449. }
  450. }
  451.  
  452. technique PostProcess5
  453. {
  454. pass P0
  455. {
  456.  
  457. VertexShader = compile vs_3_0 VS_PostProcess();
  458. PixelShader = compile ps_3_0 PS_PostProcess5();
  459.  
  460. DitherEnable=FALSE;
  461. ZEnable=FALSE;
  462. CullMode=NONE;
  463. ALPHATESTENABLE=FALSE;
  464. SEPARATEALPHABLENDENABLE=FALSE;
  465. AlphaBlendEnable=FALSE;
  466. StencilEnable=FALSE;
  467. FogEnable=FALSE;
  468. SRGBWRITEENABLE=FALSE;
  469. }
  470. }
  471. technique PostProcess6
  472. {
  473. pass P0
  474. {
  475.  
  476. VertexShader = compile vs_3_0 VS_PostProcess();
  477. PixelShader = compile ps_3_0 PS_Process6();
  478.  
  479. DitherEnable=FALSE;
  480. ZEnable=FALSE;
  481. CullMode=NONE;
  482. ALPHATESTENABLE=FALSE;
  483. SEPARATEALPHABLENDENABLE=FALSE;
  484. AlphaBlendEnable=FALSE;
  485. StencilEnable=FALSE;
  486. FogEnable=FALSE;
  487. SRGBWRITEENABLE=FALSE;
  488. }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement