u_simple_shaders.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 
00036 #include "pipe/p_context.h"
00037 #include "pipe/p_debug.h"
00038 #include "pipe/p_defines.h"
00039 #include "pipe/p_inlines.h"
00040 #include "pipe/p_winsys.h"
00041 #include "pipe/p_shader_tokens.h"
00042 
00043 #include "util/u_memory.h"
00044 #include "util/u_simple_shaders.h"
00045 
00046 #include "tgsi/tgsi_build.h"
00047 #include "tgsi/tgsi_dump.h"
00048 #include "tgsi/tgsi_parse.h"
00049 
00050 
00051 
00055 void *
00056 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
00057                                     uint num_attribs,
00058                                     const uint *semantic_names,
00059                                     const uint *semantic_indexes,
00060                                     struct pipe_shader_state *shader)
00061                                     
00062 {
00063    uint maxTokens = 100;
00064    struct tgsi_token *tokens;
00065    struct tgsi_header *header;
00066    struct tgsi_processor *processor;
00067    struct tgsi_full_declaration decl;
00068    struct tgsi_full_instruction inst;
00069    const uint procType = TGSI_PROCESSOR_VERTEX;
00070    uint ti, i;
00071 
00072    tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
00073 
00074    /* shader header
00075     */
00076    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
00077 
00078    header = (struct tgsi_header *) &tokens[1];
00079    *header = tgsi_build_header();
00080 
00081    processor = (struct tgsi_processor *) &tokens[2];
00082    *processor = tgsi_build_processor( procType, header );
00083 
00084    ti = 3;
00085 
00086    /* declare inputs */
00087    for (i = 0; i < num_attribs; i++) {
00088       decl = tgsi_default_full_declaration();
00089       decl.Declaration.File = TGSI_FILE_INPUT;
00090 
00091       decl.Declaration.Semantic = 1;
00092       decl.Semantic.SemanticName = semantic_names[i];
00093       decl.Semantic.SemanticIndex = semantic_indexes[i];
00094 
00095       decl.DeclarationRange.First = 
00096       decl.DeclarationRange.Last = i;
00097       ti += tgsi_build_full_declaration(&decl,
00098                                         &tokens[ti],
00099                                         header,
00100                                         maxTokens - ti);
00101    }
00102 
00103    /* declare outputs */
00104    for (i = 0; i < num_attribs; i++) {
00105       decl = tgsi_default_full_declaration();
00106       decl.Declaration.File = TGSI_FILE_OUTPUT;
00107       decl.Declaration.Semantic = 1;
00108       decl.Semantic.SemanticName = semantic_names[i];
00109       decl.Semantic.SemanticIndex = semantic_indexes[i];
00110       decl.DeclarationRange.First = 
00111          decl.DeclarationRange.Last = i;
00112       ti += tgsi_build_full_declaration(&decl,
00113                                         &tokens[ti],
00114                                         header,
00115                                         maxTokens - ti);
00116    }
00117 
00118    /* emit MOV instructions */
00119    for (i = 0; i < num_attribs; i++) {
00120       /* MOVE out[i], in[i]; */
00121       inst = tgsi_default_full_instruction();
00122       inst.Instruction.Opcode = TGSI_OPCODE_MOV;
00123       inst.Instruction.NumDstRegs = 1;
00124       inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
00125       inst.FullDstRegisters[0].DstRegister.Index = i;
00126       inst.Instruction.NumSrcRegs = 1;
00127       inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
00128       inst.FullSrcRegisters[0].SrcRegister.Index = i;
00129       ti += tgsi_build_full_instruction(&inst,
00130                                         &tokens[ti],
00131                                         header,
00132                                         maxTokens - ti );
00133    }
00134 
00135    /* END instruction */
00136    inst = tgsi_default_full_instruction();
00137    inst.Instruction.Opcode = TGSI_OPCODE_END;
00138    inst.Instruction.NumDstRegs = 0;
00139    inst.Instruction.NumSrcRegs = 0;
00140    ti += tgsi_build_full_instruction(&inst,
00141                                      &tokens[ti],
00142                                      header,
00143                                      maxTokens - ti );
00144 
00145 #if 0 /*debug*/
00146    tgsi_dump(tokens, 0);
00147 #endif
00148 
00149    shader->tokens = tokens;
00150    /*shader->num_tokens = ti;*/
00151 
00152    return pipe->create_vs_state(pipe, shader);
00153 }
00154 
00155 
00156 
00157 
00163 void *
00164 util_make_fragment_tex_shader(struct pipe_context *pipe,
00165                               struct pipe_shader_state *shader)
00166 {
00167    uint maxTokens = 100;
00168    struct tgsi_token *tokens;
00169    struct tgsi_header *header;
00170    struct tgsi_processor *processor;
00171    struct tgsi_full_declaration decl;
00172    struct tgsi_full_instruction inst;
00173    const uint procType = TGSI_PROCESSOR_FRAGMENT;
00174    uint ti;
00175 
00176    tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
00177 
00178    /* shader header
00179     */
00180    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
00181 
00182    header = (struct tgsi_header *) &tokens[1];
00183    *header = tgsi_build_header();
00184 
00185    processor = (struct tgsi_processor *) &tokens[2];
00186    *processor = tgsi_build_processor( procType, header );
00187 
00188    ti = 3;
00189 
00190    /* declare TEX[0] input */
00191    decl = tgsi_default_full_declaration();
00192    decl.Declaration.File = TGSI_FILE_INPUT;
00193    /* XXX this could be linear... */
00194    decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
00195    decl.Declaration.Semantic = 1;
00196    decl.Semantic.SemanticName = TGSI_SEMANTIC_GENERIC;
00197    decl.Semantic.SemanticIndex = 0;
00198    decl.DeclarationRange.First = 
00199    decl.DeclarationRange.Last = 0;
00200    ti += tgsi_build_full_declaration(&decl,
00201                                      &tokens[ti],
00202                                      header,
00203                                      maxTokens - ti);
00204 
00205    /* declare color[0] output */
00206    decl = tgsi_default_full_declaration();
00207    decl.Declaration.File = TGSI_FILE_OUTPUT;
00208    decl.Declaration.Semantic = 1;
00209    decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
00210    decl.Semantic.SemanticIndex = 0;
00211    decl.DeclarationRange.First = 
00212    decl.DeclarationRange.Last = 0;
00213    ti += tgsi_build_full_declaration(&decl,
00214                                      &tokens[ti],
00215                                      header,
00216                                      maxTokens - ti);
00217 
00218    /* declare sampler */
00219    decl = tgsi_default_full_declaration();
00220    decl.Declaration.File = TGSI_FILE_SAMPLER;
00221    decl.DeclarationRange.First = 
00222    decl.DeclarationRange.Last = 0;
00223    ti += tgsi_build_full_declaration(&decl,
00224                                      &tokens[ti],
00225                                      header,
00226                                      maxTokens - ti);
00227 
00228    /* TEX instruction */
00229    inst = tgsi_default_full_instruction();
00230    inst.Instruction.Opcode = TGSI_OPCODE_TEX;
00231    inst.Instruction.NumDstRegs = 1;
00232    inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
00233    inst.FullDstRegisters[0].DstRegister.Index = 0;
00234    inst.Instruction.NumSrcRegs = 2;
00235    inst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D;
00236    inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
00237    inst.FullSrcRegisters[0].SrcRegister.Index = 0;
00238    inst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
00239    inst.FullSrcRegisters[1].SrcRegister.Index = 0;
00240    ti += tgsi_build_full_instruction(&inst,
00241                                      &tokens[ti],
00242                                      header,
00243                                      maxTokens - ti );
00244 
00245    /* END instruction */
00246    inst = tgsi_default_full_instruction();
00247    inst.Instruction.Opcode = TGSI_OPCODE_END;
00248    inst.Instruction.NumDstRegs = 0;
00249    inst.Instruction.NumSrcRegs = 0;
00250    ti += tgsi_build_full_instruction(&inst,
00251                                      &tokens[ti],
00252                                      header,
00253                                      maxTokens - ti );
00254 
00255 #if 0 /*debug*/
00256    tgsi_dump(tokens, 0);
00257 #endif
00258 
00259    shader->tokens = tokens;
00260    /*shader->num_tokens = ti;*/
00261 
00262    return pipe->create_fs_state(pipe, shader);
00263 }
00264 
00265 
00266 
00267 
00268 
00272 void *
00273 util_make_fragment_passthrough_shader(struct pipe_context *pipe,
00274                                       struct pipe_shader_state *shader)
00275 {
00276    uint maxTokens = 40;
00277    struct tgsi_token *tokens;
00278    struct tgsi_header *header;
00279    struct tgsi_processor *processor;
00280    struct tgsi_full_declaration decl;
00281    struct tgsi_full_instruction inst;
00282    const uint procType = TGSI_PROCESSOR_FRAGMENT;
00283    uint ti;
00284 
00285    tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
00286 
00287    /* shader header
00288     */
00289    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
00290 
00291    header = (struct tgsi_header *) &tokens[1];
00292    *header = tgsi_build_header();
00293 
00294    processor = (struct tgsi_processor *) &tokens[2];
00295    *processor = tgsi_build_processor( procType, header );
00296 
00297    ti = 3;
00298 
00299    /* declare input */
00300    decl = tgsi_default_full_declaration();
00301    decl.Declaration.File = TGSI_FILE_INPUT;
00302    decl.Declaration.Semantic = 1;
00303    decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
00304    decl.Semantic.SemanticIndex = 0;
00305    decl.DeclarationRange.First = 
00306       decl.DeclarationRange.Last = 0;
00307    ti += tgsi_build_full_declaration(&decl,
00308                                      &tokens[ti],
00309                                      header,
00310                                      maxTokens - ti);
00311 
00312    /* declare output */
00313    decl = tgsi_default_full_declaration();
00314    decl.Declaration.File = TGSI_FILE_OUTPUT;
00315    decl.Declaration.Semantic = 1;
00316    decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
00317    decl.Semantic.SemanticIndex = 0;
00318    decl.DeclarationRange.First = 
00319       decl.DeclarationRange.Last = 0;
00320    ti += tgsi_build_full_declaration(&decl,
00321                                      &tokens[ti],
00322                                      header,
00323                                      maxTokens - ti);
00324 
00325 
00326    /* MOVE out[0], in[0]; */
00327    inst = tgsi_default_full_instruction();
00328    inst.Instruction.Opcode = TGSI_OPCODE_MOV;
00329    inst.Instruction.NumDstRegs = 1;
00330    inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
00331    inst.FullDstRegisters[0].DstRegister.Index = 0;
00332    inst.Instruction.NumSrcRegs = 1;
00333    inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
00334    inst.FullSrcRegisters[0].SrcRegister.Index = 0;
00335    ti += tgsi_build_full_instruction(&inst,
00336                                      &tokens[ti],
00337                                      header,
00338                                      maxTokens - ti );
00339 
00340    /* END instruction */
00341    inst = tgsi_default_full_instruction();
00342    inst.Instruction.Opcode = TGSI_OPCODE_END;
00343    inst.Instruction.NumDstRegs = 0;
00344    inst.Instruction.NumSrcRegs = 0;
00345    ti += tgsi_build_full_instruction(&inst,
00346                                      &tokens[ti],
00347                                      header,
00348                                      maxTokens - ti );
00349 
00350    assert(ti < maxTokens);
00351 
00352 #if 0 /*debug*/
00353    tgsi_dump(tokens, 0);
00354 #endif
00355 
00356    shader->tokens = tokens;
00357    /*shader->num_tokens = ti;*/
00358 
00359    return pipe->create_fs_state(pipe, shader);
00360 }
00361 
00362 
00363 void
00364 util_free_shader(struct pipe_shader_state *shader)
00365 {
00366    FREE((struct tgsi_token *)shader->tokens);
00367    shader->tokens = NULL;
00368 }

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