summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/r300/r300_vs_draw.c
blob: 45b7b9380d494db652cb13103ec1d75ed14ec45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**************************************************************************
 * 
 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/* This file contains the vertex shader tranformations for SW TCL needed
 * to overcome the limitations of the r300 rasterizer.
 *
 * Transformations:
 * 1) If the secondary color output is present, the primary color must be
 *    present too.
 * 2) If any back-face color output is present, there must be all 4 color
 *    outputs and missing ones must be inserted.
 * 3) Insert a trailing texcoord output containing a copy of POS, for WPOS.
 *
 * I know this code is cumbersome, but I don't know of any nicer way
 * of transforming TGSI shaders. ~ M.
 */

#include "r300_vs.h"

#include <stdio.h>

#include "tgsi/tgsi_transform.h"
#include "tgsi/tgsi_dump.h"

#include "draw/draw_context.h"

struct vs_transform_context {
    struct tgsi_transform_context base;

    boolean color_used[2];
    boolean bcolor_used[2];

    /* Index of the pos output, typically 0. */
    unsigned pos_output;
    /* Index of the pos temp where all writes of pos are redirected to. */
    unsigned pos_temp;
    /* The index of the last generic output, after which we insert a new
     * output for WPOS. */
    int last_generic;

    unsigned num_outputs;
    /* Used to shift output decl. indices when inserting new ones. */
    unsigned decl_shift;
    /* Used to remap writes to output decls if their indices changed. */
    unsigned out_remap[32];

    /* First instruction processed? */
    boolean first_instruction;
    /* End instruction processed? */
    boolean end_instruction;

    boolean temp_used[1024];
};

static void emit_temp(struct tgsi_transform_context *ctx, unsigned reg)
{
    struct tgsi_full_declaration decl;

    decl = tgsi_default_full_declaration();
    decl.Declaration.File = TGSI_FILE_TEMPORARY;
    decl.Range.First = decl.Range.Last = reg;
    ctx->emit_declaration(ctx, &decl);
}

static void emit_output(struct tgsi_transform_context *ctx,
                        unsigned name, unsigned index, unsigned interp,
                        unsigned reg)
{
    struct vs_transform_context *vsctx = (struct vs_transform_context *)ctx;
    struct tgsi_full_declaration decl;

    decl = tgsi_default_full_declaration();
    decl.Declaration.File = TGSI_FILE_OUTPUT;
    decl.Declaration.Interpolate = 1;
    decl.Declaration.Semantic = TRUE;
    decl.Semantic.Name = name;
    decl.Semantic.Index = index;
    decl.Range.First = decl.Range.Last = reg;
    decl.Interp.Interpolate = interp;
    ctx->emit_declaration(ctx, &decl);
    ++vsctx->num_outputs;
}

static void insert_output_before(struct tgsi_transform_context *ctx,
                                 struct tgsi_full_declaration *before,
                                 unsigned name, unsigned index, unsigned interp)
{
    struct vs_transform_context *vsctx = (struct vs_transform_context *)ctx;
    unsigned i;

    /* Make a place for the new output. */
    for (i = before->Range.First; i < Elements(vsctx->out_remap); i++) {
        ++vsctx->out_remap[i];
    }

    /* Insert the new output. */
    emit_output(ctx, name, index, interp,
                before->Range.First + vsctx->decl_shift);

    ++vsctx->decl_shift;
}

static void insert_output_after(struct tgsi_transform_context *ctx,
                                struct tgsi_full_declaration *after,
                                unsigned name, unsigned index, unsigned interp)
{
    struct vs_transform_context *vsctx = (struct vs_transform_context *)ctx;
    unsigned i;

    /* Make a place for the new output. */
    for (i = after->Range.First+1; i < Elements(vsctx->out_remap); i++) {
        ++vsctx->out_remap[i];
    }

    /* Insert the new output. */
    emit_output(ctx, name, index, interp,
                after->Range.First + 1);

    ++vsctx->decl_shift;
}

static void transform_decl(struct tgsi_transform_context *ctx,
                           struct tgsi_full_declaration *decl)
{
    struct vs_transform_context *vsctx = (struct vs_transform_context *)ctx;
    unsigned i;

