Friday, August 7, 2009

What is a shader?

This is another in the series of mini-tutorials on Graphics for Digital TV blog, aimed at newcomers to the graphics world in digital television. This one discusses shaders as they are relevant to DTV. Shading and shading languages are a much broader issue in computer graphics but in DTV for now anyway, the following information is relevant.

Todays computer graphics hardware is based around triangles. They must be moved in 3D space, placed correctly on the 2D screen taking into account perspective, then coloured correctly according to surface properties, textures and so on. From a top level view hardware today splits this into two parts: the transformation and projection of triangles, and the colouring of pixels those triangles affect. Until recently the two stages were done by fixed function hardware - silicon laid out to do one job only as fast as possible.

Shader hardware is a mini CPU buried inside fixed function hardware. It is a CPU geared toward doing what graphics requires and is optimised for this purpose. As such it is slightly slower than traditional silicon but faster than a general purpose CPU.

Point 1: Shaders require specialised hardware to run.


Shaders are programs which are compiled and loaded to this mini-CPU inside the traditional graphics hardware. In the DTV world two types of shaders will be available in the near future: one for transforming triangles and one for colouring pixels. Exactly correspoding to the two stages of traditional graphics hardware. These are known as vertex and fragment shaders respectively in OpenGL parlance. The language they are written in is known as GLSL (GL shader language, usually shortened to GLslang in speech).

In the PC world there is a third type, geometry shaders but these are not available in set top boxes, to my knowledge, next year.

Point 2: Shaders are software graphics routines written in a special language, that can run at hardware speeds. One type transforms 3D objects, one type colours pixels.

A shader then is a piece of specialised code which is compiled, linked and loaded to the hardware at RUN TIME. The shader itself is stored as text within a normal program and, to repeat, compiled at run time using calls to OpenGL to compile the shader code. It is unusual as this is code within code but the Khronos group claim this allows for more optimisation of the target code.

It is super fast and completely flexible within the contraints of useful graphics functions at least. The shader code replaces fixed function graphics hardware when it is present and this is important because it means standard functionality disappears when the graphics code is run. Those lights you carefully added, no longer work as soon as you use shaders unless you hand code the necessary lighting equations! This means using shaders requires very deep knowledge of graphics theory.

Point 3: Writing shaders is difficult compared with writing traditional graphics and requires knowledge of computer graphics theory.

Shader hardware is a maturing technology. Implementations are changing quickly and the power of shaders (e.g. how many pixels they can fill) varies enormously from generation to generation. It is not at all clear how powerful shaders will be on set top boxes yet especially with the power contraints faced by the platform. It is unlikely they will be able to replace traditional filters (comb filters in TVs for example) for quite some time yet.

Point 4: Even if shader hardware is available, does not mean it is powerful enough!

So why are shaders of interest? Long term the answer is obvious. Imagine being able to filter video images for edge enhancement, noise reduction, colour gain and contrast enhancement, and then place that video on a surface that looks like glass complete with refractions and reflections in the middle of a user interface. Its all possible with shaders.

Short term, some carefully chosen special effects, perhaps prepackaged in libraries, will be possible. Contrast enhancement, picture warmth, non-linear colour response curves should all be possible next year. Useful for highlighting areas of a user interface at the very least.

In addition, shader hardware is required to be OpenGL-ES compatible so from a marketing point of view, shader hardware is interesting, even if the reality could be that it is not so useful at present.

Conclusion

For now, shaders are of academic interest to the DTV world which uses complex filters in hardware beyond the capability of first generation shader hardware. However, the fact that standards require them means they are inevitable and in the long term they will become very useful.

More information about shaders can be found at Lighthouse.

No comments:

Post a Comment