r300_vs.c revision 3464ebd5
1/*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@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 "radeon_compiler.h"
36
37/* Convert info about VS output semantics into r300_shader_semantics. */
38static void r300_shader_read_vs_outputs(
39    struct tgsi_shader_info* info,
40    struct r300_shader_semantics* vs_outputs)
41{
42    int i;
43    unsigned index;
44
45    r300_shader_semantics_reset(vs_outputs);
46
47    for (i = 0; i < info->num_outputs; i++) {
48        index = info->output_semantic_index[i];
49
50        switch (info->output_semantic_name[i]) {
51            case TGSI_SEMANTIC_POSITION:
52                assert(index == 0);
53                vs_outputs->pos = i;
54                break;
55
56            case TGSI_SEMANTIC_PSIZE:
57                assert(index == 0);
58                vs_outputs->psize = i;
59                break;
60
61            case TGSI_SEMANTIC_COLOR:
62                assert(index < ATTR_COLOR_COUNT);
63                vs_outputs->color[index] = i;
64                break;
65
66            case TGSI_SEMANTIC_BCOLOR:
67                assert(index < ATTR_COLOR_COUNT);
68                vs_outputs->bcolor[index] = i;
69                break;
70
71            case TGSI_SEMANTIC_GENERIC:
72                assert(index < ATTR_GENERIC_COUNT);
73                vs_outputs->generic[index] = i;
74                break;
75
76            case TGSI_SEMANTIC_FOG:
77                assert(index == 0);
78                vs_outputs->fog = i;
79                break;
80
81            case TGSI_SEMANTIC_EDGEFLAG:
82                assert(index == 0);
83                fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
84                break;
85
86            default:
87                fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
88                        info->output_semantic_name[i]);
89        }
90    }
91
92    /* WPOS is a straight copy of POSITION and it's always emitted. */
93    vs_outputs->wpos = i;
94}
95
96static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
97{
98    struct r300_vertex_shader * vs = c->UserData;
99    struct r300_shader_semantics* outputs = &vs->outputs;
100    struct tgsi_shader_info* info = &vs->info;
101    int i, reg = 0;
102    boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
103                              outputs->bcolor[1] != ATTR_UNUSED;
104
105    /* Fill in the input mapping */
106    for (i = 0; i < info->num_inputs; i++)
107        c->code->inputs[i] = i;
108
109    /* Position. */
110    if (outputs->pos != ATTR_UNUSED) {
111        c->code->outputs[outputs->pos] = reg++;
112    } else {
113        assert(0);
114    }
115
116    /* Point size. */
117    if (outputs->psize != ATTR_UNUSED) {
118        c->code->outputs[outputs->psize] = reg++;
119    }
120
121    /* If we're writing back facing colors we need to send
122     * four colors to make front/back face colors selection work.
123     * If the vertex program doesn't write all 4 colors, lets
124     * pretend it does by skipping output index reg so the colors
125     * get written into appropriate output vectors.
126     */
127
128    /* Colors. */
129    for (i = 0; i < ATTR_COLOR_COUNT; i++) {
130        if (outputs->color[i] != ATTR_UNUSED) {
131            c->code->outputs[outputs->color[i]] = reg++;
132        } else if (any_bcolor_used ||
133                   outputs->color[1] != ATTR_UNUSED) {
134            reg++;
135        }
136    }
137
138    /* Back-face colors. */
139    for (i = 0; i < ATTR_COLOR_COUNT; i++) {
140        if (outputs->bcolor[i] != ATTR_UNUSED) {
141            c->code->outputs[outputs->bcolor[i]] = reg++;
142        } else if (any_bcolor_used) {
143            reg++;
144        }
145    }
146
147    /* Texture coordinates. */
148    for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
149        if (outputs->generic[i] != ATTR_UNUSED) {
150            c->code->outputs[outputs->generic[i]] = reg++;
151        }
152    }
153
154    /* Fog coordinates. */
155    if (outputs->fog != ATTR_UNUSED) {
156        c->code->outputs[outputs->fog] = reg++;
157    }
158
159    /* WPOS. */
160    c->code->outputs[outputs->wpos] = reg++;
161}
162
163void r300_init_vs_outputs(struct r300_vertex_shader *vs)
164{
165    tgsi_scan_shader(vs->state.tokens, &vs->info);
166    r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
167}
168
169static void r300_dummy_vertex_shader(
170    struct r300_context* r300,
171    struct r300_vertex_shader* shader)
172{
173    struct ureg_program *ureg;
174    struct ureg_dst dst;
175    struct ureg_src imm;
176
177    /* Make a simple vertex shader which outputs (0, 0, 0, 1),
178     * effectively rendering nothing. */
179    ureg = ureg_create(TGSI_PROCESSOR_VERTEX);
180    dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
181    imm = ureg_imm4f(ureg, 0, 0, 0, 1);
182
183    ureg_MOV(ureg, dst, imm);
184    ureg_END(ureg);
185
186    shader->state.tokens = tgsi_dup_tokens(ureg_finalize(ureg));
187    ureg_destroy(ureg);
188
189    shader->dummy = TRUE;
190    r300_init_vs_outputs(shader);
191    r300_translate_vertex_shader(r300, shader);
192}
193
194void r300_translate_vertex_shader(struct r300_context *r300,
195                                  struct r300_vertex_shader *vs)
196{
197    struct r300_vertex_program_compiler compiler;
198    struct tgsi_to_rc ttr;
199    unsigned i;
200
201    /* Setup the compiler */
202    memset(&compiler, 0, sizeof(compiler));
203    rc_init(&compiler.Base);
204
205    DBG_ON(r300, DBG_VP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
206    DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;
207    compiler.code = &vs->code;
208    compiler.UserData = vs;
209    compiler.Base.is_r500 = r300->screen->caps.is_r500;
210    compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
211    compiler.Base.has_half_swizzles = FALSE;
212    compiler.Base.has_presub = FALSE;
213    compiler.Base.max_temp_regs = 32;
214    compiler.Base.max_constants = 256;
215    compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;
216
217    if (compiler.Base.Debug & RC_DBG_LOG) {
218        DBG(r300, DBG_VP, "r300: Initial vertex program\n");
219        tgsi_dump(vs->state.tokens, 0);
220    }
221
222    /* Translate TGSI to our internal representation */
223    ttr.compiler = &compiler.Base;
224    ttr.info = &vs->info;
225    ttr.use_half_swizzles = FALSE;
226
227    r300_tgsi_to_rc(&ttr, vs->state.tokens);
228
229    if (ttr.error) {
230        fprintf(stderr, "r300 VP: Cannot translate a shader. "
231                "Using a dummy shader instead.\n");
232        r300_dummy_vertex_shader(r300, vs);
233        return;
234    }
235
236    if (compiler.Base.Program.Constants.Count > 200) {
237        compiler.Base.remove_unused_constants = TRUE;
238    }
239
240    compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs + 1));
241    compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
242
243    /* Insert the WPOS output. */
244    rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
245
246    /* Invoke the compiler */
247    r3xx_compile_vertex_program(&compiler);
248    if (compiler.Base.Error) {
249        fprintf(stderr, "r300 VP: Compiler error:\n%sUsing a dummy shader"
250                " instead.\n", compiler.Base.ErrorMsg);
251
252        if (vs->dummy) {
253            fprintf(stderr, "r300 VP: Cannot compile the dummy shader! "
254                    "Giving up...\n");
255            abort();
256        }
257
258        rc_destroy(&compiler.Base);
259        r300_dummy_vertex_shader(r300, vs);
260        return;
261    }
262
263    /* Initialize numbers of constants for each type. */
264    vs->externals_count = 0;
265    for (i = 0;
266         i < vs->code.constants.Count &&
267         vs->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
268        vs->externals_count = i+1;
269    }
270    for (; i < vs->code.constants.Count; i++) {
271        assert(vs->code.constants.Constants[i].Type == RC_CONSTANT_IMMEDIATE);
272    }
273    vs->immediates_count = vs->code.constants.Count - vs->externals_count;
274
275    /* And, finally... */
276    rc_destroy(&compiler.Base);
277}
278