    if (decl->Declaration.File == TGSI_FILE_OUTPUT) {
        switch (decl->Semantic.Name) {
            case TGSI_SEMANTIC_POSITION:
                vsctx->pos_output = decl->Range.First;
                break;

            case TGSI_SEMANTIC_COLOR:
                assert(decl->Semantic.Index < 2);

                /* We must rasterize the first color if the second one is
                 * used, otherwise the rasterizer doesn't do the color
                 * selection correctly. Declare it, but don't write to it. */
                if (decl->Semantic.Index == 1 && !vsctx->color_used[0]) {
                    insert_output_before(ctx, decl, TGSI_SEMANTIC_COLOR, 0,
                                         TGSI_INTERPOLATE_LINEAR);
                    vsctx->color_used[0] = TRUE;
                }
                break;

            case TGSI_SEMANTIC_BCOLOR:
                assert(decl->Semantic.Index < 2);

                /* We must rasterize all 4 colors if back-face colors are
                 * used, otherwise the rasterizer doesn't do the color
                 * selection correctly. Declare it, but don't write to it. */
                if (!vsctx->color_used[0]) {
                    insert_output_before(ctx, decl, TGSI_SEMANTIC_COLOR, 0,
                                         TGSI_INTERPOLATE_LINEAR);
                    vsctx->color_used[0] = TRUE;
                }
                if (!vsctx->color_used[1]) {
                    insert_output_before(ctx, decl, TGSI_SEMANTIC_COLOR, 1,
                                         TGSI_INTERPOLATE_LINEAR);
                    vsctx->color_used[1] = TRUE;
                }
                if (decl->Semantic.Index == 1 && !vsctx->bcolor_used[0]) {
                    insert_output_before(ctx, decl, TGSI_SEMANTIC_BCOLOR, 0,
                                         TGSI_INTERPOLATE_LINEAR);
                    vsctx->bcolor_used[0] = TRUE;
                }
                break;

            case TGSI_SEMANTIC_GENERIC:
                vsctx->last_generic = MAX2(vsctx->last_generic, decl->Semantic.Index);
                break;
        }

        /* Since we're inserting new outputs in between, the following outputs
         * should be moved to the right so that they don't overlap with
         * the newly added ones. */
        decl->Range.First += vsctx->decl_shift;
        decl->Range.Last += vsctx->decl_shift;

        ++vsctx->num_outputs;
    } else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
        for (i = decl->Range.First; i <= decl->Range.Last; i++) {
           vsctx->temp_used[i] = TRUE;
        }
    }

    ctx->emit_declaration(ctx, decl);

    /* Insert BCOLOR1 if needed. */
    if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
        decl->Semantic.Name == TGSI_SEMANTIC_BCOLOR &&
        !vsctx->bcolor_used[1]) {
        insert_output_after(ctx, decl, TGSI_SEMANTIC_BCOLOR, 1,
                            TGSI_INTERPOLATE_LINEAR);
    }
}

static void transform_inst(struct tgsi_transform_context *ctx,
                           struct tgsi_full_instruction *inst)
{
    struct vs_transform_context *vsctx = (struct vs_transform_context *) ctx;
    struct tgsi_full_instruction new_inst;
    unsigned i;

    if (!vsctx->first_instruction) {
        vsctx->first_instruction = TRUE;

        /* Insert the generic output for WPOS. */
        emit_output(ctx, TGSI_SEMANTIC_GENERIC, vsctx->last_generic + 1,
                    TGSI_INTERPOLATE_PERSPECTIVE, vsctx->num_outputs);

        /* Find a free temp for POSITION. */
        for (i = 0; i < Elements(vsctx->temp_used); i++) {
            if (!vsctx->temp_used[i]) {
                emit_temp(ctx, i);
                vsctx->pos_temp = i;
                break;
            }
        }
    }

    if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
        /* MOV OUT[pos_output], TEMP[pos_temp]; */
        new_inst = tgsi_default_full_instruction();
        new_inst.Instruction.Opcode = TGSI_OPCODE_MOV;
        new_inst.Instruction.NumDstRegs = 1;
        new_inst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
        new_inst.Dst[0].Register.Index = vsctx->pos_output;
        new_inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;
        new_inst.Instruction.NumSrcRegs = 1;
        new_inst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
        new_inst.Src[0].Register.Index = vsctx->pos_temp;
        ctx->emit_instruction(ctx, &new_inst);

