Skip to content

Control Statements

Sylvain Doremus edited this page Oct 14, 2020 · 7 revisions

The header ShaderWriter/Writer.hpp describes the sdw::ShaderWriter class and derived, but it also introduces helper macros to write control statements.

Each control statement begin macro (IF, FOR, SWITCH...) must be matched with the corresponding control statement end macro (FI, ROF, HCTIWS...).

Example:

    FOR( writer, Int, i, 0_i, i < count, ++i )
    {
        // i is an Int variable you can use inside the loop block
    }
    ROF;

They can have nice interactions with C++ control statements, like writing code inside a C++ loop, that will come unrolled in shader code:

    SWITCH( writer, value )
    {
        for ( auto i = 0; i < count; ++i )
        {
            CASE( Int( i ) )
            {
                result = somethingThatNeedsUniformAccess[Int( i )];
                writer.caseBreakStmt();
            }
            ESAC;
        }
    }
    HCTIWS;

This thing will result in the following GLSL code (with count being 3):

    switch ( value )
    {
        case 0:
            result = somethingThatNeedsUniformAccess[0];
            break;
        case 1:
            result = somethingThatNeedsUniformAccess[1];
            break;
        case 2:
            result = somethingThatNeedsUniformAccess[2];
            break;
    }
Clone this wiki locally