1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
use super::{ProgramSettings, GLSL_VERSION, default_settings, fbo};
use rin_gl::{self as gl, uniforms, Renderer3d, fbo::TextureLevelRef};
use rin_util::{Result, Error, LogErr};
use rin_math::{vec2, vec4, Angle};
#[cfg(not(feature = "ssao_alchemy"))]
use rin_math::{vec3, Pnt3, lerp, ToPnt, };
use std::rc::Rc;
use std::borrow::Borrow;
use std::cell::{Cell, UnsafeCell};
use rin_graphics::{CameraExt, Vertex2DTex};
use rin_events::{RangedPropertyMut, Property, ParamsDefault};
#[cfg(gui)]
use rin_gui::ParameterGroup;
use color::consts::BLACK;
#[cfg(any(feature = "ssao_alchemy", feature= "ssao_blur_gaussian"))]
use color::consts::WHITE;
#[cfg(not(feature = "ssao_blur_gaussian"))]
use color::{Rgb, rgb, };
#[cfg(not(feature = "ssao_alchemy"))]
use rand::Rng;
#[cfg(glsl_debug)]
use rin_util::AutoLoader;

pub struct Ssao {
    fbo_depth_mipmaps: Vec<gl::Fbo<gl::fbo::TextureLevelRef<Rc<gl::Texture>>>>,
    linear_depth_mipmaps: Rc<gl::Texture>,
    fbo_ssao: gl::Fbo,
    #[cfg(not(feature = "ssao_blur_gaussian"))]
    fbo_ssao_blur: gl::Fbo,
    #[cfg(feature = "ssao_blur_gaussian")]
    fbo_ssao_blur: [gl::Fbo;2],
    fbo_ssao_color: Rc<gl::Fbo>,

    #[cfg(not(feature = "ssao_blur_gaussian"))]
    ssao_noise: UnsafeCell<gl::Texture>,

    #[cfg(feature = "ssao_alchemy")]
    start_depth_minify: UnsafeCell<gl::Program>,
    #[cfg(feature = "ssao_alchemy")]
    other_depth_minify: UnsafeCell<gl::Program>,
    ssao: UnsafeCell<gl::Program>,
    ssao_blur: UnsafeCell<gl::Program>,
    ssao_apply: UnsafeCell<gl::Program>,
    combine_color_ambient: UnsafeCell<gl::Program>,

    #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
    start_depth_minify_loader: UnsafeCell<AutoLoader<gl::Program>>,
    #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
    other_depth_minify_loader: UnsafeCell<AutoLoader<gl::Program>>,
    #[cfg(glsl_debug)]
    ssao_loader: UnsafeCell<AutoLoader<gl::Program>>,
    #[cfg(glsl_debug)]
    ssao_blur_loader: UnsafeCell<AutoLoader<gl::Program>>,
    #[cfg(glsl_debug)]
    ssao_apply_loader: UnsafeCell<AutoLoader<gl::Program>>,
    #[cfg(glsl_debug)]
    combine_color_ambient_loader: UnsafeCell<AutoLoader<gl::Program>>,

    ssao_kernel_samples: Cell<u32>,
    ssao_blur_radius: Cell<u32>,
    ssao_blur_step_size: Cell<u32>,
    ssao_depth_samples: Cell<u32>,
    ssao_high_quality_blur: Cell<bool>,
    ssao_edge_sharpness: Cell<f32>,
    ssao_linear_depth: Cell<bool>,
    ssao_normals_from_position: Cell<bool>,
    ssao_far_at_inifinity: Cell<bool>,
    ssao_orthographic_camera: Cell<bool>,
    ssao_reversed_z: Cell<bool>,
    dithering: Cell<bool>,

    fullscreen_quad: Rc<gl::SimpleVao<Vertex2DTex>>,
}

#[derive(Clone, Debug, ParamsDefault)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(gui, derive(ParameterGroup))]
pub struct SSAOParameters{
    #[param(default = 0.75, range = 0.01 .. 10.0)]
    pub radius: RangedPropertyMut<'static,f32>,

    #[param(default = 0.02, range_log = 0.001 .. 1.0)]
    pub bias: RangedPropertyMut<'static,f32>,

    #[param(default = 1., range = 0.1 .. 10.0)]
    pub power: RangedPropertyMut<'static,f32>,

    #[cfg(not(feature = "ssao_alchemy"))]
    #[param(default = 32, range = 4 .. 256)]
    pub kernel_samples: RangedPropertyMut<'static,u32>,

    #[cfg(feature = "ssao_alchemy")]
    #[param(default = 8, range = 1 .. 256)]
    pub kernel_samples: RangedPropertyMut<'static,u32>,

    #[param(default = 2, range = 0 .. 6)]
    pub blur_radius: RangedPropertyMut<'static,u32>,

    #[param(default = 1, range = 1 .. 4)]
    pub blur_step_size: RangedPropertyMut<'static,u32>,

    #[param(default = 1., range = 0. .. 16.)]
    pub edge_sharpness: RangedPropertyMut<'static,f32>,

    #[param(default = true)]
    pub high_quality_blur: Property<'static,bool>,

    #[param(default = false)]
    #[cfg_attr(gui, gui(not_parameter))]
    pub dithering: Property<'static,bool>,
}

#[derive(Clone, Copy)]
pub enum SSAOPosition<'a>{
    FromDepth(&'a gl::Texture),
    FromLinearDepth(&'a gl::Texture, &'a gl::Texture),
    Position(&'a gl::Texture, &'a gl::Texture),
}

#[cfg(not(feature = "ssao_alchemy"))]
fn ssao_kernel(len: u32) -> impl Iterator<Item=Pnt3>{
    let mut rng = rand::thread_rng();
    (0..len).map(move |i| {
        let sample = vec3(rng.gen_range(-1., 1.), rng.gen_range(-1., 1.), rng.gen_range(0., 1.))
            .normalize()
            .to_pnt() * rng.gen_range(0., 1.);

        let pct = i as f32 / len as f32;
        let scale = lerp(0.1, 1., pct * pct);
        sample * scale
    })
}

#[cfg(not(feature = "ssao_alchemy"))]
fn ssao_kernel_str(kernel_size: u32) -> String{
    let kernel = ssao_kernel(kernel_size)
        .map(|p| format!("vec3({},{},{}),\n", p.x, p.y, p.z))
        .collect::<String>();

    format!("vec3[](
        {}
    )", &kernel[..kernel.len() - 2])
}

