Real-Time Lighting Effects Using Deferred Shading
Total Page:16
File Type:pdf, Size:1020Kb
Real-time Lighting Effects using Deferred Shading Michal Ferko∗ Supervised by: Michal Valient† Faculty of Mathematics, Physics and Informatics Comenius University Bratislava / Slovakia Abstract We are targeting OpenGL 3 capable hardware, because we require the framebuffer object features as well as mul- Rendering realistic objects at interactive frame rates is a tiple render targets. necessary goal for many of today’s applications, especially computer games. However, most rendering engines used in these games induce certain limitations regarding mov- 2 Related Work ing of objects or the amount of lights used. We present a rendering system that helps overcome these limitations There are many implementations of Deferred Shading and while the system is still able to render complex scenes at this concept has been widely used in modern games [15] 60 FPS. Our system uses Deferred Shading with Shadow [12] [5], coupled with techniques used in our paper as well Mapping for a more efficient way to synthesize lighting as certain other. coupled with Screen-Space Ambient Occlusion to fine- Deferred Shading does not directly allow rendering of tune the final shading. We also provide a way to render transparent objects and therefore, we need to use a differ- transparent objects efficiently without encumbering the ent method to render transparent objects. There are several CPU. approaches to hardware-accelerated rendering of transpar- ent objects without the need to sort geometry. This group Keywords: Real-time Rendering, Deferred Shading, of algorithms is referred to as Order-Independent Trans- High-dynamic range rendering, Tone-mapping, Order- parency. Independent Transparency, Ambient Occlusion, Screen- An older approach is Depth Peeling [7] [4], which re- Space Ambient Occlusion, Stencil Routed A-Buffer quires N scene rendering passes to capture N layers of transparent geometry. Dual Depth Peeling [1] improves the algorithm by capturing two layers of geometry in 1 Introduction one pass. However, the objects still need to be rendered multiple times and the performance is still unacceptable Our rendering engine is based on concept of Deferred for large scenes. Once the layers are captured, a final Shading [3], which avoids shading occluded pixels and by fullscreen pass blends them together. postponing the lighting evaluation allows one pixel to be A newer approach, Stencil Routed A-Buffer [10], al- affected by hundreds of lights. lows capturing of up to 32 layers during one rendering pass Our system uses HDR rendering coupled with the tone- thanks to multisample render targets on OpenGL 3 hard- mapping operator by Reinhard et al. [11] and Bloom, ware. An additional pass for sorting the values is used. Shadow Mapping [16] to provide hard-edged shadows, This approach is part of our system. Screen Space Ambient Occlusion to simulate indirect With OpenGL 4 hardware, it is possible to actually have lighting and Stencil Routed A-Buffer [8] to render trans- per-pixel linked lists [13] and thus generate an arbitrary parent objects. All these techniques allow easy integration number of layers and sort these samples afterwards in a into a deferred renderer while providing much more real- fullscreen pass. This approach is very similar to Sten- istic display of scenes. cil Routed A-Buffer except for the way the samples are The main contribution of this paper is a complete stored. We did not use this approach due to the lack of rendering pipeline incorporating these well-known tech- OpenGL 3 support. niques. Our aim is to determine in which order these tech- To further improve the visual quality of rendered im- niques should be applied to avoid incorrect artifacts and ages, we include standard Shadow Mapping [16] for how to maintain reasonable quality while allowing real- shadow-casting lights and real-time Ambient Occlusion. time display even on older hardware. There has been much research done regarding real-time Ambient Occlusion. In [2], the authors convert polygonal ∗[email protected] meshes into disks, for which the occlusion computation †[email protected] is simplified and allows dynamic rendering. In [6], the au- Proceedings of CESCG 2012: The 16th Central European Seminar on Computer Graphics (non-peer-reviewed) Figure 1: Images of test scenes rendered with our system at interactive frame rates. Dragon scene (Left), House scene (Middle) and Sponza scene (Right) thors propose a method to generate fields around objects in on the screen, but usually more post-processing steps are pre-processing. As long as the objects are not deformed, executed after this pass and the results should instead be the fields do not need to be recomputed. It thus allows rendered into a texture - the lighting buffer (L-Buffer). real-time estimation of occlusion. A similar technique [8] When rendering light shapes, we use front-face culling performs a slightly different field generation in the geome- to avoid problems when the camera is inside a light vol- try shader and allows for fully-dynamic ambient occlusion ume. Furthermore, for every pixel getting rendered, it is even on deformable meshes. needed to reconstruct the eye-space position correspond- Our work is based on Screen-Space Ambient Occlusion ing to the current pixel position and the depth stored in [9] which uses the scene depth information captured dur- the G-Buffer. For this position, we calculate whether it ing the first stage of deferred shading to approximate scene actually is inside the light volume, since we might be ren- geometry and thus compute occlusion. The choice was dering nearby light volumes while the G-Buffer contains made mainly due to the fact that the previous methods are information about distant objects unaffected by the light. scene-dependent and perform slower than SSAO when the Deferred Shading mainly outperforms Forward Shading scene contains hundreds of thousands of triangles. SSAO’s when there are many lights that do not cover large por- performance depends only on the number of samples taken tions of the screen when being rendered. Many directional and the screen resolution, being totally independent from lights (which affect the entire screen) pose a problem for the actual scene. Deferred Shading, because we do extra work when com- pared to Forward Shading. Therefore, some implementa- tions evaluate directional lights using Forward Shading [5] 3 Deferred Shading and all other lights using Deferred Shading. In a typical outdoor scene there is usually one direc- Deferred Shading [3] is an alternative to Forward Shading, tional light representing the sun and in indoor scenes, di- the traditional rendering where a fragment shader accesses rectional lights are avoided altogether. Our system cal- all light information at once and outputs the final light con- culates directional lights with forward shading during the tribution directly into the window’s framebuffer. G-Buffer generation phase. Due to this fact, only a fixed The main idea of Deferred Shading is separation of the amount of directional lights can be forward shaded, for lighting calculations from the scene rendering pass (or more lights the system would have to switch to deferred the geometry pass). During this pass, material and ob- shading for the additional lights. ject properties (usually albedo, depth, normal and specular power) are stored into a geometry buffer (G-Buffer). 3.1 HDR and Tone-mapping When compared to forward rendering or multi-pass ren- dering, the scene is rendered only once and only the frag- Having evaluated lighting in a texture adds direct support ments that are visible are shaded. No shading needs to be for HDR rendering. Performing tone-mapping during the evaluated for objects that are not affected by a certain light L-Buffer generation phase is not possible, since the results (the object is outside of the light volume - part of the scene get additively blended and we cannot guarantee that a pixel that is affected by the light). will not be affected by a large number of lights, resulting During the consecutive lighting pass, the light volumes in a sum of many tone-mapped values which result in a are rendered (cones for spot lights and spheres for point luminance value higher than 1. lights) and during the fragment shader execution, the G- Our system is open to many tone-mapping operators. Buffer data is read and used to synthesize lighting. The Currently, we are using the global part of the operator by light shapes are rendered with additive blending thanks to Reinhard et al. [11]. As an input into the tone-mapping the additive nature of light. The results can be displayed stage, we have the L-Buffer which contains 16-bit floating Proceedings of CESCG 2012: The 16th Central European Seminar on Computer Graphics (non-peer-reviewed) 3.3 Shadow Mapping Incorporating Shadow Mapping into a Deferred Renderer is straightforward and allows for hard-edged shadow with- out much effort. Shadow Mapping [16] is a fully GPU accelerated method for rendering real-time shadows. The entire scene is rendered one additional time (from the light’s point of view in the light’s direction) for every shadow-casting Figure 2: Real-time Reinhard’s tonemapping. HDR im- light, storing the depth values generated during this ren- age before applying tonemapping (Left) and after applying dering. tonemapping (Right). Afterwards, when evaluating the lighting equation, the world position of the current surface point is projected (us- ing the same projection as was used during the shadow map generation phase and remapping from [−1;1]3 range into [0;1]3) which gives us the (x;y) coordinates in the shadow map and a depth value z. The fragment shader reads the depth value d at position (x;y) in the shadow map and compares it to z. If z = d, the point with depth z is the closest point to the light source and therefore is not shadowed.