utils/testoutput.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 29 May 2012 02:43:24 -0400
changeset 1104 9147482e1ec7
parent 588 ef3d2efa822d
child 1156 6e760a19f456
permissions -rw-r--r--
Allow sampler type remapping. Allows workaround for Shader Model 1 not specifying what a sampler should be in the bytecode itself.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
390
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdio.h>
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdlib.h>
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include "mojoshader.h"
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
static int do_parse(const unsigned char *buf, const int len, const char *prof)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
{
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    const MOJOSHADER_parseData *pd;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    int retval = 0;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
1104
9147482e1ec7 Allow sampler type remapping.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
    19
    pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, NULL, 0, NULL, NULL, NULL);
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    20
    if (pd->error_count > 0)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    21
    {
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    22
        int i;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    23
        for (i = 0; i < pd->error_count; i++)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    24
        {
577
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 536
diff changeset
    25
            printf("%s:%d: ERROR: %s\n",
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 536
diff changeset
    26
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    27
                    pd->errors[i].error_position,
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    28
                    pd->errors[i].error);
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    29
        } // for
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 475
diff changeset
    30
    } // if
390
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    {
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        retval = 1;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        if (pd->output != NULL)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
        {
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
            int i;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
            for (i = 0; i < pd->output_len; i++)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
                putchar((int) pd->output[i]);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
            printf("\n");
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
        } // if
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    } // else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    printf("\n\n");
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    MOJOSHADER_freeParseData(pd);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    return retval;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
} // do_parse
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
int main(int argc, char **argv)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
{
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    int retval = 0;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    if (argc <= 2)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    {
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
        const char *profile = argv[1];
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        int i;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        for (i = 2; i < argc; i++)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        {
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
            FILE *io = fopen(argv[i], "rb");
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
            if (io == NULL)
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
                printf(" ... fopen('%s') failed.\n", argv[i]);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
            else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
            {
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
                unsigned char *buf = (unsigned char *) malloc(1000000);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
                int rc = fread(buf, 1, 1000000, io);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
                fclose(io);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
                if (!do_parse(buf, rc, profile))
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
                    retval = 1;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
                free(buf);
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            } // else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        } // for
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    } // else
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    return retval;
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
} // main
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
// end of testoutput.c ...
4effa7e6b49c Added testoutput program for outputting just the generated shaders.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81