Shader pipeline guidelines

Each shader that modifies TShadingSample is responsible for saving and restoring the original input values.

Use virtual functions for variable inquiry. In the previous example, when the drawing port wants to know what variables a shader pipeline needs, it calls ShaderA::NeedNormal. The implementation of NeedNormal (shown in the code below) searches through all of the NeedNormal functions of the shaders in the pipeline and returns a correct Boolean value. The drawing port only calls this function once for a surface.

      virtual Boolean ShaderA::NeedNormal()
      {
          //When NeedNormalForMyself is TRUE, the function returns.
          return NeedNormalForMyself || GetChild()>NeedNormal(); 
      }
Only the terminal node alters the computed color to fResultantColor only. The first shader is allowed to perform postprocessing to modify fResultantColor. This ensures that there is no confusion when an intermediate shader chooses a color between fBaseColor and fResultantColor in the middle of the pipeline.

To ensure that you design exceptionsafe shaders in a shader pipeline, construct a local instance that resides in the implementation of ComputeShade for swapping the variables to be modified. The exception handler calls the destructor when a software exception is raised, but the data in TShadingSample remains valid. The following code example defines the TSwapVariable class and rewrites ShaderB::ComputeShade to do an exceptionsafe swap.

      class TSwapVariables
      {
          TSwapVariables( TShadingSample& shadingSample )
          {
               //Save variables 
          }
      
          ~TSwapVariables()
          {
               //Restore variables 
          }
      };
      
      shaderB::ComputeShade( TShadingSample& sSample, const TSceneBundle& )
      {
          TSwapVariables( TShadingSample* sSample );
          *( sSample.fBaseColor ) *= 3.;
          sSample.fShadingNormal *= 1.;
          fShaderC>ComputeShade( TShadingSample& sSample, const TSceneBundle );
      }


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker