# Shading Language Support

This page describes the features and status of Mesa's support for the OpenGL Shading Language.

Last updated on 28 March 2007.

Contents

- Unsupported Features
- Implementation Notes
- Programming Hints
- Stand-alone Compiler
- Compiler Implementation
- Compiler Validation

## Unsupported Features

The following features of the shading language are not yet supported in Mesa:

- Dereferencing arrays with non-constant indexes
- Comparison of user-defined structs
- Linking of multiple shaders is not supported
- gl_ClipVertex
- The derivative functions such as dFdx() are not implemented
- The inverse trig functions asin(), acos(), and atan() are not implemented
- The gl_Color and gl_SecondaryColor varying vars are interpolated without perspective correction
- Floating point literal suffixes 'f' and 'F' aren't allowed.

All other major features of the shading language should function.

## Implementation Notes

- Shading language programs are compiled into low-level programs very similar to those of GL_ARB_vertex/fragment_program.
- All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full float[4] registers.
- Float constants and variables are packed so that up to four floats can occupy one program parameter/register.
- All function calls are inlined.
- Shaders which use too many registers will not compile.
- The quality of generated code is pretty good, register usage is fair.
- Shader error detection and reporting of errors (InfoLog) is not very good yet.
- The ftransform() function doesn't necessarily match the results of fixed-function transformation.

These issues will be addressed/resolved in the future.

## Programming Hints

- Declare in function parameters as const whenever possible. This improves the efficiency of function inlining.
- To reduce register usage, declare variables within smaller scopes. For example, the following code:

          void main()
          {
             vec4 a1, a2, b1, b2;
             gl_Position = expression using a1, a2.
             gl_Color = expression using b1, b2;
          }

  Can be rewritten as follows to use half as many registers:

          void main()
          {
             {
                vec4 a1, a2;
                gl_Position = expression using a1, a2.
             }
             {
                vec4 b1, b2;
                gl_Color = expression using b1, b2;
             }
          }

  Alternately, rather than using several float variables, use a vec4 instead. Use swizzling and writemasks to access the components of the vec4 as floats.
- Use the built-in library functions whenever possible. For example, instead of writing this:

              float x = 1.0 / sqrt(y);

  Write this:

              float x = inversesqrt(y);

- Use ++i when possible as it's more efficient than i++

## Stand-alone Compiler

A unique stand-alone GLSL compiler driver has been added to Mesa.

The stand-alone compiler (like a conventional command-line compiler) is a tool that accepts Shading Language programs and emits low-level GPU programs.

This tool is useful for:

- Inspecting GPU code to gain insight into compilation
- Generating initial GPU code for subsequent hand-tuning
- Debugging the GLSL compiler itself

To build the glslcompiler program (this will be improved someday):

        cd src/mesa
        make libmesa.a
        cd drivers/glslcompiler
        make

Here's an example of using the compiler to compile a vertex shader and emit GL_ARB_vertex_program-style instructions:

        glslcompiler --arb --linenumbers --vs vertshader.txt

The output may look similar to this:

    !!ARBvp1.0
      0: MOV result.texcoord[0], vertex.texcoord[0];
      1: DP4 temp0.x, state.matrix.mvp.row[0], vertex.position;
      2: DP4 temp0.y, state.matrix.mvp.row[1], vertex.position;
      3: DP4 temp0.z, state.matrix.mvp.row[2], vertex.position;
      4: DP4 temp0.w, state.matrix.mvp.row[3], vertex.position;
      5: MOV result.position, temp0;
      6: END

Note that some shading language constructs (such as uniform and varying variables) aren't expressible in ARB or NV-style programs. Therefore, the resulting output is not always legal by definition of those program languages.

Also note that this compiler driver is still under development. Over time, the correctness of the GPU programs, with respect to the ARB and NV languagues, should improve.

## Compiler Implementation

The source code for Mesa's shading language compiler is in the `src/mesa/shader/slang/` directory.

The compiler follows a fairly standard design and basically works as follows:

- The input string is tokenized (see grammar.c) and parsed (see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST). The nodes in this tree are slang_operation structures (see slang_compile_operation.h). The nodes are decorated with symbol table, scoping and datatype information.
- The AST is converted into an Intermediate representation (IR) tree (see the slang_codegen.c file). The IR nodes represent basic GPU instructions, like add, dot product, move, etc. The IR tree is mostly a binary tree, but a few nodes have three or four children. In principle, the IR tree could be executed by doing an in-order traversal.
- The IR tree is traversed in-order to emit code (see slang_emit.c). This is also when registers are allocated to store variables and temps.
- In the future, a pattern-matching code generator-generator may be used for code generation. Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for patterns in IR trees, compute weights for subtrees and use the weights to select the best instructions to represent the sub-tree.
- The emitted GPU instructions (see prog_instruction.h) are stored in a gl_program object (see mtypes.h).
- When a fragment shader and vertex shader are linked (see slang_link.c) the varying vars are matched up, uniforms are merged, and vertex attributes are resolved (rewriting instructions as needed).

The final vertex and fragment programs may be interpreted in software (see prog_execute.c) or translated into a specific hardware architecture (see drivers/dri/i915/i915_fragprog.c for example).

### Code Generation Options

Internally, there are several options that control the compiler's code generation and instruction selection. These options are seen in the gl_shader_state struct and may be set by the device driver to indicate its preferences:

    struct gl_shader_state
    {
       ...
       /** Driver-selectable options: */
       GLboolean EmitHighLevelInstructions;
       GLboolean EmitCondCodes;
       GLboolean EmitComments;
    };

- EmitHighLevelInstructions
  This option controls instruction selection for loops and conditionals. If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK instructions will be emitted. Otherwise, those constructs will be implemented with BRA instructions.
- EmitCondCodes
  If set, condition codes (ala GL_NV_fragment_program) will be used for branching and looping. Otherwise, ordinary registers will be used (the IF instruction will examine the first operand's X component and do the if-part if non-zero). This option is only relevant if EmitHighLevelInstructions is set.
- EmitComments
  If set, instructions will be annoted with comments to help with debugging. Extra NOP instructions will also be inserted.

## Compiler Validation

A new Glean test has been create to exercise the GLSL compiler.

The glsl1 test runs over 150 sub-tests to check that the language features and built-in functions work properly. This test should be run frequently while working on the compiler to catch regressions.

The test coverage is reasonably broad and complete but additional tests should be added.