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
use super::{ProgramSettings, GLSL_VERSION, default_settings, fbo};
use rin_gl::{self as gl, uniforms, Renderer3d};
use rin_util::{Result, Error, LogErr};
use rin_math::{vec2, Angle, Rect};
use std::rc::Rc;
use rin_graphics::{CameraExt, Vertex2DTex};
use std::cell::{Cell, UnsafeCell};
use rin_events::{RangedPropertyMut, Property, ParamsDefault};
#[cfg(gui)]
use rin_gui::{ParameterGroup, EnumParam};
#[cfg(glsl_debug)]
use rin_util::AutoLoader;

const LOW_RESOLUTION_FACTOR: u32 = 2;

pub struct Dof {
    fbo_coc: gl::Fbo,
    fbo_blur_h: gl::Fbo,
    fbo_blur_v: gl::Fbo,
    fbo_composite: gl::Fbo,

    coc: gl::Program,
    blur_h: gl::Program,
    blur_v: gl::Program,
    composite: UnsafeCell<gl::Program>,

    #[cfg(glsl_debug)]
    coc_loader: AutoLoader<gl::Program>,
    #[cfg(glsl_debug)]
    blur_h_loader: AutoLoader<gl::Program>,
    #[cfg(glsl_debug)]
    blur_v_loader: AutoLoader<gl::Program>,
    #[cfg(glsl_debug)]
    composite_loader: UnsafeCell<AutoLoader<gl::Program>>,

    fullscreen_quad: Rc<gl::SimpleVao<Vertex2DTex>>,
    current_dof_debug: Cell<DofDebug>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(gui, derive(EnumParam))]
pub enum DofTy {
    Artistic,
    Physical,
}

impl DofTy {
    fn to_str(&self) -> &'static str {
        match self {
            DofTy::Artistic => "ARTIST",
            DofTy::Physical => "PHYSICAL",
        }
    }
}

impl Default for DofTy {
    fn default() -> Self {
        DofTy::Physical
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(gui, derive(EnumParam))]
pub enum DofDebug {
    None,
    ShowCoc,
    ShowRegion,
    // ShowNear,
    // ShowBlurry,
    // ShowInput,
    // ShowMidAndFar,
    ShowSignedCoc,
}

impl Default for DofDebug {
    fn default() -> Self {
        DofDebug::None
    }
}

impl ToString for DofDebug {
    fn to_string(&self) -> String {
        match self {
            DofDebug::None => "NO_DEBUG",
            DofDebug::ShowCoc => "SHOW_COC",
            DofDebug::ShowRegion => "SHOW_REGION",
            DofDebug::ShowSignedCoc => "SHOW_SIGNED_COC",
        }.to_owned()
    }
}

#[derive(ParamsDefault, Clone, Debug)]
#[cfg_attr(gui, derive(ParameterGroup))]
pub struct DofParameters {
    // #[gui(name = "type")]
    // ty: Property<'static, DofTy>,

    #[param(default = 2.2, range = 0.1 .. 128.)]
    fstop: RangedPropertyMut<'static, f32>,
    #[param(default = 0., range = 0. .. 100.)]
    focus_plane_z: RangedPropertyMut<'static, f32>,

    // #[param(default = 0.01, range_log = 0. .. 1.)]
    // lens_radius: RangedPropertyMut<'static, f32>,
    // #[param(default = 0., range = 0. .. 100.)]
    // near_sharp_plane_z: RangedPropertyMut<'static, f32>,
    // #[param(default = 0.2, range = 0. .. 100.)]
    // closest_near_plane: RangedPropertyMut<'static, f32>,

    // // Artistic
    // #[param(default = 0., range = 0. .. 100.)]
    // near_blurry_plane_z: RangedPropertyMut<'static, f32>,
    // #[param(default = 0., range = 0. .. 100.)]
    // far_sharp_plane_z: RangedPropertyMut<'static, f32>,
    // #[param(default = 0., range = 0. .. 100.)]
    // far_blurry_plane_z: RangedPropertyMut<'static, f32>,
    // #[param(default = 1., range = 0. .. 100.)]
    // near_scale: RangedPropertyMut<'static, f32>,
    // #[param(default = 1., range = 0. .. 100.)]
    // far_scale: RangedPropertyMut<'static, f32>,
    // #[param(default = 0.25, range = 0. .. 4.)]
    // near_blur_radius_fraction: RangedPropertyMut<'static, f32>,

    #[param(default = 7, range = 3 .. 100)]
    max_coc_radius_pixels: RangedPropertyMut<'static, i32>,
    #[param(default = 0.14, range = 0.01 .. 1.)]
    near_blur_radius_factor: RangedPropertyMut<'static, f32>,
    // #[param(default = 0.03, range = 0. .. 4.)]
    // max_viewport_fraction_coc: RangedPropertyMut<'static, f32>,

