Fencing Code From Glslang

You need the following Simdify® modules to complete this exercise: Simdify® Free Edition

Glslang is amazing but it doesn't support absolutely every single GLSL feature. Shader subroutines are a great example. This means that if you write GLSL code that uses subroutines, or possibly other GLSL features that Glslang does not support, the Module application will present a Glslang compiler error when you rebuild a document. For this reason, we provide a very simple way of fencing code from Glslang. You just wrap the code in preprocessor statements and that's it. In this exercise you'll learn how to use the preprocessor to fence code from Glslang.

Start The Module Application

  1. Start the Module app. (Start > Programs > Scenomics > Module) or (Windows® key and then type 'Module' to find the app icon.)

    The application displays a splash screen and then the application desktop appears. The main menu is composed of three items that contain commands relevant to the current context (which is an empty document). The interface changes when you create a new file or load a file from disk.

    This is a picture of the desktop.

Open Subroutines Sample

  1. Select File > Open from the main menu.

    The software displays the file dialog in the Module library folder.

    This is a picture of the new project dialog.
  2. Enter the directory named Experiments.
  3. Enter the directory named Subroutines and open the file named Subroutines.box.

    The application loads the document.

    This is a picture of the subroutines shader.

Switch The Subroutine

  1. Examine the hierarchy and right click over the <SubroutineBindNode> named uniform int bindSubroutines.
  2. Select Fragment Subroutines > SetFragmentColorGreen from the listed options. The shader subroutine changes. This is a picture of the subroutines shader.

Copy The Fragment Shader Path

  1. Examine the hierarchy and right click over the <Program> node named Program.
  2. Choose Copy Source Path > Fragment Shader from the listed options.

    This copies the absolute path to the fragment shader source code to the Windows® clipboard. For example, a file path like the following: D:\Release6\Content\Library\Shader\User Basic Exercise\460\user_basic_exercise_fragment_shader.glsl is copied to the clipboard.

Examine The Fragment Shader

  1. Start a text editor of your choice and select the option to open a file from disk.
  2. Select CTRL + V to paste the fragment shader file path (into the place in the dialog where you specify the file to open) and open the file.

    This is the fragment shader.

    // #version 400
    // The version number is automatically injected by the application.
    // It is included above for reference purposes only.
    #include <SPA_Version.glsl>
    #include "vertex_attributes.glsl"
    
    #ifndef GLSLANG_IGNORE
    
    subroutine vec4 RenderOutputColor();
    
    subroutine( RenderOutputColor ) vec4 SetFragmentColorRed()
    {
       return vec4( 1.0, 0.0, 0.0, 1.0 );
    }
    
    subroutine( RenderOutputColor ) vec4 SetFragmentColorGreen()
    {
       return vec4( 0.0, 1.0, 0.0, 1.0 );
    }
    
    subroutine ( RenderOutputColor ) vec4 SetFragmentColorBlue()
    {
       return vec4( 0.0, 0.0, 1.0, 1.0 );
    }
    
    subroutine ( RenderOutputColor ) vec4 SetFragmentColorOrange()
    {
       return vec4( 1.0, 0.6, 0.0, 1.0 );
    }
    
    subroutine uniform RenderOutputColor SetFragmentColor;
    
    #endif
    
    in Data { vertexData attributes; } DataIn;
    out vec4 fragColor;
    
    void main(void)
    {
       fragColor = SetFragmentColor();
    }

    The preprocessor include statement #include <SPA_Version.glsl> is included at the top. To fence Glslang code, you must include this module. The shader subroutine code is wrapped inside #ifndef GLSLANG_IGNORE with a trailing #endif at the end. The Module app doesn't process code inside void main(void) so you don't have to put fences around code in that section of your shader. To fence code from Glslang computation, just include SPA_Version.glsl as shown above, and declare your code inside the preprocessor block as shown.

    Note that the because of the code fencing, the Module application build process cannot create subroutine uniform Noise SelectNoise in the document. To create this, simply right click over the <Program> and select Bind Subroutines from the listed options.

  3. Close all Simdify applications started during this exercise.