        /* MOV OUT[n-1], TEMP[pos_temp]; */
        new_inst = tgsi_default_full_instruction();
        new_inst.Instruction.Opcode = TGSI_OPCODE_MOV;
        new_inst.Instruction.NumDstRegs = 1;
        new_inst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
        new_inst.Dst[0].Register.Index = vsctx->num_outputs - 1;
        new_inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;
        new_inst.Instruction.NumSrcRegs = 1;
        new_inst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
        new_inst.Src[0].Register.Index = vsctx->pos_temp;
        ctx->emit_instruction(ctx, &new_inst);

        vsctx->end_instruction = TRUE;
    } else {
        /* Not an END instruction. */
        /* Fix writes to outputs. */
        for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
            struct tgsi_full_dst_register *dst = &inst->Dst[i];
            if (dst->Register.File == TGSI_FILE_OUTPUT) {
                if (dst->Register.Index == vsctx->pos_output) {
                    /* Replace writes to OUT[pos_output] with TEMP[pos_temp]. */
                    dst->Register.File = TGSI_FILE_TEMPORARY;
                    dst->Register.Index = vsctx->pos_temp;
                } else {
                    /* Not a position, good...
                     * Since we were changing the indices of output decls,
                     * we must redirect writes into them too. */
                    dst->Register.Index = vsctx->out_remap[dst->Register.Index];
                }
            }
        }

        /* Inserting 2 instructions before the END opcode moves all following
         * labels by 2. Subroutines are always after the END opcode so
         * they're always moved. */
        if (inst->Instruction.Opcode == TGSI_OPCODE_CAL) {
            inst->Label.Label += 2;
        }
        /* The labels of the following opcodes are moved only after
         * the END opcode. */
        if (vsctx->end_instruction &&
            (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
             inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
             inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP ||
             inst->Instruction.Opcode == TGSI_OPCODE_ENDLOOP)) {
            inst->Label.Label += 2;
        }
    }

    ctx->emit_instruction(ctx, inst);
}

void r300_draw_init_vertex_shader(struct r300_context *r300,
                                  struct r300_vertex_shader *vs)
{
    struct draw_context *draw = r300->draw;
    struct pipe_shader_state new_vs;
    struct tgsi_shader_info info;
    struct vs_transform_context transform;
    const uint newLen = tgsi_num_tokens(vs->state.tokens) + 100 /* XXX */;
    unsigned i;

    tgsi_scan_shader(vs->state.tokens, &info);

    new_vs.tokens = tgsi_alloc_tokens(newLen);
    if (new_vs.tokens == NULL)
        return;

    memset(&transform, 0, sizeof(transform));
    for (i = 0; i < Elements(transform.out_remap); i++) {
        transform.out_remap[i] = i;
    }
    transform.last_generic = -1;
    transform.base.transform_instruction = transform_inst;
    transform.base.transform_declaration = transform_decl;

    for (i = 0; i < info.num_outputs; i++) {
        unsigned index = info.output_semantic_index[i];

        switch (info.output_semantic_name[i]) {
            case TGSI_SEMANTIC_COLOR:
                assert(index < 2);
                transform.color_used[index] = TRUE;
                break;

            case TGSI_SEMANTIC_BCOLOR:
                assert(index < 2);
                transform.bcolor_used[index] = TRUE;
                break;
        }
    }

    tgsi_transform_shader(vs->state.tokens,
                          (struct tgsi_token*)new_vs.tokens,
                          newLen, &transform.base);

#if 0
    printf("----------------------------------------------\norig shader:\n");
    tgsi_dump(vs->state.tokens, 0);
    printf("----------------------------------------------\nnew shader:\n");
    tgsi_dump(new_vs.tokens, 0);
    printf("----------------------------------------------\n");
#endif

    /* Free old tokens. */
    FREE((void*)vs->state.tokens);

    vs->draw_vs = draw_create_vertex_shader(draw, &new_vs);

    /* Instead of duplicating and freeing the tokens, copy the pointer directly. */
    vs->state.tokens = new_vs.tokens;

    /* Init the VS output table for the rasterizer. */
    r300_init_vs_outputs(r300, vs);

    /* Make the last generic be WPOS. */
    vs->outputs.wpos = vs->outputs.generic[transform.last_generic + 1];
    vs->outputs.generic[transform.last_generic + 1] = ATTR_UNUSED;
}