02dd7be7c494897010b2b7e8eda702ec38122047
[mesa.git] / src / gallium / drivers / r300 / r300_vs.c
1 /*
2 * Copyright 2009 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_vs.h"
24
25 #include "r300_context.h"
26 #include "r300_tgsi_to_rc.h"
27
28 #include "tgsi/tgsi_dump.h"
29 #include "tgsi/tgsi_parse.h"
30
31 #include "radeon_compiler.h"
32
33
34 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
35 {
36 struct r300_vertex_shader * vs = c->UserData;
37 struct tgsi_shader_info* info = &vs->info;
38 boolean pointsize = false;
39 int out_colors = 0;
40 int colors = 0;
41 int out_generic = 0;
42 int generic = 0;
43 int i;
44
45 /* Fill in the input mapping */
46 for (i = 0; i < info->num_inputs; i++)
47 c->code->inputs[i] = i;
48
49 /* Fill in the output mapping */
50 for (i = 0; i < info->num_outputs; i++) {
51 switch (info->output_semantic_name[i]) {
52 case TGSI_SEMANTIC_PSIZE:
53 pointsize = true;
54 break;
55 case TGSI_SEMANTIC_COLOR:
56 out_colors++;
57 break;
58 case TGSI_SEMANTIC_FOG:
59 case TGSI_SEMANTIC_GENERIC:
60 out_generic++;
61 break;
62 }
63 }
64
65 struct tgsi_parse_context parser;
66
67 tgsi_parse_init(&parser, vs->state.tokens);
68
69 while (!tgsi_parse_end_of_tokens(&parser)) {
70 tgsi_parse_token(&parser);
71
72 if (parser.FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
73 continue;
74
75 struct tgsi_full_declaration * decl = &parser.FullToken.FullDeclaration;
76
77 if (decl->Declaration.File != TGSI_FILE_OUTPUT)
78 continue;
79
80 switch (decl->Semantic.SemanticName) {
81 case TGSI_SEMANTIC_POSITION:
82 c->code->outputs[decl->DeclarationRange.First] = 0;
83 break;
84 case TGSI_SEMANTIC_PSIZE:
85 c->code->outputs[decl->DeclarationRange.First] = 1;
86 break;
87 case TGSI_SEMANTIC_COLOR:
88 c->code->outputs[decl->DeclarationRange.First] = 1 +
89 (pointsize ? 1 : 0) +
90 colors++;
91 break;
92 case TGSI_SEMANTIC_FOG:
93 case TGSI_SEMANTIC_GENERIC:
94 c->code->outputs[decl->DeclarationRange.First] = 1 +
95 (pointsize ? 1 : 0) +
96 out_colors +
97 generic++;
98 break;
99 default:
100 debug_printf("r300: vs: Bad semantic declaration %d\n",
101 decl->Semantic.SemanticName);
102 break;
103 }
104 }
105
106 tgsi_parse_free(&parser);
107 }
108
109
110 void r300_translate_vertex_shader(struct r300_context* r300,
111 struct r300_vertex_shader* vs)
112 {
113 struct r300_vertex_program_compiler compiler;
114 struct tgsi_to_rc ttr;
115
116 printf("translate_vertex_shader\n");
117
118 /* Setup the compiler */
119 rc_init(&compiler.Base);
120
121 compiler.Base.Debug = 1;
122 compiler.code = &vs->code;
123 compiler.UserData = vs;
124
125 if (compiler.Base.Debug) {
126 debug_printf("r300: Initial vertex program\n");
127 tgsi_dump(vs->state.tokens, 0);
128 }
129
130 /* Translate TGSI to our internal representation */
131 ttr.compiler = &compiler.Base;
132 ttr.info = &vs->info;
133
134 r300_tgsi_to_rc(&ttr, vs->state.tokens);
135
136 compiler.RequiredOutputs = ~(~0 << vs->info.num_outputs);
137 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
138
139 /* Invoke the compiler */
140 r3xx_compile_vertex_program(&compiler);
141 if (compiler.Base.Error) {
142 /* Todo: Fail gracefully */
143 fprintf(stderr, "r300 VP: Compiler error\n");
144 abort();
145 }
146
147 /* And, finally... */
148 rc_destroy(&compiler.Base);
149 vs->translated = TRUE;
150 }
151
152
153 /* XXX get these to r300_reg */
154 #define R300_PVS_DST_OPCODE(x) ((x) << 0)
155 # define R300_VE_DOT_PRODUCT 1
156 # define R300_VE_MULTIPLY 2
157 # define R300_VE_ADD 3
158 # define R300_VE_MAXIMUM 7
159 # define R300_VE_SET_LESS_THAN 10
160 #define R300_PVS_DST_MATH_INST (1 << 6)
161 # define R300_ME_RECIP_DX 6
162 #define R300_PVS_DST_MACRO_INST (1 << 7)
163 # define R300_PVS_MACRO_OP_2CLK_MADD 0
164 #define R300_PVS_DST_REG_TYPE(x) ((x) << 8)
165 # define R300_PVS_DST_REG_TEMPORARY 0
166 # define R300_PVS_DST_REG_A0 1
167 # define R300_PVS_DST_REG_OUT 2
168 # define R300_PVS_DST_REG_OUT_REPL_X 3
169 # define R300_PVS_DST_REG_ALT_TEMPORARY 4
170 # define R300_PVS_DST_REG_INPUT 5
171 #define R300_PVS_DST_OFFSET(x) ((x) << 13)
172 #define R300_PVS_DST_WE(x) ((x) << 20)
173 #define R300_PVS_DST_WE_XYZW (0xf << 20)
174
175 #define R300_PVS_SRC_REG_TYPE(x) ((x) << 0)
176 # define R300_PVS_SRC_REG_TEMPORARY 0
177 # define R300_PVS_SRC_REG_INPUT 1
178 # define R300_PVS_SRC_REG_CONSTANT 2
179 # define R300_PVS_SRC_REG_ALT_TEMPORARY 3
180 #define R300_PVS_SRC_OFFSET(x) ((x) << 5)
181 #define R300_PVS_SRC_SWIZZLE(x) ((x) << 13)
182 # define R300_PVS_SRC_SELECT_X 0
183 # define R300_PVS_SRC_SELECT_Y 1
184 # define R300_PVS_SRC_SELECT_Z 2
185 # define R300_PVS_SRC_SELECT_W 3
186 # define R300_PVS_SRC_SELECT_FORCE_0 4
187 # define R300_PVS_SRC_SELECT_FORCE_1 5
188 # define R300_PVS_SRC_SWIZZLE_XYZW \
189 ((R300_PVS_SRC_SELECT_X | (R300_PVS_SRC_SELECT_Y << 3) | \
190 (R300_PVS_SRC_SELECT_Z << 6) | (R300_PVS_SRC_SELECT_W << 9)) << 13)
191 # define R300_PVS_SRC_SWIZZLE_ZERO \
192 ((R300_PVS_SRC_SELECT_FORCE_0 | (R300_PVS_SRC_SELECT_FORCE_0 << 3) | \
193 (R300_PVS_SRC_SELECT_FORCE_0 << 6) | \
194 (R300_PVS_SRC_SELECT_FORCE_0 << 9)) << 13)
195 # define R300_PVS_SRC_SWIZZLE_ONE \
196 ((R300_PVS_SRC_SELECT_FORCE_1 | (R300_PVS_SRC_SELECT_FORCE_1 << 3) | \
197 (R300_PVS_SRC_SELECT_FORCE_1 << 6) | \
198 (R300_PVS_SRC_SELECT_FORCE_1 << 9)) << 13)
199 #define R300_PVS_MODIFIER_X (1 << 25)
200 #define R300_PVS_MODIFIER_Y (1 << 26)
201 #define R300_PVS_MODIFIER_Z (1 << 27)
202 #define R300_PVS_MODIFIER_W (1 << 28)
203 #define R300_PVS_NEGATE_XYZW \
204 (R300_PVS_MODIFIER_X | R300_PVS_MODIFIER_Y | \
205 R300_PVS_MODIFIER_Z | R300_PVS_MODIFIER_W)
206
207 struct r300_vertex_program_code r300_passthrough_vertex_shader = {
208 .length = 8, /* two instructions */
209
210 /* MOV out[0], in[0] */
211 .body.d[0] = R300_PVS_DST_OPCODE(R300_VE_ADD) |
212 R300_PVS_DST_REG_TYPE(R300_PVS_DST_REG_OUT) |
213 R300_PVS_DST_OFFSET(0) | R300_PVS_DST_WE_XYZW,
214 .body.d[1] = R300_PVS_SRC_REG_TYPE(R300_PVS_SRC_REG_INPUT) |
215 R300_PVS_SRC_OFFSET(0) | R300_PVS_SRC_SWIZZLE_XYZW,
216 .body.d[2] = R300_PVS_SRC_SWIZZLE_ZERO,
217 .body.d[3] = 0x0,
218
219 /* MOV out[1], in[1] */
220 .body.d[4] = R300_PVS_DST_OPCODE(R300_VE_ADD) |
221 R300_PVS_DST_REG_TYPE(R300_PVS_DST_REG_OUT) |
222 R300_PVS_DST_OFFSET(1) | R300_PVS_DST_WE_XYZW,
223 .body.d[5] = R300_PVS_SRC_REG_TYPE(R300_PVS_SRC_REG_INPUT) |
224 R300_PVS_SRC_OFFSET(1) | R300_PVS_SRC_SWIZZLE_XYZW,
225 .body.d[6] = R300_PVS_SRC_SWIZZLE_ZERO,
226 .body.d[7] = 0x0,
227
228 .inputs[0] = 0,
229 .inputs[1] = 1,
230 .outputs[0] = 0,
231 .outputs[1] = 1,
232
233 .InputsRead = 3,
234 .OutputsWritten = 3
235 };
236