[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.3.1 Using `lib3delight' to Interrogate Shaders

You can link your program with `lib3delight' (see section 8.1 Linking with 3Delight to query informations about shaders. Include the file `slo.h' to get the prototypes of the following functions and the type definitions they use.

void Slo_SetPath ( char* i_path )
Set the paths where the library will look for shaders.

void Slo_SetShader ( char* i_name )
Find and open the shader named i_name. Close any shader that was previously opened by Slo_SetShader.

char* Slo_GetName ( void )
Returns the name of the currently opened shader.

SLO_TYPE Slo_GetType ( void )
Return the type of the currently opened shader. See the file `slo.h' for details about SLO_TYPE.

int Slo_GetNArgs ( void )
Return the number of parameters of this shader.

SLO_VISSYMDEF* Slo_GetArgById ( int i )
Return information about the i-th parameter of the shader. The first parameter has an id of 1. See the file `slo.h' for details about SLO_VISSYMDEF.

SLO_VISSYMDEF* Slo_GetArgByName ( char *i_name )
Return information about the parameter named i_name. See the file `slo.h' for details about SLO_VISSYMDEF.

SLO_VISSYMDEF *Slo_GetArrayArgElement ( SLO_VISSYMDEF *i_array, int i_index )
If a parameter is an array (as specified by Slo_GetArgById() or Slo_GetArgByName), each of its element should be accessed using this function.

void Slo_EndShader ( void )
Close the current shader.

char* Slo_TypetoStr ( SLO_TYPE i_type )
Get a string representation of type i_type.

char* Slo_StortoStr ( SLO_STORAGE i_storage )
Get a string representation of storage class i_storage.

char* Slo_DetailtoStr ( SLO_DETAIL i_detail )
Get a string representation of variable detail i_detail.

Next is the complete source code of the shaderinfo utility, it is compilable on all supported platforms, see section 8.1 Linking with 3Delight. Note that string parameters are represented using a const char * const * and not a const char * like in some implementations.

 
/******************************************************************************/
/*                                                                            */
/*    Copyright (c)The 3Delight Team.                                         */
/*    All Rights Reserved.                                                    */
/*                                                                            */
/******************************************************************************/

// ===========================================================================
// = VERSION
//     $Revision: 1.49 $
// = AUTHOR
//     Aghiles Kheffache
// = DATE RELEASED
//     $Date: 2003/07/30 20:21:54 $
// = RCSID
//     $Id: 3delight.texinfo,v 1.49 2003/07/30 20:21:54 olivier Exp $
// ===========================================================================

#include "slo.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <assert.h>

int main( int argc, char ** argv )
{
    unsigned i, j, k, kk;

    int exitCode = 0;

    bool RIBDeclare = false;
    int start = 1;

    /* DlDebug::InitErrorSignalsHandling(); */

    if( argc<=1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") )
    {
        printf( "Usage: shaderinfo file1 [file2 ... fileN]\n" );
        printf( "  -d : output declarations in RIB format\n" );
        printf( "  -v : show version to console\n" );
        printf( "  -h : show this help\n\n" );

        return 0;
    }

    if( argc==2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) )
    {
        printf( "shaderinfo version 2.0.\n" );
        printf( "Copyright (c) 1999-2003 The 3Delight Team.\n" );

        return 0;
    }

    if( (argc == 1) || !strcmp(argv[1], "-d") )
    {
        RIBDeclare = true;
        start++;
    }

    const char* path = getenv( "DL_SHADERS_PATH" );

    Slo_SetPath( path ? path : "." );

    for( i = start; i < argc; i++ )
    {
        int err = Slo_SetShader( argv[i] );

        if( err != 0 )
        {
            fprintf(
                stderr,
                "shaderinfo: unable to open shader \"%s\" (error %d).\n",
                argv[i], err );

            exitCode = 1;

            continue;
        }

        printf( "\n%s \"%s\"\n", Slo_TypetoStr(Slo_GetType()), Slo_GetName() );

        for( j = 0; j < Slo_GetNArgs(); j++ )
        {
            SLO_VISSYMDEF *parameter = Slo_GetArgById( j+1 );

            if( !parameter || !parameter->svd_valisvalid )
            {
                fprintf( stderr, "shaderinfor: parameter %d is invalid\n", j );
                continue;
            }

            const char *name = parameter->svd_name;
            const char *detail = Slo_DetailtoStr( parameter->svd_detail );
            const char *type = Slo_TypetoStr( parameter->svd_type );
            unsigned arraylen = parameter->svd_arraylen;

            if( RIBDeclare )
            {
                /* Output a string suitable for RIB syntax */
                
                if( arraylen == 1 )
                    printf( "Declare \"%s\" \"%s %s\"\n", name, detail, type );
                else
                    printf( "Declare \"%s\" \"%s %s[%d]\"\n",
                        name, detail, type, arraylen );

                continue;
            }

            if( parameter->svd_storage == SLO_STOR_OUTPUTPARAMETER )
                printf( "    \"%s\" \"output %s %s", name, detail, type );
            else
                printf( "    \"%s\" \"%s %s", name, detail, type );

            if( arraylen > 1 )
                printf( "[%d]", arraylen );

            printf( "\"\n" );
            printf( "\t\tDefault value: " );

            switch( parameter->svd_type )
            {
            case SLO_TYPE_COLOR:
            case SLO_TYPE_POINT:
            case SLO_TYPE_VECTOR:
            case SLO_TYPE_NORMAL:
            case SLO_TYPE_MATRIX:

                if( parameter->svd_spacename[0] != (char)0 )
                    printf( "\"%s\" ", parameter->svd_spacename );

                break;

            default:
                break;
            }

            if( arraylen > 1 )
                printf( "{" );

            for( k=0; k<arraylen; k++ )
            {
                SLO_VISSYMDEF *elem = Slo_GetArrayArgElement( parameter, k+1 );

                if( !elem )
                {
                    printf( "<error>" );
                    continue;
                }

                switch( parameter->svd_type )
                {
                case SLO_TYPE_SCALAR:
                    printf( "%g", *elem->svd_default.scalarval );
                    break;

                case SLO_TYPE_POINT:
                case SLO_TYPE_VECTOR:
                case SLO_TYPE_NORMAL:
                case SLO_TYPE_COLOR:
                    printf( "[%g %g %g]",
                        elem->svd_default.pointval->xval,
                        elem->svd_default.pointval->yval,
                        elem->svd_default.pointval->zval );
                    break;

                case SLO_TYPE_MATRIX:
                    printf( "[%g ",  elem->svd_default.matrixval[0] );

                    for( kk = 1; kk < 15; kk++ )
                        printf( "%g ", elem->svd_default.matrixval[kk] );

                    printf( "%g]",  elem->svd_default.matrixval[15] );
                    break;

                case SLO_TYPE_STRING:
                    printf( "\"%s\"", *elem->svd_default.stringval );
                    break;

                default:
                    break;
                }

                if( k != (arraylen-1) )
                {
                    printf( ", " );
                }
            }

            if( arraylen > 1 )
                printf( "}" );

            printf( "\n" );
        }

        Slo_EndShader();

        printf( "\n" );
    }

    return exitCode;
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Aghiles Kheffache on July, 31 2003 using texi2html
3Delight 1.0.0. Copyright 2000-2003 The 3Delight Team. All Rights Reserved.