#[cfg(not(feature = "ssao_blur_gaussian"))]
fn ssao_noise_texture(size: u32) -> impl Iterator<Item = Rgb<f32>>{
    let mut rng = rand::thread_rng();
    (0 .. size * size).map(move |_|{
        rgb!(rng.gen_range(-1., 1.), rng.gen_range(-1., 1.), 0.)
    })
}

#[cfg(not(feature = "ssao_alchemy"))]
fn ssao_noise_str(noise_size: u32) -> String{
    let kernel = ssao_noise_texture(noise_size)
        .map(|p| format!("vec3({},{},{}),\n", p.r, p.g, p.b))
        .collect::<String>();

    format!("vec3[](
        {}
    );", &kernel[..kernel.len() - 2])
}

#[cfg(not(feature = "ssao_blur_gaussian"))]
fn ssao_noise(noise_radius: u32, gl: &gl::Renderer) -> gl::Result<gl::Texture>{
    let noise_size = noise_radius * 2;
    let ssao_noise_data = ssao_noise_texture(noise_size).collect::<Vec<Rgb<f32>>>();
    let mut ssao_noise = gl.new_texture().from_data_format(gl::texture::LoadDataFormat{
        format: gl::RGB,
        ty: gl::FLOAT,
        texture_format: gl::texture::Format::Texture2D {
            internal_format: gl::RGB32F,
            width: noise_size,
            height: noise_size,
            levels: 1,
        },
    }, &ssao_noise_data);

    if let Ok(ssao_noise) = ssao_noise.as_mut() {
        ssao_noise.set_wrap_r(gl::REPEAT);
        ssao_noise.set_wrap_s(gl::REPEAT);
        ssao_noise.set_min_mag_filters(gl::NEAREST, gl::NEAREST);
    }

    ssao_noise
}

#[cfg(not(feature = "ssao_alchemy"))]
fn ssao_settings(
    kernel_size: u32,
    noise_radius: u32,
    w: u32, h: u32,
    depth_samples: u32,
    has_position: bool,
    has_normals: bool,
    has_linear_depth: bool) -> ProgramSettings
{
    let noise_size = noise_radius * 2;
    let kernel = ssao_kernel_str(kernel_size);


    let shader_replace = vec![
        (gl::FRAGMENT_SHADER, ("${kernel}".to_owned(), kernel)),
        (gl::FRAGMENT_SHADER, ("${kernel_size}".to_owned(), kernel_size.to_string())),
        (gl::FRAGMENT_SHADER, ("${noise_size}".to_owned(), format!("{:.1}", noise_size as f32))),
        (gl::FRAGMENT_SHADER, ("${view_width}".to_owned(), format!("{:.1}", w as f32))),
        (gl::FRAGMENT_SHADER, ("${view_height}".to_owned(), format!("{:.1}", h as f32))),
        (gl::FRAGMENT_SHADER, ("${depth_samples}".to_owned(), depth_samples.to_string())),
    ];

    let fragment = shader!("shaders/ssao.fs.glsl");

    #[cfg(not(glsl_debug))]
    let fragment = shader_replace.into_iter()
        .fold(fragment, |shader_src, (_, (from, to))| {
            shader_src.replace(&from, &to)
        });

    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("POSITION_FROM_DEPTH".to_owned(), (!has_position as u8).to_string()),
            ("NORMALS_FROM_POSITION".to_owned(), (!has_normals as u8).to_string()),
            ("LINEAR_DEPTH".to_owned(), (has_linear_depth as u8).to_string()),
            ("MSAA_DEPTH".to_owned(), ((depth_samples != 0) as u8).to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/ssao.vs.glsl")),
            (gl::FRAGMENT_SHADER, fragment),
        ],
        #[cfg(glsl_debug)]
        shader_replace,
        .. default_settings()
    }
}

#[cfg(feature = "ssao_alchemy")]
fn alchemy_ssao_settings(
    w: u32, h: u32,
    kernel_samples: u32,
    depth_samples: u32,
    has_position: bool,
    has_normals: bool,
    has_linear_depth: bool) -> ProgramSettings
{
    let shader_replace = vec![
        (gl::FRAGMENT_SHADER, ("${view_width}".to_owned(), format!("{:.1}", w as f32))),
        (gl::FRAGMENT_SHADER, ("${view_height}".to_owned(), format!("{:.1}", h as f32))),
        (gl::FRAGMENT_SHADER, ("${depth_samples}".to_owned(), depth_samples.to_string())),
    ];

    let fragment = shader!("shaders/ssao_alchemy.fs.glsl");

    #[cfg(not(glsl_debug))]
    let fragment = shader_replace.into_iter()
        .fold(fragment, |shader_src, (_, (from, to))| {
            shader_src.replace(&from, &to)
        });

    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("POSITION_FROM_DEPTH".to_owned(), (!has_position as u8).to_string()),
            ("NORMALS_FROM_POSITION".to_owned(), (!has_normals as u8).to_string()),
            ("LINEAR_DEPTH".to_owned(), (has_linear_depth as u8).to_string()),
            ("MSAA_DEPTH".to_owned(), ((depth_samples != 0) as u8).to_string()),
            ("NUM_SAMPLES".to_owned(), kernel_samples.to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/ssao.vs.glsl")),
            (gl::FRAGMENT_SHADER, fragment),
        ],
        #[cfg(glsl_debug)]
        shader_replace,
        .. default_settings()
    }
}

#[cfg(not(feature = "ssao_blur_gaussian"))]
fn ssao_blur_settings(noise_radius: u32) -> ProgramSettings{
    let noise_size = noise_radius * 2;
    let shader_replace = vec![
        (gl::FRAGMENT_SHADER, ("${half_kernel}".to_owned(), noise_radius.to_string())),
        (gl::FRAGMENT_SHADER, ("${kernel_size}".to_owned(), format!("{:.1}", noise_size as f32))),
    ];
    let fragment = shader!("shaders/ssao_blur_box.fs.glsl");

    #[cfg(not(glsl_debug))]
    let fragment = shader_replace.into_iter()
        .fold(fragment, |shader_src, (_, (from, to))| {
            shader_src.replace(&from, &to)
        });

    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, fragment),
        ],
        #[cfg(glsl_debug)]
        shader_replace,
        .. default_settings()
    }
}

