6f4087048636b25cde8a2c0a19d3787521ead46c
[mesa.git] / src / gallium / drivers / svga / svga_tgsi_decl_sm30.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "pipe/p_shader_tokens.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "util/u_memory.h"
30
31 #include "svga_tgsi_emit.h"
32
33 static boolean translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic,
34 unsigned *usage,
35 unsigned *idx )
36 {
37 switch (semantic.Name) {
38 case TGSI_SEMANTIC_POSITION:
39 *idx = semantic.Index;
40 *usage = SVGA3D_DECLUSAGE_POSITION;
41 break;
42 case TGSI_SEMANTIC_COLOR:
43
44 *idx = semantic.Index;
45 *usage = SVGA3D_DECLUSAGE_COLOR;
46 break;
47 case TGSI_SEMANTIC_BCOLOR:
48 *idx = semantic.Index + 2; /* sharing with COLOR */
49 *usage = SVGA3D_DECLUSAGE_COLOR;
50 break;
51 case TGSI_SEMANTIC_FOG:
52 *idx = 0;
53 assert(semantic.Index == 0);
54 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
55 break;
56 case TGSI_SEMANTIC_PSIZE:
57 *idx = semantic.Index;
58 *usage = SVGA3D_DECLUSAGE_PSIZE;
59 break;
60 case TGSI_SEMANTIC_GENERIC:
61 *idx = semantic.Index + 1; /* texcoord[0] is reserved for fog & position */
62 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
63 break;
64 case TGSI_SEMANTIC_NORMAL:
65 *idx = semantic.Index;
66 *usage = SVGA3D_DECLUSAGE_NORMAL;
67 break;
68 default:
69 assert(0);
70 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
71 *idx = 0;
72 return FALSE;
73 }
74
75 return TRUE;
76 }
77
78
79 static boolean emit_decl( struct svga_shader_emitter *emit,
80 SVGA3dShaderDestToken reg,
81 unsigned usage,
82 unsigned index )
83 {
84 SVGA3DOpDclArgs dcl;
85 SVGA3dShaderInstToken opcode;
86
87 opcode = inst_token( SVGA3DOP_DCL );
88 dcl.values[0] = 0;
89 dcl.values[1] = 0;
90
91 dcl.dst = reg;
92 dcl.usage = usage;
93 dcl.index = index;
94 dcl.values[0] |= 1<<31;
95
96 return (emit_instruction(emit, opcode) &&
97 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
98 }
99
100 static boolean emit_vface_decl( struct svga_shader_emitter *emit )
101 {
102 if (!emit->emitted_vface) {
103 SVGA3dShaderDestToken reg =
104 dst_register( SVGA3DREG_MISCTYPE,
105 SVGA3DMISCREG_FACE );
106
107 if (!emit_decl( emit, reg, 0, 0 ))
108 return FALSE;
109
110 emit->emitted_vface = TRUE;
111 }
112 return TRUE;
113 }
114
115 static boolean
116 ps30_input_emit_depth_fog( struct svga_shader_emitter *emit,
117 struct src_register *out )
118 {
119 struct src_register reg;
120
121
122 if (emit->emitted_depth_fog) {
123 *out = emit->ps_depth_fog;
124 return TRUE;
125 }
126
127 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
128 return FALSE;
129
130 reg = src_register( SVGA3DREG_INPUT,
131 emit->ps30_input_count++ );
132
133 *out = emit->ps_depth_fog = reg;
134
135 emit->emitted_depth_fog = TRUE;
136
137 return emit_decl( emit, dst( reg ), SVGA3D_DECLUSAGE_TEXCOORD, 0 );
138 }
139
140 static boolean ps30_input( struct svga_shader_emitter *emit,
141 struct tgsi_declaration_semantic semantic,
142 unsigned idx )
143 {
144 unsigned usage, index;
145 SVGA3dShaderDestToken reg;
146
147 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
148
149 emit->ps_true_pos = src_register( SVGA3DREG_MISCTYPE,
150 SVGA3DMISCREG_POSITION );
151 emit->ps_true_pos.base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
152 TGSI_SWIZZLE_Y,
153 TGSI_SWIZZLE_Y,
154 TGSI_SWIZZLE_Y );
155 reg = writemask( dst(emit->ps_true_pos),
156 TGSI_WRITEMASK_XY );
157 emit->ps_reads_pos = TRUE;
158
159 if (emit->info.reads_z) {
160 emit->ps_temp_pos = dst_register( SVGA3DREG_TEMP,
161 emit->nr_hw_temp );
162
163 emit->input_map[idx] = src_register( SVGA3DREG_TEMP,
164 emit->nr_hw_temp );
165 emit->nr_hw_temp++;
166
167 if (!ps30_input_emit_depth_fog( emit, &emit->ps_depth_pos ))
168 return FALSE;
169
170 emit->ps_depth_pos.base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_Z,
171 TGSI_SWIZZLE_Z,
172 TGSI_SWIZZLE_Z,
173 TGSI_SWIZZLE_W );
174 }
175 else {
176 emit->input_map[idx] = emit->ps_true_pos;
177 }
178
179 return emit_decl( emit, reg, 0, 0 );
180 }
181 else if (emit->key.fkey.light_twoside &&
182 (semantic.Name == TGSI_SEMANTIC_COLOR)) {
183
184 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
185 return FALSE;
186
187 emit->internal_color_idx[emit->internal_color_count] = idx;
188 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count );
189 emit->ps30_input_count++;
190 emit->internal_color_count++;
191
192 reg = dst( emit->input_map[idx] );
193
194 if (!emit_decl( emit, reg, usage, index ))
195 return FALSE;
196
197 semantic.Name = TGSI_SEMANTIC_BCOLOR;
198 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
199 return FALSE;
200
201 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
202 return FALSE;
203
204 reg = dst_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
205
206 if (!emit_decl( emit, reg, usage, index ))
207 return FALSE;
208
209 if (!emit_vface_decl( emit ))
210 return FALSE;
211
212 return TRUE;
213 }
214 else if (semantic.Name == TGSI_SEMANTIC_FACE) {
215 if (!emit_vface_decl( emit ))
216 return FALSE;
217 emit->emit_frontface = TRUE;
218 emit->internal_frontface_idx = idx;
219 return TRUE;
220 }
221 else if (semantic.Name == TGSI_SEMANTIC_FOG) {
222
223 assert(semantic.Index == 0);
224
225 if (!ps30_input_emit_depth_fog( emit, &emit->input_map[idx] ))
226 return FALSE;
227
228 emit->input_map[idx].base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
229 TGSI_SWIZZLE_X,
230 TGSI_SWIZZLE_X,
231 TGSI_SWIZZLE_X );
232
233 return TRUE;
234 }
235 else {
236
237 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
238 return FALSE;
239
240 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
241 return FALSE;
242
243 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
244 reg = dst( emit->input_map[idx] );
245
246 if (!emit_decl( emit, reg, usage, index ))
247 return FALSE;
248
249 if (semantic.Name == TGSI_SEMANTIC_GENERIC &&
250 emit->key.fkey.sprite_origin_lower_left &&
251 index >= 1 &&
252 emit->key.fkey.tex[index - 1].sprite_texgen) {
253 /* This is a sprite texture coord with lower-left origin.
254 * We need to invert the texture T coordinate since the SVGA3D
255 * device only supports an upper-left origin.
256 */
257 unsigned unit = index - 1;
258
259 emit->inverted_texcoords |= (1 << unit);
260
261 /* save original texcoord reg */
262 emit->ps_true_texcoord[unit] = emit->input_map[idx];
263
264 /* this temp register will be the results of the MAD instruction */
265 emit->ps_inverted_texcoord[unit] =
266 src_register(SVGA3DREG_TEMP, emit->nr_hw_temp);
267 emit->nr_hw_temp++;
268
269 emit->ps_inverted_texcoord_input[unit] = idx;
270
271 /* replace input_map entry with the temp register */
272 emit->input_map[idx] = emit->ps_inverted_texcoord[unit];
273 }
274
275 return TRUE;
276 }
277
278 }
279
280
281 /* PS output registers are the same as 2.0
282 */
283 static boolean ps30_output( struct svga_shader_emitter *emit,
284 struct tgsi_declaration_semantic semantic,
285 unsigned idx )
286 {
287 SVGA3dShaderDestToken reg;
288
289 switch (semantic.Name) {
290 case TGSI_SEMANTIC_COLOR:
291 if (emit->unit == PIPE_SHADER_FRAGMENT &&
292 emit->key.fkey.white_fragments) {
293
294 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
295 emit->nr_hw_temp++ );
296 emit->temp_col[idx] = emit->output_map[idx];
297 emit->true_col[idx] = dst_register( SVGA3DREG_COLOROUT,
298 semantic.Index );
299 }
300 else {
301 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
302 semantic.Index );
303 }
304 break;
305 case TGSI_SEMANTIC_POSITION:
306 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
307 emit->nr_hw_temp++ );
308 emit->temp_pos = emit->output_map[idx];
309 emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
310 semantic.Index );
311 break;
312 default:
313 assert(0);
314 reg = dst_register( SVGA3DREG_COLOROUT, 0 );
315 break;
316 }
317
318 return TRUE;
319 }
320
321
322 /* We still make up the input semantics the same as in 2.0
323 */
324 static boolean vs30_input( struct svga_shader_emitter *emit,
325 struct tgsi_declaration_semantic semantic,
326 unsigned idx )
327 {
328 SVGA3DOpDclArgs dcl;
329 SVGA3dShaderInstToken opcode;
330 unsigned usage, index;
331
332 opcode = inst_token( SVGA3DOP_DCL );
333 dcl.values[0] = 0;
334 dcl.values[1] = 0;
335
336 if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
337 unsigned i;
338 unsigned offset = 0;
339 unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
340 /* adjust for prescale constants */
341 start_idx += emit->key.vkey.need_prescale ? 2 : 0;
342 /* compute the offset from the start of zero stride constants */
343 for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
344 if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
345 ++offset;
346 }
347 emit->input_map[idx] = src_register( SVGA3DREG_CONST,
348 start_idx + offset );
349 } else {
350 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
351 dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
352
353 assert(dcl.dst.reserved0);
354
355 svga_generate_vdecl_semantics( idx, &usage, &index );
356
357 dcl.usage = usage;
358 dcl.index = index;
359 dcl.values[0] |= 1<<31;
360
361 return (emit_instruction(emit, opcode) &&
362 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
363 }
364 return TRUE;
365 }
366
367 static boolean vs30_output_emit_depth_fog( struct svga_shader_emitter *emit,
368 SVGA3dShaderDestToken *out )
369 {
370 SVGA3dShaderDestToken reg;
371
372 if (emit->emitted_depth_fog) {
373 *out = emit->vs_depth_fog;
374 return TRUE;
375 }
376
377 reg = dst_register( SVGA3DREG_OUTPUT, emit->vs30_output_count++ );
378
379 *out = emit->vs_depth_fog = reg;
380
381 emit->emitted_depth_fog = TRUE;
382
383 return emit_decl( emit, reg, SVGA3D_DECLUSAGE_TEXCOORD, 0 );
384 }
385
386 /* VS3.0 outputs have proper declarations and semantic info for
387 * matching against PS inputs.
388 */
389 static boolean vs30_output( struct svga_shader_emitter *emit,
390 struct tgsi_declaration_semantic semantic,
391 unsigned idx )
392 {
393 SVGA3DOpDclArgs dcl;
394 SVGA3dShaderInstToken opcode;
395 unsigned usage, index;
396
397 opcode = inst_token( SVGA3DOP_DCL );
398 dcl.values[0] = 0;
399 dcl.values[1] = 0;
400
401 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
402 return FALSE;
403
404 if (emit->vs30_output_count >= SVGA3D_OUTPUTREG_MAX)
405 return FALSE;
406
407 dcl.dst = dst_register( SVGA3DREG_OUTPUT, emit->vs30_output_count++ );
408 dcl.usage = usage;
409 dcl.index = index;
410 dcl.values[0] |= 1<<31;
411
412 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
413 assert(idx == 0);
414 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
415 emit->nr_hw_temp++ );
416 emit->temp_pos = emit->output_map[idx];
417 emit->true_pos = dcl.dst;
418
419 /* Grab an extra output for the depth output */
420 if (!vs30_output_emit_depth_fog( emit, &emit->depth_pos ))
421 return FALSE;
422
423 }
424 else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
425 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
426 emit->nr_hw_temp++ );
427 emit->temp_psiz = emit->output_map[idx];
428
429 /* This has the effect of not declaring psiz (below) and not
430 * emitting the final MOV to true_psiz in the postamble.
431 */
432 if (!emit->key.vkey.allow_psiz)
433 return TRUE;
434
435 emit->true_psiz = dcl.dst;
436 }
437 else if (semantic.Name == TGSI_SEMANTIC_FOG) {
438 /*
439 * Fog is shared with depth.
440 * So we need to decrement out_count since emit_depth_fog will increment it.
441 */
442 emit->vs30_output_count--;
443
444 if (!vs30_output_emit_depth_fog( emit, &emit->output_map[idx] ))
445 return FALSE;
446
447 return TRUE;
448 }
449 else {
450 emit->output_map[idx] = dcl.dst;
451 }
452
453
454 return (emit_instruction(emit, opcode) &&
455 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
456 }
457
458 static boolean ps30_sampler( struct svga_shader_emitter *emit,
459 struct tgsi_declaration_semantic semantic,
460 unsigned idx )
461 {
462 SVGA3DOpDclArgs dcl;
463 SVGA3dShaderInstToken opcode;
464
465 opcode = inst_token( SVGA3DOP_DCL );
466 dcl.values[0] = 0;
467 dcl.values[1] = 0;
468
469 dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
470 dcl.type = svga_tgsi_sampler_type( emit, idx );
471 dcl.values[0] |= 1<<31;
472
473 return (emit_instruction(emit, opcode) &&
474 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
475 }
476
477
478 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
479 const struct tgsi_full_declaration *decl )
480 {
481 unsigned first = decl->Range.First;
482 unsigned last = decl->Range.Last;
483 unsigned semantic = 0;
484 unsigned semantic_idx = 0;
485 unsigned idx;
486
487 if (decl->Declaration.Semantic) {
488 semantic = decl->Semantic.Name;
489 semantic_idx = decl->Semantic.Index;
490 }
491
492 for( idx = first; idx <= last; idx++ ) {
493 boolean ok;
494
495 switch (decl->Declaration.File) {
496 case TGSI_FILE_SAMPLER:
497 assert (emit->unit == PIPE_SHADER_FRAGMENT);
498 ok = ps30_sampler( emit, decl->Semantic, idx );
499 break;
500
501 case TGSI_FILE_INPUT:
502 if (emit->unit == PIPE_SHADER_VERTEX)
503 ok = vs30_input( emit, decl->Semantic, idx );
504 else
505 ok = ps30_input( emit, decl->Semantic, idx );
506 break;
507
508 case TGSI_FILE_OUTPUT:
509 if (emit->unit == PIPE_SHADER_VERTEX)
510 ok = vs30_output( emit, decl->Semantic, idx );
511 else
512 ok = ps30_output( emit, decl->Semantic, idx );
513 break;
514
515 default:
516 /* don't need to declare other vars */
517 ok = TRUE;
518 }
519
520 if (!ok)
521 return FALSE;
522 }
523
524 return TRUE;
525 }
526
527
528