1.8.3. Data and Transfer Functions

VolumeViz does most of the work to make data (volumes and transfer functions) conveniently available to shader functions. Custom shaders can be used with a single volume or with multiple volumes. Custom shaders can be used with a single transfer function or with multiple transfer functions. Additional parameters can be sent to shaders using uniform parameters. In this subsection we will discuss what needs to be done in the application code. In the next subsection we will discuss how to access the data in the shader code.

When multiple volumes are used with a custom shader it is assumed that the shader will somehow combine the volumes to produce a single color/opacity value for each voxel. Some guidelines must be followed in order to use multiple volumes:

  • All the SoVolumeData( C++ | Java | .NET ) nodes must be under an SoMultiDataSeparator( C++ | Java | .NET ) node in the scene graph.

    VolumeViz will load data for all the active data sets under the SoMultiDataSeparator( C++ | Java | .NET ) node (active meaning that the SoVolumeData( C++ | Java | .NET ) node is traversed with the current SoSwitch( C++ | Java | .NET ) settings). Loading is synchronized, so the same tiles, at the resolution levels, are loaded for each volume. All the related nodes (range, transfer function, etc) should also be under the SoMultiDataSeparator( C++ | Java | .NET ) node.

  • All the volumes must have exactly the same dimensions (number of voxels in X, Y and Z) and tile size.

  • Each volume must have a unique data set id.

    See the discussion of dataSetId below.

Some additional information:

  • Each volume may have its own resources, set using SoLDMResourceParameters( C++ | Java | .NET ).

    For example the total memory required is the sum of all the volumes.

  • Each volume may have its own data range, set using SoDataRange( C++ | Java | .NET ).

    See the discussion of dataRangeId below.

  • The base material, set with SoMaterial( C++ | Java | .NET ), is associated with the (or with each) rendering node.

  • The rendering effects, rendering mode, etc, set with SoVolumeRenderingQuality( C++ | Java | .NET ), are associated with the (or with each) rendering node.

  • Multiple transfer functions (color maps) may be loaded. The shader will choose which one(s) to use.

    See the discussion of transferFunctionId below.

Each data set (SoVolumeData( C++ | Java | .NET ) node) to be combined must have a unique id. The id is normally specified by setting the dataSetId field. The default value is 1. In order to support multiple data sets (volumes), shaders use this id when requesting a voxel value or other information from a data set. For a single data set, application shaders can use the predefined uniform parameter VVizDataSetId. VolumeViz automatically sets this parameter to the id of the primary volume. The primary volume is the first volume traversed under the SoMultiDataSeparator( C++ | Java | .NET ). For multiple data sets, the application must explicitly pass the data set id values as uniform parameters.

Only the data sets actually traversed need to have unique ids. For example the application can have multiple data sets with the same id under an SoSwitch( C++ | Java | .NET ) node, as long as only one of them is traversed on any given traversal.

You should be aware that dataSetId also specifies the OpenGL texture unit in which the data textures for this data set will be stored on the GPU. The dataSetId is 1 by default because texture unit 0 is reserved for storing the transfer functions (color lookup tables) by default. The number of available texture units is limited and depends on the hardware. You can query this limit using the static method getMaxNumDataSets() in class SoDataSet( C++ | Java | .NET ).

The data set id can also be specified using an SoDataSetId( C++ | Java | .NET ) node. If an SoDataSetId( C++ | Java | .NET ) node is traversed before the SoVolumeData( C++ | Java | .NET ) node, the id from the SoDataSetId( C++ | Java | .NET ) node is used and the dataSetId field is ignored.


VolumeViz converts all data values, regardless of type, to 8 (default) or 12 bit unsigned integers in the GPU data textures. The 8 or 12 is controlled by the texturePrecision field on the SoVolumeData( C++ | Java | .NET ) node. The scaling uses the min/max values given to the SoDataRange( C++ | Java | .NET ) node, if it exists, else it uses the full range of the actual data type. For rendering purposes this is usually sufficient and maximizes the amount of data that can be loaded in the available memory on the GPU. (But note that the 12-bit option actually uses 16-bit textures to store the data on the GPU, so the memory requirement is actually double compared to using 8-bit data.) There are two issues to consider.

