This article describes how shading is performed in V-Ray and how shaders interact with lights.
Photorealistic rendering is all about light and its interaction with the surfaces in the scene. As such, there are two main components to this: describing the light sources in the scene, and the surface properties that determine how lights interact with them.
In traditional rendering systems, surface properties are described with shaders, which have the task of computing the light bouncing off the surface towards the camera. In this architecture, the shader is a "black box" to the rendering system - the rendering system doesn't know how much light is being bounced, or if the shader traces additional rays for things like reflection and refraction. Shaders calculate all of the lighting themselves.
However, some light transport algorithms (e.g. photon mapping, bi-directional path tracing) need to know more about the surface - for example, how it will react if illuminated from a specific direction, or what reflection/refraction rays are traced. Further on, some algorithms need to perform additional processing for each illuminated surface point - for example, recording photon hits in photon mapping. The traditional "black box" shader architecture does not allow for these extended capabilities, thus severely limiting the rendering system with respect to the light transport algorithms that can be implemented.
The shading system implemented in V-Ray tries to avoid these limitations. This is done by removing the actual illumination code from the shaders and implementing it in the renderer itself - shaders do not have direct access to the scene lights. They only provide the relevant information to the rendering system, so that the illumination calculations can be performed. In this way, the rendering system has full control to choose different lighting algorithms depending on the particular situation in which the shading occurs, and to implement additional algorithms in the future without requiring modifications to existing shaders. At the same time, the V-Ray architecture is flexible enough to allow special non-physical shading procedures, if necessary.
This is achieved by splitting the shading calculations between two classes - top-level material classes which are directly called by V-Ray, and BRDF classes which describe the surface properties. When V-Ray needs to calculate the appearance of a surface, it calls the material attached to that surface. The material typically creates a BRDF object and calls V-Ray to evaluate the appearance of the surface based on the BRDF and the scene lights.
Three classes from the V-Ray SDK are directly related to surface shaders: class MaterialInterface, class BSDFInterface and class BRDFInterface. The class MaterialInterface manages the surface appearance on a higher level (e.g. determining which BSDF must be used at a particular shading point). The class BSDFInterface and class BRDFInterface on the other hand, describe in detail how the surface interacts with the light falling on it.
Materials have only a single method to implement: void shade(VRayContext &rc); upon return from this method, the shading result must be stored in the mtlresult member of the ray context. Usually, materials only create an instance of class BSDFInterface and call the VRayContext::evalLight() method passing the new BSDF as a parameter. By default, the VRayContext::evalLight() method calls the VRayGlobalLightManager::evalLight() method of the global light manager stored in the sequence data. The global light manager has the uneasy task of calculating all scene lighting reflected from the scene towards the viewer through the given BSDF.
Evaluating the light reflected from the surface towards the viewer is essentially performed by integrating the rendering equation over the (hemi)sphere around the surface point. There are two major ways to do that: either by tracing rays from the scene lights towards the surface and adding their contribution, or by tracing rays from the surface towards the scene and gathering the illumination. Some surface properties are better captured by the first method (e.g. direct diffuse illumination), while other effects (e.g. reflection and refraction) are better captured by the second method.
V-Ray allows the usage of both methods. The V-Ray global light manager performs the first (light-shooting) method, since it keeps information about the lights in the scene and the global illumination at the shaded point. On the other hand, the BRDF itself must manage the second method, since it can use importance sampling to obtain better results (e.g. rays are traced in the reflection/refraction direction etc). V-Ray uses a technique called multiple importance sampling to combine the results of these two methods.
The actual visual properties of the surface are determined by the BRDF (e.g. how much light is reflected diffusely, what is the specularity etc).
Lights in V-Ray have the task of determining the amount of direct illumination reaching a given surface point. Light objects must support the class LightInterface extension (the respective plugin class is class VRayLight). The lights must support direct evaluation with the LightInterface::eval() method, and optionally - the photon tracing methods if the light supports photon tracing and bi-directional path tracing.