Shaders

From C4 Engine Wiki
Jump to: navigation, search

In the C4 Engine, shaders are created internally based on settings specified in the Material Manager window and the new Shader Editor. Due to a variety of very practical issues that are discussed in this article, shader code written in a language such as Cg, HLSL, or GLSL cannot be used. Instead, the material system in C4 is designed to satisfy the following requirements.

  • All materials should function on all platforms supported by the engine, within the limits of the available graphics hardware. The user should not have to specify different versions of a material for different platforms.
  • All materials should render correctly inside the many lighting environments support by the engine, which include several different light types, multiple ambient environments, and multiple types of fog. All materials should also work without modification when new lighting environments are added in the future.
  • It should be possible to replace the graphics library used by the engine without the need to update any materials. That is, the user should not need to know anything about the underlying graphics library or the native shading language that it supports.

The C4 Material Manager can be used to create a wide variety of materials, and all of them satisfy the above requirements. The new Shader Editor opens the possibility to create a virtually limitless numbers of custom shaders in such a way that they are cross-platform and future compatible.

Combinatorial Shader Explosion

Every modern rendering engine must deal with something known as the “Combinatorial Shader Explosion” problem. Basically, this problem refers to the fact that it's possible to create an enormous number of shaders through various combinations of material properties and lighting environments, and it wouldn't be even remotely practical to write shader code for all of them ahead of time. In C4, it is possible to generate over 4 billion different materials through fixed attribute settings in the Material Manager. Each of these materials can be applied to objects that are rendered under 8 different types of lights and 3 different types of fog. Thus, the engine must be able to correctly render objects using shaders from a potential set of over 96 billion combinations of material properties, light types, and fog types. With the new Shader Editor, the number of possible materials becomes unbounded, but our advanced shader compiler ensures that each one works properly in all environments supported by the engine and on all platforms supported by the engine.

C4 handles the combinatorial shader explosion problem by generating shader code as it is needed, on the fly. Whenever a new combination of material, light type, and fog type is encountered, the engine examines all of the properties of the material and assembles a new shader using small snippets of code that perform specific functions. That shader is then cached in memory and used to render all objects that require the same material within the same type of lighting environment. Only the shaders that are actually needed for rendering ever get generated.

If it were possible to import text-based shader files, then 24 variations of the shader would be required to support the 8 types of lights and 3 types of fog supported by the engine. The biggest disadvantage to using text-based shaders, however, is that new light types or fog types added in the future would not be supported until new variations of the shader were created. Under the system used by C4, this is not an issue—all materials will work correctly with any new types of light or fog added to the engine in the future.

Graphics Library Abstraction

The C4 Engine currently runs on three different platforms (PC, Mac, and PlayStation 4), and support for additional platforms is planned. These platforms use different native graphics libraries that support different shading languages. In the case of GLSL, the shading language frequently changes as OpenGL is updated, so various changes need to be made to the native code passed to the driver. C4 is designed so that the user is not required to have any knowledge of the underlying graphics library or the shading language that it uses. A material created on one platform will work correctly on all platforms because the engine generates the correct shader code for whichever platform it is currently running on. If it were possible to import text-based shader files, then this would no longer be the case. Separate shaders would have to be written for each platform, and they would require maintenance as support for newer versions of OpenGL was added to the engine.

Furthermore, under the current design of the C4 Engine, it is possible for us to completely replace the underlying graphics library without any change to the materials being necessary. For example, we could switch from OpenGL to DirectX 10/11 on Windows, and not a single shader would have to be rewritten by the user.

See Also