r300_vs.c revision 4a49301e
1/*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#include "r300_vs.h"
24
25#include "r300_context.h"
26#include "r300_tgsi_to_rc.h"
27
28#include "tgsi/tgsi_dump.h"
29#include "tgsi/tgsi_parse.h"
30
31#include "radeon_compiler.h"
32
33
34static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
35{
36    struct r300_vertex_shader * vs = c->UserData;
37    struct tgsi_shader_info* info = &vs->info;
38    struct tgsi_parse_context parser;
39    struct tgsi_full_declaration * decl;
40    boolean pointsize = FALSE;
41    int out_colors = 0;
42    int colors = 0;
43    int out_generic = 0;
44    int generic = 0;
45    int i;
46
47    /* Fill in the input mapping */
48    for (i = 0; i < info->num_inputs; i++)
49        c->code->inputs[i] = i;
50
51    /* Fill in the output mapping */
52    for (i = 0; i < info->num_outputs; i++) {
53        switch (info->output_semantic_name[i]) {
54            case TGSI_SEMANTIC_PSIZE:
55                pointsize = TRUE;
56                break;
57            case TGSI_SEMANTIC_COLOR:
58                out_colors++;
59                break;
60            case TGSI_SEMANTIC_FOG:
61            case TGSI_SEMANTIC_GENERIC:
62                out_generic++;
63                break;
64        }
65    }
66
67    tgsi_parse_init(&parser, vs->state.tokens);
68
69    while (!tgsi_parse_end_of_tokens(&parser)) {
70        tgsi_parse_token(&parser);
71
72        if (parser.FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
73            continue;
74
75        decl = &parser.FullToken.FullDeclaration;
76
77        if (decl->Declaration.File != TGSI_FILE_OUTPUT)
78            continue;
79
80        switch (decl->Semantic.SemanticName) {
81            case TGSI_SEMANTIC_POSITION:
82                c->code->outputs[decl->DeclarationRange.First] = 0;
83                break;
84            case TGSI_SEMANTIC_PSIZE:
85                c->code->outputs[decl->DeclarationRange.First] = 1;
86                break;
87            case TGSI_SEMANTIC_COLOR:
88                c->code->outputs[decl->DeclarationRange.First] = 1 +
89                    (pointsize ? 1 : 0) +
90                    colors++;
91                break;
92            case TGSI_SEMANTIC_FOG:
93            case TGSI_SEMANTIC_GENERIC:
94                c->code->outputs[decl->DeclarationRange.First] = 1 +
95                    (pointsize ? 1 : 0) +
96                    out_colors +
97                    generic++;
98                break;
99            default:
100                debug_printf("r300: vs: Bad semantic declaration %d\n",
101                    decl->Semantic.SemanticName);
102                break;
103        }
104    }
105
106    tgsi_parse_free(&parser);
107}
108
109
110void r300_translate_vertex_shader(struct r300_context* r300,
111                                  struct r300_vertex_shader* vs)
112{
113    struct r300_vertex_program_compiler compiler;
114    struct tgsi_to_rc ttr;
115
116    /* Setup the compiler */
117    rc_init(&compiler.Base);
118
119    compiler.Base.Debug = DBG_ON(r300, DBG_VP);
120    compiler.code = &vs->code;
121    compiler.UserData = vs;
122
123    if (compiler.Base.Debug) {
124        debug_printf("r300: Initial vertex program\n");
125        tgsi_dump(vs->state.tokens, 0);
126    }
127
128    /* Translate TGSI to our internal representation */
129    ttr.compiler = &compiler.Base;
130    ttr.info = &vs->info;
131
132    r300_tgsi_to_rc(&ttr, vs->state.tokens);
133
134    compiler.RequiredOutputs = ~(~0 << vs->info.num_outputs);
135    compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
136
137    /* Invoke the compiler */
138    r3xx_compile_vertex_program(&compiler);
139    if (compiler.Base.Error) {
140        /* Todo: Fail gracefully */
141        fprintf(stderr, "r300 VP: Compiler error\n");
142        abort();
143    }
144
145    /* And, finally... */
146    rc_destroy(&compiler.Base);
147    vs->translated = TRUE;
148}
149