Overview

In my Master's Degree project Bound by Death, after implementing PBR, we got serious performance problems. This was caused by the large number of meshes that we had to render, due to the level being made from small modular pieces.

We decided to implement deferred rendering to minimize the number of light calculations per frame, and a classmate took the task, but after some time he asked me for help adding the normal map, emissive and AO GBuffers, and I ended up finishing the implementation myself.

Problems and objectives

I planned the following tasks:

Technical implementation


First task: Add normal map GBuffer.

First, I modified the Geometry Pass, which contains the pipeline that generates the GBuffers, and added a new Render Target that will store the normal map of the scene.

Then, I modified the root signature to be able to send the normal map textures of the meshes to the pixel shader.

Next, I took the normal calculations that we already had in the forward rendering pipeline, implemented them in the pixel shader, and stored the final calculated value in the new Render Target.

With the Geometry Pass finished, I modified the Deferred Shading Pass, which contains the pipeline that calculates lighting from the GBuffers, to send the new normal map GBuffer to the pixel shader.

Finally, I used the normal coming from the GBuffer to calculate lighting instead of the polygon normal.


Second task: Add emissive GBuffer.

I followed the same steps as I did with the normal map, but with the emissive texture.

  1. Create a new Render Target to store the emissive GBuffer.
  2. Modify the root signature to send the textures to the pixel shader.
  3. Sample the emissive texture into the new Render Target.
  4. Modify the Deferred Shading Pass to send the new emissive GBuffer to the pixel shader.
  5. Add the emissive color to the final color calculated from the lighting.

Third task: Add Ambient Occlusion to the Metallic Roughness GBuffer.

For the AO, I modified the Geometry Pass to send the AO textures to the pixel shader.

Then, I sampled the texture into the R channel of the Metallic Roughness GBuffer. This is to maintain how the textures were used in the previous forward rendering pipeline, so we can reuse as much code as possible.

And finally, I modified the pixel shader of the Deferred Shading Pass to sample the AO from the R channel of the Metallic Roughness GBuffer and use it to dim the specular and diffuse results of the PBR calculation.

Challenges and problem-solving

I had one major problem during this development:


Major performance issues.

When I took over the task from my classmate, he told me that, for some reason, the performance using the Deferred Shading Pipeline was even worse than using forward rendering with the PBR calculations.

I used PIX to compare the Deferred Shading Pipeline and the Forward Rendering Pipeline, and found out that there was an excessive number of draw calls in the Deferred Shading Pipeline.

After checking the Deferred Pipeline, I found a nested loop in the code that creates the draw calls for the meshes: the outer loop iterated over all the scene meshes, and for each one, the inner loop iterated over all the meshes again and submitted the draw commands. This caused a quadratic growth of draw calls: 3 meshes generated 9 draw calls, and 10 meshes generated 100.

To fix it, I removed one of the loops, which directly improved the performance from around 10 FPS to 400+ FPS with the whole first part of the first level being rendered at once.

Takeaways and next steps

With this task, I learned how Deferred Shading works and how it improves performance in a clever way.

In the future, continuing with the render pipeline optimization, I would like to implement light culling, since in Bound by Death, another classmate took that task and I didn't have the opportunity to implement it myself.