Overview

In my Master's Degree project Bound by Death, we needed a good atmosphere for our dark fantasy game, and following our teacher's advice, we decided that we wanted to have PBR for realistic lighting in order to make the best atmosphere possible.

Another classmate and I took on the task, where the objective was to replace the basic Phong lighting that we had at the beginning with a complete forward PBR pipeline, with support for base color, metallic, roughness, normal map, emissive and AO textures, and compatible with our current light game objects: Point Light, Spot Light and Directional Light.

Problems and objectives

Since we had a basic Phong lighting system, we only had support for base color textures and for metallic and roughness factors, but nothing more, so with that in mind, we started to plan the tasks.

For the first task, my classmate and I worked together.

For the second, we worked together on implementing the metallic-roughness calculations, and then I supported my classmate with the implementation of the normal calculations, since I moved on to another task.

Technical implementation

First task: Support for new textures.

The first thing we did was to modify the material data struct that we send to the pixel shader. Before, it only had:

So first, we decided to use Unity's texture channel standard for metallic, roughness and AO textures.

Then, in the material data struct, we added bools to indicate whether the material contains a metallic/roughness/AO texture, a normal map texture and an emissive texture, and two values, the normal factor and the emissive color value, so the final struct contains:

With the new material data, we modified the creation of the material from a glTF file to load the new textures and generate the updated material data.

Then we modified the Root Signature of the pipeline to support the new textures inside the Descriptor Table for Shader Resource Views that we already had.

Finally, we modified the material cBuffer in the HLSL code to match the new material data struct, and added the registers for the new textures.


Second task: Normal and light calculations.

Following our teacher's advice and class materials, we implemented the Cook-Torrance BRDF model:

After that, I moved on to another task, but I supported my classmate with the normal unpacking and the transformation from tangent space to world space.

Challenges and problem-solving

We had two major issues during this development:


Texture allocation.

Before this implementation, we only had one texture per mesh, so we just stored the SRV and used its GPU handle to send it to the shader, but now we needed all the textures to be contiguous in memory.

For this, we thought about a reusable way to allocate a block of memory of configurable size inside a heap and get the CPU and GPU handles to the first element of that block.

Finally, another classmate who was already working on the views made this implementation.


Color space problems with textures.

When supporting my classmate with the normal calculations, we faced a problem where, when applying the tangent space to world space transformation of the normals, the light affecting the meshes was distorted.

The problem was that, in the texture loading process, all textures were being initialized as sRGB textures, and then, depending on the type of texture, the color space was changed to linear. This changed the colors of the normal map texture, so the resulting normal vectors were shifted.

To fix this, we changed the texture loading process to initialize each texture with the color space specified in the glTF file.

Takeaways and next steps

This was my first complex graphics task, and it is what sparked my interest in graphics programming.

With it, I learned how light is simulated on a computer and the math behind it, and how to send data to the shaders efficiently through the Root Signature.

The implementation of PBR for Bound by Death is finished, but I plan to implement it again in my own engine, where I will add support for more features such as opacity, anisotropy or subsurface scattering.