Alpha sorting

From TrainzOnline
Jump to: navigation, search

Prior to TS2009, all materials which made use of alpha blending, with a very few exceptions, were passed through a very slow alpha sorting process. This process is necessary to achieve some semblance of correct transparency ordering, however it is a burden on the CPU and results in the geometry being broken up in a manner that gives a very inefficient input to the GPU. There is no real solution to this problem - current rendering technology based on polygon rasterization is fundamentally inefficient when dealing with blended alpha sorting.

Alpha sorting is the process of presorting all alpha blended polygons prior to passing them to the GPU for rendering. This is used for an implementation of the painter's algorithm. It is slow for the following reasons:

  • Alpha sorting occurs on the CPU, which means that the CPU needs to become involved in the rendering of every alpha-sorted polygon, rather than working at the object level.
  • Alpha sorting must be performed per object per frame.
  • Sorting is inherently an expensive operation.
  • Alpha sorting requires splitting an object into component polygons, since objects may intersect each other's bounding space in non-trivial ways. These polygons need to be fed to the GPU piecemeal, significantly increasing the number of draw calls.
  • Alpha sorted geometry cannot be cached on the GPU, since it is being constantly updated by the CPU. It must be sent from the CPU to the GPU during each frame update.


There are several techniques for dealing with this speed penalty:

  • Use opaque or alpha masked materials, instead of alpha blended materials. This needs to be the case for the majority of the scene, because it's the only failsafe case - this means that we should not be making Trees, Fences, etc. using alpha blending.
  • Avoid the need for sorting when using alpha blending, by ensuring that overlapping alphas do not occur. For example, by creating a model than includes an alpha blended material but has an opaque material behind the alpha blend such that the object as a whole is opaque regardless of the viewing angle.
  • Avoid the need for sorting when using alpha blending, by ensuring that any overlapping alphas are in a small enough area of the screen, or are unlikely enough to not be a concern. For example, glass windows on a vehicle may be created as outward-facing such that looking into one window uses alpha blending for only that window, and not for the opposite window. This will still fail if you look through a vehicle into another vehicle's window, but that is a significantly less likely occurrence.
  • Use alpha blending carelessly, and live with the visual artifacts.
  • Use alpha blending with alpha sorting, and live with the performance hit. This is also likely to interact poorly with any non-sorted alpha blends in the scene.
Personal tools