First, for 8-bit data (and for 12-bit data when texturePrecision is set to 12), the values stored on the GPU are the actual data values. However for larger data types, some range of actual data values is usually "aliased" onto each GPU data value. For example, all the data values in the range 32 to 47 might end up as 32 in the GPU data. In summary, the default data storage allows 256 distinct values on the GPU and 12-bit storage allows 4096 distinct values.

Second, GLSL always returns the voxel value from the GPU data texture as a floating point number in the range 0..1. This is true even for 8-bit data. So if your shader needs to know the actual data value, it needs to convert the 0..1 value either by knowing the actual data range or getting the actual data range through some uniform parameters.

Computing the value that the shader will see for a particular data value is straightforward, but remember to include the truncation that will occur when converting to unsigned integer in the data texture. The data min and max values are either the values specified to SoDataRange( C++ | Java | .NET ) or the min and max of the volume’s data type (e.g. 0 and 65535 for the unsigned short data type):


RGBA data is a special case, but quite different. If you give VolumeViz a volume containing 32-bit RGBA values, it will store those 32-bit values on the GPU. Of course in this case there is no color map.

When using multiple volumes, a single SoDataRange( C++ | Java | .NET ) node can be used to specify a data range that applies to all volumes. However each volume may have its own separate data range. This is important because the data range specifies the range of values that will be scaled into the data values on the GPU. Seismic attribute volumes and medical volumes from different modalities generally have different data ranges. In this case, create an SoDataRange( C++ | Java | .NET ) node for each volume and set the dataRangeId equal to the dataSetId of the corresponding SoVolumeData( C++ | Java | .NET ) node.


Each transfer function (SoTransferFunction( C++ | Java | .NET ) node) to be combined must have a unique id. The id is specified by setting the transferFunctionId field. The default value is 0. Shaders, for example VVizComputeFragmentColor(), use this id when requesting a color value from the VVizTransferFunction() method. VolumeViz will load all the transfer functions under the SoMultiDataSeparator( C++ | Java | .NET ), even if there is only one data set. So another way to switch between different color maps is to send an id as a uniform parameter and use that parameter to call VVizTransferFunction in VVizComputeFragmentColor().

Only the transfer functions actually traversed need to have unique ids. For example the application can have multiple transfer functions with the same id under an SoSwitch( C++ | Java | .NET ) node, as long as only one of them is traversed on any given traversal.

Unlike data sets, all transfer functions are combined into a single data texture. So the transferFunctionId simply identifies the location of a particular color map in the transfer function texture, it does not affect the number of OpenGL texture units needed. By default the transfer function texture is stored in texture unit 0 (which is why data set ids normally start at 1). The texture unit for transfer functions can be changed through SoPreferences( C++ | Java | .NET ) using the variable IVVR_TF_TEX_UNIT, but dataSetId can never be set to the texture unit number used to store the transfer functions. Application shaders should access the transfer functions using the VVizTransferFunction method. At this point voxel values are normalized to the range 0..1 (as discussed above under Data).

Generally the transfer function ids should start at zero and be consecutive values. This is not required, just recommended, because VolumeViz does not compact the range of transfer function ids and the transfer function texture is initialized to zero values. So if the application uses transfer function ids 1 and 5, but the shader does a color lookup using id 3, the resulting color and opacity will be zero. The order of the SoTransferFunction( C++ | Java | .NET ) nodes in the scene graph does not matter. Transfer functions are stored in the texture in the order specified by their id.


Uniform parameters are named values that the application can set and the shader code can use, but not modify. GLSL calls them “uniform” variables because they cannot change during the rendering of a primitive, unlike shader stage outputs (formerly called “varying” variables in GLSL). The application can use these parameters to pass data set ids (as mentioned above), data set min and max values, blend factors or anything else useful in a shader. Use one of the subclasses of SoShaderParameter( C++ | Java | .NET ) to create a uniform parameter. Then add the parameter object to the shader object (usually the SoFragmentShader( C++ | Java | .NET ) object).