Shaders In VTK

From KitwarePublic
Revision as of 04:39, 30 November 2005 by Utkarsh (talk | contribs)
Jump to navigationJump to search

in progress...


This document describes the Shader support in VTK. This is only a preliminary document. We are working on a comprehensive VTK shader guide, which should be available soon.

Hardware shaders are becoming ever popular with the introduction of sophisticated programmable GPUs. With the advent of higher level languages for shader programming such as NVidia's Cg, OpenGL's GLSL (GL Shading Language), it has become easier it harness the power of the GPU. Off late, there has been tremendous interest in using the GPU for general purpose computing (GPGPU) as well.

VTK supports shaders written in both, GLSL as well as Cg. This document does not cover how to write shaders in either language. We discuss, however, ways to apply shaders to VTK geometric objects or actors.


The shaders in VTK are encapsulated in a Material. A material is described in a xml file. The format of this file is discussed later*TODO*). The material description may include

  • vertex/fragment shaders
  • uniform variables passed to the shaders
  • surface properties i.e. attribute values for vtkProperty such as AmbientColor,Specular etc.

To apply a material to an actor, the material must be loaded on the actor's property using vtkProperty::LoadMaterial(const char*). The argument can be a location a xml file describing the material, or name of a material defined by VTK MaterialLibrary (vtkMaterialLibrary). (It can also be a material provided in the Material Repository, but we will discuss that later *TODO*).

When a material is loaded successfully, the vtkProperty is updated using the surface property values defined in the xml. It also instantiates an appropriate vtkShaderProgram subclass, which manages all the shaders defined in the XML. Currently, shaders with different languages cannot be used in the same material. The ShaderProgram compiles the shader code and binds the shaders everytime the actor is rendered.

The uniform parameters passed to the Shaders can be of following types:

  • Parameters whose values are constants defined in the xml: these are identified with xml tag Uniform
  • Parameters whose values are constants defined at runtime. These are called application uniform variables. These are identified by tag ApplicationUniform. The value for Application uniform variables can be set at run time using vtkProperty::AddShaderVariable. (NOTE: we are planning to merge the distinction between Uniform with value specified in xml or at runtime. Once that's done, it will be possible to change the value of any Uniform variable at run time using vtkProperty::AddShaderVariable).
  • Parameters whose values are obtained from the active vtkCamera during rendering: these are identified by xml tag CameraUniform.
  • Parameters whose values are obtained from the vtkProperty to which this material is applied: these are PropertyUniform parameters.
  • Parameters whose values are obtained from the Lights in the renderer: LightUniform.
  • Parameters which are data matrices (or in case of Cg, can be GL state matrices): MatrixUniform.
  • Parameters which are textures/samplers: SamplerUniform.


  • Shader support in VTK is a collaborative effort between Sandia National Labs and Kitware Inc.