Shaders In VTK

From KitwarePublic
Jump to navigationJump to search

in progress...


This document describes the Shader support in VTK. In 2015 we created a new OpenGL backend fully based on shaders. This is the default backend for VTK versions 7.0 and later. You can extend some mapper's shaders using methods the mapper's provide. You can also subclass to create your own mappers that have custom shaders. VTK guarantees you will have at least OpenGL version 2.1 with the gpu_shader4 extension or version 3.2.


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.


  • When a material is unloaded, if any PropertyUniforms are present in the material description, they remain after the shader has been unset i.e. there is not way to unload a material and return to the state of the property/actor before the material was loaded. The correct behaviour is that the property values return to the ones which the user set last (or those which we present before the material was loaded) when a material in unloaded.


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

VTK: [Welcome | Site Map]