r300-gallium: Start swizzles.
[mesa.git] / src / gallium / drivers / r300 / r300_state_shader.c
1 /*
2 * Copyright 2008 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_state_shader.h"
24
25 static void r300_copy_passthrough_shader(struct r300_fragment_shader* fs)
26 {
27 struct r300_fragment_shader* pt = &r300_passthrough_fragment_shader;
28 fs->shader.stack_size = pt->shader.stack_size;
29 fs->alu_instruction_count = pt->alu_instruction_count;
30 fs->tex_instruction_count = pt->tex_instruction_count;
31 fs->indirections = pt->indirections;
32 fs->instructions[0] = pt->instructions[0];
33 }
34
35 static void r500_copy_passthrough_shader(struct r500_fragment_shader* fs)
36 {
37 struct r500_fragment_shader* pt = &r500_passthrough_fragment_shader;
38 fs->shader.stack_size = pt->shader.stack_size;
39 fs->instruction_count = pt->instruction_count;
40 fs->instructions[0] = pt->instructions[0];
41 }
42
43 static void r300_fs_declare(struct r300_fs_asm* assembler,
44 struct tgsi_full_declaration* decl)
45 {
46 switch (decl->Declaration.File) {
47 case TGSI_FILE_INPUT:
48 switch (decl->Semantic.SemanticName) {
49 case TGSI_SEMANTIC_COLOR:
50 assembler->color_count++;
51 break;
52 case TGSI_SEMANTIC_GENERIC:
53 assembler->tex_count++;
54 break;
55 default:
56 debug_printf("r300: fs: Bad semantic declaration %d\n",
57 decl->Semantic.SemanticName);
58 break;
59 }
60 break;
61 case TGSI_FILE_OUTPUT:
62 break;
63 default:
64 debug_printf("r300: fs: Bad file %d\n", decl->Declaration.File);
65 break;
66 }
67
68 assembler->temp_offset = assembler->color_count + assembler->tex_count;
69 }
70
71 /* XXX cover extended cases */
72 static INLINE uint32_t r500_rgb_swiz(struct tgsi_src_register* reg)
73 {
74 uint32_t temp = reg->SwizzleX | (reg->SwizzleY << 3) |
75 (reg->SwizzleZ << 6);
76 return temp;
77 }
78
79 static INLINE uint32_t r500_alpha_swiz(struct tgsi_src_register* reg)
80 {
81 return reg->SwizzleZ;
82 }
83
84 static INLINE void r500_emit_mov(struct r500_fragment_shader* fs,
85 struct r300_fs_asm* assembler,
86 struct tgsi_full_src_register* src,
87 struct tgsi_full_dst_register* dst)
88 {
89 int i = fs->instruction_count;
90 fs->instructions[i].inst0 = R500_INST_TYPE_OUT |
91 R500_INST_TEX_SEM_WAIT | R500_INST_LAST |
92 R500_INST_RGB_OMASK_RGB | R500_INST_ALPHA_OMASK |
93 R500_INST_RGB_CLAMP | R500_INST_ALPHA_CLAMP;
94 fs->instructions[i].inst1 =
95 R500_RGB_ADDR0(0) | R500_RGB_ADDR1(0) | R500_RGB_ADDR1_CONST |
96 R500_RGB_ADDR2(0) | R500_RGB_ADDR2_CONST;
97 fs->instructions[i].inst2 =
98 R500_ALPHA_ADDR0(0) | R500_ALPHA_ADDR1(0) | R500_ALPHA_ADDR1_CONST |
99 R500_ALPHA_ADDR2(0) | R500_ALPHA_ADDR2_CONST;
100 fs->instructions[i].inst3 = R500_ALU_RGB_SEL_A_SRC0 |
101 R500_SWIZ_RGB_A(r500_rgb_swiz(&src->SrcRegister)) |
102 R500_ALU_RGB_SEL_B_SRC0 |
103 R500_SWIZ_RGB_B(r500_rgb_swiz(&src->SrcRegister));
104 fs->instructions[i].inst4 = R500_ALPHA_OP_CMP |
105 R500_SWIZ_ALPHA_A(r500_alpha_swiz(&src->SrcRegister)) |
106 R500_SWIZ_ALPHA_B(r500_alpha_swiz(&src->SrcRegister));
107 fs->instructions[i].inst5 =
108 R500_ALU_RGBA_OP_CMP | R500_ALU_RGBA_R_SWIZ_0 |
109 R500_ALU_RGBA_G_SWIZ_0 | R500_ALU_RGBA_B_SWIZ_0 |
110 R500_ALU_RGBA_A_SWIZ_0;
111
112 fs->instruction_count++;
113 }
114
115 static void r500_fs_instruction(struct r500_fragment_shader* fs,
116 struct r300_fs_asm* assembler,
117 struct tgsi_full_instruction* inst)
118 {
119 /* Switch between opcodes. When possible, prefer using the official
120 * AMD/ATI names for opcodes, please, as it facilitates using the
121 * documentation. */
122 switch (inst->Instruction.Opcode) {
123 case TGSI_OPCODE_MOV:
124 r500_emit_mov(fs, assembler, &inst->FullSrcRegisters[0],
125 &inst->FullDstRegisters[0]);
126 break;
127 case TGSI_OPCODE_END:
128 break;
129 default:
130 debug_printf("r300: fs: Bad opcode %d\n",
131 inst->Instruction.Opcode);
132 break;
133 }
134 }
135
136 void r300_translate_fragment_shader(struct r300_context* r300,
137 struct r300_fragment_shader* fs)
138 {
139 struct tgsi_parse_context parser;
140
141 tgsi_parse_init(&parser, fs->shader.state.tokens);
142
143 while (!tgsi_parse_end_of_tokens(&parser)) {
144 tgsi_parse_token(&parser);
145 }
146
147 r300_copy_passthrough_shader(fs);
148 }
149
150 void r500_translate_fragment_shader(struct r300_context* r300,
151 struct r500_fragment_shader* fs)
152 {
153 struct r300_fs_asm* assembler = CALLOC_STRUCT(r300_fs_asm);
154 if (assembler == NULL) {
155 return;
156 }
157 struct tgsi_parse_context parser;
158
159 tgsi_parse_init(&parser, fs->shader.state.tokens);
160
161 while (!tgsi_parse_end_of_tokens(&parser)) {
162 tgsi_parse_token(&parser);
163
164 /* This is seriously the lamest way to create fragment programs ever.
165 * I blame TGSI. */
166 switch (parser.FullToken.Token.Type) {
167 case TGSI_TOKEN_TYPE_DECLARATION:
168 /* Allocated registers sitting at the beginning
169 * of the program. */
170 r300_fs_declare(assembler, &parser.FullToken.FullDeclaration);
171 break;
172 case TGSI_TOKEN_TYPE_INSTRUCTION:
173 r500_fs_instruction(fs, assembler,
174 &parser.FullToken.FullInstruction);
175 }
176
177 }
178
179 debug_printf("%d texs and %d colors, first free reg is %d\n",
180 assembler->tex_count, assembler->color_count,
181 assembler->tex_count + assembler->color_count);
182
183 /* XXX subtly wrong */
184 fs->shader.stack_size = assembler->temp_offset;
185
186 tgsi_dump(fs->shader.state.tokens);
187
188 //r500_copy_passthrough_shader(fs);
189
190 tgsi_parse_free(&parser);
191 FREE(assembler);
192 }