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 Layout 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 Layout application

  1. Start the Layout app. (Start » Programs » Scenomics » Layout) or (Windows® key and then type 'Layout' 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 » Open from the main menu.

    The software displays the file dialog in the \Shader folder.

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

    The application loads the document.

    This is a picture of the cellular noise shader.

Switch The Subroutine

  1. Examine the hierarchy and right click over the <SubroutineBindNode> named uniform int SelectNoise.
  2. Select Compute Subroutines » SNoise from the listed options. The shader subroutine changes. This is a picture of the s-noise shader.

Copy The Compute Shader Path

  1. Examine the hierarchy and right click over the <Program> node named Compute.
  2. Choose Copy Source Path » Compute 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 Compute 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 noise compute shader.

    // #version 460
    // The version number is automatically injected by the application.
    // It is included above for reference purposes only.
    #extension GL_ARB_compute_variable_group_size : enable
    #ifdef GL_ARB_compute_variable_group_size
       layout( local_size_variable ) in;
       layout( local_size_x = 32, local_size_y = 32, local_size_z = 1  ) in;
    #include <SPA_Version.glsl>
    #include <Modules\SPA_Noise.glsl>
    #include <Modules\SPA_Ranges.glsl>
    uniform float zoom = 0.01;
    layout( r32f ) uniform writeonly image2D results;
    #ifndef GLSLANG_IGNORE
    subroutine vec4 Noise( vec2 pos, float zoom );
    subroutine( Noise ) vec4 CellularNoise( vec2 pos, float zoom )
       vec2 cell = cellular( pos.xy * zoom );
       return vec4( cell, 0.0, 1.0 );
    subroutine( Noise ) vec4 SNoise( vec2 pos, float zoom )
       float noise = snoise( vec3( pos.xy * zoom, 0.0 ) );
       return vec4( noise, 0.0, 0.0, 1.0 );
    subroutine uniform Noise SelectNoise;
    void main()
       ivec2 pos = ivec2( gl_GlobalInvocationID );
       ivec2 src_dims = imageSize( results );
       vec4 noise = SelectNoise( pos, zoom );
       imageStore( results, pos, noise );

    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 Layout 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 Layout 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.