summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r300/r300_vs.c
blob: 741a1b69895c6cffc560679114a58900baa9a5a7 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright 2009 Corbin Simpson <MostAwesomeDude@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
 * on 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
 * THE AUTHOR(S) AND/OR THEIR 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. */

#include "r300_vs.h"

static void r300_vs_declare(struct r300_vs_asm* assembler,
                            struct tgsi_full_declaration* decl)
{
    switch (decl->Declaration.File) {
        case TGSI_FILE_INPUT:
            break;
        case TGSI_FILE_OUTPUT:
            switch (decl->Semantic.SemanticName) {
                case TGSI_SEMANTIC_POSITION:
                    assembler->tab[decl->DeclarationRange.First] = 0;
                    break;
                case TGSI_SEMANTIC_COLOR:
                    assembler->tab[decl->DeclarationRange.First] =
                        (assembler->point_size ? 1 : 0) +
                        assembler->out_colors;
                    break;
                case TGSI_SEMANTIC_FOG:
                case TGSI_SEMANTIC_GENERIC:
                    /* XXX multiple? */
                    assembler->tab[decl->DeclarationRange.First] =
                        (assembler->point_size ? 1 : 0) +
                        assembler->out_colors +
                        assembler->out_texcoords;
                    break;
                case TGSI_SEMANTIC_PSIZE:
                    assembler->tab[decl->DeclarationRange.First] = 1;
                    break;
                default:
                    debug_printf("r300: vs: Bad semantic declaration %d\n",
                        decl->Semantic.SemanticName);
                    break;
            }
            break;
        case TGSI_FILE_CONSTANT:
            break;
        case TGSI_FILE_TEMPORARY:
            assembler->temp_count++;
            break;
        default:
            debug_printf("r300: vs: Bad file %d\n", decl->Declaration.File);
            break;
    }
}

static INLINE unsigned r300_vs_src_type(struct r300_vs_asm* assembler,
                                        struct tgsi_src_register* src)
{
    switch (src->File) {
        case TGSI_FILE_NULL:
        case TGSI_FILE_INPUT:
            /* Probably a zero or one swizzle */
            return R300_PVS_SRC_REG_INPUT;
        case TGSI_FILE_TEMPORARY:
            return R300_PVS_SRC_REG_TEMPORARY;
        case TGSI_FILE_CONSTANT:
        case TGSI_FILE_IMMEDIATE:
            return R300_PVS_SRC_REG_CONSTANT;
        default:
            debug_printf("r300: vs: Unimplemented src type %d\n", src->File);
            break;
    }
    return 0;
}

static INLINE unsigned r300_vs_src(struct r300_vs_asm* assembler,
                                   struct tgsi_src_register* src)
{
    switch (src->File) {
        case TGSI_FILE_NULL:
        case TGSI_FILE_INPUT:
        case TGSI_FILE_TEMPORARY:
        case TGSI_FILE_CONSTANT:
            return src->Index;
        case TGSI_FILE_IMMEDIATE:
            return src->Index + assembler->imm_offset;
        default:
            debug_printf("r300: vs: Unimplemented src type %d\n", src->File);
            break;
    }
    return 0;
}

static INLINE unsigned r300_vs_dst_type(struct r300_vs_asm* assembler,
                                        struct tgsi_dst_register* dst)
{
    switch (dst->File) {
        case TGSI_FILE_TEMPORARY:
            return R300_PVS_DST_REG_TEMPORARY;
        case TGSI_FILE_OUTPUT:
            return R300_PVS_DST_REG_OUT;
        default:
            debug_printf("r300: vs: Unimplemented dst type %d\n", dst->File);
            break;
    }
    return 0;
}

static INLINE unsigned r300_vs_dst(struct r300_vs_asm* assembler,
                                   struct tgsi_dst_register* dst)
{
    switch (dst->File) {
        case TGSI_FILE_TEMPORARY:
            return dst->Index;
        case TGSI_FILE_OUTPUT:
            return assembler->tab[dst->Index];
        default:
            debug_printf("r300: vs: Unimplemented dst %d\n", dst->File);
            break;
    }
    return 0;
}

