a4e80b5571d77b7495972460affe571956c1a051
[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
34 /**
35 * Translate TGSI semantic info into SVGA3d semantic info.
36 * This is called for VS outputs and PS inputs only.
37 */
38 static boolean
39 translate_vs_ps_semantic(struct svga_shader_emitter *emit,
40 struct tgsi_declaration_semantic semantic,
41 unsigned *usage,
42 unsigned *idx)
43 {
44 switch (semantic.Name) {
45 case TGSI_SEMANTIC_POSITION:
46 *idx = semantic.Index;
47 *usage = SVGA3D_DECLUSAGE_POSITION;
48 break;
49 case TGSI_SEMANTIC_COLOR:
50 *idx = semantic.Index;
51 *usage = SVGA3D_DECLUSAGE_COLOR;
52 break;
53 case TGSI_SEMANTIC_BCOLOR:
54 *idx = semantic.Index + 2; /* sharing with COLOR */
55 *usage = SVGA3D_DECLUSAGE_COLOR;
56 break;
57 case TGSI_SEMANTIC_FOG:
58 *idx = 0;
59 assert(semantic.Index == 0);
60 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
61 break;
62 case TGSI_SEMANTIC_PSIZE:
63 *idx = semantic.Index;
64 *usage = SVGA3D_DECLUSAGE_PSIZE;
65 break;
66 case TGSI_SEMANTIC_GENERIC:
67 *idx = svga_remap_generic_index(emit->key.generic_remap_table,
68 semantic.Index);
69 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
70 break;
71 case TGSI_SEMANTIC_NORMAL:
72 *idx = semantic.Index;
73 *usage = SVGA3D_DECLUSAGE_NORMAL;
74 break;
75 default:
76 assert(0);
77 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
78 *idx = 0;
79 return FALSE;
80 }
81
82 return TRUE;
83 }
84
85
86 /**
87 * Emit a PS input (or VS depth/fog output) register declaration.
88 * For example, if usage = SVGA3D_DECLUSAGE_TEXCOORD, reg.num = 1, and
89 * index = 3, we'll emit "dcl_texcoord3 v1".
90 */
91 static boolean
92 emit_decl(struct svga_shader_emitter *emit,
93 SVGA3dShaderDestToken reg,
94 unsigned usage,
95 unsigned index)
96 {
97 SVGA3DOpDclArgs dcl;
98 SVGA3dShaderInstToken opcode;
99
100 /* check values against bitfield sizes */
101 assert(index < 16);
102 assert(usage <= SVGA3D_DECLUSAGE_MAX);
103
104 opcode = inst_token( SVGA3DOP_DCL );
105 dcl.values[0] = 0;
106 dcl.values[1] = 0;
107
108 dcl.dst = reg;
109 dcl.usage = usage;
110 dcl.index = index;
111 dcl.values[0] |= 1<<31;
112
113 return (emit_instruction(emit, opcode) &&
114 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
115 }
116
117
118 /**
119 * Emit declaration for PS front/back-face input register.
120 */
121 static boolean
122 emit_vface_decl(struct svga_shader_emitter *emit)
123 {
124 if (!emit->emitted_vface) {
125 SVGA3dShaderDestToken reg =
126 dst_register(SVGA3DREG_MISCTYPE, SVGA3DMISCREG_FACE);
127
128 if (!emit_decl( emit, reg, 0, 0 ))
129 return FALSE;
130
131 emit->emitted_vface = TRUE;
132 }
133 return TRUE;
134 }
135
136
137 /**
138 * Emit PS input register to pass depth/fog coordinates.
139 * Note that this always goes into texcoord[0].
140 */
141 static boolean
142 ps30_input_emit_depth_fog( struct svga_shader_emitter *emit,
143 struct src_register *out )
144 {
145 struct src_register reg;
146
147 if (emit->emitted_depth_fog) {
148 *out = emit->ps_depth_fog;
149 return TRUE;
150 }
151
152 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
153 return FALSE;
154
155 reg = src_register( SVGA3DREG_INPUT,
156 emit->ps30_input_count++ );
157
158 *out = emit->ps_depth_fog = reg;
159
160 emit->emitted_depth_fog = TRUE;
161
162 return emit_decl( emit, dst( reg ), SVGA3D_DECLUSAGE_TEXCOORD, 0 );
163 }
164
165
166 /**
167 * Process a PS input declaration.
168 * We'll emit a declaration like "dcl_texcoord1 v2"
169 */
170 static boolean
171 ps30_input(struct svga_shader_emitter *emit,
172 struct tgsi_declaration_semantic semantic,
173 unsigned idx)
174 {
175 unsigned usage, index;
176 SVGA3dShaderDestToken reg;
177
178 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
179
180 emit->ps_true_pos = src_register( SVGA3DREG_MISCTYPE,
181 SVGA3DMISCREG_POSITION );
182 emit->ps_true_pos.base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
183 TGSI_SWIZZLE_Y,
184 TGSI_SWIZZLE_Y,
185 TGSI_SWIZZLE_Y );
186 reg = writemask( dst(emit->ps_true_pos),
187 TGSI_WRITEMASK_XY );
188 emit->ps_reads_pos = TRUE;
189
190 if (emit->info.reads_z) {
191 emit->ps_temp_pos = dst_register( SVGA3DREG_TEMP,
192 emit->nr_hw_temp );
193
194 emit->input_map[idx] = src_register( SVGA3DREG_TEMP,
195 emit->nr_hw_temp );
196 emit->nr_hw_temp++;
197
198 if (!ps30_input_emit_depth_fog( emit, &emit->ps_depth_pos ))
199 return FALSE;
200
201 emit->ps_depth_pos.base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_Z,
202 TGSI_SWIZZLE_Z,
203 TGSI_SWIZZLE_Z,
204 TGSI_SWIZZLE_W );
205 }
206 else {
207 emit->input_map[idx] = emit->ps_true_pos;
208 }
209
210 return emit_decl( emit, reg, 0, 0 );
211 }
212 else if (emit->key.fkey.light_twoside &&
213 (semantic.Name == TGSI_SEMANTIC_COLOR)) {
214
215 if (!translate_vs_ps_semantic( emit, semantic, &usage, &index ))
216 return FALSE;
217
218 emit->internal_color_idx[emit->internal_color_count] = idx;
219 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count );
220 emit->ps30_input_count++;
221 emit->internal_color_count++;
222
223 reg = dst( emit->input_map[idx] );
224
225 if (!emit_decl( emit, reg, usage, index ))
226 return FALSE;
227
228 semantic.Name = TGSI_SEMANTIC_BCOLOR;
229 if (!translate_vs_ps_semantic( emit, semantic, &usage, &index ))
230 return FALSE;
231
232 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
233 return FALSE;
234
235 reg = dst_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
236
237 if (!emit_decl( emit, reg, usage, index ))
238 return FALSE;
239
240 if (!emit_vface_decl( emit ))
241 return FALSE;
242
243 return TRUE;
244 }
245 else if (semantic.Name == TGSI_SEMANTIC_FACE) {
246 if (!emit_vface_decl( emit ))
247 return FALSE;
248 emit->emit_frontface = TRUE;
249 emit->internal_frontface_idx = idx;
250 return TRUE;
251 }
252 else if (semantic.Name == TGSI_SEMANTIC_FOG) {
253
254 assert(semantic.Index == 0);
255
256 if (!ps30_input_emit_depth_fog( emit, &emit->input_map[idx] ))
257 return FALSE;
258
259 emit->input_map[idx].base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
260 TGSI_SWIZZLE_X,
261 TGSI_SWIZZLE_X,
262 TGSI_SWIZZLE_X );
263
264 return TRUE;
265 }
266 else {
267
268 if (!translate_vs_ps_semantic( emit, semantic, &usage, &index ))
269 return FALSE;
270
271 if (emit->ps30_input_count >= SVGA3D_INPUTREG_MAX)
272 return FALSE;
273
274 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
275 reg = dst( emit->input_map[idx] );
276
277 if (!emit_decl( emit, reg, usage, index ))
278 return FALSE;
279
280 if (semantic.Name == TGSI_SEMANTIC_GENERIC &&
281 emit->key.fkey.sprite_origin_lower_left &&
282 index >= 1 &&
283 emit->key.fkey.tex[index - 1].sprite_texgen) {
284 /* This is a sprite texture coord with lower-left origin.
285 * We need to invert the texture T coordinate since the SVGA3D
286 * device only supports an upper-left origin.
287 */
288 unsigned unit = index - 1;
289
290 emit->inverted_texcoords |= (1 << unit);
291
292 /* save original texcoord reg */
293 emit->ps_true_texcoord[unit] = emit->input_map[idx];
294
295 /* this temp register will be the results of the MAD instruction */
296 emit->ps_inverted_texcoord[unit] =
297 src_register(SVGA3DREG_TEMP, emit->nr_hw_temp);
298 emit->nr_hw_temp++;
299
300 emit->ps_inverted_texcoord_input[unit] = idx;
301
302 /* replace input_map entry with the temp register */
303 emit->input_map[idx] = emit->ps_inverted_texcoord[unit];
304 }
305
306 return TRUE;
307 }
308
309 }
310
311
312 /**
313 * Process a PS output declaration.
314 * Note that we don't actually emit a SVGA3DOpDcl for PS outputs.
315 */
316 static boolean
317 ps30_output(struct svga_shader_emitter *emit,
318 struct tgsi_declaration_semantic semantic,
319 unsigned idx)
320 {
321 switch (semantic.Name) {
322 case TGSI_SEMANTIC_COLOR:
323 if (emit->unit == PIPE_SHADER_FRAGMENT &&
324 emit->key.fkey.white_fragments) {
325
326 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
327 emit->nr_hw_temp++ );
328 emit->temp_col[idx] = emit->output_map[idx];
329 emit->true_col[idx] = dst_register( SVGA3DREG_COLOROUT,
330 semantic.Index );
331 }
332 else {
333 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
334 semantic.Index );
335 }
336 break;
337 case TGSI_SEMANTIC_POSITION:
338 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
339 emit->nr_hw_temp++ );
340 emit->temp_pos = emit->output_map[idx];
341 emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
342 semantic.Index );
343 break;
344 default:
345 assert(0);
346 /* A wild stab in the dark. */
347 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT, 0 );
348 break;
349 }
350
351 return TRUE;
352 }
353
354
355 /**
356 * Declare a VS input register.
357 * We still make up the input semantics the same as in 2.0
358 */
359 static boolean
360 vs30_input(struct svga_shader_emitter *emit,
361 struct tgsi_declaration_semantic semantic,
362 unsigned idx)
363 {
364 SVGA3DOpDclArgs dcl;
365 SVGA3dShaderInstToken opcode;
366 unsigned usage, index;
367
368 opcode = inst_token( SVGA3DOP_DCL );
369 dcl.values[0] = 0;
370 dcl.values[1] = 0;
371
372 if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
373 unsigned i;
374 unsigned offset = 0;
375 unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
376 /* adjust for prescale constants */
377 start_idx += emit->key.vkey.need_prescale ? 2 : 0;
378 /* compute the offset from the start of zero stride constants */
379 for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
380 if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
381 ++offset;
382 }
383 emit->input_map[idx] = src_register( SVGA3DREG_CONST,
384 start_idx + offset );
385 } else {
386 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
387 dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
388
389 assert(dcl.dst.reserved0);
390
391 svga_generate_vdecl_semantics( idx, &usage, &index );
392
393 dcl.usage = usage;
394 dcl.index = index;
395 dcl.values[0] |= 1<<31;
396
397 return (emit_instruction(emit, opcode) &&
398 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
399 }
400 return TRUE;
401 }
402
403
404 /**
405 * Declare VS output for holding depth/fog.
406 */
407 static boolean
408 vs30_output_emit_depth_fog(struct svga_shader_emitter *emit,
409 SVGA3dShaderDestToken *out)
410 {
411 SVGA3dShaderDestToken reg;
412
413 if (emit->emitted_depth_fog) {
414 *out = emit->vs_depth_fog;
415 return TRUE;
416 }
417
418 reg = dst_register( SVGA3DREG_OUTPUT, emit->vs30_output_count++ );
419
420 *out = emit->vs_depth_fog = reg;
421
422 emit->emitted_depth_fog = TRUE;
423
424 return emit_decl( emit, reg, SVGA3D_DECLUSAGE_TEXCOORD, 0 );
425 }
426
427
428 /**
429 * Declare a VS output.
430 * VS3.0 outputs have proper declarations and semantic info for
431 * matching against PS inputs.
432 */
433 static boolean
434 vs30_output(struct svga_shader_emitter *emit,
435 struct tgsi_declaration_semantic semantic,
436 unsigned idx)
437 {
438 SVGA3DOpDclArgs dcl;
439 SVGA3dShaderInstToken opcode;
440 unsigned usage, index;
441
442 opcode = inst_token( SVGA3DOP_DCL );
443 dcl.values[0] = 0;
444 dcl.values[1] = 0;
445
446 if (!translate_vs_ps_semantic( emit, semantic, &usage, &index ))
447 return FALSE;
448
449 if (emit->vs30_output_count >= SVGA3D_OUTPUTREG_MAX)
450 return FALSE;
451
452 dcl.dst = dst_register( SVGA3DREG_OUTPUT, emit->vs30_output_count++ );
453 dcl.usage = usage;
454 dcl.index = index;
455 dcl.values[0] |= 1<<31;
456
457 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
458 assert(idx == 0);
459 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
460 emit->nr_hw_temp++ );
461 emit->temp_pos = emit->output_map[idx];
462 emit->true_pos = dcl.dst;
463
464 /* Grab an extra output for the depth output */
465 if (!vs30_output_emit_depth_fog( emit, &emit->depth_pos ))
466 return FALSE;
467
468 }
469 else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
470 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
471 emit->nr_hw_temp++ );
472 emit->temp_psiz = emit->output_map[idx];
473
474 /* This has the effect of not declaring psiz (below) and not
475 * emitting the final MOV to true_psiz in the postamble.
476 */
477 if (!emit->key.vkey.allow_psiz)
478 return TRUE;
479
480 emit->true_psiz = dcl.dst;
481 }
482 else if (semantic.Name == TGSI_SEMANTIC_FOG) {
483 /*
484 * Fog is shared with depth.
485 * So we need to decrement out_count since emit_depth_fog will increment it.
486 */
487 emit->vs30_output_count--;
488
489 if (!vs30_output_emit_depth_fog( emit, &emit->output_map[idx] ))
490 return FALSE;
491
492 return TRUE;
493 }
494 else {
495 emit->output_map[idx] = dcl.dst;
496 }
497
498 return (emit_instruction(emit, opcode) &&
499 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
500 }
501
502
503 static boolean
504 ps30_sampler( struct svga_shader_emitter *emit,
505 struct tgsi_declaration_semantic semantic,
506 unsigned idx )
507 {
508 SVGA3DOpDclArgs dcl;
509 SVGA3dShaderInstToken opcode;
510
511 opcode = inst_token( SVGA3DOP_DCL );
512 dcl.values[0] = 0;
513 dcl.values[1] = 0;
514
515 dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
516 dcl.type = svga_tgsi_sampler_type( emit, idx );
517 dcl.values[0] |= 1<<31;
518
519 return (emit_instruction(emit, opcode) &&
520 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
521 }
522
523
524 boolean
525 svga_translate_decl_sm30( struct svga_shader_emitter *emit,
526 const struct tgsi_full_declaration *decl )
527 {
528 unsigned first = decl->Range.First;
529 unsigned last = decl->Range.Last;
530 unsigned idx;
531
532 for( idx = first; idx <= last; idx++ ) {
533 boolean ok;
534
535 switch (decl->Declaration.File) {
536 case TGSI_FILE_SAMPLER:
537 assert (emit->unit == PIPE_SHADER_FRAGMENT);
538 ok = ps30_sampler( emit, decl->Semantic, idx );
539 break;
540
541 case TGSI_FILE_INPUT:
542 if (emit->unit == PIPE_SHADER_VERTEX)
543 ok = vs30_input( emit, decl->Semantic, idx );
544 else
545 ok = ps30_input( emit, decl->Semantic, idx );
546 break;
547
548 case TGSI_FILE_OUTPUT:
549 if (emit->unit == PIPE_SHADER_VERTEX)
550 ok = vs30_output( emit, decl->Semantic, idx );
551 else
552 ok = ps30_output( emit, decl->Semantic, idx );
553 break;
554
555 default:
556 /* don't need to declare other vars */
557 ok = TRUE;
558 }
559
560 if (!ok)
561 return FALSE;
562 }
563
564 return TRUE;
565 }