Home | History | Annotate | Download | only in r300
      1 /*
      2  * Copyright 2009 Corbin Simpson <MostAwesomeDude (at) gmail.com>
      3  * Copyright 2009 Marek Olk <maraeo (at) gmail.com>
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the "Software"),
      7  * to deal in the Software without restriction, including without limitation
      8  * on the rights to use, copy, modify, merge, publish, distribute, sub
      9  * license, and/or sell copies of the Software, and to permit persons to whom
     10  * the Software is furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     19  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
     20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     22  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
     23 
     24 #include "r300_vs.h"
     25 
     26 #include "r300_context.h"
     27 #include "r300_screen.h"
     28 #include "r300_tgsi_to_rc.h"
     29 #include "r300_reg.h"
     30 
     31 #include "tgsi/tgsi_dump.h"
     32 #include "tgsi/tgsi_parse.h"
     33 #include "tgsi/tgsi_ureg.h"
     34 
     35 #include "compiler/radeon_compiler.h"
     36 
     37 /* Convert info about VS output semantics into r300_shader_semantics. */
     38 static void r300_shader_read_vs_outputs(
     39     struct r300_context *r300,
     40     struct tgsi_shader_info* info,
     41     struct r300_shader_semantics* vs_outputs)
     42 {
     43     int i;
     44     unsigned index;
     45 
     46     r300_shader_semantics_reset(vs_outputs);
     47 
     48     for (i = 0; i < info->num_outputs; i++) {
     49         index = info->output_semantic_index[i];
     50 
     51         switch (info->output_semantic_name[i]) {
     52             case TGSI_SEMANTIC_POSITION:
     53                 assert(index == 0);
     54                 vs_outputs->pos = i;
     55                 break;
     56 
     57             case TGSI_SEMANTIC_PSIZE:
     58                 assert(index == 0);
     59                 vs_outputs->psize = i;
     60                 break;
     61 
     62             case TGSI_SEMANTIC_COLOR:
     63                 assert(index < ATTR_COLOR_COUNT);
     64                 vs_outputs->color[index] = i;
     65                 break;
     66 
     67             case TGSI_SEMANTIC_BCOLOR:
     68                 assert(index < ATTR_COLOR_COUNT);
     69                 vs_outputs->bcolor[index] = i;
     70                 break;
     71 
     72             case TGSI_SEMANTIC_GENERIC:
     73                 assert(index < ATTR_GENERIC_COUNT);
     74                 vs_outputs->generic[index] = i;
     75                 break;
     76 
     77             case TGSI_SEMANTIC_FOG:
     78                 assert(index == 0);
     79                 vs_outputs->fog = i;
     80                 break;
     81 
     82             case TGSI_SEMANTIC_EDGEFLAG:
     83                 assert(index == 0);
     84                 fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
     85                 break;
     86 
     87             case TGSI_SEMANTIC_CLIPVERTEX:
     88                 assert(index == 0);
     89                 /* Draw does clip vertex for us. */
     90                 if (r300->screen->caps.has_tcl) {
     91                     fprintf(stderr, "r300 VP: cannot handle clip vertex output.\n");
     92                 }
     93                 break;
     94 
     95             default:
     96                 fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
     97                         info->output_semantic_name[i]);
     98         }
     99     }
    100 
    101     /* WPOS is a straight copy of POSITION and it's always emitted. */
    102     vs_outputs->wpos = i;
    103 }
    104 
    105 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
    106 {
    107     struct r300_vertex_shader * vs = c->UserData;
    108     struct r300_shader_semantics* outputs = &vs->outputs;
    109     struct tgsi_shader_info* info = &vs->info;
    110     int i, reg = 0;
    111     boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
    112                               outputs->bcolor[1] != ATTR_UNUSED;
    113 
    114     /* Fill in the input mapping */
    115     for (i = 0; i < info->num_inputs; i++)
    116         c->code->inputs[i] = i;
    117 
    118     /* Position. */
    119     if (outputs->pos != ATTR_UNUSED) {
    120         c->code->outputs[outputs->pos] = reg++;
    121     } else {
    122         assert(0);
    123     }
    124 
    125     /* Point size. */
    126     if (outputs->psize != ATTR_UNUSED) {
    127         c->code->outputs[outputs->psize] = reg++;
    128     }
    129 
    130     /* If we're writing back facing colors we need to send
    131      * four colors to make front/back face colors selection work.
    132      * If the vertex program doesn't write all 4 colors, lets
    133      * pretend it does by skipping output index reg so the colors
    134      * get written into appropriate output vectors.
    135      */
    136 
    137     /* Colors. */
    138     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
    139         if (outputs->color[i] != ATTR_UNUSED) {
    140             c->code->outputs[outputs->color[i]] = reg++;
    141         } else if (any_bcolor_used ||
    142                    outputs->color[1] != ATTR_UNUSED) {
    143             reg++;
    144         }
    145     }
    146 
    147     /* Back-face colors. */
    148     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
    149         if (outputs->bcolor[i] != ATTR_UNUSED) {
    150             c->code->outputs[outputs->bcolor[i]] = reg++;
    151         } else if (any_bcolor_used) {
    152             reg++;
    153         }
    154     }
    155 
    156     /* Texture coordinates. */
    157     for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
    158         if (outputs->generic[i] != ATTR_UNUSED) {
    159             c->code->outputs[outputs->generic[i]] = reg++;
    160         }
    161     }
    162 
    163     /* Fog coordinates. */
    164     if (outputs->fog != ATTR_UNUSED) {
    165         c->code->outputs[outputs->fog] = reg++;
    166     }
    167 
    168     /* WPOS. */
    169     c->code->outputs[outputs->wpos] = reg++;
    170 }
    171 
    172 void r300_init_vs_outputs(struct r300_context *r300,
    173                           struct r300_vertex_shader *vs)
    174 {
    175     tgsi_scan_shader(vs->state.tokens, &vs->info);
    176     r300_shader_read_vs_outputs(r300, &vs->info, &vs->outputs);
    177 }
    178 
    179 static void r300_dummy_vertex_shader(
    180     struct r300_context* r300,
    181     struct r300_vertex_shader* shader)
    182 {
    183     struct ureg_program *ureg;
    184     struct ureg_dst dst;
    185     struct ureg_src imm;
    186 
    187     /* Make a simple vertex shader which outputs (0, 0, 0, 1),
    188      * effectively rendering nothing. */
    189     ureg = ureg_create(TGSI_PROCESSOR_VERTEX);
    190     dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
    191     imm = ureg_imm4f(ureg, 0, 0, 0, 1);
    192 
    193     ureg_MOV(ureg, dst, imm);
    194     ureg_END(ureg);
    195 
    196     shader->state.tokens = tgsi_dup_tokens(ureg_finalize(ureg));
    197     ureg_destroy(ureg);
    198 
    199     shader->dummy = TRUE;
    200     r300_init_vs_outputs(r300, shader);
    201     r300_translate_vertex_shader(r300, shader);
    202 }
    203 
    204 void r300_translate_vertex_shader(struct r300_context *r300,
    205                                   struct r300_vertex_shader *vs)
    206 {
    207     struct r300_vertex_program_compiler compiler;
    208     struct tgsi_to_rc ttr;
    209     unsigned i;
    210 
    211     /* Setup the compiler */
    212     memset(&compiler, 0, sizeof(compiler));
    213     rc_init(&compiler.Base);
    214 
    215     DBG_ON(r300, DBG_VP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
    216     DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;
    217     compiler.code = &vs->code;
    218     compiler.UserData = vs;
    219     compiler.Base.is_r500 = r300->screen->caps.is_r500;
    220     compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
    221     compiler.Base.has_half_swizzles = FALSE;
    222     compiler.Base.has_presub = FALSE;
    223     compiler.Base.has_omod = FALSE;
    224     compiler.Base.max_temp_regs = 32;
    225     compiler.Base.max_constants = 256;
    226     compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;
    227 
    228     if (compiler.Base.Debug & RC_DBG_LOG) {
    229         DBG(r300, DBG_VP, "r300: Initial vertex program\n");
    230         tgsi_dump(vs->state.tokens, 0);
    231     }
    232 
    233     /* Translate TGSI to our internal representation */
    234     ttr.compiler = &compiler.Base;
    235     ttr.info = &vs->info;
    236     ttr.use_half_swizzles = FALSE;
    237 
    238     r300_tgsi_to_rc(&ttr, vs->state.tokens);
    239 
    240     if (ttr.error) {
    241         fprintf(stderr, "r300 VP: Cannot translate a shader. "
    242                 "Using a dummy shader instead.\n");
    243         r300_dummy_vertex_shader(r300, vs);
    244         return;
    245     }
    246 
    247     if (compiler.Base.Program.Constants.Count > 200) {
    248         compiler.Base.remove_unused_constants = TRUE;
    249     }
    250 
    251     compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs + 1));
    252     compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
    253 
    254     /* Insert the WPOS output. */
    255     rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
    256 
    257     /* Invoke the compiler */
    258     r3xx_compile_vertex_program(&compiler);
    259     if (compiler.Base.Error) {
    260         fprintf(stderr, "r300 VP: Compiler error:\n%sUsing a dummy shader"
    261                 " instead.\n", compiler.Base.ErrorMsg);
    262 
    263         if (vs->dummy) {
    264             fprintf(stderr, "r300 VP: Cannot compile the dummy shader! "
    265                     "Giving up...\n");
    266             abort();
    267         }
    268 
    269         rc_destroy(&compiler.Base);
    270         r300_dummy_vertex_shader(r300, vs);
    271         return;
    272     }
    273 
    274     /* Initialize numbers of constants for each type. */
    275     vs->externals_count = 0;
    276     for (i = 0;
    277          i < vs->code.constants.Count &&
    278          vs->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
    279         vs->externals_count = i+1;
    280     }
    281     for (; i < vs->code.constants.Count; i++) {
    282         assert(vs->code.constants.Constants[i].Type == RC_CONSTANT_IMMEDIATE);
    283     }
    284     vs->immediates_count = vs->code.constants.Count - vs->externals_count;
    285 
    286     /* And, finally... */
    287     rc_destroy(&compiler.Base);
    288 }
    289