static uint32_t r300_vs_op(unsigned op)
{
    switch (op) {
        case TGSI_OPCODE_DP3:
        case TGSI_OPCODE_DP4:
            return R300_VE_DOT_PRODUCT;
        case TGSI_OPCODE_MUL:
            return R300_VE_MULTIPLY;
        case TGSI_OPCODE_ADD:
        case TGSI_OPCODE_MOV:
        case TGSI_OPCODE_SUB:
        case TGSI_OPCODE_SWZ:
            return R300_VE_ADD;
        case TGSI_OPCODE_MAX:
            return R300_VE_MAXIMUM;
        case TGSI_OPCODE_SLT:
            return R300_VE_SET_LESS_THAN;
        case TGSI_OPCODE_RSQ:
            return R300_PVS_DST_MATH_INST | R300_ME_RECIP_DX;
        case TGSI_OPCODE_MAD:
            return R300_PVS_DST_MACRO_INST | R300_PVS_MACRO_OP_2CLK_MADD;
        default:
            break;
    }
    return 0;
}

static uint32_t r300_vs_swiz(struct tgsi_full_src_register* reg)
{
    if (reg->SrcRegister.Extended) {
        return (reg->SrcRegister.Negate ? (0xf << 12) : 0) |
            reg->SrcRegisterExtSwz.ExtSwizzleX |
            (reg->SrcRegisterExtSwz.ExtSwizzleY << 3) |
            (reg->SrcRegisterExtSwz.ExtSwizzleZ << 6) |
            (reg->SrcRegisterExtSwz.ExtSwizzleW << 9);
    } else {
        return (reg->SrcRegister.Negate ? (0xf << 12) : 0) |
            reg->SrcRegister.SwizzleX |
            (reg->SrcRegister.SwizzleY << 3) |
            (reg->SrcRegister.SwizzleZ << 6) |
            (reg->SrcRegister.SwizzleW << 9);
    }
}

/* XXX icky icky icky icky */
static uint32_t r300_vs_scalar_swiz(struct tgsi_full_src_register* reg)
{
    if (reg->SrcRegister.Extended) {
        return (reg->SrcRegister.Negate ? (0xf << 12) : 0) |
            reg->SrcRegisterExtSwz.ExtSwizzleX |
            (reg->SrcRegisterExtSwz.ExtSwizzleX << 3) |
            (reg->SrcRegisterExtSwz.ExtSwizzleX << 6) |
            (reg->SrcRegisterExtSwz.ExtSwizzleX << 9);
    } else {
        return (reg->SrcRegister.Negate ? (0xf << 12) : 0) |
            reg->SrcRegister.SwizzleX |
            (reg->SrcRegister.SwizzleX << 3) |
            (reg->SrcRegister.SwizzleX << 6) |
            (reg->SrcRegister.SwizzleX << 9);
    }
}

/* XXX scalar stupidity */
static void r300_vs_emit_inst(struct r300_vertex_shader* vs,
                              struct r300_vs_asm* assembler,
                              struct tgsi_full_src_register* src,
                              struct tgsi_full_dst_register* dst,
                              unsigned op,
                              unsigned count,
                              boolean is_scalar)
{
    int i = vs->instruction_count;
    vs->instructions[i].inst0 = R300_PVS_DST_OPCODE(r300_vs_op(op)) |
        R300_PVS_DST_REG_TYPE(r300_vs_dst_type(assembler, &dst->DstRegister)) |
        R300_PVS_DST_OFFSET(r300_vs_dst(assembler, &dst->DstRegister)) |
        R300_PVS_DST_WE(dst->DstRegister.WriteMask);
    switch (count) {
        case 3:
            vs->instructions[i].inst3 =
                R300_PVS_SRC_REG_TYPE(r300_vs_src_type(assembler,
                            &src[2].SrcRegister)) |
                R300_PVS_SRC_OFFSET(r300_vs_src(assembler,
                            &src[2].SrcRegister)) |
                R300_PVS_SRC_SWIZZLE(r300_vs_swiz(&src[2]));
            /* Fall through */
        case 2:
            vs->instructions[i].inst2 =
                R300_PVS_SRC_REG_TYPE(r300_vs_src_type(assembler,
                            &src[1].SrcRegister)) |
                R300_PVS_SRC_OFFSET(r300_vs_src(assembler,
                            &src[1].SrcRegister)) |
                R300_PVS_SRC_SWIZZLE(r300_vs_swiz(&src[1]));
            /* Fall through */
        case 1:
            vs->instructions[i].inst1 =
                R300_PVS_SRC_REG_TYPE(r300_vs_src_type(assembler,
                            &src[0].SrcRegister)) |
                R300_PVS_SRC_OFFSET(r300_vs_src(assembler,
                            &src[0].SrcRegister)) |
                /* XXX the icky, it burns */
                R300_PVS_SRC_SWIZZLE(is_scalar ? r300_vs_scalar_swiz(&src[0])
                        : r300_vs_swiz(&src[0]));
            break;
    }
    vs->instruction_count++;
}

