osgEarth uses GLSL shaders in several of its rendering modes. By default
osgEarth will detect the capabilities of your graphics hardware and
automatically select an appropriate mode to use.

Since osgEarth relies on shaders, you as a developer may wish to customize
the rendering or add your own effects and features in GLSL. Anyone who has
worked with shaders has run into the same challenges:

Shader programs are monolithic. Adding new shader code requires you to
copy, modify, and replace the existing code so you don’t lose its
functionality.

Keeping your changes in sync with changes to the original code’s
shaders is a maintenance nightmare.

Maintaining multiple versions of shader main()s is cumbersome and
difficult.

Maintaining the dreaded “uber shader” becomes unmanageable as the
GLSL code base grows in complexity and you add more features.

Shader Composition solves these problems by modularizing the shader
pipeline. You can add and remove functions at any point in the program
without copying, pasting, or hacking other people’s GLSL code.

Next we will discuss the structure of osgEarth’s shader composition framework.

As you can see, we have made the design decision to designate function
injection points that make sense for most applications. That is not to say
that they are perfect for everything, rather that we believe this approach
makes the Framework easy to use and not too “low-level”.

Important: The Shader Composition Framework at this time only supports VERTEX and FRAGMENT
shaders. It does not support GEOMETRY or TESSELLATION shaders yet. We are planning
to add this in the future.

osgEarth introduces a new OSG state attribute called VirtualProgram that performs
the runtime shader composition. Since VirtualProgram is an osg::StateAttribute,
you can attach one to any node in the scene graph. Shaders that belong to a
VirtualProgram can override shaders higher up in the scene graph.
In this way you can add, combine, and override individual shader functions in osgEarth.

At run time, a VirtualProgram will look at the current state and assemble a full
osg::Program that uses the built-in main()s and calls all the functions that you
have injected via VirtualProgram.

From the generated mains we saw earlier, osgEarth calls into user functions.
These don’t exist in the default shaders that osgEarth generates;
rather, they represent code that you as the developer can “inject”
into various locations in the shader pipeline.

For example, let’s use user functions to create a simple “haze” effect:

In this example, the function setup_haze is called from the built-in vertex shader
main() after the built-in vertex functions. The apply_haze function gets called from
the core fragment shader main() after the built-in fragment functions.

There are SIX injection points, as follows:

Location

Shader Type

Signature

ShaderComp::LOCATION_VERTEX_MODEL

VERTEX

void func(inout vec4 vertex)

ShaderComp::LOCATION_VERTEX_VIEW

VERTEX

void func(inout vec4 vertex)

ShaderComp::LOCATION_VERTEX_CLIP

VERTEX

void func(inout vec4 vertex)

ShaderComp::LOCATION_FRAGMENT_COLORING

FRAGMENT

void func(inout vec4 color)

ShaderComp::LOCATION_FRAGMENT_LIGHTING

FRAGMENT

void func(inout vec4 color)

ShaderComp::LOCATION_FRAGMENT_OUTPUT

FRAGMENT

void func(inout vec4 color)

Each VERTEX locations let you operate on the vertex in a particular coordinate space.
You can alter the vertex, but you must leave it in the same space.

MODEL:

Vertex is the raw, untransformed values from the geometry.

VIEW:

Vertex is relative to the eyepoint, which lies at the origin (0,0,0) and
points down the -Z axis. In VIEW space, the original vertex has been
transformed by gl_ModelViewMatrix.

CLIP:

Post-projected clip space. CLIP space lies in the [-w..w] range along all
three axis, and is the result of transforming the original vertex by
gl_ModelViewProjectionMatrix.

The FRAGMENT locations are as follows.

COLORING:

Functions here are called when resolving the fragment color before
lighting is applied. Texturing or color adjustments typically
happen during this stage.

LIGHTING:

Functions here affect the lighting applied to a fragment color. This is
where things like sun lighting, bump mapping or normal mapping would
typically occur.

OUTPUT:

This is where gl_FragColor is set. By default, the built-in fragment
main() will set it for you. But you can set an OUTPUT shader to
replace this behavior with your own. A typical reason to do this would
be to implement MRT rendering (see the osgearth_mrt example).

Earlier we showed you how to inject functions using VirtualProgram.
The Shader Composition Framework also provides the concept of a ShaderPackage that supports
more advanced methods of shader management. We will talk about some of those now.

As we have seen, when you add a shader function to the pipeline using VirtualProgram
you need to tell osgEarth the name of the GLSL function to call, and the location in
the pipeline at which to call it, like so:

The ShaderPackage lets you load GLSL code from either a file or a string.
When you call the add method as show above, this tells the package to
(a) first look for a file by that name and load from that file; and
(b) if the file doesn’t exist, use the code in the source string.

The package will try to load the shader from the GLSL file. It will search for it in the OSG_FILE_PATH.
If it cannot find the file, it will load the shader from the backup source code associated with
that shader in the package.

osgEarth uses this technique internally to “inline” its stock shader code.
That gives you the option of deploying GLSL files with your application OR
keeping them inline – the application will still work either way.

The ShaderPackage support the concept if include files. Your GLSL code
can include any other shaders in the same package by referencing their file names.
Use a custom #pragma to include another file:

#pragma include myCode.vertex.glsl

Just as in C++, the include will load the other file (or source code) directly
inline. So the file you are including must be structured as if you had placed it right
in the including file. (That means it cannot have its own #version string, for example.)

Again: the includer and the includee must be registered with the same ShaderPackage.

Even though the VirtualProgram framework is included in the osgEarth SDK,
it really has nothing to do with map rendering. In this section we will go over some
of the things that osgEarth does with shader composition.

Sometimes you want to access more than one image layer at a time.
For example, you might have a masking layer that indicates land vs. water.
You may not actually want to draw this layer, but you want to use it to modulate
another visible layer.

You can do this using shared image layers. In the Map, mark an image layer as
shared (using ImageLayerOptions::shared()) and the renderer will make it available
to all the other layers in a secondary sampler.