#[cfg(feature = "ssao_blur_gaussian")]
fn ssao_blur_gaussian_settings(
    w: u32, h: u32,
    has_linear_depth: bool,
    has_normals: bool,
    noise_radius: u32,
    scale: u32,
    high_quality: bool,
    edge_sharpness: f32) -> ProgramSettings
{
    let shader_replace = vec![
        (gl::FRAGMENT_SHADER, ("${view_width}".to_owned(), format!("{:.1}", w as f32))),
        (gl::FRAGMENT_SHADER, ("${view_height}".to_owned(), format!("{:.1}", h as f32))),
    ];
    let fragment = shader!("shaders/ssao_blur_gaussian.fs.glsl");

    #[cfg(not(glsl_debug))]
    let fragment = shader_replace.into_iter()
        .fold(fragment, |shader_src, (_, (from, to))| {
            shader_src.replace(&from, &to)
        });

    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("LINEAR_DEPTH".to_owned(), (has_linear_depth as u8).to_string()),
            ("R".to_owned(), noise_radius.to_string()),
            ("SCALE".to_owned(), scale.to_string()),
            ("HIGH_QUALITY".to_owned(), (high_quality as u8).to_string()),
            ("EDGE_SHARPNESS".to_owned(), edge_sharpness.to_string()),
            ("NORMALS_BUFFER".to_owned(), (has_normals as u8).to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, fragment),
        ],
        #[cfg(glsl_debug)]
        shader_replace,
        .. default_settings()
    }
}

#[cfg(feature = "ssao_alchemy")]
fn ssao_depth_minify_settings(
    has_linear_depth: bool,
    start: bool,
    orthographic_camera: bool,
    far_at_inifinity: bool,
    reversed_z: bool) -> ProgramSettings
{
    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("LINEAR_DEPTH".to_owned(), (has_linear_depth as u8).to_string()),
            ("PHASE".to_owned(), "RECONSTRUCT_Z".to_string()),
            ("ORTHOGRAPHIC_CAMERA".to_owned(), (orthographic_camera as u8).to_string()),
            ("FAR_AT_INFINITY".to_owned(), (far_at_inifinity as u8).to_string()),
            ("REVERSED_Z".to_owned(), (reversed_z as u8).to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/ssao_depth_minify.fs.glsl")),
        ],
        .. default_settings()
    }
}

fn ssao_apply_settings(dithering: bool) -> ProgramSettings{
    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("USE_DITHERING".to_owned(), (dithering as u8).to_string()),
            ("HAS_SSAO".to_owned(), "1".to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/apply_ssao.fs.glsl")),
        ],
        .. default_settings()
    }
}

fn combine_color_ambient_settings(dithering: bool) -> ProgramSettings{
    ProgramSettings{
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("USE_DITHERING".to_owned(), (dithering as u8).to_string()),
            ("HAS_SSAO".to_owned(), "0".to_string()),
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/apply_ssao.fs.glsl")),
        ],
        .. default_settings()
    }
}

impl Ssao {
    pub fn new(gl: &gl::Renderer, w: u32, h: u32, format: gl::fbo::ColorFormat, fullscreen_quad: Rc<gl::SimpleVao<Vertex2DTex>>) -> Result<Ssao> {
        let ssao_format = gl::fbo::ColorFormat::RG16F;
        // let ssao_format = gl::fbo::ColorFormat::R8;

        let mut fbo_ssao = fbo(gl, w, h, ssao_format)
            .map_err(|err| Error::with_cause("Couldn't create ssao fbo", err))?;
        fbo_ssao.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        #[cfg(renderer_ty="gl")]
        fbo_ssao.color_tex_mut(0).unwrap().set_anisotropy_level(1.);

        #[cfg(not(feature = "ssao_blur_gaussian"))]
        let mut fbo_ssao_blur = fbo(gl, w, h, gl::fbo::ColorFormat::R8)
            .map_err(|err| Error::with_cause("Couldn't create ssao blur fbo", err))?;

        #[cfg(feature = "ssao_blur_gaussian")]
        let mut fbo_ssao_blur = [
            fbo(gl, w, h, ssao_format)
                .map_err(|err| Error::with_cause("Couldn't create ssao blur fbo", err))?,
            fbo(gl, w, h, ssao_format)
                .map_err(|err| Error::with_cause("Couldn't create ssao blur fbo", err))?,
        ];
        #[cfg(not(feature = "ssao_blur_gaussian"))]
        fbo_ssao_blur.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);

        #[cfg(feature = "ssao_blur_gaussian")]
        {
            fbo_ssao_blur[0].color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
            #[cfg(renderer_ty="gl")]
            fbo_ssao_blur[0].color_tex_mut(0).unwrap().set_anisotropy_level(1.);
            fbo_ssao_blur[1].color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
            #[cfg(renderer_ty="gl")]
            fbo_ssao_blur[1].color_tex_mut(0).unwrap().set_anisotropy_level(1.);
        }


        // #[cfg(renderer_ty="gl")]
        // {
        //     fbo_ssao.color_tex_mut(0).unwrap().set_swizzles(gl::texture::Swizzles::r_to_rgb());
        //     #[cfg(not(feature = "ssao_blur_gaussian"))]
        //     fbo_ssao_blur.color_tex_mut(0).unwrap().set_swizzles(gl::texture::Swizzles::r_to_rgb());
        //     #[cfg(feature = "ssao_blur_gaussian")]
        //     fbo_ssao_blur[0].color_tex_mut(0).unwrap().set_swizzles(gl::texture::Swizzles::r_to_rgb());
        //     #[cfg(feature = "ssao_blur_gaussian")]
        //     fbo_ssao_blur[1].color_tex_mut(0).unwrap().set_swizzles(gl::texture::Swizzles::r_to_rgb());
        // }

        let fbo_ssao_color = fbo(gl, w, h, format)
            .map_err(|err| Error::with_cause("Couldn't create ssao color fbo", err))?;