static void r300_vs_instruction(struct r300_vertex_shader* vs,
                                struct r300_vs_asm* assembler,
                                struct tgsi_full_instruction* inst)
{
    switch (inst->Instruction.Opcode) {
        case TGSI_OPCODE_RSQ:
            r300_vs_emit_inst(vs, assembler, inst->FullSrcRegisters,
                    &inst->FullDstRegisters[0], inst->Instruction.Opcode,
                    1, TRUE);
            break;
        case TGSI_OPCODE_SUB:
            inst->FullSrcRegisters[1].SrcRegister.Negate =
                !inst->FullSrcRegisters[1].SrcRegister.Negate;
            /* Fall through */
        case TGSI_OPCODE_ADD:
        case TGSI_OPCODE_MUL:
        case TGSI_OPCODE_MAX:
        case TGSI_OPCODE_SLT:
            r300_vs_emit_inst(vs, assembler, inst->FullSrcRegisters,
                    &inst->FullDstRegisters[0], inst->Instruction.Opcode,
                    2, FALSE);
            break;
        case TGSI_OPCODE_DP3:
            /* Set alpha swizzle to zero for src0 and src1 */
            if (!inst->FullSrcRegisters[0].SrcRegister.Extended) {
                inst->FullSrcRegisters[0].SrcRegister.Extended = TRUE;
                inst->FullSrcRegisters[0].SrcRegisterExtSwz.ExtSwizzleX =
                    inst->FullSrcRegisters[0].SrcRegister.SwizzleX;
                inst->FullSrcRegisters[0].SrcRegisterExtSwz.ExtSwizzleY =
                    inst->FullSrcRegisters[0].SrcRegister.SwizzleY;
                inst->FullSrcRegisters[0].SrcRegisterExtSwz.ExtSwizzleZ =
                    inst->FullSrcRegisters[0].SrcRegister.SwizzleZ;
            }
            inst->FullSrcRegisters[0].SrcRegisterExtSwz.ExtSwizzleW =
                TGSI_EXTSWIZZLE_ZERO;
            if (!inst->FullSrcRegisters[1].SrcRegister.Extended) {
                inst->FullSrcRegisters[1].SrcRegister.Extended = TRUE;
                inst->FullSrcRegisters[1].SrcRegisterExtSwz.ExtSwizzleX =
                    inst->FullSrcRegisters[1].SrcRegister.SwizzleX;
                inst->FullSrcRegisters[1].SrcRegisterExtSwz.ExtSwizzleY =
                    inst->FullSrcRegisters[1].SrcRegister.SwizzleY;
                inst->FullSrcRegisters[1].SrcRegisterExtSwz.ExtSwizzleZ =
                    inst->FullSrcRegisters[1].SrcRegister.SwizzleZ;
            }
            inst->FullSrcRegisters[1].SrcRegisterExtSwz.ExtSwizzleW =
                TGSI_EXTSWIZZLE_ZERO;
            /* Fall through */
        case TGSI_OPCODE_DP4:
            r300_vs_emit_inst(vs, assembler, inst->FullSrcRegisters,
                    &inst->FullDstRegisters[0], inst->Instruction.Opcode,
                    2, FALSE);
            break;
        case TGSI_OPCODE_MOV:
        case TGSI_OPCODE_SWZ:
            inst->FullSrcRegisters[1] = r300_constant_zero;
            r300_vs_emit_inst(vs, assembler, inst->FullSrcRegisters,
                    &inst->FullDstRegisters[0], inst->Instruction.Opcode,
                    2, FALSE);
            break;
        case TGSI_OPCODE_MAD:
            r300_vs_emit_inst(vs, assembler, inst->FullSrcRegisters,
                    &inst->FullDstRegisters[0], inst->Instruction.Opcode,
                    3, FALSE);
            break;
        case TGSI_OPCODE_END:
            break;
        default:
            debug_printf("r300: vs: Bad opcode %d\n",
                    inst->Instruction.Opcode);
            break;
    }
}