    dof_debug: Property<'static, DofDebug>
}

// impl DofParameters {
//     pub fn radius_circle_of_confusion_pixels(&self, z: f32, vfov: Rad<f32>, screen_height: f32) -> f32 {
//         (self.lens_radius.get() * (-self.focus_plane_z.get() - z) * (screen_height * 0.5))
//             / ((vfov * 0.5).tan() * (-self.focus_plane_z.get() * z))
//     }
// }

fn coc_settings(ty: DofTy) -> ProgramSettings {
    ProgramSettings {
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("MODEL".to_owned(), ty.to_str().to_owned())
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/dof_coc.frag")),
        ],
        .. default_settings()
    }
}

fn blur_h_settings() -> ProgramSettings {
    ProgramSettings {
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("HORIZONTAL".to_owned(), "1".to_owned())
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/dof_blur.frag")),
        ],
        .. default_settings()
    }
}

fn blur_v_settings() -> ProgramSettings {
    ProgramSettings {
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("HORIZONTAL".to_owned(), "0".to_owned())
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/dof_blur.frag")),
        ],
        .. default_settings()
    }
}

fn composite_settings(dof_debug: DofDebug) -> ProgramSettings {
    ProgramSettings {
        version: GLSL_VERSION,
        precision: gl::program::ShaderPrecision::High,
        extensions: vec![],
        defines: vec![
            ("DOF_DEBUG_OPTION".to_owned(), dof_debug.to_string())
        ],
        shaders: vec![
            (gl::VERTEX_SHADER, shader!("shaders/full_quad.vs.glsl")),
            (gl::FRAGMENT_SHADER, shader!("shaders/dof_composite.frag")),
        ],
        .. default_settings()
    }
}
#[derive(Clone, Copy)]
pub enum DofDepth<'a>{
    Depth(&'a gl::Texture),
    LinearDepth(&'a gl::Texture),
}

impl Dof {
    pub fn new(gl: &gl::Renderer, w: u32, h: u32, format: gl::fbo::ColorFormat, fullscreen_quad: Rc<gl::SimpleVao<Vertex2DTex>>) -> Result<Dof> {
        let mut fbo_coc = fbo(gl, w, h, gl::fbo::ColorFormat::RGBA16F)
            .map_err(|err| Error::with_cause("Couldn't create dof coc fbo", err))?;
        fbo_coc.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        let blur_h_near_attachment = gl.new_fbo_color_attachment()
            .texture(w/LOW_RESOLUTION_FACTOR, h, gl::fbo::ColorFormat::RGBA16F)
            .map_err(|err| Error::with_cause("Couldn't create dof blur attachment", err))?;
        let blur_h_blur_attachment = gl.new_fbo_color_attachment()
            .texture(w/LOW_RESOLUTION_FACTOR, h, gl::fbo::ColorFormat::RGBA16F)
            .map_err(|err| Error::with_cause("Couldn't create dof blur attachment", err))?;
        let mut fbo_blur_h = gl.new_fbo().from_colors_no_depth(vec![
            blur_h_near_attachment,
            blur_h_blur_attachment,
        ]).map_err(|err| Error::with_cause("Couldn't create dof blur fbo", err))?;
        fbo_blur_h.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        fbo_blur_h.color_tex_mut(1).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        let blur_v_near_attachment = gl.new_fbo_color_attachment()
            .texture(w/LOW_RESOLUTION_FACTOR, h/LOW_RESOLUTION_FACTOR, gl::fbo::ColorFormat::RGBA16F)
            .map_err(|err| Error::with_cause("Couldn't create dof blur attachment", err))?;
        let blur_v_blur_attachment = gl.new_fbo_color_attachment()
            .texture(w/LOW_RESOLUTION_FACTOR, h/LOW_RESOLUTION_FACTOR, gl::fbo::ColorFormat::RGBA16F)
            .map_err(|err| Error::with_cause("Couldn't create dof blur attachment", err))?;
        let mut fbo_blur_v = gl.new_fbo().from_colors_no_depth(vec![
            blur_v_near_attachment,
            blur_v_blur_attachment,
        ]).map_err(|err| Error::with_cause("Couldn't create dof blur fbo", err))?;
        fbo_blur_v.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        fbo_blur_v.color_tex_mut(1).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);
        let mut fbo_composite = fbo(gl, w, h, format)
            .map_err(|err| Error::with_cause("Couldn't create dof composite fbo", err))?;
        fbo_composite.color_tex_mut(0).unwrap().set_min_mag_filters(gl::NEAREST, gl::NEAREST);

        let coc_settings = coc_settings(DofTy::default());
        let blur_h_settings = blur_h_settings();
        let blur_v_settings = blur_v_settings();
        let composite_settings = composite_settings(DofDebug::default());

        let coc;
        let blur_h;
        let blur_v;
        let composite;

        #[cfg(glsl_debug)]
        let mut coc_loader = gl.new_auto_program(coc_settings);
        #[cfg(glsl_debug)]
        let mut blur_h_loader = gl.new_auto_program(blur_h_settings);
        #[cfg(glsl_debug)]
        let mut blur_v_loader = gl.new_auto_program(blur_v_settings);
        #[cfg(glsl_debug)]
        let mut composite_loader = gl.new_auto_program(composite_settings);

        #[cfg(glsl_debug)]
        {
            coc = coc_loader.load().log_err("")?;
            blur_h = blur_h_loader.load().log_err("")?;
            blur_v = blur_v_loader.load().log_err("")?;
            composite = composite_loader.load().log_err("")?;
        }


        #[cfg(not(glsl_debug))]
        {
            coc = gl.new_program().from_settings(coc_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading dof coc shader", err))?;
            blur_h = gl.new_program().from_settings(blur_h_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading dof blur_h shader", err))?;
            blur_v = gl.new_program().from_settings(blur_v_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading dof blur_v shader", err))?;
            composite = gl.new_program().from_settings(composite_settings)
                .log_err("")
                .map_err(|err| Error::with_cause("Error loading dof composite shader", err))?;
        }

        Ok(Dof{
            fbo_coc,
            fbo_blur_h,
            fbo_blur_v,
            fbo_composite,

            coc,
            blur_h,
            blur_v,
            composite: UnsafeCell::new(composite),

            #[cfg(glsl_debug)]
            coc_loader,
            #[cfg(glsl_debug)]
            blur_h_loader,
            #[cfg(glsl_debug)]
            blur_v_loader,
            #[cfg(glsl_debug)]
            composite_loader: UnsafeCell::new(composite_loader),

            fullscreen_quad,
            current_dof_debug: Cell::default(),
        })
    }


    #[cfg(glsl_debug)]
    pub fn update(&mut self) {
        match self.coc_loader.update(){
            Ok(Some(program)) => {
                self.coc = program;
                println!("dof coc program reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }
        match self.blur_h_loader.update(){
            Ok(Some(program)) => {
                self.blur_h = program;
                println!("dof blur_h program reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }
        match self.blur_v_loader.update(){
            Ok(Some(program)) => {
                self.blur_v = program;
                println!("dof blur_v program reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }
        match unsafe{ (*self.composite_loader.get()).update() }{
            Ok(Some(program)) => {
                unsafe{ *self.composite.get() = program };
                println!("dof composite program reloaded correctly");
            }
            Err(err) => {
                println!("{}", err);
            }
            _ => {}
        }
    }

    pub fn process(
        &self,
        gl: &gl::Renderer,
        camera: &dyn CameraExt,
        viewport: &Rect<i32>,
        color: &gl::Texture,
        depth: DofDepth,
        parameters: &DofParameters) -> &gl::Texture
    {
        #[cfg(gl_debug_groups)]
        let _debug_group = gl.new_debug_group(0, "Depth of field");

        if *parameters.dof_debug.get() != self.current_dof_debug.get() {
            let composite_settings = composite_settings(*parameters.dof_debug.get());

            #[cfg(glsl_debug)]
            unsafe {
                *self.composite_loader.get() = gl.new_auto_program(composite_settings);
                *self.composite.get() = (*self.composite_loader.get()).load().log_err("").unwrap();
            }

            #[cfg(not(glsl_debug))]
            unsafe{
                *self.composite.get() = gl.new_program().from_settings(composite_settings)
                    .log_err("")
                    .unwrap();
            }

            self.current_dof_debug.set(*parameters.dof_debug.get());
        }

        let to_meters = 0.001;
        let focal_length_m = to_meters * camera.focal_length().unwrap();
        let aperture_m = focal_length_m / parameters.fstop.get();
        let sensor_m = to_meters * camera.sensor_width_mm().unwrap();
        let view_sensor = viewport.width as f32 / sensor_m;

        // https://developer.nvidia.com/sites/all/modules/custom/gpugems/books/GPUGems/gpugems_ch23.html
        // coc = aperture * (focallength * (objectdistance - planeinfocus)) / (objectdistance * (planeinfocus - focallength))
        let scale = aperture_m * focal_length_m / (parameters.focus_plane_z.get() - focal_length_m) * view_sensor;

        let mut coc_uniforms = uniforms! {
            zfar: camera.far_clip().unwrap_or(f32::INFINITY),
            znear: camera.near_clip(),
            focusPlaneZ: parameters.focus_plane_z,
            scale: scale,
            aperture: aperture_m,
            focallength: focal_length_m,
            view_sensor: view_sensor,
            // nearSharpPlaneZ: parameters.near_sharp_plane_z,
            // nearBlurryPlaneZ: parameters.near_blurry_plane_z,
            // farSharpPlaneZ: parameters.far_sharp_plane_z,
            // farBlurryPlaneZ: parameters.far_blurry_plane_z,
            // nearScale: parameters.near_scale,
            // farScale: parameters.far_scale,
            COLOR_buffer: (color, 2),
        };

        match depth {
            DofDepth::Depth(depth) => coc_uniforms.extend_from_slice(&uniforms!{
                g_depth: (depth, 1)
            }),
            DofDepth::LinearDepth(linear_depth) => coc_uniforms.extend_from_slice(&uniforms!{
                g_linear_depth: (linear_depth, 1)
            }),
        }

        gl.with_fbo(&self.fbo_coc)
            .draw_vao(&*self.fullscreen_quad, &self.coc, &coc_uniforms);

        // let near_blur_radius_pixels = {
        //     let n = if *parameters.ty.get() == DofTy::Artistic {
        //         parameters.near_blur_radius_fraction.get() * viewport.height as f32
        //     }else{
        //         let near = parameters.closest_near_plane.get().max(camera.near_clip());
        //         -parameters.radius_circle_of_confusion_pixels(
        //             -near,
        //             camera.fov().unwrap().to_rad(),
        //             viewport.height as f32
        //         )
        //         // dof_param_0 / -near - dof_param_1
        //     };

        //     (parameters.max_viewport_fraction_coc.get() * viewport.width as f32).min(n).ceil() as i32
        // };

        let blur_source = self.fbo_coc.color_tex(0).unwrap();
        let blur_h_uniforms = uniforms!{
            maxCoCRadiusPixels: parameters.max_coc_radius_pixels,
            blurSourceBuffer: (blur_source, 1),
            // nearBlurRadiusPixels: near_blur_radius_pixels,
            invNearBlurRadiusPixels: parameters.near_blur_radius_factor, //1. / (near_blur_radius_pixels as f32).max(0.0001),
            lowResolutionFactor: LOW_RESOLUTION_FACTOR as f32,
            fieldOfView: camera.fov().unwrap().to_rad().value(),
        };

        gl.with_fbo(&self.fbo_blur_h)
            .draw_vao(&*self.fullscreen_quad, &self.blur_h, &blur_h_uniforms);

        let near_h_tex = self.fbo_blur_h.color_tex(0).unwrap();
        let blur_h_tex = self.fbo_blur_h.color_tex(1).unwrap();
        let blur_v_uniforms = uniforms!{
            maxCoCRadiusPixels: parameters.max_coc_radius_pixels,
            blurSourceBuffer: (blur_h_tex, 1),
            nearSourceBuffer: (near_h_tex, 2),
            // nearBlurRadiusPixels: near_blur_radius_pixels,
            invNearBlurRadiusPixels: parameters.near_blur_radius_factor, //1. / (near_blur_radius_pixels as f32).max(0.0001),
            lowResolutionFactor: LOW_RESOLUTION_FACTOR as f32,
            fieldOfView: camera.fov().unwrap().to_rad().value(),
        };

        gl.with_fbo(&self.fbo_blur_v)
            .draw_vao(&*self.fullscreen_quad, &self.blur_v, &blur_v_uniforms);


        let near_v_tex = self.fbo_blur_v.color_tex(0).unwrap();
        let blur_v_tex = self.fbo_blur_v.color_tex(1).unwrap();
        // let far_radius_scale =
        //     max(camera->depthOfFieldSettings().farBlurRadiusFraction(), camera->depthOfFieldSettings().nearBlurRadiusFraction()) /
        //     max(camera->depthOfFieldSettings().farBlurRadiusFraction(), 0.0001f);
        let composite_uniforms = uniforms!{
            packedBuffer: (blur_source, 1),
            packedBufferInvSize: vec2(1. / color.width() as f32, 1. / color.height() as f32),
            blurBuffer: (blur_v_tex, 2),
            nearBuffer: (near_v_tex, 3),
            farRadiusRescale: 1.,
        };

        gl.with_fbo(&self.fbo_composite)
            .draw_vao(&*self.fullscreen_quad, unsafe{ &*self.composite.get() }, &composite_uniforms);

        self.fbo_composite.color_tex(0).unwrap()
    }
}