tgsi_transform.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00034 #include "pipe/p_debug.h"
00035 
00036 #include "tgsi_transform.h"
00037 
00038 
00039 
00040 static void
00041 emit_instruction(struct tgsi_transform_context *ctx,
00042                  const struct tgsi_full_instruction *inst)
00043 {
00044    uint ti = ctx->ti;
00045 
00046    ti += tgsi_build_full_instruction(inst,
00047                                      ctx->tokens_out + ti,
00048                                      ctx->header,
00049                                      ctx->max_tokens_out - ti);
00050    ctx->ti = ti;
00051 }
00052 
00053 
00054 static void
00055 emit_declaration(struct tgsi_transform_context *ctx,
00056                  const struct tgsi_full_declaration *decl)
00057 {
00058    uint ti = ctx->ti;
00059 
00060    ti += tgsi_build_full_declaration(decl,
00061                                      ctx->tokens_out + ti,
00062                                      ctx->header,
00063                                      ctx->max_tokens_out - ti);
00064    ctx->ti = ti;
00065 }
00066 
00067 
00068 static void
00069 emit_immediate(struct tgsi_transform_context *ctx,
00070                const struct tgsi_full_immediate *imm)
00071 {
00072    uint ti = ctx->ti;
00073 
00074    ti += tgsi_build_full_immediate(imm,
00075                                    ctx->tokens_out + ti,
00076                                    ctx->header,
00077                                    ctx->max_tokens_out - ti);
00078    ctx->ti = ti;
00079 }
00080 
00081 
00082 
00092 int
00093 tgsi_transform_shader(const struct tgsi_token *tokens_in,
00094                       struct tgsi_token *tokens_out,
00095                       uint max_tokens_out,
00096                       struct tgsi_transform_context *ctx)
00097 {
00098    uint procType;
00099 
00100    /* input shader */
00101    struct tgsi_parse_context parse;
00102 
00103    /* output shader */
00104    struct tgsi_processor *processor;
00105 
00106 
00110    ctx->emit_instruction = emit_instruction;
00111    ctx->emit_declaration = emit_declaration;
00112    ctx->emit_immediate = emit_immediate;
00113    ctx->tokens_out = tokens_out;
00114    ctx->max_tokens_out = max_tokens_out;
00115 
00116 
00120    if (tgsi_parse_init( &parse, tokens_in ) != TGSI_PARSE_OK) {
00121       debug_printf("tgsi_parse_init() failed in tgsi_transform_shader()!\n");
00122       return -1;
00123    }
00124    procType = parse.FullHeader.Processor.Processor;
00125    assert(procType == TGSI_PROCESSOR_FRAGMENT ||
00126           procType == TGSI_PROCESSOR_VERTEX ||
00127           procType == TGSI_PROCESSOR_GEOMETRY);
00128 
00129 
00133    *(struct tgsi_version *) &tokens_out[0] = tgsi_build_version();
00134 
00135    ctx->header = (struct tgsi_header *) (tokens_out + 1);
00136    *ctx->header = tgsi_build_header();
00137 
00138    processor = (struct tgsi_processor *) (tokens_out + 2);
00139    *processor = tgsi_build_processor( procType, ctx->header );
00140 
00141    ctx->ti = 3;
00142 
00143 
00147    while( !tgsi_parse_end_of_tokens( &parse ) ) {
00148 
00149       tgsi_parse_token( &parse );
00150 
00151       switch( parse.FullToken.Token.Type ) {
00152       case TGSI_TOKEN_TYPE_INSTRUCTION:
00153          {
00154             struct tgsi_full_instruction *fullinst
00155                = &parse.FullToken.FullInstruction;
00156 
00157             if (ctx->transform_instruction)
00158                ctx->transform_instruction(ctx, fullinst);
00159             else
00160                ctx->emit_instruction(ctx, fullinst);
00161          }
00162          break;
00163 
00164       case TGSI_TOKEN_TYPE_DECLARATION:
00165          {
00166             struct tgsi_full_declaration *fulldecl
00167                = &parse.FullToken.FullDeclaration;
00168 
00169             if (ctx->transform_declaration)
00170                ctx->transform_declaration(ctx, fulldecl);
00171             else
00172                ctx->emit_declaration(ctx, fulldecl);
00173          }
00174          break;
00175 
00176       case TGSI_TOKEN_TYPE_IMMEDIATE:
00177          {
00178             struct tgsi_full_immediate *fullimm
00179                = &parse.FullToken.FullImmediate;
00180 
00181             if (ctx->transform_immediate)
00182                ctx->transform_immediate(ctx, fullimm);
00183             else
00184                ctx->emit_immediate(ctx, fullimm);
00185          }
00186          break;
00187 
00188       default:
00189          assert( 0 );
00190       }
00191    }
00192 
00193    if (ctx->epilog) {
00194       ctx->epilog(ctx);
00195    }
00196 
00197    tgsi_parse_free (&parse);
00198 
00199    return ctx->ti;
00200 }

Generated on Tue Sep 29 06:25:15 2009 for Gallium3D by  doxygen 1.5.4