        let linear_depth_format = gl::texture::Format::Texture2D{
            internal_format: gl::R32F,
            width: w,
            height: h,
            levels: 4,
        };
        let mut linear_depth_mipmaps = gl.new_texture().from_format(linear_depth_format).unwrap();
        linear_depth_mipmaps.set_min_mag_filters(gl::NEAREST_MIPMAP_NEAREST, gl::NEAREST);
        linear_depth_mipmaps.set_base_level(0);
        linear_depth_mipmaps.set_max_level(3);
        let linear_depth_mipmaps = Rc::new(linear_depth_mipmaps);
        let fbo_depth_mipmaps = (0..4).map(|level| {
            let attachment = TextureLevelRef::new(linear_depth_mipmaps.clone(), level);
            gl.new_fbo().from_color_no_depth(attachment)
        }).collect::<gl::Result<Vec<_>>>()
            .map_err(|err| Error::with_cause("Couldn't create ssao depth mips fbos", err))?;

        let ssao_depth_samples = 0;
        let ssao_has_position = false;
        let ssao_has_normals = false;
        let ssao_linear_depth = false;
        let ssao_kernel_samples = 8;
        let ssao_blur_radius = 2;
        let ssao_blur_step_size = 1;
        let ssao_high_quality_blur = true;
        let ssao_edge_sharpness = 1.;
        #[cfg(not(feature = "ssao_alchemy"))]
        let ssao_settings = ssao_settings(
            ssao_kernel_samples,
            ssao_blur_radius,
            w, h,
            ssao_depth_samples,
            ssao_has_position,
            ssao_has_normals,
            ssao_linear_depth);
        #[cfg(feature = "ssao_alchemy")]
        let ssao_settings = alchemy_ssao_settings(
            w, h,
            ssao_kernel_samples,
            ssao_depth_samples,
            ssao_has_position,
            ssao_has_normals,
            ssao_linear_depth);

        #[cfg(feature = "ssao_alchemy")]
        let start_depth_minify_settings = ssao_depth_minify_settings(
            ssao_linear_depth,
            true,
            false,
            false,
            false
        );
        #[cfg(feature = "ssao_alchemy")]
        let other_depth_minify_settings = ssao_depth_minify_settings(
            ssao_linear_depth,
            false,
            false,
            false,
            false
        );
        #[cfg(not(feature = "ssao_blur_gaussian"))]
        let ssao_blur_settings = ssao_blur_settings(ssao_blur_radius);
        #[cfg(feature = "ssao_blur_gaussian")]
        let ssao_blur_settings = ssao_blur_gaussian_settings(
            w, h,
            ssao_linear_depth,
            ssao_has_normals,
            ssao_blur_radius,
            ssao_blur_step_size,
            ssao_high_quality_blur,
            ssao_edge_sharpness,
        );
        let ssao_apply_settings = ssao_apply_settings(false);
        let combine_color_ambient_settings = combine_color_ambient_settings(false);


        #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
        let mut start_depth_minify_loader = gl.new_auto_program(start_depth_minify_settings);
        #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
        let mut other_depth_minify_loader = gl.new_auto_program(other_depth_minify_settings);
        #[cfg(glsl_debug)]
        let mut ssao_loader = gl.new_auto_program(ssao_settings);
        #[cfg(glsl_debug)]
        let mut ssao_blur_loader = gl.new_auto_program(ssao_blur_settings);
        #[cfg(glsl_debug)]
        let mut ssao_apply_loader = gl.new_auto_program(ssao_apply_settings);
        #[cfg(glsl_debug)]
        let mut combine_color_ambient_loader = gl.new_auto_program(combine_color_ambient_settings);

        #[cfg(feature = "ssao_alchemy")]
        let start_depth_minify;
        #[cfg(feature = "ssao_alchemy")]
        let other_depth_minify;
        let ssao;
        let ssao_blur;
        let ssao_apply;
        let combine_color_ambient;


