Lesson 5 Screen-space ambient occlusion (SSAO) Depth of field (DoF) PV227 – GPU Rendering Jiˇrí Chmelík, Jan ˇCejka Fakulta informatiky Masarykovy univerzity 17. 10. 2016 PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 1 / 24 Ambient occlusion Ambient occlusion (HBAO+ technique, from GeForce.com) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 2 / 24 Principles of screen-space ambient occlusion Basic principle look at each pixel’s neighbourhood and estimate the occlusion PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 3 / 24 Sampling the neighbourhood Sampling points in the hemisphere Image from learnopengl.com PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 4 / 24 Tangent space for the hemisphere Basic position p0 = (x, y, z) Tangent space (t, b, n) p = C + xt + yb + zn PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 5 / 24 Finding the tangent and bitangent Choose a random t0 in xy plane in view space Compute b = n × t0 Compute t = b × n PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 6 / 24 Comparing the distance Compare the distances from the viewer of: sample (occludee) object visible at that point (occluder) a) b) c) d) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 7 / 24 Blurring the result Without blur With blur PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 8 / 24 Task: Evaluate SSAO Task 1: Evaluate SSAO Don’t worry, a lot of things is already done. G-buffer already contains positions and normals in view space. Random positions of samples are already computed and stored in a UBO. Random directions for the tangents are already computed and stored in a texture. Blurring of SSAO texture is already implemented. Computation of the tangent and bitangent is already done. Computation of the position of the sample and the position of the possible occluder is already done. In evaluate_ssao_fragment.glsl, compute the distance of the possible occluder and the sample (occludee) from the camera. Implement comparing cases a), b), and c). PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 9 / 24 Task: Evaluate SSAO Result PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 10 / 24 Task: Apply SSAO Task 2: Apply SSAO to the lighting In evaluate_lighting_fragment.glsl, apply the value in texture ssao_tex in the computation of lighting. PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 11 / 24 Task: Apply SSAO Without SSAO With SSAO PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 12 / 24 Task: Improve SSAO Task 3: Implement comparison case d) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 13 / 24 Task: Improve SSAO Without comparison d) With comparison d) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 14 / 24 Other methods for ambient occlusion Screen-space Directional Occlusion (SSDO) Horizon based: HBAO, HBAO+ Voxels: VXGI PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 15 / 24 Depth of Field Depth of field (Source Wikipedia) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 16 / 24 Circle of confusion Objects out of focus appears as circles (circle of confusion) Radius of CoC can be calculated from the parameters of the camera Circles of confusion (Source Wikipedia) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 17 / 24 Our simple solution A simple version: Linearly increase the blur radius, up to some maximum PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 18 / 24 Task: Evaluate Depth of Field Task 4: Evaluate DoF Shader dof_fragment.glsl already blurs the image using gaussian blur. Compute width of the blur and store it into blur_width. The distance of objects in focus is in uniform variable focus_distance. PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 19 / 24 Task: Evaluate Depth of Field Close focus Middle focus Far focus PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 20 / 24 Other methods for DoF Splat circles or polygons, one per pixel → create bokeh Bokeh (Source Wikipedia) PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 21 / 24 Other methods for DoF Render the scene multiple times, each time with a slightly shifted camera, and average the results Source OpenGL Programming Guide PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 22 / 24 Other methods for DoF Render multiple layers, blur each layer uniformly In raytracing, simulate the optics of the camera. PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 23 / 24 Things we used textureLod Just like texture, but explicitely states LOD for mipmap. Necessary when LOD cannot be computed automatically (in divergent code like if’s, for’s etc.) textureSize Returns the size of given mipmap level of the texture gl_FragCoord Only in fragment shaders .xy contain the coordinate of the fragment on the screen .z contains the depth to be stored into depth buffer .w contains 1/w of gl_Position variable, after interpolation. PV227 – GPU Rendering (FI MUNI) Lesson 5 – SSAO and DoF 17. 10. 2016 24 / 24