summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r300/r300_state_shader.c
blob: 65d5c5a596efe8f898a6d0484a2e6b64606237c3 (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
/*
 * Copyright 2008 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_state_shader.h"

static void r300_copy_passthrough_shader(struct r300_fragment_shader* fs)
{
    struct r300_fragment_shader* pt = &r300_passthrough_fragment_shader;
    fs->shader.stack_size = pt->shader.stack_size;
    fs->alu_instruction_count = pt->alu_instruction_count;
    fs->tex_instruction_count = pt->tex_instruction_count;
    fs->indirections = pt->indirections;
    fs->instructions[0] = pt->instructions[0];
}

static void r500_copy_passthrough_shader(struct r500_fragment_shader* fs)
{
    struct r500_fragment_shader* pt = &r500_passthrough_fragment_shader;
    fs->shader.stack_size = pt->shader.stack_size;
    fs->instruction_count = pt->instruction_count;
    fs->instructions[0] = pt->instructions[0];
}

static void r300_fs_declare(struct r300_fs_asm* assembler,
                            struct tgsi_full_declaration* decl)
{
    switch (decl->Declaration.File) {
        case TGSI_FILE_INPUT:
            switch (decl->Semantic.SemanticName) {
                case TGSI_SEMANTIC_COLOR:
                    assembler->color_count++;
                    break;
                case TGSI_SEMANTIC_GENERIC:
                    assembler->tex_count++;
                    break;
                default:
                    debug_printf("r300: fs: Bad semantic declaration %d\n",
                        decl->Semantic.SemanticName);
                    break;
            }
            break;
        case TGSI_FILE_OUTPUT:
            break;
        default:
            debug_printf("r300: fs: Bad file %d\n", decl->Declaration.File);
            break;
    }

    assembler->temp_offset = assembler->color_count + assembler->tex_count;
}

/* XXX cover extended cases */
static INLINE uint32_t r500_rgb_swiz(struct tgsi_src_register* reg)
{
    uint32_t temp = reg->SwizzleX | (reg->SwizzleY << 3) |
        (reg->SwizzleZ << 6);
    return temp;
}

static INLINE uint32_t r500_alpha_swiz(struct tgsi_src_register* reg)
{
    return reg->SwizzleZ;
}

static INLINE void r500_emit_mov(struct r500_fragment_shader* fs,
                                 struct r300_fs_asm* assembler,
                                 struct tgsi_full_src_register* src,
                                 struct tgsi_full_dst_register* dst)
{
    int i = fs->instruction_count;
    fs->instructions[i].inst0 = R500_INST_TYPE_OUT |
        R500_INST_TEX_SEM_WAIT | R500_INST_LAST |
        R500_INST_RGB_OMASK_RGB | R500_INST_ALPHA_OMASK |
        R500_INST_RGB_CLAMP | R500_INST_ALPHA_CLAMP;
    fs->instructions[i].inst1 =
        R500_RGB_ADDR0(0) | R500_RGB_ADDR1(0) | R500_RGB_ADDR1_CONST |
        R500_RGB_ADDR2(0) | R500_RGB_ADDR2_CONST;
    fs->instructions[i].inst2 =
        R500_ALPHA_ADDR0(0) | R500_ALPHA_ADDR1(0) | R500_ALPHA_ADDR1_CONST |
        R500_ALPHA_ADDR2(0) | R500_ALPHA_ADDR2_CONST;
    fs->instructions[i].inst3 = R500_ALU_RGB_SEL_A_SRC0 |
        R500_SWIZ_RGB_A(r500_rgb_swiz(&src->SrcRegister)) |
        R500_ALU_RGB_SEL_B_SRC0 |
        R500_SWIZ_RGB_B(r500_rgb_swiz(&src->SrcRegister));
    fs->instructions[i].inst4 = R500_ALPHA_OP_CMP |
        R500_SWIZ_ALPHA_A(r500_alpha_swiz(&src->SrcRegister)) |
        R500_SWIZ_ALPHA_B(r500_alpha_swiz(&src->SrcRegister));
    fs->instructions[i].inst5 =
        R500_ALU_RGBA_OP_CMP | R500_ALU_RGBA_R_SWIZ_0 |
        R500_ALU_RGBA_G_SWIZ_0 | R500_ALU_RGBA_B_SWIZ_0 |
        R500_ALU_RGBA_A_SWIZ_0;

    fs->instruction_count++;
}

static void r500_fs_instruction(struct r500_fragment_shader* fs,
                                struct r300_fs_asm* assembler,
                                struct tgsi_full_instruction* inst)
{
    /* Switch between opcodes. When possible, prefer using the official
     * AMD/ATI names for opcodes, please, as it facilitates using the
     * documentation. */
    switch (inst->Instruction.Opcode) {
        case TGSI_OPCODE_MOV:
            r500_emit_mov(fs, assembler, &inst->FullSrcRegisters[0],
                    &inst->FullDstRegisters[0]);
            break;
        case TGSI_OPCODE_END:
            break;
        default:
            debug_printf("r300: fs: Bad opcode %d\n",
                    inst->Instruction.Opcode);
            break;
    }
}

void r300_translate_fragment_shader(struct r300_context* r300,
                                    struct r300_fragment_shader* fs)
{
    struct tgsi_parse_context parser;

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

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

    r300_copy_passthrough_shader(fs);
}

void r500_translate_fragment_shader(struct r300_context* r300,
                                    struct r500_fragment_shader* fs)
{
    struct r300_fs_asm* assembler = CALLOC_STRUCT(r300_fs_asm);
    if (assembler == NULL) {
        return;
    }
    struct tgsi_parse_context parser;

    tgsi_parse_init(&parser, fs->shader.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_fs_declare(assembler, &parser.FullToken.FullDeclaration);
                break;
            case TGSI_TOKEN_TYPE_INSTRUCTION:
                r500_fs_instruction(fs, assembler,
                        &parser.FullToken.FullInstruction);
        }

    }

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

    /* XXX subtly wrong */
    fs->shader.stack_size = assembler->temp_offset;

    tgsi_dump(fs->shader.state.tokens);

    //r500_copy_passthrough_shader(fs);

    tgsi_parse_free(&parser);
    FREE(assembler);
}