        #[cfg(glsl_debug)]
        {
            ssao = ssao_loader.load().log_err("")?;
            ssao_blur = ssao_blur_loader.load().log_err("")?;
            ssao_apply = ssao_apply_loader.load().log_err("")?;
            combine_color_ambient = combine_color_ambient_loader.load().log_err("")?;
        }
        #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
        {
            start_depth_minify = start_depth_minify_loader.load().log_err("")?;
            other_depth_minify = other_depth_minify_loader.load().log_err("")?;
        }
        #[cfg(not(glsl_debug))]
        {
            #[cfg(feature = "ssao_alchemy")]
            {
                start_depth_minify = gl.new_program().from_settings(start_depth_minify_settings)
                    .log_err("")
                    .map_err(|err| Error::with_cause("Error loading start depth minify shader", err))?;
                other_depth_minify = gl.new_program().from_settings(other_depth_minify_settings)
                    .log_err("")
                    .map_err(|err| Error::with_cause("Error loading other depth minify shader", err))?;
            }

            ssao = gl.new_program().from_settings(ssao_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading ssao shader", err))?;
            ssao_blur = gl.new_program().from_settings(ssao_blur_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading ssao blur shader", err))?;
            ssao_apply = gl.new_program().from_settings(ssao_apply_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading ssao apply shader", err))?;
            combine_color_ambient = gl.new_program().from_settings(combine_color_ambient_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading combine color + ambient shader", err))?;
        }

        #[cfg(not(feature = "ssao_blur_gaussian"))]
        let ssao_noise = ssao_noise(ssao_blur_radius, gl)
            .map_err(|err| Error::with_cause("Error creating SSAO noise texture", err))?;

        Ok(Ssao{
            fbo_depth_mipmaps,
            linear_depth_mipmaps,
            fbo_ssao,
            fbo_ssao_blur,
            fbo_ssao_color: Rc::new(fbo_ssao_color),

            #[cfg(not(feature = "ssao_blur_gaussian"))]
            ssao_noise: UnsafeCell::new(ssao_noise),

            #[cfg(feature = "ssao_alchemy")]
            start_depth_minify: UnsafeCell::new(start_depth_minify),
            #[cfg(feature = "ssao_alchemy")]
            other_depth_minify: UnsafeCell::new(other_depth_minify),
            ssao: UnsafeCell::new(ssao),
            ssao_blur: UnsafeCell::new(ssao_blur),
            ssao_apply: UnsafeCell::new(ssao_apply),
            combine_color_ambient: UnsafeCell::new(combine_color_ambient),

            ssao_kernel_samples: Cell::new(ssao_kernel_samples),
            ssao_blur_radius: Cell::new(ssao_blur_radius),
            ssao_depth_samples: Cell::new(ssao_depth_samples),
            ssao_blur_step_size: Cell::new(ssao_blur_step_size),
            ssao_high_quality_blur: Cell::new(ssao_high_quality_blur),
            ssao_edge_sharpness: Cell::new(ssao_edge_sharpness),
            ssao_linear_depth: Cell::new(ssao_linear_depth),
            ssao_normals_from_position: Cell::new(!ssao_has_normals),
            ssao_far_at_inifinity: Cell::new(false),
            ssao_orthographic_camera: Cell::new(false),
            ssao_reversed_z: Cell::new(false),
            dithering: Cell::new(false),

            #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
            start_depth_minify_loader: UnsafeCell::new(start_depth_minify_loader),
            #[cfg(all(glsl_debug, feature="ssao_alchemy"))]
            other_depth_minify_loader: UnsafeCell::new(other_depth_minify_loader),
            #[cfg(glsl_debug)]
            ssao_loader: UnsafeCell::new(ssao_loader),
            #[cfg(glsl_debug)]
            ssao_blur_loader: UnsafeCell::new(ssao_blur_loader),
            #[cfg(glsl_debug)]
            ssao_apply_loader: UnsafeCell::new(ssao_apply_loader),
            #[cfg(glsl_debug)]
            combine_color_ambient_loader: UnsafeCell::new(combine_color_ambient_loader),

            fullscreen_quad,
        })
    }

    #[cfg(glsl_debug)]
    pub fn update(&mut self){
        #[cfg(feature="ssao_alchemy")]
        match unsafe{ (*self.start_depth_minify_loader.get()).update() }{
            Ok(Some(start_depth_minify)) => {
                self.start_depth_minify = UnsafeCell::new(start_depth_minify);
                println!("start depth minify shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }

        #[cfg(feature="ssao_alchemy")]
        match unsafe{ (*self.other_depth_minify_loader.get()).update() }{
            Ok(Some(other_depth_minify)) => {
                self.other_depth_minify = UnsafeCell::new(other_depth_minify);
                println!("other depth minify shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }

        match unsafe{ (*self.ssao_loader.get()).update() }{
            Ok(Some(ssao)) => {
                self.ssao = UnsafeCell::new(ssao);
                println!("ssao shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }

        match unsafe{ (*self.ssao_blur_loader.get()).update() }{
            Ok(Some(ssao_blur)) => {
                self.ssao_blur = UnsafeCell::new(ssao_blur);
                println!("ssao blur shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }

        match unsafe{ (*self.ssao_apply_loader.get()).update() } {
            Ok(Some(ssao_apply)) => {
                unsafe{ *self.ssao_apply.get() = ssao_apply };
                println!("ssao apply shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }

        match unsafe{ (*self.combine_color_ambient_loader.get()).update() } {
            Ok(Some(combine_color_ambient)) => {
                unsafe{ *self.combine_color_ambient.get() = combine_color_ambient };
                println!("combine color ambient shader reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }
    }

    #[cfg(glsl_debug)]
    fn refresh_color_ambient_combine(&self, gl: &gl::Renderer, dithering: bool) -> Result<()>{
        if dithering != self.dithering.get() {
            let mut ssao_apply_loader = gl.new_auto_program(ssao_apply_settings(dithering));
            unsafe{ *self.ssao_apply.get() = ssao_apply_loader.load()? };

            unsafe{ *self.ssao_apply_loader.get() = ssao_apply_loader };

            let mut combine_color_ambient_loader = gl.new_auto_program(combine_color_ambient_settings(dithering));
            unsafe{ *self.combine_color_ambient.get() = combine_color_ambient_loader.load()? };

            unsafe{ *self.combine_color_ambient_loader.get() = combine_color_ambient_loader };

            self.dithering.set(dithering);
        }

        Ok(())
    }

    #[cfg(not(glsl_debug))]
    fn refresh_color_ambient_combine(&self, gl: &gl::Renderer, dithering: bool) -> Result<()>{
        if dithering != self.dithering.get() {
            let ssao_apply = gl.new_program()
                .from_settings(ssao_apply_settings(dithering))
                .map_err(|err| Error::with_cause("Error re-loading ssao apply shader", err))?;
            unsafe{ *self.ssao_apply.get() = ssao_apply };

            let combine_color_ambient = gl.new_program()
                .from_settings(combine_color_ambient_settings(dithering))
                .map_err(|err| Error::with_cause("Error re-loading color + ambient shader", err))?;
            unsafe{ *self.combine_color_ambient.get() = combine_color_ambient };

            self.dithering.set(dithering);
        }

        Ok(())
    }

    pub fn fbo(&self) -> &Rc<gl::Fbo> {
        &self.fbo_ssao_color
    }

    pub fn ssao_texture(&self) -> &gl::Texture{
        self.fbo_ssao.color_tex(0).unwrap()
    }

    pub fn ssao_blur_texture(&self) -> &gl::Texture{
        #[cfg(not(feature = "ssao_blur_gaussian"))]
        {
            self.fbo_ssao_blur.color_tex(0).unwrap()
        }

        #[cfg(feature = "ssao_blur_gaussian")]
        {
            self.fbo_ssao_blur[1].color_tex(0).unwrap()
        }
    }

    pub fn ssao_color_texture(&self) -> &gl::Texture{
        self.fbo_ssao_color.color_tex(0).unwrap()
    }

    pub fn ssao_color_attachment(&self) -> &gl::fbo::ColorAttachment{
        self.fbo_ssao_color.color(0).unwrap()
    }

    pub fn process(&self, gl: &gl::Renderer,
        camera: &dyn CameraExt,
        color: &gl::Texture,
        position: SSAOPosition,
        normals: Option<&gl::Texture>,
        ambient: &gl::Texture,
        parameters: &SSAOParameters) -> Result<&gl::Texture>
    {
        #[cfg(gl_debug_groups)]
        let _debug_group = gl.new_debug_group(0, "SSAO");

        let intensity: f32 = *parameters.power.borrow();
        let radius: f32 = *parameters.radius.borrow();
        let kernel_samples: u32 = *parameters.kernel_samples.borrow();
        let blur_radius: u32 = *parameters.blur_radius.borrow();
        let blur_step_size: u32 = *parameters.blur_step_size.borrow();
        let high_quality_blur: bool = *parameters.high_quality_blur.borrow();
        let edge_sharpness: f32 = *parameters.edge_sharpness.borrow();
        let dithering: bool = *parameters.dithering.borrow();

        let has_position;
        let position_uniforms;
        let depth_samples;
        let has_linear_depth;
        let depth_attachment;
        match position{
            SSAOPosition::Position(depth, position) => {
                has_position = true;
                depth_samples = 0;
                has_linear_depth = false;
                position_uniforms = uniforms!{
                    g_position: (position, 1)
                };
                depth_attachment = depth;
            }
            SSAOPosition::FromLinearDepth(depth, linear_depth) => {
                has_position = false;

                #[cfg(renderer_ty="gl")]
                {
                    depth_samples = linear_depth.samples();
                }

                #[cfg(any(renderer_ty="gles", renderer_ty="webgl"))]
                {
                    depth_samples = 0;
                }

                has_linear_depth = true;
                position_uniforms = uniforms!{
                    g_linear_depth: (linear_depth, 1)
                };
                depth_attachment = depth;
            }
            SSAOPosition::FromDepth(depth) => {
                has_position = false;

                #[cfg(renderer_ty="gl")]
                {
                    depth_samples = depth.samples();
                }

                #[cfg(any(renderer_ty="gles", renderer_ty="webgl"))]
                {
                    depth_samples = 0;
                }
                has_linear_depth = false;
                position_uniforms = uniforms!{
                    g_depth: (depth, 1)
                };
                depth_attachment = depth;
            }
        }

        let normals_from_position = normals.is_none();
        let orthographic_camera = camera.fov().is_none();
        let far_at_inifinity = camera.far_clip().is_none();
        let reversed_z = camera.reversed_z();

        if kernel_samples != self.ssao_kernel_samples.get()
            || depth_samples != self.ssao_depth_samples.get()
            || has_linear_depth != self.ssao_linear_depth.get()
            || normals_from_position != self.ssao_normals_from_position.get()
            || orthographic_camera != self.ssao_orthographic_camera.get()
            || far_at_inifinity != self.ssao_far_at_inifinity.get()
            || reversed_z != self.ssao_reversed_z.get()
        {
            let w = self.fbo_ssao.width();
            let h = self.fbo_ssao.height();
            #[cfg(not(feature = "ssao_alchemy"))]
            let ssao_settings = ssao_settings(
                kernel_samples,
                blur_radius,
                w, h,
                depth_samples,
                has_position,
                !normals_from_position,
                has_linear_depth);
            #[cfg(feature = "ssao_alchemy")]
            let ssao_settings = alchemy_ssao_settings(
                w, h,
                kernel_samples,
                depth_samples,
                has_position,
                !normals_from_position,
                has_linear_depth);

            #[cfg(feature = "ssao_alchemy")]
            let start_minify_settings = ssao_depth_minify_settings(
                has_linear_depth,
                true,
                orthographic_camera,
                far_at_inifinity,
                reversed_z
            );
            #[cfg(feature = "ssao_alchemy")]
            let other_minify_settings = ssao_depth_minify_settings(
                has_linear_depth,
                false,
                orthographic_camera,
                far_at_inifinity,
                reversed_z
            );
            #[cfg(glsl_debug)]
            {
                let mut ssao_loader = gl.new_auto_program(ssao_settings);
                unsafe{ *self.ssao.get() = ssao_loader.load()? };

                unsafe{ *self.ssao_loader.get() = ssao_loader };

                #[cfg(feature = "ssao_alchemy")]
                {
                    let mut start_minify_loader = gl.new_auto_program(start_minify_settings);
                    unsafe{ *self.start_depth_minify.get() = start_minify_loader.load()? };

                    unsafe{ *self.start_depth_minify_loader.get() = start_minify_loader };

                    let mut other_minify_loader = gl.new_auto_program(other_minify_settings);
                    unsafe{ *self.other_depth_minify.get() = other_minify_loader.load()? };

                    unsafe{ *self.other_depth_minify_loader.get() = other_minify_loader };
                }
            }

            #[cfg(not(glsl_debug))]
            {
                let ssao = gl.new_program()
                    .from_settings(ssao_settings)
                    .map_err(|err| Error::with_cause("Error re-loading ssao shader", err))?;
                unsafe{ *self.ssao.get() = ssao };

                #[cfg(feature = "ssao_alchemy")]
                {
                    let start_minify = gl.new_program()
                        .from_settings(start_minify_settings)
                        .map_err(|err| Error::with_cause("Error re-loading ssao shader", err))?;
                    unsafe{ *self.start_depth_minify.get() = start_minify };

                    let other_minify = gl.new_program()
                        .from_settings(other_minify_settings)
                        .map_err(|err| Error::with_cause("Error re-loading ssao shader", err))?;
                    unsafe{ *self.other_depth_minify.get() = other_minify };
                }
            }

            self.ssao_kernel_samples.set(kernel_samples);
            self.ssao_depth_samples.set(depth_samples);
            self.ssao_orthographic_camera.set(orthographic_camera);
            self.ssao_far_at_inifinity.set(far_at_inifinity);
            self.ssao_reversed_z.set(reversed_z);
        }

        #[cfg(not(feature = "ssao_blur_gaussian"))]
        {
            if blur_radius != self.ssao_blur_radius.get() {
                let ssao_noise = ssao_noise(blur_radius, gl)
                    .map_err(|err| Error::with_cause("Error creating SSAO noise texture", err))?;
                unsafe{ *self.ssao_noise.get() = ssao_noise };

                #[cfg(glsl_debug)]
                {
                    let mut ssao_blur_loader = gl.new_auto_program(ssao_blur_settings(blur_radius));
                    unsafe{ *self.ssao_blur.get() = ssao_blur_loader.load()? };

                    unsafe{ *self.ssao_blur_loader.get() = ssao_blur_loader };
                }

                #[cfg(not(glsl_debug))]
                {
                    let ssao_blur = gl.new_program()
                        .from_settings(ssao_blur_settings(blur_radius))
                        .map_err(|err| Error::with_cause("Error re-loading ssao blur shader", err))?;
                    unsafe{ *self.ssao_blur.get() = ssao_blur };
                }

                self.ssao_blur_radius.set(blur_radius);
            }
        }

        #[cfg(feature = "ssao_blur_gaussian")]
        {
            if blur_radius > 0
                && (blur_radius != self.ssao_blur_radius.get()
                || blur_step_size != self.ssao_blur_step_size.get()
                || high_quality_blur != self.ssao_high_quality_blur.get()
                || edge_sharpness != self.ssao_edge_sharpness.get()
                || has_linear_depth != self.ssao_linear_depth.get()
                || normals_from_position != self.ssao_normals_from_position.get())
            {
                let w = self.fbo_ssao.width();
                let h = self.fbo_ssao.height();

                let settings = ssao_blur_gaussian_settings(
                    w, h,
                    has_linear_depth,
                    !normals_from_position,
                    blur_radius,
                    blur_step_size,
                    high_quality_blur,
                    edge_sharpness);

                #[cfg(glsl_debug)]
                {
                    let mut ssao_blur_loader = gl.new_auto_program(settings);
                    unsafe{ *self.ssao_blur.get() = ssao_blur_loader.load()? };

                    unsafe{ *self.ssao_blur_loader.get() = ssao_blur_loader };
                }

                #[cfg(not(glsl_debug))]
                {
                    let ssao_blur = gl.new_program()
                        .from_settings(settings)
                        .map_err(|err| Error::with_cause("Error re-loading ssao blur shader", err))?;
                    unsafe{ *self.ssao_blur.get() = ssao_blur };
                }

                self.ssao_blur_radius.set(blur_radius);
                self.ssao_blur_step_size.set(blur_step_size);
                self.ssao_high_quality_blur.set(high_quality_blur);
                self.ssao_edge_sharpness.set(edge_sharpness);
                self.ssao_linear_depth.set(has_linear_depth);
                self.ssao_normals_from_position.set(normals_from_position);
            }
        }

        self.refresh_color_ambient_combine(gl, dithering)?;

        #[cfg(not(feature = "ssao_alchemy"))]
        {
            let tex_noise = unsafe{ &*self.ssao_noise.get() };
            let ssao = unsafe{ &*self.ssao.get() };
            let projection = camera.projection();
            let tan_half_fov = (camera.fov().unwrap().to_rad() / 2.).tan();
            let top = camera.near_clip() * tan_half_fov;
            let right = camera.aspect_ratio() * top;
            let right_top = vec2(right, top);

            let mut ssao_uniforms = uniforms!{
                tex_noise: (tex_noise, 2),
                projectionMatrix: projection,
                invProjectionMatrix: projection.try_inverse().unwrap(),
                right_top: right_top,
                znear: camera.near_clip(),
                zfar: camera.far_clip().unwrap_or(f32::INFINITY),
                radius: parameters.radius,
                bias: parameters.bias,
                power: parameters.power,
            };

            ssao_uniforms.extend(position_uniforms.iter().cloned());

            ssao_uniforms.extend(normals.into_iter().flat_map(|normals| uniforms!{
                g_normal: (normals, 3)
            }));

            let gl = gl.with_fbo(&self.fbo_ssao);
            gl.clear_color(&BLACK);
            gl.draw_vao(&*self.fullscreen_quad, ssao, &ssao_uniforms);
        }

        let projection = camera.projection();
        let width = self.fbo_ssao.width() as f32;
        let height = self.fbo_ssao.height() as f32;
        let proj_info = vec4(
            -2.0 / (width * projection[(0,0)]),
            -2.0 / (height * projection[(1,1)]),
            (1.0 - projection[(0,2)]) / projection[(0,0)],
            (1.0 - projection[(1,2)]) / projection[(1,1)]
        );
        let tan_half_fov = (camera.fov().unwrap().to_rad() * 0.5).tan();
        let scale = 2. * tan_half_fov;
        let pixels_per_meter = (height / scale).abs();
        const MIN_AO_SS_RADIUS: f32 = 1.0;
        // From g3d: Second parameter of max is just solving for Z coordinate at which we hit MIN_AO_SS_RADIUS
        #[allow(unused_mut)]
        let mut zfar = camera.far_clip()
            .unwrap_or(f32::INFINITY)
            .min(pixels_per_meter * radius / MIN_AO_SS_RADIUS);
        // From g3d: Hack because setting farZ lower results in banding artefacts on some scenes, should tune later.
        // zfar = zfar.max(1000.0);
        let depth_func = gl::GREATER;//if reversed_z {gl::LESS} else {gl::GREATER};

        #[cfg(feature = "ssao_alchemy")]
        {
            // Minify depth to mipmaps
            let start_depth_minify = unsafe{ &*self.start_depth_minify.get() };
            let mut depth_minify_uniforms = uniforms!{
                znear: camera.near_clip(),
                zfar: camera.far_clip().unwrap_or(f32::INFINITY),
                source_mipmap: 0i32,
                projection: projection,
            };
            depth_minify_uniforms.extend(position_uniforms.iter().cloned());
            let fbo = self.fbo_depth_mipmaps[0].with_depth(depth_attachment).unwrap();
            let gl_fbo = gl.with_fbo(&fbo);
            let gl_fbo = gl_fbo.with_properties(&[
                gl::Property::DepthMask(false),
                gl::Property::DepthTest(true),
                gl::Property::DepthFunc(depth_func)
            ]);
            gl_fbo.draw_vao(&*self.fullscreen_quad, start_depth_minify, &depth_minify_uniforms);

            let other_depth_minify = unsafe{ &*self.other_depth_minify.get() };
            for (i, fbo) in self.fbo_depth_mipmaps[1..].iter().enumerate(){
                let depth_minify_uniforms = uniforms!{
                    znear: camera.near_clip(),
                    zfar: camera.far_clip().unwrap_or(f32::INFINITY),
                    source_mipmap: i as i32,
                    g_depth: (&*self.linear_depth_mipmaps, 1),
                    projection: projection,
                };
                let gl_fbo = gl.with_fbo(fbo);
                let gl_fbo = gl_fbo.with_properties(&[
                    gl::Property::DepthMask(false),
                ]);
                gl_fbo.draw_vao(&*self.fullscreen_quad, other_depth_minify, &depth_minify_uniforms);
            }

            // Actual SSAO
            let ssao = unsafe{ &*self.ssao.get() };


            let mut ssao_uniforms = uniforms!{
                znear: camera.near_clip(),
                zfar: zfar,
                radius: parameters.radius,
                bias: parameters.bias,
                intensity: intensity,
                neg_inv_radius2: -1. / (radius * radius),
                proj_scale: pixels_per_meter,
                proj_info: proj_info,
                source_depth_mipmaps: (&*self.linear_depth_mipmaps, 4),
            };

            ssao_uniforms.extend(position_uniforms.iter().cloned());

            ssao_uniforms.extend(normals.into_iter().flat_map(|normals| uniforms!{
                g_normal: (normals, 3)
            }));

            let fbo = self.fbo_ssao.with_depth(depth_attachment).unwrap();
            let gl = gl.with_fbo(&fbo);
            let gl = gl.with_properties(&[
                gl::Property::DepthMask(false),
                gl::Property::DepthTest(true),
                gl::Property::DepthFunc(depth_func)
            ]);
            gl.clear_color(&WHITE);
            gl.draw_vao(&*self.fullscreen_quad, ssao, &ssao_uniforms);
        }

        // Blur SSAO with box filter
        #[cfg(not(feature = "ssao_blur_gaussian"))]
        {
            if blur_radius > 0 {
                let ssao_blur = unsafe{ &*self.ssao_blur.get() };
                let ssao_tex = self.fbo_ssao.color_tex(0).unwrap();
                let ssao_uniforms = uniforms!{
                    ssao: ssao_tex,
                };
                let gl = gl.with_fbo(&self.fbo_ssao_blur);
                gl.clear_color(&BLACK);
                gl.draw_vao(&*self.fullscreen_quad, ssao_blur, &ssao_uniforms);
            }
        }

        // Blur SSAO with separable gaussian filter
        #[cfg(feature = "ssao_blur_gaussian")]
        {
            if blur_radius > 0 {
                let ssao_blur = unsafe{ &*self.ssao_blur.get() };
                let ssao_tex = self.fbo_ssao.color_tex(0).unwrap();
                let mut ssao_uniforms = uniforms!{
                    source_ssao: (ssao_tex, 0),
                    g_depth: (&*self.linear_depth_mipmaps, 1),
                    camera_z_near: camera.near_clip(),
                    camera_z_far: zfar,
                    axis: vec2(1i32, 0),
                    edge_sharpness: 1.,
                    filter_scale: 1i32,
                    invRadius: 1. / radius,
                    proj_info: proj_info,

                };
                ssao_uniforms.extend(normals.into_iter().flat_map(|normals| uniforms!{
                    g_normal: (normals, 2)
                }));
                let fbo = self.fbo_ssao_blur[0].with_depth(depth_attachment).unwrap();
                let gl_fbo = gl.with_fbo(&fbo);
                let gl_fbo = gl_fbo.with_properties(&[
                    gl::Property::DepthMask(false),
                    gl::Property::DepthTest(true),
                    gl::Property::DepthFunc(depth_func)
                ]);
                gl_fbo.clear_color(&WHITE);
                gl_fbo.draw_vao(&*self.fullscreen_quad, ssao_blur, &ssao_uniforms);


                let ssao_tex = self.fbo_ssao_blur[0].color_tex(0).unwrap();
                let mut ssao_uniforms = uniforms!{
                    source_ssao: (ssao_tex, 0),
                    g_depth: (&*self.linear_depth_mipmaps, 1),
                    camera_z_near: camera.near_clip(),
                    camera_z_far: zfar,
                    axis: vec2(0i32, 1),
                    edge_sharpness: 1.,
                    filter_scale: 1i32,
                    invRadius: 1. / radius,
                    proj_info: proj_info,
                };
                ssao_uniforms.extend(normals.into_iter().flat_map(|normals| uniforms!{
                    g_normal: (normals, 2)
                }));
                let fbo = self.fbo_ssao_blur[1].with_depth(depth_attachment).unwrap();
                let gl_fbo = gl.with_fbo(&fbo);
                let gl_fbo = gl_fbo.with_properties(&[
                    gl::Property::DepthMask(false),
                    gl::Property::DepthTest(true),
                    gl::Property::DepthFunc(depth_func)
                ]);
                gl_fbo.clear_color(&WHITE);
                gl_fbo.draw_vao(&*self.fullscreen_quad, ssao_blur, &ssao_uniforms);
            }
        }

        {
            let ssao_apply = unsafe{ &*self.ssao_apply.get() };

            let ssao_tex;
            if blur_radius > 0 {
                #[cfg(not(feature = "ssao_blur_gaussian"))]
                {
                    ssao_tex = self.fbo_ssao_blur.color_tex(0).unwrap();
                }

                #[cfg(feature = "ssao_blur_gaussian")]
                {
                    ssao_tex = self.fbo_ssao_blur[1].color_tex(0).unwrap();
                }
            }else{
                ssao_tex = self.fbo_ssao.color_tex(0).unwrap();
            }

            let ssao_uniforms = uniforms!{
                ssao: (ssao_tex, 0),
                color: (color, 1),
                ambient: (ambient, 2),
            };
            let gl = gl.with_fbo(&*self.fbo_ssao_color);
            gl.clear_color(&BLACK);
            gl.draw_vao(&*self.fullscreen_quad, &ssao_apply, &ssao_uniforms);
        }

        Ok(self.fbo_ssao_color.color_tex(0).unwrap())
    }

    fn combine_color_ambient_(&self,
        gl: &gl::Renderer,
        color: &gl::Texture,
        ambient: &gl::Texture,
        dithering: bool) -> Result<&gl::Texture>
    {
        #[cfg(gl_debug_groups)]
        let _debug_group = gl.new_debug_group(0, "Combine color & ambient");

        self.refresh_color_ambient_combine(gl, dithering)?;

        let combine_color_ambient = unsafe{ &*self.combine_color_ambient.get() };
        let uniforms = uniforms!{
            color: (color, 0),
            ambient: (ambient, 1)
        };
        let gl = gl.with_fbo(&*self.fbo_ssao_color);
        gl.clear_color(&BLACK);
        gl.draw_vao(&*self.fullscreen_quad, &combine_color_ambient, &uniforms);

        Ok(self.fbo_ssao_color.color_tex(0).unwrap())
    }

    pub fn combine_color_ambient(&self,
        gl: &gl::Renderer,
        color: &gl::Texture,
        ambient: &gl::Texture) -> Result<&gl::Texture>
    {
        self.combine_color_ambient_(gl, color, ambient, false)
    }

    pub fn combine_color_ambient_with_dither(&self,
        gl: &gl::Renderer,
        color: &gl::Texture,
        ambient: &gl::Texture) -> Result<&gl::Texture>
    {
        self.combine_color_ambient_(gl, color, ambient, true)
    }
}