Advertisement
Guest User

ENB - Depth bloom

a guest
Mar 6th, 2016
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.73 KB | None | 0 0
  1. // THERE IS NOTHING TO EDIT BELOW THIS POINT ++++++++++++++++++++++++++++++++++++++++++++
  2. // GUI ELEMENTS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3. bool Section_Options <
  4. string UIName = "------Options & Settings----";
  5. > = {false};
  6. float QUALITYLEVEL <
  7. string UIName="Bloom Quality";
  8. string UIWidget="Quality";
  9. float UIMin=0;
  10. float UIMax=2;
  11. float UIStep=1;
  12. > = {0}; //0 best compromise between quality and performance
  13. bool Section_Depth <
  14. string UIName = "------Depth-----------------";
  15. > = {false};
  16. float zfar <
  17. string UIName="Far Depth";
  18. string UIWidget="Spinner";
  19. float UIMin=0.0;
  20. float UIMax=100000.0;
  21. float UIStep=0.1;
  22. > = {50.0};
  23. float minDepthE <
  24. string UIName="Exterior: Min Depth";
  25. string UIWidget="Spinner";
  26. float UIMin=0;
  27. float UIMax=1;
  28. float UIStep=0.001;
  29. > = {0.67};
  30. float minDepthI <
  31. string UIName="Interior: Min Depth";
  32. string UIWidget="Spinner";
  33. float UIMin=0;
  34. float UIMax=1;
  35. float UIStep=0.001;
  36. > = {0.67};
  37. bool Section_Tint <
  38. string UIName = "------Tinting---------------";
  39. > = {false};
  40. bool use_tinting <
  41. string UIName="Tinting: Enable Bloom Tinting";
  42. > = {true};
  43. float tint_type <
  44. string UIName="Tinting: Tint Method";
  45. string UIWidget="Spinner";
  46. float UIMin=1;
  47. float UIMax=3;
  48. float UIStep=1;
  49. > = {1};
  50. float3 tint_colorD <
  51. string UIName="Tinting: Day Tint RGB Color";
  52. string UIWidget="Color";
  53. > = {0.6, 0.4, 1.0};
  54. float tint_levelD <
  55. string UIName="Tinting: Day Tint Level";
  56. string UIWidget="Spinner";
  57. float UIMin=0.0;
  58. float UIMax=1.0;
  59. float UIStep=0.001;
  60. > = {0.44};
  61. float tint_mixD <
  62. string UIName="Tinting: (method 3) Day Tint Mix";
  63. string UIWidget="Spinner";
  64. float UIMin=0.0;
  65. float UIMax=1.0;
  66. float UIStep=0.001;
  67. > = {0.22};
  68. float3 tint_colorN <
  69. string UIName="Tinting: Night Tint RGB Color";
  70. string UIWidget="Color";
  71. > = {0.6, 0.4, 1.0};
  72. float tint_levelN <
  73. string UIName="Tinting: Night Tint Level";
  74. string UIWidget="Spinner";
  75. float UIMin=0.0;
  76. float UIMax=1.0;
  77. float UIStep=0.001;
  78. > = {0.44};
  79. float tint_mixN <
  80. string UIName="Tinting: (method 3) Night Tint Mix";
  81. string UIWidget="Spinner";
  82. float UIMin=0.0;
  83. float UIMax=1.0;
  84. float UIStep=0.001;
  85. > = {0.22};
  86. float3 tint_colorI <
  87. string UIName="Tinting: Interior Tint RGB Color";
  88. string UIWidget="Color";
  89. > = {0.6, 0.4, 1.0};
  90. float tint_levelI <
  91. string UIName="Tinting: Interior Tint Level";
  92. string UIWidget="Spinner";
  93. float UIMin=0.0;
  94. float UIMax=1.0;
  95. float UIStep=0.001;
  96. > = {0.44};
  97. float tint_mixI <
  98. string UIName="Tinting: (method 3) Interior Tint Mix";
  99. string UIWidget="Spinner";
  100. float UIMin=0.0;
  101. float UIMax=1.0;
  102. float UIStep=0.001;
  103. > = {0.22};
  104. bool Section_BDay <
  105. string UIName = "------Bloom Day-------------";
  106. > = {false};
  107. float BinBlackD <
  108. string UIName="Day Bloom: Black Level";
  109. string UIWidget="Spinner";
  110. float UIMin=0.0;
  111. float UIMax=1.0;
  112. float UIStep=0.001;
  113. > = {0.0};
  114. float BinGammaD <
  115. string UIName="Day Bloom: Gamma Curve";
  116. string UIWidget="Spinner";
  117. float UIMin=0.0;
  118. float UIMax=5.0;
  119. float UIStep=0.001;
  120. > = {1.0};
  121. float BinWhiteD <
  122. string UIName="Day Bloom: White Level";
  123. string UIWidget="Spinner";
  124. float UIMin=0.0;
  125. float UIMax=1.0;
  126. float UIStep=0.001;
  127. > = {1.0};
  128. float BoutBlackD <
  129. string UIName="Day Bloom: Black Cutoff";
  130. string UIWidget="Spinner";
  131. float UIMin=0.0;
  132. float UIMax=1.0;
  133. float UIStep=0.001;
  134. > = {0.0};
  135. float BoutWhiteD <
  136. string UIName="Day Bloom: White Cutoff";
  137. string UIWidget="Spinner";
  138. float UIMin=0.0;
  139. float UIMax=1.0;
  140. float UIStep=0.001;
  141. > = {1.0};
  142. float bsatD <
  143. string UIName="Day Bloom: Saturation";
  144. string UIWidget="Spinner";
  145. float UIMin=0.0;
  146. float UIMax=2.0;
  147. float UIStep=0.001;
  148. > = {1.0};
  149. float BloomSigmaD <
  150. string UIName="Day Bloom: Sigma (Blur width)";
  151. string UIWidget="Spinner";
  152. float UIMin=1.2;
  153. float UIMax=64.0;
  154. float UIStep=0.001;
  155. > = {6.0};
  156. float hIntensityD <
  157. string UIName="Day Bloom: X Intensity";
  158. string UIWidget="Spinner";
  159. float UIMin=1.0;
  160. float UIMax=10.0;
  161. float UIStep=0.001;
  162. > = {1.0};
  163. float vIntensityD <
  164. string UIName="Day Bloom: X+Y Intensity";
  165. string UIWidget="Spinner";
  166. float UIMin=1.0;
  167. float UIMax=10.0;
  168. float UIStep=0.001;
  169. > = {1.0};
  170. bool Section_BNight <
  171. string UIName = "------Bloom Night-----------";
  172. > = {false};
  173. float BinBlackN <
  174. string UIName="Night Bloom: Black Level";
  175. string UIWidget="Spinner";
  176. float UIMin=0.0;
  177. float UIMax=1.0;
  178. float UIStep=0.001;
  179. > = {0.0};
  180. float BinGammaN <
  181. string UIName="Night Bloom: Gamma Curve";
  182. string UIWidget="Spinner";
  183. float UIMin=0.0;
  184. float UIMax=5.0;
  185. float UIStep=0.001;
  186. > = {1.0};
  187. float BinWhiteN <
  188. string UIName="Night Bloom: White Level";
  189. string UIWidget="Spinner";
  190. float UIMin=0.0;
  191. float UIMax=1.0;
  192. float UIStep=0.001;
  193. > = {1.0};
  194. float BoutBlackN <
  195. string UIName="Night Bloom: Black Cutoff";
  196. string UIWidget="Spinner";
  197. float UIMin=0.0;
  198. float UIMax=1.0;
  199. float UIStep=0.001;
  200. > = {0.0};
  201. float BoutWhiteN <
  202. string UIName="Night Bloom: White Cutoff";
  203. string UIWidget="Spinner";
  204. float UIMin=0.0;
  205. float UIMax=1.0;
  206. float UIStep=0.001;
  207. > = {1.0};
  208. float bsatN <
  209. string UIName="Night Bloom: Saturation";
  210. string UIWidget="Spinner";
  211. float UIMin=0.0;
  212. float UIMax=2.0;
  213. float UIStep=0.001;
  214. > = {1.0};
  215. float BloomSigmaN <
  216. string UIName="Night Bloom: Sigma (Blur width)";
  217. string UIWidget="Spinner";
  218. float UIMin=1.2;
  219. float UIMax=64.0;
  220. float UIStep=0.001;
  221. > = {6.0};
  222. float hIntensityN <
  223. string UIName="Night Bloom: X Intensity";
  224. string UIWidget="Spinner";
  225. float UIMin=1.0;
  226. float UIMax=10.0;
  227. float UIStep=0.001;
  228. > = {1.0};
  229. float vIntensityN <
  230. string UIName="Night Bloom: X+Y Intensity";
  231. string UIWidget="Spinner";
  232. float UIMin=1.0;
  233. float UIMax=10.0;
  234. float UIStep=0.001;
  235. > = {1.0};
  236. bool Section_BInterior <
  237. string UIName = "------Bloom Interior--------";
  238. > = {false};
  239. float BinBlackI <
  240. string UIName="Interior Bloom: Black Level";
  241. string UIWidget="Spinner";
  242. float UIMin=0.0;
  243. float UIMax=1.0;
  244. float UIStep=0.001;
  245. > = {0.0};
  246. float BinGammaI <
  247. string UIName="Interior Bloom: Gamma Curve";
  248. string UIWidget="Spinner";
  249. float UIMin=0.0;
  250. float UIMax=5.0;
  251. float UIStep=0.001;
  252. > = {1.0};
  253. float BinWhiteI <
  254. string UIName="Interior Bloom: White Level";
  255. string UIWidget="Spinner";
  256. float UIMin=0.0;
  257. float UIMax=1.0;
  258. float UIStep=0.001;
  259. > = {1.0};
  260. float BoutBlackI <
  261. string UIName="Interior Bloom: Black Cutoff";
  262. string UIWidget="Spinner";
  263. float UIMin=0.0;
  264. float UIMax=1.0;
  265. float UIStep=0.001;
  266. > = {0.0};
  267. float BoutWhiteI <
  268. string UIName="Interior Bloom: White Cutoff";
  269. string UIWidget="Spinner";
  270. float UIMin=0.0;
  271. float UIMax=1.0;
  272. float UIStep=0.001;
  273. > = {1.0};
  274. float bsatI <
  275. string UIName="Interior Bloom: Saturation";
  276. string UIWidget="Spinner";
  277. float UIMin=0.0;
  278. float UIMax=2.0;
  279. float UIStep=0.001;
  280. > = {1.0};
  281. float BloomSigmaI <
  282. string UIName="Interior Bloom: Sigma (Blur width)";
  283. string UIWidget="Spinner";
  284. float UIMin=1.2;
  285. float UIMax=64.0;
  286. float UIStep=0.001;
  287. > = {6.0};
  288. float hIntensityI <
  289. string UIName="Interior Bloom: X Intensity";
  290. string UIWidget="Spinner";
  291. float UIMin=1.0;
  292. float UIMax=10.0;
  293. float UIStep=0.001;
  294. > = {1.0};
  295. float vIntensityI <
  296. string UIName="Interior Bloom: X+Y Intensity";
  297. string UIWidget="Spinner";
  298. float UIMin=1.0;
  299. float UIMax=10.0;
  300. float UIStep=0.001;
  301. > = {1.0};
  302. bool Section_Textures <
  303. string UIName = "------Texture Selection-----";
  304. > = {false};
  305. bool tex_select1 <
  306. string UIName="Enable Tex 1: 512x512";
  307. > = {true};
  308. bool tex_select2 <
  309. string UIName="Enable Tex 2: 256x256";
  310. > = {true};
  311. bool tex_select3 <
  312. string UIName="Enable Tex 3: 128x128";
  313. > = {true};
  314. bool tex_select4 <
  315. string UIName="Enable Tex 4: 64x64";
  316. > = {true};
  317. bool tex_select5 <
  318. string UIName="Enable Tex 5: 32x32";
  319. > = {true};
  320. bool tex_select6 <
  321. string UIName="Enable Tex 6: 16x16";
  322. > = {true};
  323. bool tex_select7 <
  324. string UIName="Enable Tex 7: 8x8";
  325. > = {true};
  326. bool Section_SR <
  327. string UIName = "------Texture Sample Range--";
  328. > = {false};
  329. float BloomTex1SR <
  330. string UIName="Tex 1: Sample Range";
  331. string UIWidget="Spinner";
  332. float UIMin=0.0;
  333. float UIMax=0.5;
  334. float UIStep=0.001;
  335. > = {0.5};
  336. float BloomTex2SR <
  337. string UIName="Tex 2: Sample Range";
  338. string UIWidget="Spinner";
  339. float UIMin=0.0;
  340. float UIMax=0.5;
  341. float UIStep=0.001;
  342. > = {0.25};
  343. float BloomTex3SR <
  344. string UIName="Tex 3: Sample Range";
  345. string UIWidget="Spinner";
  346. float UIMin=0.0;
  347. float UIMax=0.5;
  348. float UIStep=0.001;
  349. > = {0.25};
  350. float BloomTex4SR <
  351. string UIName="Tex 4: Sample Range";
  352. string UIWidget="Spinner";
  353. float UIMin=0.0;
  354. float UIMax=0.5;
  355. float UIStep=0.001;
  356. > = {0.25};
  357. float BloomTex5SR <
  358. string UIName="Tex 5: Sample Range";
  359. string UIWidget="Spinner";
  360. float UIMin=0.0;
  361. float UIMax=0.5;
  362. float UIStep=0.001;
  363. > = {0.25};
  364. float BloomTex6SR <
  365. string UIName="Tex 6: Sample Range";
  366. string UIWidget="Spinner";
  367. float UIMin=0.0;
  368. float UIMax=0.5;
  369. float UIStep=0.001;
  370. > = {0.25};
  371. float BloomTex7SR <
  372. string UIName="Tex 7: Sample Range";
  373. string UIWidget="Spinner";
  374. float UIMin=0.0;
  375. float UIMax=0.5;
  376. float UIStep=0.001;
  377. > = {0.25};
  378. bool Section_Noise <
  379. string UIName = "------Noise-----------------";
  380. > = {false};
  381. float GrainMotion <
  382. string UIName="Noise Motion";
  383. string UIWidget="Spinner";
  384. float UIMin=0.0;
  385. float UIMax=50.0;
  386. float UIStep=0.001;
  387. > = {0.004};
  388. float GrainSaturation <
  389. string UIName="Noise Saturation";
  390. string UIWidget="Spinner";
  391. float UIMin=0.0;
  392. float UIMax=50.0;
  393. float UIStep=0.001;
  394. > = {1.0};
  395. float GrainIntensityE <
  396. string UIName="Noise Intensity Exterior";
  397. string UIWidget="Spinner";
  398. float UIMin=0.0;
  399. float UIMax=50.0;
  400. float UIStep=0.001;
  401. > = {0.004};
  402. float GrainIntensityI <
  403. string UIName="Noise Intensity Interior";
  404. string UIWidget="Spinner";
  405. float UIMin=0.0;
  406. float UIMax=50.0;
  407. float UIStep=0.001;
  408. > = {0.000};
  409.  
  410. // SOME STANDARD VARIABLES ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  411.  
  412. //float4 tempF1; // Not used //0,1,2,3
  413. //float4 tempF2; // Not used //5,6,7,8
  414. //float4 tempF3; // Not used //9,0
  415. float4 ScreenSize; // x=Width, y=1/Width, z=ScreenScaleY (Width/Height), w=1/ScreenScaleY
  416. float4 Timer; // Used for Noise
  417. float4 TempParameters; // Used to correct DIRECT3D half pixel offset correction in vertex (.xy), Screenfactor per pass 1/TextureSize (.z), Passnumber (.w)
  418. //float4 LenzParameters; // Not used
  419. float4 BloomParameters; // Not used: BloomRadius1 (GTA4) (.x), BloomRadius2(GTA4) (.y) //Used: BloomBlueShiftAmount (.z), BloomContrast (.w)
  420. float ENightDayFactor; // 0=Night, 1=Day
  421. float EInteriorFactor; // 0=Exterior, 1=Interior
  422.  
  423. // TEXTURES AND SAMPLERS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  424.  
  425. texture2D texDepth;
  426. texture2D texBloom1;
  427. texture2D texBloom2;
  428. texture2D texBloom3;
  429. texture2D texBloom4;
  430. texture2D texBloom5;
  431. texture2D texBloom6;
  432. texture2D texBloom7;//additional bloom tex
  433. texture2D texBloom8;//additional bloom tex
  434.  
  435. sampler2D SamplerDepth = sampler_state
  436. {
  437. Texture = <texDepth>;
  438. MinFilter = POINT;
  439. MagFilter = POINT;
  440. MipFilter = NONE;
  441. AddressU = Clamp;
  442. AddressV = Clamp;
  443. SRGBTexture=FALSE;
  444. MaxMipLevel=0;
  445. MipMapLodBias=0;
  446. };
  447.  
  448. sampler2D SamplerBloom1 = sampler_state
  449. {
  450. Texture = <texBloom1>;
  451. MinFilter = LINEAR;
  452. MagFilter = LINEAR;
  453. MipFilter = NONE;//NONE;
  454. AddressU = Clamp;
  455. AddressV = Clamp;
  456. SRGBTexture=FALSE;
  457. MaxMipLevel=0;
  458. MipMapLodBias=0;
  459. };
  460.  
  461. sampler2D SamplerBloom2 = sampler_state
  462. {
  463. Texture = <texBloom2>;
  464. MinFilter = LINEAR;
  465. MagFilter = LINEAR;
  466. MipFilter = NONE;//NONE;
  467. AddressU = Clamp;
  468. AddressV = Clamp;
  469. SRGBTexture=FALSE;
  470. MaxMipLevel=0;
  471. MipMapLodBias=0;
  472. };
  473.  
  474. sampler2D SamplerBloom3 = sampler_state
  475. {
  476. Texture = <texBloom3>;
  477. MinFilter = LINEAR;
  478. MagFilter = LINEAR;
  479. MipFilter = NONE;//NONE;
  480. AddressU = Clamp;
  481. AddressV = Clamp;
  482. SRGBTexture=FALSE;
  483. MaxMipLevel=0;
  484. MipMapLodBias=0;
  485. };
  486.  
  487. sampler2D SamplerBloom4 = sampler_state
  488. {
  489. Texture = <texBloom4>;
  490. MinFilter = LINEAR;
  491. MagFilter = LINEAR;
  492. MipFilter = NONE;//NONE;
  493. AddressU = Clamp;
  494. AddressV = Clamp;
  495. SRGBTexture=FALSE;
  496. MaxMipLevel=0;
  497. MipMapLodBias=0;
  498. };
  499.  
  500. sampler2D SamplerBloom5 = sampler_state
  501. {
  502. Texture = <texBloom5>;
  503. MinFilter = LINEAR;
  504. MagFilter = LINEAR;
  505. MipFilter = NONE;//NONE;
  506. AddressU = Clamp;
  507. AddressV = Clamp;
  508. SRGBTexture=FALSE;
  509. MaxMipLevel=0;
  510. MipMapLodBias=0;
  511. };
  512.  
  513. sampler2D SamplerBloom6 = sampler_state
  514. {
  515. Texture = <texBloom6>;
  516. MinFilter = LINEAR;
  517. MagFilter = LINEAR;
  518. MipFilter = NONE;//NONE;
  519. AddressU = Clamp;
  520. AddressV = Clamp;
  521. SRGBTexture=FALSE;
  522. MaxMipLevel=0;
  523. MipMapLodBias=0;
  524. };
  525.  
  526. sampler2D SamplerBloom7 = sampler_state
  527. {
  528. Texture = <texBloom7>;
  529. MinFilter = LINEAR;
  530. MagFilter = LINEAR;
  531. MipFilter = NONE;//NONE;
  532. AddressU = Clamp;
  533. AddressV = Clamp;
  534. SRGBTexture=FALSE;
  535. MaxMipLevel=0;
  536. MipMapLodBias=0;
  537. };
  538.  
  539. sampler2D SamplerBloom8 = sampler_state
  540. {
  541. Texture = <texBloom8>;
  542. MinFilter = LINEAR;
  543. MagFilter = LINEAR;
  544. MipFilter = NONE;//NONE;
  545. AddressU = Clamp;
  546. AddressV = Clamp;
  547. SRGBTexture=FALSE;
  548. MaxMipLevel=0;
  549. MipMapLodBias=0;
  550. };
  551.  
  552. // STRUCTURES +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  553.  
  554. struct VS_OUTPUT_POST
  555. {
  556. float4 vpos : POSITION;
  557. float2 txcoord0 : TEXCOORD0;
  558. };
  559.  
  560. struct VS_INPUT_POST
  561. {
  562. float3 pos : POSITION;
  563. float2 txcoord0 : TEXCOORD0;
  564. };
  565.  
  566. // VERTEX SHADER ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  567.  
  568. VS_OUTPUT_POST VS_Bloom(VS_INPUT_POST IN)
  569. {
  570. VS_OUTPUT_POST OUT;
  571. OUT.vpos = float4(IN.pos.x, IN.pos.y, IN.pos.z, 1.0);
  572. OUT.txcoord0.xy = IN.txcoord0.xy + TempParameters.xy;
  573.  
  574. return OUT;
  575. };
  576.  
  577. // HELPER FUNCTIONS AND CONSTANTS +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  578.  
  579. //Set loopcount limit - DO NOT CHANGE, EVER.
  580. #define LOOPCOUNT 150
  581.  
  582. // PI, required to calculate weight
  583. #define PI 3.1415926535897932384626433832795
  584.  
  585. float linearDepth(float d, float n, float f)
  586. {
  587. return (2.0 * n)/(f + n - d * (f - n));
  588. }
  589.  
  590. // Pseudo Random Number generator.
  591. float random(in float2 uv)
  592. {
  593. float2 noise = (frac(sin(dot(uv , float2(12.9898,78.233) * 2.0)) * 43758.5453));
  594. return abs(noise.x + noise.y) * 0.5;
  595. }
  596.  
  597. // SHADERS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  598.  
  599. float4 PS_BloomPrePass(VS_OUTPUT_POST IN) : COLOR
  600. {
  601. float4 bloomuv;
  602. float4 bloom = 0.0;
  603.  
  604. float2 invtexSize = TempParameters.z;
  605. if( TempParameters.w == 0 ) invtexSize.y *= ScreenSize.z;
  606. float2 coord = IN.txcoord0.xy;
  607.  
  608. const float2 offset[4]=
  609. {
  610. float2(0.25, 0.25),
  611. float2(0.25, -0.25),
  612. float2(-0.25, 0.25),
  613. float2(-0.25, -0.25)
  614. };
  615. for( int i=0; i<4; i++ )
  616. {
  617. bloomuv.xy = offset[i];
  618. bloomuv.xy = ( bloomuv.xy * invtexSize.xy ) + coord.xy;
  619. bloom += tex2D( SamplerBloom1, bloomuv.xy );
  620. }
  621. bloom.xyz *= 0.25;
  622.  
  623. if( TempParameters.w == 1 )
  624. {
  625. float BinBlack = lerp( lerp( BinBlackN, BinBlackD, ENightDayFactor ), BinBlackI, EInteriorFactor );
  626. float BinGamma = lerp( lerp( BinGammaN, BinGammaD, ENightDayFactor ), BinGammaI, EInteriorFactor );
  627. float BinWhite = lerp( lerp( BinWhiteN, BinWhiteD, ENightDayFactor ), BinWhiteI, EInteriorFactor );
  628. float BoutWhite = lerp( lerp( BoutWhiteN, BoutWhiteD, ENightDayFactor ), BoutWhiteI, EInteriorFactor );
  629. float BoutBlack = lerp( lerp( BoutBlackN, BoutBlackD, ENightDayFactor ), BoutBlackI, EInteriorFactor );
  630. bloom.xyz = pow( max( bloom.xyz - BinBlack , 0.0f ) / max( BinWhite - BinBlack, 0.0001f ), BinGamma ) * max( BoutWhite - BoutBlack, 0.0001f ) + BoutBlack;
  631. }
  632.  
  633. bloom.xyz = min( bloom.xyz, 32768.0 );
  634. bloom.xyz = max( bloom.xyz, 0.0 );
  635. bloom.w = 1;
  636. return bloom;
  637. }
  638.  
  639. float4 PS_BloomTexture1(VS_OUTPUT_POST IN) : COLOR
  640. {
  641. // --------------------------- //
  642. // FIRST GAUSSIAN PASSES //
  643. // BLOOM TINT HAPPENS HERE //
  644. // --------------------------- //
  645.  
  646. float2 coord = IN.txcoord0.xy;
  647.  
  648. float Q = 0.0f;
  649. if ( QUALITYLEVEL == 2 )
  650. Q = 0.6f;
  651. else if ( QUALITYLEVEL == 1 )
  652. Q = 0.8f;
  653. else if ( QUALITYLEVEL == 0 ) //best
  654. Q = 0.985f;
  655.  
  656. float hIntensity = lerp( lerp( hIntensityN, hIntensityD, ENightDayFactor ), hIntensityI, EInteriorFactor );
  657. float BloomSigma = lerp( lerp( BloomSigmaN, BloomSigmaD, ENightDayFactor ), BloomSigmaI, EInteriorFactor );
  658. float minDepth = lerp( minDepthE, minDepthI, EInteriorFactor );
  659. float px = TempParameters.z;
  660. float py = px;
  661. BloomSigma = BloomSigma * (ScreenSize.y/TempParameters.z);
  662.  
  663. // Get Depth info and apply to Sigma
  664. float Depth = tex2D( SamplerDepth, float2( IN.txcoord0.xy )).x;
  665. float SigmaDepth = min( max( linearDepth( Depth, 0.5f, zfar ), minDepth ), 1.0f );
  666. BloomSigma = max( BloomSigma * SigmaDepth, 0.6f ); //limited to not give weird result
  667.  
  668. // Declare all needed variables
  669. float SigmaSum;
  670. float3 Sigma;
  671. float sampleOffset;
  672. float4 bloom;
  673. float4 bloom1;
  674. float4 bloom2;
  675. float4 srcbloom;
  676. float d;
  677. float d1;
  678. float d2;
  679. float2 q;
  680.  
  681. // Get bloom
  682. bloom = tex2D( SamplerBloom1, float2( coord.xy ));
  683. srcbloom = bloom;
  684. d = linearDepth( Depth, 0.5f, 3500.0f );
  685.  
  686. q = float2(1, 0); //horizontal, left switch for future use
  687.  
  688. SigmaSum = 0.0f;
  689. Sigma.x = 1.0f / ( sqrt( 2.0f * PI ) * BloomSigma );
  690. Sigma.y = exp( -0.5f / ( BloomSigma * BloomSigma ));
  691. Sigma.z = Sigma.y * Sigma.y;
  692.  
  693. sampleOffset = 1.0f;
  694. bloom *= Sigma.x;
  695. SigmaSum += Sigma.x;
  696. Sigma.xy *= Sigma.yz;
  697.  
  698. for(int i = 1; i < LOOPCOUNT && SigmaSum < Q; ++i) {
  699.  
  700. bloom1 = tex2Dlod( SamplerBloom1, float4( coord.xy + float2( sampleOffset*px*q.x, sampleOffset*py*q.y ), 0, 0 )) * Sigma.x;
  701. bloom2 = tex2Dlod( SamplerBloom1, float4( coord.xy - float2( sampleOffset*px*q.x, sampleOffset*py*q.y ), 0, 0 )) * Sigma.x;
  702.  
  703. d1 = tex2Dlod(SamplerDepth, float4( IN.txcoord0.xy + float2(sampleOffset*px*q.x, sampleOffset*py*q.y), 0, 0 )).x;
  704. d1 = linearDepth( d1, 0.5f, 3500.0f );
  705. d2 = tex2Dlod(SamplerDepth, float4( IN.txcoord0.xy - float2(sampleOffset*px*q.x, sampleOffset*py*q.y), 0, 0 )).x;
  706. d2 = linearDepth( d2, 0.5f, 3500.0f );
  707.  
  708. bloom += lerp( srcbloom * Sigma.x, bloom1, smoothstep( d-d1, d, d1 ));
  709. bloom += lerp( srcbloom * Sigma.x, bloom2, smoothstep( d-d2, d, d2 ));
  710.  
  711. SigmaSum += ( 2.0f * Sigma.x );
  712. sampleOffset += 1.0f;
  713. Sigma.xy *= Sigma.yz;
  714. }
  715. bloom.xyz /= SigmaSum;
  716. bloom.xyz *= hIntensity;
  717.  
  718. //Bloom Tinting
  719. if (use_tinting==true) {
  720.  
  721. float3 tint_color = lerp( lerp( tint_colorN, tint_colorD, ENightDayFactor ), tint_colorI, EInteriorFactor );
  722. float tint_level = lerp( lerp( tint_levelN, tint_levelD, ENightDayFactor ), tint_levelI, EInteriorFactor );
  723. float tint_mix = lerp( lerp( tint_mixN, tint_mixD, ENightDayFactor ), tint_mixI, EInteriorFactor );
  724.  
  725. float3 tintColor = 0.0f;
  726.  
  727. if (tint_type==1)
  728. tintColor = tint_color * tint_level;
  729. if (tint_type==2)
  730. tintColor = tex2Dlod(SamplerBloom6, float4( coord.xy, 0, 0 )).xyz * tint_level;
  731. if (tint_type==3) {
  732. float3 tcol1 = tint_color;
  733. float3 tcol2 = tex2Dlod(SamplerBloom6, float4( coord.xy, 0, 0 )).xyz;
  734. tintColor = lerp( tcol1.xyz, tcol2.xyz, tint_mix ) * tint_level;
  735. }
  736.  
  737. float ttt = max( dot( bloom.xyz, 0.33333332f ) - dot( srcbloom.xyz, 0.33333332f ), 0.0f );
  738. float gray = BloomParameters.z * ttt * 10.0f;
  739. float mixfact = gray / ( gray + 1.0f );
  740. mixfact *= 1.0f - saturate( ( TempParameters.w - 1.0f ) * 0.2f );
  741. tintColor.xy += 1.0f - saturate( ( TempParameters.w - 1.0f ) * 0.3f );
  742. tintColor.xy = saturate( tintColor.xy );
  743.  
  744. bloom.xyz *= lerp( 1.0f, tintColor.xyz, mixfact );
  745. }
  746. bloom.xyz = saturate( bloom.xyz );
  747. bloom.w = 1.0f;
  748.  
  749. return bloom;
  750. }
  751.  
  752. float4 PS_BloomTexture2(VS_OUTPUT_POST IN) : COLOR
  753. {
  754. // --------------------------- //
  755. // SECOND GAUSSIAN PASSES //
  756. // --------------------------- //
  757.  
  758. float2 coord = IN.txcoord0.xy;
  759.  
  760. float Q = 0.0f;
  761. if ( QUALITYLEVEL == 2 )
  762. Q = 0.6f;
  763. else if ( QUALITYLEVEL == 1 )
  764. Q = 0.8f;
  765. else if ( QUALITYLEVEL == 0 ) //best
  766. Q = 0.985f;
  767.  
  768. float vIntensity = lerp( lerp( vIntensityN, vIntensityD, ENightDayFactor ), vIntensityI, EInteriorFactor );
  769. float BloomSigma = lerp( lerp( BloomSigmaN, BloomSigmaD, ENightDayFactor ), BloomSigmaI, EInteriorFactor );
  770. float minDepth = lerp( minDepthE, minDepthI, EInteriorFactor );
  771. float px = TempParameters.z;
  772. float py = px;
  773. BloomSigma = BloomSigma * (ScreenSize.y/TempParameters.z) * ScreenSize.z;
  774.  
  775. // Get Depth info and apply to Sigma
  776. float Depth = tex2D( SamplerDepth, float2( IN.txcoord0.xy )).x;
  777. float SigmaDepth = min( max( linearDepth( Depth, 0.5f, zfar ), minDepth ), 1.0f );
  778. BloomSigma = max( BloomSigma * SigmaDepth, 0.6f ); //limited to not give weird result
  779.  
  780. // Declare all needed variables
  781. float SigmaSum;
  782. float3 Sigma;
  783. float sampleOffset;
  784. float4 bloom;
  785. float4 bloom1;
  786. float4 bloom2;
  787. float4 srcbloom;
  788. float d;
  789. float d1;
  790. float d2;
  791. float2 q;
  792.  
  793. // Get bloom
  794. bloom = tex2D( SamplerBloom1, float2( coord.xy ));
  795. srcbloom = bloom;
  796. d = linearDepth( Depth, 0.5f, 3500.0f );
  797.  
  798. q = float2(0, 1); //vertical, left switch for future use
  799.  
  800. SigmaSum = 0.0f;
  801. Sigma.x = 1.0f / ( sqrt( 2.0f * PI ) * BloomSigma );
  802. Sigma.y = exp( -0.5f / ( BloomSigma * BloomSigma ));
  803. Sigma.z = Sigma.y * Sigma.y;
  804.  
  805. sampleOffset = 1.0f;
  806. bloom *= Sigma.x;
  807. SigmaSum += Sigma.x;
  808. Sigma.xy *= Sigma.yz;
  809.  
  810. for(int i = 1; i < LOOPCOUNT && SigmaSum < Q; ++i) {
  811.  
  812. bloom1 = tex2Dlod( SamplerBloom1, float4( coord.xy + float2( sampleOffset*px*q.x, sampleOffset*py*q.y ), 0, 0 )) * Sigma.x;
  813. bloom2 = tex2Dlod( SamplerBloom1, float4( coord.xy - float2( sampleOffset*px*q.x, sampleOffset*py*q.y ), 0, 0 )) * Sigma.x;
  814.  
  815. d1 = tex2Dlod(SamplerDepth, float4( IN.txcoord0.xy + float2(sampleOffset*px*q.x, sampleOffset*py*q.y), 0, 0 )).x;
  816. d1 = linearDepth( d1, 0.5f, 3500.0f );
  817. d2 = tex2Dlod(SamplerDepth, float4( IN.txcoord0.xy - float2(sampleOffset*px*q.x, sampleOffset*py*q.y), 0, 0 )).x;
  818. d2 = linearDepth( d2, 0.5f, 3500.0f );
  819.  
  820. bloom += lerp( srcbloom * Sigma.x, bloom1, smoothstep( d-d1, d, d1 ));
  821. bloom += lerp( srcbloom * Sigma.x, bloom2, smoothstep( d-d2, d, d2 ));
  822.  
  823. SigmaSum += ( 2.0f * Sigma.x );
  824. sampleOffset += 1.0f;
  825. Sigma.xy *= Sigma.yz;
  826. }
  827. bloom.xyz /= SigmaSum;
  828. bloom.xyz *= vIntensity;
  829. bloom.xyz = saturate( bloom.xyz );
  830. bloom.w = 1.0f;
  831.  
  832. return bloom;
  833. }
  834.  
  835. float4 PS_BloomPostPass(VS_OUTPUT_POST IN) : COLOR
  836. {
  837. // --------------------------- //
  838. // POSTPASS TO COMBINE TEX //
  839. // --------------------------- //
  840.  
  841. // NOTES [prod80]
  842. // Filter, Mix and Match some textures
  843.  
  844. float4 bloom = 0;
  845. float4 temp = 0;
  846. float2 invtexSize = 0.001953125; //(1/512)
  847. float2 bcoord = 0;
  848. float2 steps = 0;
  849. bcoord = IN.txcoord0.xy * 512;
  850. bcoord = floor( bcoord.xy ) + 0.5;
  851. bcoord *= invtexSize.xy; //snap to texel center on 512px resolution
  852.  
  853. //Bloom Sampler 5 - 512x512 texture, result from prepass blur
  854. steps = 1; //(512/512)
  855. steps *= invtexSize.xy;
  856. temp = tex2D( SamplerBloom5, float2( -BloomTex1SR, BloomTex1SR ) * steps.xy + bcoord.xy );
  857. temp += tex2D( SamplerBloom5, float2( -BloomTex1SR, -BloomTex1SR ) * steps.xy + bcoord.xy );
  858. temp += tex2D( SamplerBloom5, float2( BloomTex1SR, -BloomTex1SR ) * steps.xy + bcoord.xy );
  859. temp += tex2D( SamplerBloom5, float2( BloomTex1SR, BloomTex1SR ) * steps.xy + bcoord.xy );
  860. bloom += 0.25 * temp * tex_select1;
  861.  
  862. //Bloom Sampler 1 - 256x256 texture
  863. steps = 2; //(512/256)
  864. steps *= invtexSize.xy;
  865. temp = tex2D( SamplerBloom1, float2( -BloomTex2SR, BloomTex2SR ) * steps.xy + bcoord.xy );
  866. temp += tex2D( SamplerBloom1, float2( -BloomTex2SR, -BloomTex2SR ) * steps.xy + bcoord.xy );
  867. temp += tex2D( SamplerBloom1, float2( BloomTex2SR, -BloomTex2SR ) * steps.xy + bcoord.xy );
  868. temp += tex2D( SamplerBloom1, float2( BloomTex2SR, BloomTex2SR ) * steps.xy + bcoord.xy );
  869. bloom += 0.25 * temp * tex_select2;
  870.  
  871. //Bloom Sampler 2 - 128x128 texture
  872. steps = 4; //(512/128)
  873. steps *= invtexSize.xy;
  874. temp = tex2D( SamplerBloom2, float2( -BloomTex3SR, BloomTex3SR ) * steps.xy + bcoord.xy );
  875. temp += tex2D( SamplerBloom2, float2( -BloomTex3SR, -BloomTex3SR ) * steps.xy + bcoord.xy );
  876. temp += tex2D( SamplerBloom2, float2( BloomTex3SR, -BloomTex3SR ) * steps.xy + bcoord.xy );
  877. temp += tex2D( SamplerBloom2, float2( BloomTex3SR, BloomTex3SR ) * steps.xy + bcoord.xy );
  878. bloom += 0.25 * temp * tex_select3;
  879.  
  880. //Bloom Sampler 3 - 64x64 texture
  881. steps = 8; //(512/64)
  882. steps *= invtexSize.xy;
  883. temp = tex2D( SamplerBloom3, float2( -BloomTex4SR, BloomTex4SR ) * steps.xy + bcoord.xy );
  884. temp += tex2D( SamplerBloom3, float2( -BloomTex4SR, -BloomTex4SR ) * steps.xy + bcoord.xy );
  885. temp += tex2D( SamplerBloom3, float2( BloomTex4SR, -BloomTex4SR ) * steps.xy + bcoord.xy );
  886. temp += tex2D( SamplerBloom3, float2( BloomTex4SR, BloomTex4SR ) * steps.xy + bcoord.xy );
  887. bloom += 0.25 * temp * tex_select4;
  888.  
  889. //Bloom Sampler 4 - 32x32 texture
  890. steps = 16; //(512/32)
  891. steps *= invtexSize.xy;
  892. temp = tex2D( SamplerBloom4, float2( -BloomTex5SR, BloomTex5SR ) * steps.xy + bcoord.xy );
  893. temp += tex2D( SamplerBloom4, float2( -BloomTex5SR, -BloomTex5SR ) * steps.xy + bcoord.xy );
  894. temp += tex2D( SamplerBloom4, float2( BloomTex5SR, -BloomTex5SR ) * steps.xy + bcoord.xy );
  895. temp += tex2D( SamplerBloom4, float2( BloomTex5SR, BloomTex5SR ) * steps.xy + bcoord.xy );
  896. bloom += 0.25 * temp * tex_select5;
  897.  
  898. //Not using Sampler 6 (Original input)
  899.  
  900. //Bloom Sampler 7 - 16x16 texture
  901. steps = 32; //(512/16)
  902. steps *= invtexSize.xy;
  903. temp = tex2D( SamplerBloom7, float2( -BloomTex6SR, BloomTex6SR ) * steps.xy + bcoord.xy );
  904. temp += tex2D( SamplerBloom7, float2( -BloomTex6SR, -BloomTex6SR ) * steps.xy + bcoord.xy );
  905. temp += tex2D( SamplerBloom7, float2( BloomTex6SR, -BloomTex6SR ) * steps.xy + bcoord.xy );
  906. temp += tex2D( SamplerBloom7, float2( BloomTex6SR, BloomTex6SR ) * steps.xy + bcoord.xy );
  907. bloom += 0.25 * temp * tex_select6;
  908.  
  909. //Bloom Sampler 8 - 8x8 texture
  910. steps = 64; //(512/8)
  911. steps *= invtexSize.xy;
  912. temp = tex2D( SamplerBloom8, float2( -BloomTex7SR, BloomTex7SR ) * steps.xy + bcoord.xy );
  913. temp += tex2D( SamplerBloom8, float2( -BloomTex7SR, -BloomTex7SR ) * steps.xy + bcoord.xy );
  914. temp += tex2D( SamplerBloom8, float2( BloomTex7SR, -BloomTex7SR ) * steps.xy + bcoord.xy );
  915. temp += tex2D( SamplerBloom8, float2( BloomTex7SR, BloomTex7SR ) * steps.xy + bcoord.xy );
  916. bloom += 0.25 * temp * tex_select7;
  917.  
  918. //Final result
  919. bloom /= ( tex_select1 + tex_select2 + tex_select3 + tex_select4 + tex_select5 + tex_select6 + tex_select7 );
  920.  
  921. //Saturation
  922. float bloomsat = lerp( lerp( bsatN, bsatD, ENightDayFactor ), bsatI, EInteriorFactor );
  923. float4 saturation = 0;
  924. saturation.xy = float2( min( min( bloom.x, bloom.y ), bloom.z ), max( max( bloom.x, bloom.y ), bloom.z ));
  925. saturation.z = saturation.y - saturation.x;
  926. saturation.w = dot( bloom.xyz, float3( 0.2125, 0.7154, 0.0721 ));
  927. float3 min_sat = lerp( saturation.w, bloom.xyz, bloomsat );
  928. //Below math may produce negatives when color close to pure, ie 0.75, 0.01, 0.025 or 0.95, 0.75, 0.03 Compensating by giving correct return to add negative(s) to positive(s)
  929. float3 max_sat = lerp( saturation.w, bloom.xyz, 1 + ( bloomsat - 1 ) * ( 1 - saturation.z ));
  930. float3 neg = min( max_sat.xyz + 1, 1 );
  931. neg.xyz = saturate( 1 - neg.xyz );
  932. float negsum = dot( neg.xyz, 1 );
  933. max_sat.xyz = max( max_sat.xyz, 0 );
  934. max_sat.xyz = max_sat.xyz + saturate( sign( max_sat.xyz )) * negsum.xxx;
  935. bloom.xyz = lerp( min_sat.xyz, max_sat.xyz, step( 1, bloomsat ));
  936.  
  937. //Adding noice to fight off banding
  938. float GrainTimerSeed = Timer.x * GrainMotion;
  939. float2 GrainTexCoordSeed = IN.txcoord0.xy * 1.0;
  940.  
  941. //Generate grain seeds
  942. float2 GrainSeed1 = GrainTexCoordSeed + float2( 0.0, GrainTimerSeed );
  943. float2 GrainSeed2 = GrainTexCoordSeed + float2( GrainTimerSeed, 0.0 );
  944. float2 GrainSeed3 = GrainTexCoordSeed + float2( GrainTimerSeed, GrainTimerSeed );
  945.  
  946. //Generate pseudo random noise
  947. float GrainNoise1 = random( GrainSeed1 );
  948. float GrainNoise2 = random( GrainSeed2 );
  949. float GrainNoise3 = random( GrainSeed3 );
  950. float GrainNoise4 = ( GrainNoise1 + GrainNoise2 + GrainNoise3 ) * 0.333333333;
  951.  
  952. //Combine results
  953. float GrainIntensity= lerp( GrainIntensityE, GrainIntensityI, EInteriorFactor );
  954. float3 GrainNoise = float3( GrainNoise4, GrainNoise4, GrainNoise4 );
  955. float3 GrainColor = float3( GrainNoise1, GrainNoise2, GrainNoise3 );
  956. float3 Noise = ( lerp( GrainNoise, GrainColor, GrainSaturation ) * GrainIntensity ) - ( GrainIntensity * 0.5);
  957.  
  958. //Add noise to color
  959. bloom.xyz += Noise.xyz;
  960.  
  961. bloom.xyz = saturate( bloom.xyz );
  962. bloom.w = 1.0f;
  963.  
  964. //Final output of shader is 512x512 texture
  965. return bloom;
  966. }
  967.  
  968. // TECHNIQUES +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  969.  
  970. technique BloomPrePass
  971. {
  972. pass p0
  973. {
  974. VertexShader = compile vs_3_0 VS_Bloom();
  975. PixelShader = compile ps_3_0 PS_BloomPrePass();
  976.  
  977. ColorWriteEnable = ALPHA|RED|GREEN|BLUE;
  978. CullMode = NONE;
  979. AlphaBlendEnable = FALSE;
  980. AlphaTestEnable = FALSE;
  981. SEPARATEALPHABLENDENABLE = FALSE;
  982. FogEnable = FALSE;
  983. SRGBWRITEENABLE = FALSE;
  984. }
  985. }
  986.  
  987. technique BloomTexture1
  988. {
  989. pass p0
  990. {
  991. VertexShader = compile vs_3_0 VS_Bloom();
  992. PixelShader = compile ps_3_0 PS_BloomTexture1();
  993.  
  994. ColorWriteEnable = ALPHA|RED|GREEN|BLUE;
  995. CullMode = NONE;
  996. AlphaBlendEnable = FALSE;
  997. AlphaTestEnable = FALSE;
  998. SEPARATEALPHABLENDENABLE = FALSE;
  999. FogEnable = FALSE;
  1000. SRGBWRITEENABLE = FALSE;
  1001. }
  1002. }
  1003.  
  1004. technique BloomTexture2
  1005. {
  1006. pass p0
  1007. {
  1008. VertexShader = compile vs_3_0 VS_Bloom();
  1009. PixelShader = compile ps_3_0 PS_BloomTexture2();
  1010.  
  1011. ColorWriteEnable = ALPHA|RED|GREEN|BLUE;
  1012. CullMode = NONE;
  1013. AlphaBlendEnable = FALSE;
  1014. AlphaTestEnable = FALSE;
  1015. SEPARATEALPHABLENDENABLE = FALSE;
  1016. FogEnable = FALSE;
  1017. SRGBWRITEENABLE = FALSE;
  1018. }
  1019. }
  1020.  
  1021. technique BloomPostPass
  1022. {
  1023. pass p0
  1024. {
  1025. VertexShader = compile vs_3_0 VS_Bloom();
  1026. PixelShader = compile ps_3_0 PS_BloomPostPass();
  1027.  
  1028. ColorWriteEnable = ALPHA|RED|GREEN|BLUE;
  1029. CullMode = NONE;
  1030. AlphaBlendEnable = FALSE;
  1031. AlphaTestEnable = FALSE;
  1032. SEPARATEALPHABLENDENABLE = FALSE;
  1033. FogEnable = FALSE;
  1034. SRGBWRITEENABLE = FALSE;
  1035. }
  1036. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement