Physically Based Rendering
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.
- First task: Add support for metallic, roughness, normal map, emissive and AO textures.
- Second task: Implement normal calculations and the new render equation on the Pixel Shader.
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:
- The base color value.
- If the material contains a base color texture.
- Metallic factor.
- Roughness factor.
- 8 bytes of padding to align memory with HLSL cBuffers.
So first, we decided to use Unity's texture channel standard for metallic, roughness and AO textures.
- R channel is AO.
- G channel is roughness.
- B channel is metallic.
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:
- The base color value.
- If the material contains a base color texture.
- Metallic factor.
- Roughness factor.
- If the material contains a metallic/roughness/AO texture.
- Normal factor.
- If the material contains a normal map texture.
- Emissive color value.
- If the material contains an emissive texture.
- 12 bytes of padding to align memory with HLSL cBuffers.
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:
- Schlick's Approximation for the Fresnel effect.
- Smith Masking Function for microfacet visibility.
- GGX Normal Distribution Function for microfacet distribution.
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.