static void r300_vs_init(struct r300_vertex_shader* vs,
                         struct r300_vs_asm* assembler)
{
    struct tgsi_shader_info* info = &vs->info;
    int i;

    for (i = 0; i < info->num_outputs; i++) {
        switch (info->output_semantic_name[i]) {
            case TGSI_SEMANTIC_PSIZE:
                assembler->point_size = TRUE;
                break;
            case TGSI_SEMANTIC_COLOR:
                assembler->out_colors++;
                break;
            case TGSI_SEMANTIC_FOG:
            case TGSI_SEMANTIC_GENERIC:
                assembler->out_texcoords++;
                break;
        }
    }

    vs->instruction_count = 0;
}

void r300_translate_vertex_shader(struct r300_context* r300,
                                  struct r300_vertex_shader* vs)
{
    struct tgsi_parse_context parser;
    int i;
    struct r300_constant_buffer* consts =
        &r300->shader_constants[PIPE_SHADER_VERTEX];

    struct r300_vs_asm* assembler = CALLOC_STRUCT(r300_vs_asm);
    if (assembler == NULL) {
        return;
    }

    /* Init assembler. */
    r300_vs_init(vs, assembler);

    /* Setup starting offset for immediates. */
    assembler->imm_offset = consts->user_count;

    tgsi_parse_init(&parser, vs->state.tokens);

    while (!tgsi_parse_end_of_tokens(&parser)) {
        tgsi_parse_token(&parser);

        /* This is seriously the lamest way to create fragment programs ever.
         * I blame TGSI. */
        switch (parser.FullToken.Token.Type) {
            case TGSI_TOKEN_TYPE_DECLARATION:
                /* Allocated registers sitting at the beginning
                 * of the program. */
                r300_vs_declare(assembler, &parser.FullToken.FullDeclaration);
                break;
            case TGSI_TOKEN_TYPE_IMMEDIATE:
                debug_printf("r300: Emitting immediate to constant buffer, "
                        "position %d\n",
                        assembler->imm_offset + assembler->imm_count);
                /* I am not amused by the length of these. */
                for (i = 0; i < 4; i++) {
                    consts->constants[assembler->imm_offset +
                        assembler->imm_count][i] =
                        parser.FullToken.FullImmediate.u[i].Float;
                }
                assembler->imm_count++;
                break;
            case TGSI_TOKEN_TYPE_INSTRUCTION:
                r300_vs_instruction(vs, assembler,
                        &parser.FullToken.FullInstruction);
                break;
        }
    }

    debug_printf("r300: vs: %d texs and %d colors, first free reg is %d\n",
            assembler->tex_count, assembler->color_count,
            assembler->tex_count + assembler->color_count);

    consts->count = consts->user_count + assembler->imm_count;
    vs->uses_imms = assembler->imm_count;
    debug_printf("r300: vs: %d total constants, "
            "%d from user and %d from immediates\n", consts->count,
            consts->user_count, assembler->imm_count);

    debug_printf("r300: vs: tab: %d %d %d %d\n", assembler->tab[0],
            assembler->tab[1], assembler->tab[2], assembler->tab[3]);

    tgsi_dump(vs->state.tokens, 0);
    /* XXX finish r300 vertex shader dumper */
    r300_vs_dump(vs);

    tgsi_parse_free(&parser);
    FREE(assembler);

    /* And, finally... */
    vs->translated = TRUE;
}