Debian Codegraph @

src/gallium/drivers/svga/svga_tgsi_insn.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8
9 #include "pipe/p_shader_tokens.h"
10 #include "tgsi/tgsi_dump.h"
11 #include "tgsi/tgsi_parse.h"
12 #include "util/u_memory.h"
13 #include "util/u_math.h"
14 #include "util/u_pstipple.h"
15
16 #include "svga_tgsi_emit.h"
17 #include "svga_context.h"
18
19
20 static bool
21 emit_vs_postamble(struct svga_shader_emitter *emit);
22
23 static bool
24 emit_ps_postamble(struct svga_shader_emitter *emit);
25
26
27 static SVGA3dShaderOpCodeType
28 translate_opcode(enum tgsi_opcode opcode)
29 {
30 switch (opcode) {
31 case TGSI_OPCODE_ADD: return SVGA3DOP_ADD;
32 case TGSI_OPCODE_DP3: return SVGA3DOP_DP3;
33 case TGSI_OPCODE_DP4: return SVGA3DOP_DP4;
34 case TGSI_OPCODE_FRC: return SVGA3DOP_FRC;
35 case TGSI_OPCODE_MAD: return SVGA3DOP_MAD;
36 case TGSI_OPCODE_MAX: return SVGA3DOP_MAX;
37 case TGSI_OPCODE_MIN: return SVGA3DOP_MIN;
38 case TGSI_OPCODE_MOV: return SVGA3DOP_MOV;
39 case TGSI_OPCODE_MUL: return SVGA3DOP_MUL;
40 case TGSI_OPCODE_NOP: return SVGA3DOP_NOP;
41 default:
42 assert(!"svga: unexpected opcode in translate_opcode()");
43 return SVGA3DOP_LAST_INST;
44 }
45 }
46
47
48 static SVGA3dShaderRegType
49 translate_file(enum tgsi_file_type file)
50 {
51 switch (file) {
52 case TGSI_FILE_TEMPORARY: return SVGA3DREG_TEMP;
53 case TGSI_FILE_INPUT: return SVGA3DREG_INPUT;
54 case TGSI_FILE_OUTPUT: return SVGA3DREG_OUTPUT; /* VS3.0+ only */
55 case TGSI_FILE_IMMEDIATE: return SVGA3DREG_CONST;
56 case TGSI_FILE_CONSTANT: return SVGA3DREG_CONST;
57 case TGSI_FILE_SAMPLER: return SVGA3DREG_SAMPLER;
58 case TGSI_FILE_ADDRESS: return SVGA3DREG_ADDR;
59 default:
60 assert(!"svga: unexpected register file in translate_file()");
61 return SVGA3DREG_TEMP;
62 }
63 }
64
65
66 /**
67 * Translate a TGSI destination register to an SVGA3DShaderDestToken.
68 * \param insn the TGSI instruction
69 * \param idx which TGSI dest register to translate (usually (always?) zero)
70 */
71 static SVGA3dShaderDestToken
72 translate_dst_register(struct svga_shader_emitter *emit,
73 const struct tgsi_full_instruction *insn,
74 unsigned idx)
75 {
76 const struct tgsi_full_dst_register *reg = &insn->Dst[idx];
77 SVGA3dShaderDestToken dest;
78
79 switch (reg->Register.File) {
80 case TGSI_FILE_OUTPUT:
81 /* Output registers encode semantic information in their name.
82 * Need to lookup a table built at decl time:
83 */
84 dest = emit->output_map[reg->Register.Index];
85 emit->num_output_writes++;
86 break;
87
88 default:
89 {
90 unsigned index = reg->Register.Index;
91 assert(index < SVGA3D_TEMPREG_MAX);
92 index = MIN2(index, SVGA3D_TEMPREG_MAX - 1);
93 dest = dst_register(translate_file(reg->Register.File), index);
94 }
95 break;
96 }
97
98 if (reg->Register.Indirect) {
99 debug_warning("Indirect indexing of dest registers is not supported!\n");
100 }
101
102 dest.mask = reg->Register.WriteMask;
103 assert(dest.mask);
104
105 if (insn->Instruction.Saturate)
106 dest.dstMod = SVGA3DDSTMOD_SATURATE;
107
108 return dest;
109 }
110
111
112 /**
113 * Apply a swizzle to a src_register, returning a new src_register
114 * Ex: swizzle(SRC.ZZYY, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y)
115 * would return SRC.YYZZ
116 */
117 static struct src_register
118 swizzle(struct src_register src,
119 unsigned x, unsigned y, unsigned z, unsigned w)
120 {
121 assert(x < 4);
122 assert(y < 4);
123 assert(z < 4);
124 assert(w < 4);
125 x = (src.base.swizzle >> (x * 2)) & 0x3;
126 y = (src.base.swizzle >> (y * 2)) & 0x3;
127 z = (src.base.swizzle >> (z * 2)) & 0x3;
128 w = (src.base.swizzle >> (w * 2)) & 0x3;
129
130 src.base.swizzle = TRANSLATE_SWIZZLE(x, y, z, w);
131
132 return src;
133 }
134
135
136 /**
137 * Apply a "scalar" swizzle to a src_register returning a new
138 * src_register where all the swizzle terms are the same.
139 * Ex: scalar(SRC.WZYX, SWIZZLE_Y) would return SRC.ZZZZ
140 */
141 static struct src_register
142 scalar(struct src_register src, unsigned comp)
143 {
144 assert(comp < 4);
145 return swizzle(src, comp, comp, comp, comp);
146 }
147
148
149 static bool
150 svga_arl_needs_adjustment(const struct svga_shader_emitter *emit)
151 {
152 for (unsigned i = 0; i < emit->num_arl_consts; ++i) {
153 if (emit->arl_consts[i].arl_num == emit->current_arl)
154 return true;
155 }
156 return false;
157 }
158
159
160 static int
161 svga_arl_adjustment(const struct svga_shader_emitter *emit)
162 {
163 for (unsigned i = 0; i < emit->num_arl_consts; ++i) {
164 if (emit->arl_consts[i].arl_num == emit->current_arl)
165 return emit->arl_consts[i].number;
166 }
167 return 0;
168 }
169
170
171 /**
172 * Translate a TGSI src register to a src_register.
173 */
174 static struct src_register
175 translate_src_register(const struct svga_shader_emitter *emit,
176 const struct tgsi_full_src_register *reg)
177 {
178 struct src_register src;
179
180 switch (reg->Register.File) {
181 case TGSI_FILE_INPUT:
182 /* Input registers are referred to by their semantic name rather
183 * than by index. Use the mapping build up from the decls:
184 */
185 src = emit->input_map[reg->Register.Index];
186 break;
187
188 case TGSI_FILE_IMMEDIATE:
189 /* Immediates are appended after TGSI constants in the D3D
190 * constant buffer.
191 */
192 src = src_register(translate_file(reg->Register.File),
193 reg->Register.Index + emit->imm_start);
194 break;
195
196 default:
197 src = src_register(translate_file(reg->Register.File),
198 reg->Register.Index);
199 break;
200 }
201
202 /* Indirect addressing.
203 */
204 if (reg->Register.Indirect) {
205 if (emit->unit == MESA_SHADER_FRAGMENT) {
206 /* Pixel shaders have only loop registers for relative
207 * addressing into inputs. Ignore the redundant address
208 * register, the contents of aL should be in sync with it.
209 */
210 if (reg->Register.File == TGSI_FILE_INPUT) {
211 src.base.relAddr = 1;
212 src.indirect = src_token(SVGA3DREG_LOOP, 0);
213 }
214 } else {
215 /* Constant buffers only.
216 */
217 if (reg->Register.File == TGSI_FILE_CONSTANT) {
218 /* we shift the offset towards the minimum */
219 if (svga_arl_needs_adjustment(emit)) {
220 src.base.num -= svga_arl_adjustment(emit);
221 }
222 src.base.relAddr = 1;
223
224 /* Not really sure what should go in the second token:
225 */
226 src.indirect = src_token(SVGA3DREG_ADDR,
227 reg->Indirect.Index);
228
229 src.indirect.swizzle = SWIZZLE_XXXX;
230 }
231 }
232 }
233
234 src = swizzle(src,
235 reg->Register.SwizzleX,
236 reg->Register.SwizzleY,
237 reg->Register.SwizzleZ,
238 reg->Register.SwizzleW);
239
240 /* src.mod isn't a bitfield, unfortunately */
241 if (reg->Register.Absolute) {
242 if (reg->Register.Negate)
243 src.base.srcMod = SVGA3DSRCMOD_ABSNEG;
244 else
245 src.base.srcMod = SVGA3DSRCMOD_ABS;
246 } else {
247 if (reg->Register.Negate)
248 src.base.srcMod = SVGA3DSRCMOD_NEG;
249 else
250 src.base.srcMod = SVGA3DSRCMOD_NONE;
251 }
252
253 return src;
254 }
255
256
257 /*
258 * Get a temporary register.
259 * Note: if we exceed the temporary register limit we just use
260 * register SVGA3D_TEMPREG_MAX - 1.
261 */
262 static SVGA3dShaderDestToken
263 get_temp(struct svga_shader_emitter *emit)
264 {
265 int i = emit->nr_hw_temp + emit->internal_temp_count++;
266 if (i >= SVGA3D_TEMPREG_MAX) {
267 debug_warn_once("svga: Too many temporary registers used in shader\n");
268 i = SVGA3D_TEMPREG_MAX - 1;
269 }
270 return dst_register(SVGA3DREG_TEMP, i);
271 }
272
273
274 /**
275 * Release a single temp. Currently only effective if it was the last
276 * allocated temp, otherwise release will be delayed until the next
277 * call to reset_temp_regs().
278 */
279 static void
280 release_temp(struct svga_shader_emitter *emit,
281 SVGA3dShaderDestToken temp)
282 {
283 if (temp.num == emit->internal_temp_count - 1)
284 emit->internal_temp_count--;
285 }
286
287
288 /**
289 * Release all temps.
290 */
291 static void
292 reset_temp_regs(struct svga_shader_emitter *emit)
293 {
294 emit->internal_temp_count = 0;
295 }
296
297
298 /** Emit bytecode for a src_register */
299 static bool
300 emit_src(struct svga_shader_emitter *emit, const struct src_register src)
301 {
302 if (src.base.relAddr) {
303 assert(src.base.reserved0);
304 assert(src.indirect.reserved0);
305 return (svga_shader_emit_dword(emit, src.base.value) &&
306 svga_shader_emit_dword(emit, src.indirect.value));
307 } else {
308 assert(src.base.reserved0);
309 return svga_shader_emit_dword(emit, src.base.value);
310 }
311 }
312
313
314 /** Emit bytecode for a dst_register */
315 static bool
316 emit_dst(struct svga_shader_emitter *emit, SVGA3dShaderDestToken dest)
317 {
318 assert(dest.reserved0);
319 assert(dest.mask);
320 return svga_shader_emit_dword(emit, dest.value);
321 }
322
323
324 /** Emit bytecode for a 1-operand instruction */
325 static bool
326 emit_op1(struct svga_shader_emitter *emit,
327 SVGA3dShaderInstToken inst,
328 SVGA3dShaderDestToken dest,
329 struct src_register src0)
330 {
331 return (emit_instruction(emit, inst) &&
332 emit_dst(emit, dest) &&
333 emit_src(emit, src0));
334 }
335
336
337 /** Emit bytecode for a 2-operand instruction */
338 static bool
339 emit_op2(struct svga_shader_emitter *emit,
340 SVGA3dShaderInstToken inst,
341 SVGA3dShaderDestToken dest,
342 struct src_register src0,
343 struct src_register src1)
344 {
345 return (emit_instruction(emit, inst) &&
346 emit_dst(emit, dest) &&
347 emit_src(emit, src0) &&
348 emit_src(emit, src1));
349 }
350
351
352 /** Emit bytecode for a 3-operand instruction */
353 static bool
354 emit_op3(struct svga_shader_emitter *emit,
355 SVGA3dShaderInstToken inst,
356 SVGA3dShaderDestToken dest,
357 struct src_register src0,
358 struct src_register src1,
359 struct src_register src2)
360 {
361 return (emit_instruction(emit, inst) &&
362 emit_dst(emit, dest) &&
363 emit_src(emit, src0) &&
364 emit_src(emit, src1) &&
365 emit_src(emit, src2));
366 }
367
368
369 /** Emit bytecode for a 4-operand instruction */
370 static bool
371 emit_op4(struct svga_shader_emitter *emit,
372 SVGA3dShaderInstToken inst,
373 SVGA3dShaderDestToken dest,
374 struct src_register src0,
375 struct src_register src1,
376 struct src_register src2,
377 struct src_register src3)
378 {
379 return (emit_instruction(emit, inst) &&
380 emit_dst(emit, dest) &&
381 emit_src(emit, src0) &&
382 emit_src(emit, src1) &&
383 emit_src(emit, src2) &&
384 emit_src(emit, src3));
385 }
386
387
388 /**
389 * Apply the absolute value modifier to the given src_register, returning
390 * a new src_register.
391 */
392 static struct src_register
393 absolute(struct src_register src)
394 {
395 src.base.srcMod = SVGA3DSRCMOD_ABS;
396 return src;
397 }
398
399
400 /**
401 * Apply the negation modifier to the given src_register, returning
402 * a new src_register.
403 */
404 static struct src_register
405 negate(struct src_register src)
406 {
407 switch (src.base.srcMod) {
408 case SVGA3DSRCMOD_ABS:
409 src.base.srcMod = SVGA3DSRCMOD_ABSNEG;
410 break;
411 case SVGA3DSRCMOD_ABSNEG:
412 src.base.srcMod = SVGA3DSRCMOD_ABS;
413 break;
414 case SVGA3DSRCMOD_NEG:
415 src.base.srcMod = SVGA3DSRCMOD_NONE;
416 break;
417 case SVGA3DSRCMOD_NONE:
418 src.base.srcMod = SVGA3DSRCMOD_NEG;
419 break;
420 }
421 return src;
422 }
423
424
425
426 /* Replace the src with the temporary specified in the dst, but copying
427 * only the necessary channels, and preserving the original swizzle (which is
428 * important given that several opcodes have constraints in the allowed
429 * swizzles).
430 */
431 static bool
432 emit_repl(struct svga_shader_emitter *emit,
433 SVGA3dShaderDestToken dst,
434 struct src_register *src0)
435 {
436 assert(SVGA3dShaderGetRegType(dst.value) == SVGA3DREG_TEMP);
437
438 const unsigned src0_swizzle = src0->base.swizzle;
439
440 dst.mask = 0;
441 for (unsigned chan = 0; chan < 4; ++chan) {
442 unsigned swizzle = (src0_swizzle >> (chan *2)) & 0x3;
443 dst.mask |= 1 << swizzle;
444 }
445 assert(dst.mask);
446
447 src0->base.swizzle = SVGA3DSWIZZLE_NONE;
448
449 if (!emit_op1(emit, inst_token(SVGA3DOP_MOV), dst, *src0))
450 return false;
451
452 *src0 = src(dst);
453 src0->base.swizzle = src0_swizzle;
454
455 return true;
456 }
457
458
459 /**
460 * Submit/emit an instruction with zero operands.
461 */
462 static bool
463 submit_op0(struct svga_shader_emitter *emit,
464 SVGA3dShaderInstToken inst,
465 SVGA3dShaderDestToken dest)
466 {
467 return (emit_instruction(emit, inst) &&
468 emit_dst(emit, dest));
469 }
470
471
472 /**
473 * Submit/emit an instruction with one operand.
474 */
475 static bool
476 submit_op1(struct svga_shader_emitter *emit,
477 SVGA3dShaderInstToken inst,
478 SVGA3dShaderDestToken dest,
479 struct src_register src0)
480 {
481 return emit_op1(emit, inst, dest, src0);
482 }
483
484
485 /**
486 * Submit/emit an instruction with two operands.
487 *
488 * SVGA shaders may not refer to >1 constant register in a single
489 * instruction. This function checks for that usage and inserts a
490 * move to temporary if detected.
491 *
492 * The same applies to input registers -- at most a single input
493 * register may be read by any instruction.
494 */
495 static bool
496 submit_op2(struct svga_shader_emitter *emit,
497 SVGA3dShaderInstToken inst,
498 SVGA3dShaderDestToken dest,
499 struct src_register src0,
500 struct src_register src1)
501 {
502 SVGA3dShaderDestToken temp;
503 SVGA3dShaderRegType type0, type1;
504 bool need_temp = false;
505
506 temp.value = 0;
507 type0 = SVGA3dShaderGetRegType(src0.base.value);
508 type1 = SVGA3dShaderGetRegType(src1.base.value);
509
510 if (type0 == SVGA3DREG_CONST &&
511 type1 == SVGA3DREG_CONST &&
512 src0.base.num != src1.base.num)
513 need_temp = true;
514
515 if (type0 == SVGA3DREG_INPUT &&
516 type1 == SVGA3DREG_INPUT &&
517 src0.base.num != src1.base.num)
518 need_temp = true;
519
520 if (need_temp) {
521 temp = get_temp(emit);
522
523 if (!emit_repl(emit, temp, &src0))
524 return false;
525 }
526
527 if (!emit_op2(emit, inst, dest, src0, src1))
528 return false;
529
530 if (need_temp)
531 release_temp(emit, temp);
532
533 return true;
534 }
535
536
537 /**
538 * Submit/emit an instruction with three operands.
539 *
540 * SVGA shaders may not refer to >1 constant register in a single
541 * instruction. This function checks for that usage and inserts a
542 * move to temporary if detected.
543 */
544 static bool
545 submit_op3(struct svga_shader_emitter *emit,
546 SVGA3dShaderInstToken inst,
547 SVGA3dShaderDestToken dest,
548 struct src_register src0,
549 struct src_register src1,
550 struct src_register src2)
551 {
552 SVGA3dShaderDestToken temp0;
553 SVGA3dShaderDestToken temp1;
554 bool need_temp0 = false;
555 bool need_temp1 = false;
556 SVGA3dShaderRegType type0, type1, type2;
557
558 temp0.value = 0;
559 temp1.value = 0;
560 type0 = SVGA3dShaderGetRegType(src0.base.value);
561 type1 = SVGA3dShaderGetRegType(src1.base.value);
562 type2 = SVGA3dShaderGetRegType(src2.base.value);
563
564 if (inst.op != SVGA3DOP_SINCOS) {
565 if (type0 == SVGA3DREG_CONST &&
566 ((type1 == SVGA3DREG_CONST && src0.base.num != src1.base.num) ||
567 (type2 == SVGA3DREG_CONST && src0.base.num != src2.base.num)))
568 need_temp0 = true;
569
570 if (type1 == SVGA3DREG_CONST &&
571 (type2 == SVGA3DREG_CONST && src1.base.num != src2.base.num))
572 need_temp1 = true;
573 }
574
575 if (type0 == SVGA3DREG_INPUT &&
576 ((type1 == SVGA3DREG_INPUT && src0.base.num != src1.base.num) ||
577 (type2 == SVGA3DREG_INPUT && src0.base.num != src2.base.num)))
578 need_temp0 = true;
579
580 if (type1 == SVGA3DREG_INPUT &&
581 (type2 == SVGA3DREG_INPUT && src1.base.num != src2.base.num))
582 need_temp1 = true;
583
584 if (need_temp0) {
585 temp0 = get_temp(emit);
586
587 if (!emit_repl(emit, temp0, &src0))
588 return false;
589 }
590
591 if (need_temp1) {
592 temp1 = get_temp(emit);
593
594 if (!emit_repl(emit, temp1, &src1))
595 return false;
596 }
597
598 if (!emit_op3(emit, inst, dest, src0, src1, src2))
599 return false;
600
601 if (need_temp1)
602 release_temp(emit, temp1);
603 if (need_temp0)
604 release_temp(emit, temp0);
605 return true;
606 }
607
608
609 /**
610 * Submit/emit an instruction with four operands.
611 *
612 * SVGA shaders may not refer to >1 constant register in a single
613 * instruction. This function checks for that usage and inserts a
614 * move to temporary if detected.
615 */
616 static bool
617 submit_op4(struct svga_shader_emitter *emit,
618 SVGA3dShaderInstToken inst,
619 SVGA3dShaderDestToken dest,
620 struct src_register src0,
621 struct src_register src1,
622 struct src_register src2,
623 struct src_register src3)
624 {
625 SVGA3dShaderDestToken temp0;
626 SVGA3dShaderDestToken temp3;
627 bool need_temp0 = false;
628 bool need_temp3 = false;
629 SVGA3dShaderRegType type0, type1, type2, type3;
630
631 temp0.value = 0;
632 temp3.value = 0;
633 type0 = SVGA3dShaderGetRegType(src0.base.value);
634 type1 = SVGA3dShaderGetRegType(src1.base.value);
635 type2 = SVGA3dShaderGetRegType(src2.base.value);
636 type3 = SVGA3dShaderGetRegType(src2.base.value);
637
638 /* Make life a little easier - this is only used by the TXD
639 * instruction which is guaranteed not to have a constant/input reg
640 * in one slot at least:
641 */
642 assert(type1 == SVGA3DREG_SAMPLER);
643 (void) type1;
644
645 if (type0 == SVGA3DREG_CONST &&
646 ((type3 == SVGA3DREG_CONST && src0.base.num != src3.base.num) ||
647 (type2 == SVGA3DREG_CONST && src0.base.num != src2.base.num)))
648 need_temp0 = true;
649
650 if (type3 == SVGA3DREG_CONST &&
651 (type2 == SVGA3DREG_CONST && src3.base.num != src2.base.num))
652 need_temp3 = true;
653
654 if (type0 == SVGA3DREG_INPUT &&
655 ((type3 == SVGA3DREG_INPUT && src0.base.num != src3.base.num) ||
656 (type2 == SVGA3DREG_INPUT && src0.base.num != src2.base.num)))
657 need_temp0 = true;
658
659 if (type3 == SVGA3DREG_INPUT &&
660 (type2 == SVGA3DREG_INPUT && src3.base.num != src2.base.num))
661 need_temp3 = true;
662
663 if (need_temp0) {
664 temp0 = get_temp(emit);
665
666 if (!emit_repl(emit, temp0, &src0))
667 return false;
668 }
669
670 if (need_temp3) {
671 temp3 = get_temp(emit);
672
673 if (!emit_repl(emit, temp3, &src3))
674 return false;
675 }
676
677 if (!emit_op4(emit, inst, dest, src0, src1, src2, src3))
678 return false;
679
680 if (need_temp3)
681 release_temp(emit, temp3);
682 if (need_temp0)
683 release_temp(emit, temp0);
684 return true;
685 }
686
687
688 /**
689 * Do the src and dest registers refer to the same register?
690 */
691 static bool
692 alias_src_dst(struct src_register src,
693 SVGA3dShaderDestToken dst)
694 {
695 if (src.base.num != dst.num)
696 return false;
697
698 if (SVGA3dShaderGetRegType(dst.value) !=
699 SVGA3dShaderGetRegType(src.base.value))
700 return false;
701
702 return true;
703 }
704
705
706 /**
707 * Helper for emitting SVGA immediate values using the SVGA3DOP_DEF[I]
708 * instructions.
709 */
710 static bool
711 emit_def_const(struct svga_shader_emitter *emit,
712 SVGA3dShaderConstType type,
713 unsigned idx, float a, float b, float c, float d)
714 {
715 SVGA3DOpDefArgs def;
716 SVGA3dShaderInstToken opcode;
717
718 switch (type) {
719 case SVGA3D_CONST_TYPE_FLOAT:
720 opcode = inst_token(SVGA3DOP_DEF);
721 def.dst = dst_register(SVGA3DREG_CONST, idx);
722 def.constValues[0] = a;
723 def.constValues[1] = b;
724 def.constValues[2] = c;
725 def.constValues[3] = d;
726 break;
727 case SVGA3D_CONST_TYPE_INT:
728 opcode = inst_token(SVGA3DOP_DEFI);
729 def.dst = dst_register(SVGA3DREG_CONSTINT, idx);
730 def.constIValues[0] = (int)a;
731 def.constIValues[1] = (int)b;
732 def.constIValues[2] = (int)c;
733 def.constIValues[3] = (int)d;
734 break;
735 default:
736 assert(0);
737 opcode = inst_token(SVGA3DOP_NOP);
738 break;
739 }
740
741 if (!emit_instruction(emit, opcode) ||
742 !svga_shader_emit_dwords(emit, def.values, ARRAY_SIZE(def.values)))
743 return false;
744
745 return true;
746 }
747
748
749 static bool
750 create_loop_const(struct svga_shader_emitter *emit)
751 {
752 unsigned idx = emit->nr_hw_int_const++;
753
754 if (!emit_def_const(emit, SVGA3D_CONST_TYPE_INT, idx,
755 255, /* iteration count */
756 0, /* initial value */
757 1, /* step size */
758 0 /* not used, must be 0 */))
759 return false;
760
761 emit->loop_const_idx = idx;
762 emit->created_loop_const = true;
763
764 return true;
765 }
766
767 static bool
768 create_arl_consts(struct svga_shader_emitter *emit)
769 {
770 for (int i = 0; i < emit->num_arl_consts; i += 4) {
771 unsigned idx = emit->nr_hw_float_const++;
772 float vals[4];
773 int j;
774 for (j = 0; j < 4 && (j + i) < emit->num_arl_consts; ++j) {
775 vals[j] = (float) emit->arl_consts[i + j].number;
776 emit->arl_consts[i + j].idx = idx;
777 switch (j) {
778 case 0:
779 emit->arl_consts[i + 0].swizzle = TGSI_SWIZZLE_X;
780 break;
781 case 1:
782 emit->arl_consts[i + 0].swizzle = TGSI_SWIZZLE_Y;
783 break;
784 case 2:
785 emit->arl_consts[i + 0].swizzle = TGSI_SWIZZLE_Z;
786 break;
787 case 3:
788 emit->arl_consts[i + 0].swizzle = TGSI_SWIZZLE_W;
789 break;
790 }
791 }
792 while (j < 4)
793 vals[j++] = 0;
794
795 if (!emit_def_const(emit, SVGA3D_CONST_TYPE_FLOAT, idx,
796 vals[0], vals[1],
797 vals[2], vals[3]))
798 return false;
799 }
800
801 return true;
802 }
803
804
805 /**
806 * Return the register which holds the pixel shaders front/back-
807 * facing value.
808 */
809 static struct src_register
810 get_vface(struct svga_shader_emitter *emit)
811 {
812 assert(emit->emitted_vface);
813 return src_register(SVGA3DREG_MISCTYPE, SVGA3DMISCREG_FACE);
814 }
815
816
817 /**
818 * Create/emit a "common" constant with values {0, 0.5, -1, 1}.
819 * We can swizzle this to produce other useful constants such as
820 * {0, 0, 0, 0}, {1, 1, 1, 1}, etc.
821 */
822 static bool
823 create_common_immediate(struct svga_shader_emitter *emit)
824 {
825 unsigned idx = emit->nr_hw_float_const++;
826
827 /* Emit the constant (0, 0.5, -1, 1) and use swizzling to generate
828 * other useful vectors.
829 */
830 if (!emit_def_const(emit, SVGA3D_CONST_TYPE_FLOAT,
831 idx, 0.0f, 0.5f, -1.0f, 1.0f))
832 return false;
833 emit->common_immediate_idx[0] = idx;
834 idx++;
835
836 /* Emit constant {2, 0, 0, 0} (only the 2 is used for now) */
837 if (emit->key.vs.adjust_attrib_range) {
838 if (!emit_def_const(emit, SVGA3D_CONST_TYPE_FLOAT,
839 idx, 2.0f, 0.0f, 0.0f, 0.0f))
840 return false;
841 emit->common_immediate_idx[1] = idx;
842 } else {
843 emit->common_immediate_idx[1] = -1;
844 }
845
846 emit->created_common_immediate = true;
847
848 return true;
849 }
850
851
852 /**
853 * Return swizzle/position for the given value in the "common" immediate.
854 */
855 static inline unsigned
856 common_immediate_swizzle(float value)
857 {
858 if (value == 0.0f)
859 return TGSI_SWIZZLE_X;
860 else if (value == 0.5f)
861 return TGSI_SWIZZLE_Y;
862 else if (value == -1.0f)
863 return TGSI_SWIZZLE_Z;
864 else if (value == 1.0f)
865 return TGSI_SWIZZLE_W;
866 else {
867 assert(!"illegal value in common_immediate_swizzle");
868 return TGSI_SWIZZLE_X;
869 }
870 }
871
872
873 /**
874 * Returns an immediate reg where all the terms are either 0, 1, 2 or 0.5
875 */
876 static struct src_register
877 get_immediate(struct svga_shader_emitter *emit,
878 float x, float y, float z, float w)
879 {
880 unsigned sx = common_immediate_swizzle(x);
881 unsigned sy = common_immediate_swizzle(y);
882 unsigned sz = common_immediate_swizzle(z);
883 unsigned sw = common_immediate_swizzle(w);
884 assert(emit->created_common_immediate);
885 assert(emit->common_immediate_idx[0] >= 0);
886 return swizzle(src_register(SVGA3DREG_CONST, emit->common_immediate_idx[0]),
887 sx, sy, sz, sw);
888 }
889
890
891 /**
892 * returns {0, 0, 0, 0} immediate
893 */
894 static struct src_register
895 get_zero_immediate(struct svga_shader_emitter *emit)
896 {
897 assert(emit->created_common_immediate);
898 assert(emit->common_immediate_idx[0] >= 0);
899 return swizzle(src_register(SVGA3DREG_CONST,
900 emit->common_immediate_idx[0]),
901 0, 0, 0, 0);
902 }
903
904
905 /**
906 * returns {1, 1, 1, 1} immediate
907 */
908 static struct src_register
909 get_one_immediate(struct svga_shader_emitter *emit)
910 {
911 assert(emit->created_common_immediate);
912 assert(emit->common_immediate_idx[0] >= 0);
913 return swizzle(src_register(SVGA3DREG_CONST,
914 emit->common_immediate_idx[0]),
915 3, 3, 3, 3);
916 }
917
918
919 /**
920 * returns {0.5, 0.5, 0.5, 0.5} immediate
921 */
922 static struct src_register
923 get_half_immediate(struct svga_shader_emitter *emit)
924 {
925 assert(emit->created_common_immediate);
926 assert(emit->common_immediate_idx[0] >= 0);
927 return swizzle(src_register(SVGA3DREG_CONST, emit->common_immediate_idx[0]),
928 1, 1, 1, 1);
929 }
930
931
932 /**
933 * returns {2, 2, 2, 2} immediate
934 */
935 static struct src_register
936 get_two_immediate(struct svga_shader_emitter *emit)
937 {
938 /* Note we use the second common immediate here */
939 assert(emit->created_common_immediate);
940 assert(emit->common_immediate_idx[1] >= 0);
941 return swizzle(src_register(SVGA3DREG_CONST,
942 emit->common_immediate_idx[1]),
943 0, 0, 0, 0);
944 }
945
946
947 /**
948 * returns the loop const
949 */
950 static struct src_register
951 get_loop_const(struct svga_shader_emitter *emit)
952 {
953 assert(emit->created_loop_const);
954 assert(emit->loop_const_idx >= 0);
955 return src_register(SVGA3DREG_CONSTINT,
956 emit->loop_const_idx);
957 }
958
959
960 static struct src_register
961 get_fake_arl_const(struct svga_shader_emitter *emit)
962 {
963 struct src_register reg;
964 int idx = 0, swizzle = 0;
965
966 for (int i = 0; i < emit->num_arl_consts; ++ i) {
967 if (emit->arl_consts[i].arl_num == emit->current_arl) {
968 idx = emit->arl_consts[i].idx;
969 swizzle = emit->arl_consts[i].swizzle;
970 }
971 }
972
973 reg = src_register(SVGA3DREG_CONST, idx);
974 return scalar(reg, swizzle);
975 }
976
977
978 /**
979 * Return a register which holds the width and height of the texture
980 * currently bound to the given sampler.
981 */
982 static struct src_register
983 get_tex_dimensions(struct svga_shader_emitter *emit, int sampler_num)
984 {
985 int idx;
986 struct src_register reg;
987
988 /* the width/height indexes start right after constants */
989 idx = emit->key.tex[sampler_num].width_height_idx +
990 emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
991
992 reg = src_register(SVGA3DREG_CONST, idx);
993 return reg;
994 }
995
996
997 static bool
998 emit_fake_arl(struct svga_shader_emitter *emit,
999 const struct tgsi_full_instruction *insn)
1000 {
1001 const struct src_register src0 =
1002 translate_src_register(emit, &insn->Src[0]);
1003 struct src_register src1 = get_fake_arl_const(emit);
1004 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1005 SVGA3dShaderDestToken tmp = get_temp(emit);
1006
1007 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), tmp, src0))
1008 return false;
1009
1010 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), tmp, src(tmp),
1011 src1))
1012 return false;
1013
1014 /* replicate the original swizzle */
1015 src1 = src(tmp);
1016 src1.base.swizzle = src0.base.swizzle;
1017
1018 return submit_op1(emit, inst_token(SVGA3DOP_MOVA),
1019 dst, src1);
1020 }
1021
1022
1023 static bool
1024 emit_if(struct svga_shader_emitter *emit,
1025 const struct tgsi_full_instruction *insn)
1026 {
1027 struct src_register src0 =
1028 translate_src_register(emit, &insn->Src[0]);
1029 struct src_register zero = get_zero_immediate(emit);
1030 SVGA3dShaderInstToken if_token = inst_token(SVGA3DOP_IFC);
1031
1032 if_token.control = SVGA3DOPCOMP_NE;
1033
1034 if (SVGA3dShaderGetRegType(src0.base.value) == SVGA3DREG_CONST) {
1035 /*
1036 * Max different constant registers readable per IFC instruction is 1.
1037 */
1038 SVGA3dShaderDestToken tmp = get_temp(emit);
1039
1040 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), tmp, src0))
1041 return false;
1042
1043 src0 = scalar(src(tmp), TGSI_SWIZZLE_X);
1044 }
1045
1046 emit->dynamic_branching_level++;
1047
1048 return (emit_instruction(emit, if_token) &&
1049 emit_src(emit, src0) &&
1050 emit_src(emit, zero));
1051 }
1052
1053
1054 static bool
1055 emit_else(struct svga_shader_emitter *emit,
1056 const struct tgsi_full_instruction *insn)
1057 {
1058 return emit_instruction(emit, inst_token(SVGA3DOP_ELSE));
1059 }
1060
1061
1062 static bool
1063 emit_endif(struct svga_shader_emitter *emit,
1064 const struct tgsi_full_instruction *insn)
1065 {
1066 emit->dynamic_branching_level--;
1067
1068 return emit_instruction(emit, inst_token(SVGA3DOP_ENDIF));
1069 }
1070
1071
1072 /**
1073 * Translate the following TGSI FLR instruction.
1074 * FLR DST, SRC
1075 * To the following SVGA3D instruction sequence.
1076 * FRC TMP, SRC
1077 * SUB DST, SRC, TMP
1078 */
1079 static bool
1080 emit_floor(struct svga_shader_emitter *emit,
1081 const struct tgsi_full_instruction *insn)
1082 {
1083 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1084 const struct src_register src0 =
1085 translate_src_register(emit, &insn->Src[0]);
1086 SVGA3dShaderDestToken temp = get_temp(emit);
1087
1088 /* FRC TMP, SRC */
1089 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC), temp, src0))
1090 return false;
1091
1092 /* SUB DST, SRC, TMP */
1093 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), dst, src0,
1094 negate(src(temp))))
1095 return false;
1096
1097 return true;
1098 }
1099
1100
1101 /**
1102 * Translate the following TGSI CEIL instruction.
1103 * CEIL DST, SRC
1104 * To the following SVGA3D instruction sequence.
1105 * FRC TMP, -SRC
1106 * ADD DST, SRC, TMP
1107 */
1108 static bool
1109 emit_ceil(struct svga_shader_emitter *emit,
1110 const struct tgsi_full_instruction *insn)
1111 {
1112 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1113 const struct src_register src0 =
1114 translate_src_register(emit, &insn->Src[0]);
1115 SVGA3dShaderDestToken temp = get_temp(emit);
1116
1117 /* FRC TMP, -SRC */
1118 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC), temp, negate(src0)))
1119 return false;
1120
1121 /* ADD DST, SRC, TMP */
1122 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), dst, src0, src(temp)))
1123 return false;
1124
1125 return true;
1126 }
1127
1128
1129 /**
1130 * Translate the following TGSI DIV instruction.
1131 * DIV DST.xy, SRC0, SRC1
1132 * To the following SVGA3D instruction sequence.
1133 * RCP TMP.x, SRC1.xxxx
1134 * RCP TMP.y, SRC1.yyyy
1135 * MUL DST.xy, SRC0, TMP
1136 */
1137 static bool
1138 emit_div(struct svga_shader_emitter *emit,
1139 const struct tgsi_full_instruction *insn)
1140 {
1141 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1142 const struct src_register src0 =
1143 translate_src_register(emit, &insn->Src[0]);
1144 const struct src_register src1 =
1145 translate_src_register(emit, &insn->Src[1]);
1146 SVGA3dShaderDestToken temp = get_temp(emit);
1147
1148 /* For each enabled element, perform a RCP instruction. Note that
1149 * RCP is scalar in SVGA3D:
1150 */
1151 for (unsigned i = 0; i < 4; i++) {
1152 unsigned channel = 1 << i;
1153 if (dst.mask & channel) {
1154 /* RCP TMP.?, SRC1.???? */
1155 if (!submit_op1(emit, inst_token(SVGA3DOP_RCP),
1156 writemask(temp, channel),
1157 scalar(src1, i)))
1158 return false;
1159 }
1160 }
1161
1162 /* Vector mul:
1163 * MUL DST, SRC0, TMP
1164 */
1165 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL), dst, src0,
1166 src(temp)))
1167 return false;
1168
1169 return true;
1170 }
1171
1172
1173 /**
1174 * Translate the following TGSI DP2 instruction.
1175 * DP2 DST, SRC1, SRC2
1176 * To the following SVGA3D instruction sequence.
1177 * MUL TMP, SRC1, SRC2
1178 * ADD DST, TMP.xxxx, TMP.yyyy
1179 */
1180 static bool
1181 emit_dp2(struct svga_shader_emitter *emit,
1182 const struct tgsi_full_instruction *insn)
1183 {
1184 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1185 const struct src_register src0 =
1186 translate_src_register(emit, &insn->Src[0]);
1187 const struct src_register src1 =
1188 translate_src_register(emit, &insn->Src[1]);
1189 SVGA3dShaderDestToken temp = get_temp(emit);
1190 struct src_register temp_src0, temp_src1;
1191
1192 /* MUL TMP, SRC1, SRC2 */
1193 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL), temp, src0, src1))
1194 return false;
1195
1196 temp_src0 = scalar(src(temp), TGSI_SWIZZLE_X);
1197 temp_src1 = scalar(src(temp), TGSI_SWIZZLE_Y);
1198
1199 /* ADD DST, TMP.xxxx, TMP.yyyy */
1200 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), dst,
1201 temp_src0, temp_src1))
1202 return false;
1203
1204 return true;
1205 }
1206
1207
1208 /**
1209 * Sine / Cosine helper function.
1210 */
1211 static bool
1212 do_emit_sincos(struct svga_shader_emitter *emit,
1213 SVGA3dShaderDestToken dst,
1214 struct src_register src0)
1215 {
1216 src0 = scalar(src0, TGSI_SWIZZLE_X);
1217 return submit_op1(emit, inst_token(SVGA3DOP_SINCOS), dst, src0);
1218 }
1219
1220
1221 /**
1222 * Translate TGSI SIN instruction into:
1223 * SCS TMP SRC
1224 * MOV DST TMP.yyyy
1225 */
1226 static bool
1227 emit_sin(struct svga_shader_emitter *emit,
1228 const struct tgsi_full_instruction *insn)
1229 {
1230 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1231 struct src_register src0 =
1232 translate_src_register(emit, &insn->Src[0]);
1233 SVGA3dShaderDestToken temp = get_temp(emit);
1234
1235 /* SCS TMP SRC */
1236 if (!do_emit_sincos(emit, writemask(temp, TGSI_WRITEMASK_Y), src0))
1237 return false;
1238
1239 src0 = scalar(src(temp), TGSI_SWIZZLE_Y);
1240
1241 /* MOV DST TMP.yyyy */
1242 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst, src0))
1243 return false;
1244
1245 return true;
1246 }
1247
1248
1249 /*
1250 * Translate TGSI COS instruction into:
1251 * SCS TMP SRC
1252 * MOV DST TMP.xxxx
1253 */
1254 static bool
1255 emit_cos(struct svga_shader_emitter *emit,
1256 const struct tgsi_full_instruction *insn)
1257 {
1258 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1259 struct src_register src0 =
1260 translate_src_register(emit, &insn->Src[0]);
1261 SVGA3dShaderDestToken temp = get_temp(emit);
1262
1263 /* SCS TMP SRC */
1264 if (!do_emit_sincos(emit, writemask(temp, TGSI_WRITEMASK_X), src0))
1265 return false;
1266
1267 src0 = scalar(src(temp), TGSI_SWIZZLE_X);
1268
1269 /* MOV DST TMP.xxxx */
1270 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst, src0))
1271 return false;
1272
1273 return true;
1274 }
1275
1276
1277 /**
1278 * Translate/emit TGSI SSG (Set Sign: -1, 0, +1) instruction.
1279 */
1280 static bool
1281 emit_ssg(struct svga_shader_emitter *emit,
1282 const struct tgsi_full_instruction *insn)
1283 {
1284 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1285 struct src_register src0 =
1286 translate_src_register(emit, &insn->Src[0]);
1287 SVGA3dShaderDestToken temp0 = get_temp(emit);
1288 SVGA3dShaderDestToken temp1 = get_temp(emit);
1289 struct src_register zero, one;
1290
1291 if (emit->unit == MESA_SHADER_VERTEX) {
1292 /* SGN DST, SRC0, TMP0, TMP1 */
1293 return submit_op3(emit, inst_token(SVGA3DOP_SGN), dst, src0,
1294 src(temp0), src(temp1));
1295 }
1296
1297 one = get_one_immediate(emit);
1298 zero = get_zero_immediate(emit);
1299
1300 /* CMP TMP0, SRC0, one, zero */
1301 if (!submit_op3(emit, inst_token(SVGA3DOP_CMP),
1302 writemask(temp0, dst.mask), src0, one, zero))
1303 return false;
1304
1305 /* CMP TMP1, negate(SRC0), negate(one), zero */
1306 if (!submit_op3(emit, inst_token(SVGA3DOP_CMP),
1307 writemask(temp1, dst.mask), negate(src0), negate(one),
1308 zero))
1309 return false;
1310
1311 /* ADD DST, TMP0, TMP1 */
1312 return submit_op2(emit, inst_token(SVGA3DOP_ADD), dst, src(temp0),
1313 src(temp1));
1314 }
1315
1316
1317 /**
1318 * Translate/emit the conditional discard instruction (discard if
1319 * any of X,Y,Z,W are negative).
1320 */
1321 static bool
1322 emit_cond_discard(struct svga_shader_emitter *emit,
1323 const struct tgsi_full_instruction *insn)
1324 {
1325 const struct tgsi_full_src_register *reg = &insn->Src[0];
1326 struct src_register src0, srcIn;
1327 const bool special = (reg->Register.Absolute ||
1328 reg->Register.Negate ||
1329 reg->Register.Indirect ||
1330 reg->Register.SwizzleX != 0 ||
1331 reg->Register.SwizzleY != 1 ||
1332 reg->Register.SwizzleZ != 2 ||
1333 reg->Register.File != TGSI_FILE_TEMPORARY);
1334 SVGA3dShaderDestToken temp;
1335
1336 src0 = srcIn = translate_src_register(emit, reg);
1337
1338 if (special) {
1339 /* need a temp reg */
1340 temp = get_temp(emit);
1341 }
1342
1343 if (special) {
1344 /* move the source into a temp register */
1345 submit_op1(emit, inst_token(SVGA3DOP_MOV), temp, src0);
1346
1347 src0 = src(temp);
1348 }
1349
1350 /* Do the discard by checking if any of the XYZW components are < 0.
1351 * Note that ps_2_0 and later take XYZW in consideration, while ps_1_x
1352 * only used XYZ. The MSDN documentation about this is incorrect.
1353 */
1354 if (!submit_op0(emit, inst_token(SVGA3DOP_TEXKILL), dst(src0)))
1355 return false;
1356
1357 return true;
1358 }
1359
1360
1361 /**
1362 * Translate/emit the unconditional discard instruction (usually found inside
1363 * an IF/ELSE/ENDIF block).
1364 */
1365 static bool
1366 emit_discard(struct svga_shader_emitter *emit,
1367 const struct tgsi_full_instruction *insn)
1368 {
1369 SVGA3dShaderDestToken temp;
1370 struct src_register one = get_one_immediate(emit);
1371 SVGA3dShaderInstToken inst = inst_token(SVGA3DOP_TEXKILL);
1372
1373 /* texkill doesn't allow negation on the operand so lets move
1374 * negation of {1} to a temp register */
1375 temp = get_temp(emit);
1376 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), temp,
1377 negate(one)))
1378 return false;
1379
1380 return submit_op0(emit, inst, temp);
1381 }
1382
1383
1384 /**
1385 * Test if r1 and r2 are the same register.
1386 */
1387 static bool
1388 same_register(struct src_register r1, struct src_register r2)
1389 {
1390 return (r1.base.num == r2.base.num &&
1391 r1.base.type_upper == r2.base.type_upper &&
1392 r1.base.type_lower == r2.base.type_lower);
1393 }
1394
1395
1396
1397 /**
1398 * Implement conditionals by initializing destination reg to 'fail',
1399 * then set predicate reg with UFOP_SETP, then move 'pass' to dest
1400 * based on predicate reg.
1401 *
1402 * SETP src0, cmp, src1 -- do this first to avoid aliasing problems.
1403 * MOV dst, fail
1404 * MOV dst, pass, p0
1405 */
1406 static bool
1407 emit_conditional(struct svga_shader_emitter *emit,
1408 enum pipe_compare_func compare_func,
1409 SVGA3dShaderDestToken dst,
1410 struct src_register src0,
1411 struct src_register src1,
1412 struct src_register pass,
1413 struct src_register fail)
1414 {
1415 SVGA3dShaderDestToken pred_reg = dst_register(SVGA3DREG_PREDICATE, 0);
1416 SVGA3dShaderInstToken setp_token;
1417
1418 switch (compare_func) {
1419 case PIPE_FUNC_NEVER:
1420 return submit_op1(emit, inst_token(SVGA3DOP_MOV),
1421 dst, fail);
1422 break;
1423 case PIPE_FUNC_LESS:
1424 setp_token = inst_token_setp(SVGA3DOPCOMP_LT);
1425 break;
1426 case PIPE_FUNC_EQUAL:
1427 setp_token = inst_token_setp(SVGA3DOPCOMP_EQ);
1428 break;
1429 case PIPE_FUNC_LEQUAL:
1430 setp_token = inst_token_setp(SVGA3DOPCOMP_LE);
1431 break;
1432 case PIPE_FUNC_GREATER:
1433 setp_token = inst_token_setp(SVGA3DOPCOMP_GT);
1434 break;
1435 case PIPE_FUNC_NOTEQUAL:
1436 setp_token = inst_token_setp(SVGA3DOPCOMP_NE);
1437 break;
1438 case PIPE_FUNC_GEQUAL:
1439 setp_token = inst_token_setp(SVGA3DOPCOMP_GE);
1440 break;
1441 case PIPE_FUNC_ALWAYS:
1442 return submit_op1(emit, inst_token(SVGA3DOP_MOV),
1443 dst, pass);
1444 break;
1445 }
1446
1447 if (same_register(src(dst), pass)) {
1448 /* We'll get bad results if the dst and pass registers are the same
1449 * so use a temp register containing pass.
1450 */
1451 SVGA3dShaderDestToken temp = get_temp(emit);
1452 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), temp, pass))
1453 return false;
1454 pass = src(temp);
1455 }
1456
1457 /* SETP src0, COMPOP, src1 */
1458 if (!submit_op2(emit, setp_token, pred_reg,
1459 src0, src1))
1460 return false;
1461
1462 /* MOV dst, fail */
1463 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst, fail))
1464 return false;
1465
1466 /* MOV dst, pass (predicated)
1467 *
1468 * Note that the predicate reg (and possible modifiers) is passed
1469 * as the first source argument.
1470 */
1471 if (!submit_op2(emit,
1472 inst_token_predicated(SVGA3DOP_MOV), dst,
1473 src(pred_reg), pass))
1474 return false;
1475
1476 return true;
1477 }
1478
1479
1480 /**
1481 * Helper for emiting 'selection' commands. Basically:
1482 * if (src0 OP src1)
1483 * dst = 1.0;
1484 * else
1485 * dst = 0.0;
1486 */
1487 static bool
1488 emit_select(struct svga_shader_emitter *emit,
1489 enum pipe_compare_func compare_func,
1490 SVGA3dShaderDestToken dst,
1491 struct src_register src0,
1492 struct src_register src1)
1493 {
1494 /* There are some SVGA instructions which implement some selects
1495 * directly, but they are only available in the vertex shader.
1496 */
1497 if (emit->unit == MESA_SHADER_VERTEX) {
1498 switch (compare_func) {
1499 case PIPE_FUNC_GEQUAL:
1500 return submit_op2(emit, inst_token(SVGA3DOP_SGE), dst, src0, src1);
1501 case PIPE_FUNC_LEQUAL:
1502 return submit_op2(emit, inst_token(SVGA3DOP_SGE), dst, src1, src0);
1503 case PIPE_FUNC_GREATER:
1504 return submit_op2(emit, inst_token(SVGA3DOP_SLT), dst, src1, src0);
1505 case PIPE_FUNC_LESS:
1506 return submit_op2(emit, inst_token(SVGA3DOP_SLT), dst, src0, src1);
1507 default:
1508 break;
1509 }
1510 }
1511
1512 /* Otherwise, need to use the setp approach:
1513 */
1514 {
1515 struct src_register one, zero;
1516 /* zero immediate is 0,0,0,1 */
1517 zero = get_zero_immediate(emit);
1518 one = get_one_immediate(emit);
1519
1520 return emit_conditional(emit, compare_func, dst, src0, src1, one, zero);
1521 }
1522 }
1523
1524
1525 /**
1526 * Translate/emit a TGSI SEQ, SNE, SLT, SGE, etc. instruction.
1527 */
1528 static bool
1529 emit_select_op(struct svga_shader_emitter *emit,
1530 unsigned compare,
1531 const struct tgsi_full_instruction *insn)
1532 {
1533 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1534 struct src_register src0 = translate_src_register(
1535 emit, &insn->Src[0]);
1536 struct src_register src1 = translate_src_register(
1537 emit, &insn->Src[1]);
1538
1539 return emit_select(emit, compare, dst, src0, src1);
1540 }
1541
1542
1543 /**
1544 * Translate TGSI CMP instruction. Component-wise:
1545 * dst = (src0 < 0.0) ? src1 : src2
1546 */
1547 static bool
1548 emit_cmp(struct svga_shader_emitter *emit,
1549 const struct tgsi_full_instruction *insn)
1550 {
1551 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
1552 const struct src_register src0 =
1553 translate_src_register(emit, &insn->Src[0]);
1554 const struct src_register src1 =
1555 translate_src_register(emit, &insn->Src[1]);
1556 const struct src_register src2 =
1557 translate_src_register(emit, &insn->Src[2]);
1558
1559 if (emit->unit == MESA_SHADER_VERTEX) {
1560 struct src_register zero = get_zero_immediate(emit);
1561 /* We used to simulate CMP with SLT+LRP. But that didn't work when
1562 * src1 or src2 was Inf/NaN. In particular, GLSL sqrt(0) failed
1563 * because it involves a CMP to handle the 0 case.
1564 * Use a conditional expression instead.
1565 */
1566 return emit_conditional(emit, PIPE_FUNC_LESS, dst,
1567 src0, zero, src1, src2);
1568 } else {
1569 assert(emit->unit == MESA_SHADER_FRAGMENT);
1570
1571 /* CMP DST, SRC0, SRC2, SRC1 */
1572 return submit_op3(emit, inst_token(SVGA3DOP_CMP), dst,
1573 src0, src2, src1);
1574 }
1575 }
1576
1577
1578 /**
1579 * Translate/emit 2-operand (coord, sampler) texture instructions.
1580 */
1581 static bool
1582 emit_tex2(struct svga_shader_emitter *emit,
1583 const struct tgsi_full_instruction *insn,
1584 SVGA3dShaderDestToken dst)
1585 {
1586 SVGA3dShaderInstToken inst;
1587 struct src_register texcoord;
1588 struct src_register sampler;
1589 SVGA3dShaderDestToken tmp;
1590
1591 inst.value = 0;
1592
1593 switch (insn->Instruction.Opcode) {
1594 case TGSI_OPCODE_TEX:
1595 inst.op = SVGA3DOP_TEX;
1596 break;
1597 case TGSI_OPCODE_TXP:
1598 inst.op = SVGA3DOP_TEX;
1599 inst.control = SVGA3DOPCONT_PROJECT;
1600 break;
1601 case TGSI_OPCODE_TXB:
1602 inst.op = SVGA3DOP_TEX;
1603 inst.control = SVGA3DOPCONT_BIAS;
1604 break;
1605 case TGSI_OPCODE_TXL:
1606 inst.op = SVGA3DOP_TEXLDL;
1607 break;
1608 default:
1609 assert(0);
1610 return false;
1611 }
1612
1613 texcoord = translate_src_register(emit, &insn->Src[0]);
1614 sampler = translate_src_register(emit, &insn->Src[1]);
1615
1616 if (emit->key.tex[sampler.base.num].unnormalized ||
1617 emit->dynamic_branching_level > 0)
1618 tmp = get_temp(emit);
1619
1620 /* Can't do mipmapping inside dynamic branch constructs. Force LOD
1621 * zero in that case.
1622 */
1623 if (emit->dynamic_branching_level > 0 &&
1624 inst.op == SVGA3DOP_TEX &&
1625 SVGA3dShaderGetRegType(texcoord.base.value) == SVGA3DREG_TEMP) {
1626 struct src_register zero = get_zero_immediate(emit);
1627
1628 /* MOV tmp, texcoord */
1629 if (!submit_op1(emit,
1630 inst_token(SVGA3DOP_MOV),
1631 tmp,
1632 texcoord))
1633 return false;
1634
1635 /* MOV tmp.w, zero */
1636 if (!submit_op1(emit,
1637 inst_token(SVGA3DOP_MOV),
1638 writemask(tmp, TGSI_WRITEMASK_W),
1639 zero))
1640 return false;
1641
1642 texcoord = src(tmp);
1643 inst.op = SVGA3DOP_TEXLDL;
1644 }
1645
1646 /* Explicit normalization of texcoords:
1647 */
1648 if (emit->key.tex[sampler.base.num].unnormalized) {
1649 struct src_register wh = get_tex_dimensions(emit, sampler.base.num);
1650
1651 /* MUL tmp, SRC0, WH */
1652 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL),
1653 tmp, texcoord, wh))
1654 return false;
1655
1656 texcoord = src(tmp);
1657 }
1658
1659 return submit_op2(emit, inst, dst, texcoord, sampler);
1660 }
1661
1662
1663 /**
1664 * Translate/emit 4-operand (coord, ddx, ddy, sampler) texture instructions.
1665 */
1666 static bool
1667 emit_tex4(struct svga_shader_emitter *emit,
1668 const struct tgsi_full_instruction *insn,
1669 SVGA3dShaderDestToken dst)
1670 {
1671 SVGA3dShaderInstToken inst;
1672 struct src_register texcoord;
1673 struct src_register ddx;
1674 struct src_register ddy;
1675 struct src_register sampler;
1676
1677 texcoord = translate_src_register(emit, &insn->Src[0]);
1678 ddx = translate_src_register(emit, &insn->Src[1]);
1679 ddy = translate_src_register(emit, &insn->Src[2]);
1680 sampler = translate_src_register(emit, &insn->Src[3]);
1681
1682 inst.value = 0;
1683
1684 switch (insn->Instruction.Opcode) {
1685 case TGSI_OPCODE_TXD:
1686 inst.op = SVGA3DOP_TEXLDD; /* 4 args! */
1687 break;
1688 default:
1689 assert(0);
1690 return false;
1691 }
1692
1693 return submit_op4(emit, inst, dst, texcoord, sampler, ddx, ddy);
1694 }
1695
1696
1697 /**
1698 * Emit texture swizzle code. We do this here since SVGA samplers don't
1699 * directly support swizzles.
1700 */
1701 static bool
1702 emit_tex_swizzle(struct svga_shader_emitter *emit,
1703 SVGA3dShaderDestToken dst,
1704 struct src_register src,
1705 unsigned swizzle_x,
1706 unsigned swizzle_y,
1707 unsigned swizzle_z,
1708 unsigned swizzle_w)
1709 {
1710 const unsigned swizzleIn[4] = {swizzle_x, swizzle_y, swizzle_z, swizzle_w};
1711 unsigned srcSwizzle[4];
1712 unsigned srcWritemask = 0x0, zeroWritemask = 0x0, oneWritemask = 0x0;
1713
1714 /* build writemasks and srcSwizzle terms */
1715 for (unsigned i = 0; i < 4; i++) {
1716 if (swizzleIn[i] == PIPE_SWIZZLE_0) {
1717 srcSwizzle[i] = TGSI_SWIZZLE_X + i;
1718 zeroWritemask |= (1 << i);
1719 } else if (swizzleIn[i] == PIPE_SWIZZLE_1) {
1720 srcSwizzle[i] = TGSI_SWIZZLE_X + i;
1721 oneWritemask |= (1 << i);
1722 } else {
1723 srcSwizzle[i] = swizzleIn[i];
1724 srcWritemask |= (1 << i);
1725 }
1726 }
1727
1728 /* write x/y/z/w comps */
1729 if (dst.mask & srcWritemask) {
1730 if (!submit_op1(emit,
1731 inst_token(SVGA3DOP_MOV),
1732 writemask(dst, srcWritemask),
1733 swizzle(src,
1734 srcSwizzle[0],
1735 srcSwizzle[1],
1736 srcSwizzle[2],
1737 srcSwizzle[3])))
1738 return false;
1739 }
1740
1741 /* write 0 comps */
1742 if (dst.mask & zeroWritemask) {
1743 if (!submit_op1(emit,
1744 inst_token(SVGA3DOP_MOV),
1745 writemask(dst, zeroWritemask),
1746 get_zero_immediate(emit)))
1747 return false;
1748 }
1749
1750 /* write 1 comps */
1751 if (dst.mask & oneWritemask) {
1752 if (!submit_op1(emit,
1753 inst_token(SVGA3DOP_MOV),
1754 writemask(dst, oneWritemask),
1755 get_one_immediate(emit)))
1756 return false;
1757 }
1758
1759 return true;
1760 }
1761
1762
1763 /**
1764 * Translate/emit a TGSI texture sample instruction.
1765 */
1766 static bool
1767 emit_tex(struct svga_shader_emitter *emit,
1768 const struct tgsi_full_instruction *insn)
1769 {
1770 SVGA3dShaderDestToken dst =
1771 translate_dst_register(emit, insn, 0);
1772 struct src_register src0 =
1773 translate_src_register(emit, &insn->Src[0]);
1774 struct src_register src1 =
1775 translate_src_register(emit, &insn->Src[1]);
1776
1777 SVGA3dShaderDestToken tex_result;
1778 const unsigned unit = src1.base.num;
1779
1780 /* check for shadow samplers */
1781 bool compare = (emit->key.tex[unit].compare_mode ==
1782 PIPE_TEX_COMPARE_R_TO_TEXTURE);
1783
1784 /* texture swizzle */
1785 bool swizzle = (emit->key.tex[unit].swizzle_r != PIPE_SWIZZLE_X ||
1786 emit->key.tex[unit].swizzle_g != PIPE_SWIZZLE_Y ||
1787 emit->key.tex[unit].swizzle_b != PIPE_SWIZZLE_Z ||
1788 emit->key.tex[unit].swizzle_a != PIPE_SWIZZLE_W);
1789
1790 bool saturate = insn->Instruction.Saturate;
1791
1792 /* If doing compare processing or tex swizzle or saturation, we need to put
1793 * the fetched color into a temporary so it can be used as a source later on.
1794 */
1795 if (compare || swizzle || saturate) {
1796 tex_result = get_temp(emit);
1797 } else {
1798 tex_result = dst;
1799 }
1800
1801 switch (insn->Instruction.Opcode) {
1802 case TGSI_OPCODE_TEX:
1803 case TGSI_OPCODE_TXB:
1804 case TGSI_OPCODE_TXP:
1805 case TGSI_OPCODE_TXL:
1806 if (!emit_tex2(emit, insn, tex_result))
1807 return false;
1808 break;
1809 case TGSI_OPCODE_TXD:
1810 if (!emit_tex4(emit, insn, tex_result))
1811 return false;
1812 break;
1813 default:
1814 assert(0);
1815 }
1816
1817 if (compare) {
1818 SVGA3dShaderDestToken dst2;
1819
1820 if (swizzle || saturate)
1821 dst2 = tex_result;
1822 else
1823 dst2 = dst;
1824
1825 if (dst.mask & TGSI_WRITEMASK_XYZ) {
1826 SVGA3dShaderDestToken src0_zdivw = get_temp(emit);
1827 /* When sampling a depth texture, the result of the comparison is in
1828 * the Y component.
1829 */
1830 struct src_register tex_src_x = scalar(src(tex_result), TGSI_SWIZZLE_Y);
1831 struct src_register r_coord;
1832
1833 if (insn->Instruction.Opcode == TGSI_OPCODE_TXP) {
1834 /* Divide texcoord R by Q */
1835 if (!submit_op1(emit, inst_token(SVGA3DOP_RCP),
1836 writemask(src0_zdivw, TGSI_WRITEMASK_X),
1837 scalar(src0, TGSI_SWIZZLE_W)))
1838 return false;
1839
1840 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL),
1841 writemask(src0_zdivw, TGSI_WRITEMASK_X),
1842 scalar(src0, TGSI_SWIZZLE_Z),
1843 scalar(src(src0_zdivw), TGSI_SWIZZLE_X)))
1844 return false;
1845
1846 r_coord = scalar(src(src0_zdivw), TGSI_SWIZZLE_X);
1847 } else {
1848 r_coord = scalar(src0, TGSI_SWIZZLE_Z);
1849 }
1850
1851 /* Compare texture sample value against R component of texcoord */
1852 if (!emit_select(emit,
1853 emit->key.tex[unit].compare_func,
1854 writemask(dst2, TGSI_WRITEMASK_XYZ),
1855 r_coord,
1856 tex_src_x))
1857 return false;
1858 }
1859
1860 if (dst.mask & TGSI_WRITEMASK_W) {
1861 struct src_register one = get_one_immediate(emit);
1862
1863 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
1864 writemask(dst2, TGSI_WRITEMASK_W),
1865 one))
1866 return false;
1867 }
1868 }
1869
1870 if (saturate && !swizzle) {
1871 /* MOV_SAT real_dst, dst */
1872 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst, src(tex_result)))
1873 return false;
1874 } else if (swizzle) {
1875 /* swizzle from tex_result to dst (handles saturation too, if any) */
1876 emit_tex_swizzle(emit,
1877 dst, src(tex_result),
1878 emit->key.tex[unit].swizzle_r,
1879 emit->key.tex[unit].swizzle_g,
1880 emit->key.tex[unit].swizzle_b,
1881 emit->key.tex[unit].swizzle_a);
1882 }
1883
1884 return true;
1885 }
1886
1887
1888 static bool
1889 emit_bgnloop(struct svga_shader_emitter *emit,
1890 const struct tgsi_full_instruction *insn)
1891 {
1892 SVGA3dShaderInstToken inst = inst_token(SVGA3DOP_LOOP);
1893 struct src_register loop_reg = src_register(SVGA3DREG_LOOP, 0);
1894 struct src_register const_int = get_loop_const(emit);
1895
1896 emit->dynamic_branching_level++;
1897
1898 return (emit_instruction(emit, inst) &&
1899 emit_src(emit, loop_reg) &&
1900 emit_src(emit, const_int));
1901 }
1902
1903
1904 static bool
1905 emit_endloop(struct svga_shader_emitter *emit,
1906 const struct tgsi_full_instruction *insn)
1907 {
1908 SVGA3dShaderInstToken inst = inst_token(SVGA3DOP_ENDLOOP);
1909
1910 emit->dynamic_branching_level--;
1911
1912 return emit_instruction(emit, inst);
1913 }
1914
1915
1916 /**
1917 * Translate/emit TGSI BREAK (out of loop) instruction.
1918 */
1919 static bool
1920 emit_brk(struct svga_shader_emitter *emit,
1921 const struct tgsi_full_instruction *insn)
1922 {
1923 SVGA3dShaderInstToken inst = inst_token(SVGA3DOP_BREAK);
1924 return emit_instruction(emit, inst);
1925 }
1926
1927
1928 /**
1929 * Emit simple instruction which operates on one scalar value (not
1930 * a vector). Ex: LG2, RCP, RSQ.
1931 */
1932 static bool
1933 emit_scalar_op1(struct svga_shader_emitter *emit,
1934 SVGA3dShaderOpCodeType opcode,
1935 const struct tgsi_full_instruction *insn)
1936 {
1937 SVGA3dShaderInstToken inst;
1938 SVGA3dShaderDestToken dst;
1939 struct src_register src;
1940
1941 inst = inst_token(opcode);
1942 dst = translate_dst_register(emit, insn, 0);
1943 src = translate_src_register(emit, &insn->Src[0]);
1944 src = scalar(src, TGSI_SWIZZLE_X);
1945
1946 return submit_op1(emit, inst, dst, src);
1947 }
1948
1949
1950 /**
1951 * Translate/emit a simple instruction (one which has no special-case
1952 * code) such as ADD, MUL, MIN, MAX.
1953 */
1954 static bool
1955 emit_simple_instruction(struct svga_shader_emitter *emit,
1956 SVGA3dShaderOpCodeType opcode,
1957 const struct tgsi_full_instruction *insn)
1958 {
1959 const struct tgsi_full_src_register *src = insn->Src;
1960 SVGA3dShaderInstToken inst;
1961 SVGA3dShaderDestToken dst;
1962
1963 inst = inst_token(opcode);
1964 dst = translate_dst_register(emit, insn, 0);
1965
1966 switch (insn->Instruction.NumSrcRegs) {
1967 case 0:
1968 return submit_op0(emit, inst, dst);
1969 case 1:
1970 return submit_op1(emit, inst, dst,
1971 translate_src_register(emit, &src[0]));
1972 case 2:
1973 return submit_op2(emit, inst, dst,
1974 translate_src_register(emit, &src[0]),
1975 translate_src_register(emit, &src[1]));
1976 case 3:
1977 return submit_op3(emit, inst, dst,
1978 translate_src_register(emit, &src[0]),
1979 translate_src_register(emit, &src[1]),
1980 translate_src_register(emit, &src[2]));
1981 default:
1982 assert(0);
1983 return false;
1984 }
1985 }
1986
1987
1988 /**
1989 * TGSI_OPCODE_MOVE is only special-cased here to detect the
1990 * svga_fragment_shader::constant_color_output case.
1991 */
1992 static bool
1993 emit_mov(struct svga_shader_emitter *emit,
1994 const struct tgsi_full_instruction *insn)
1995 {
1996 const struct tgsi_full_src_register *src = &insn->Src[0];
1997 const struct tgsi_full_dst_register *dst = &insn->Dst[0];
1998
1999 if (emit->unit == MESA_SHADER_FRAGMENT &&
2000 dst->Register.File == TGSI_FILE_OUTPUT &&
2001 dst->Register.Index == 0 &&
2002 src->Register.File == TGSI_FILE_CONSTANT &&
2003 !src->Register.Indirect) {
2004 emit->constant_color_output = true;
2005 }
2006
2007 return emit_simple_instruction(emit, SVGA3DOP_MOV, insn);
2008 }
2009
2010
2011 /**
2012 * Translate TGSI SQRT instruction
2013 * if src1 == 0
2014 * mov dst, src1
2015 * else
2016 * rsq temp, src1
2017 * rcp dst, temp
2018 * endif
2019 */
2020 static bool
2021 emit_sqrt(struct svga_shader_emitter *emit,
2022 const struct tgsi_full_instruction *insn)
2023 {
2024 const struct src_register src1 = translate_src_register(emit, &insn->Src[0]);
2025 const struct src_register zero = get_zero_immediate(emit);
2026 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2027 SVGA3dShaderDestToken temp = get_temp(emit);
2028 SVGA3dShaderInstToken if_token = inst_token(SVGA3DOP_IFC);
2029 bool ret = true;
2030
2031 if_token.control = SVGA3DOPCOMP_EQ;
2032
2033 if (!(emit_instruction(emit, if_token) &&
2034 emit_src(emit, src1) &&
2035 emit_src(emit, zero))) {
2036 ret = false;
2037 goto cleanup;
2038 }
2039
2040 if (!submit_op1(emit,
2041 inst_token(SVGA3DOP_MOV),
2042 dst, src1)) {
2043 ret = false;
2044 goto cleanup;
2045 }
2046
2047 if (!emit_instruction(emit, inst_token(SVGA3DOP_ELSE))) {
2048 ret = false;
2049 goto cleanup;
2050 }
2051
2052 if (!submit_op1(emit,
2053 inst_token(SVGA3DOP_RSQ),
2054 temp, src1)) {
2055 ret = false;
2056 goto cleanup;
2057 }
2058
2059 if (!submit_op1(emit,
2060 inst_token(SVGA3DOP_RCP),
2061 dst, src(temp))) {
2062 ret = false;
2063 goto cleanup;
2064 }
2065
2066 if (!emit_instruction(emit, inst_token(SVGA3DOP_ENDIF))) {
2067 ret = false;
2068 goto cleanup;
2069 }
2070
2071 cleanup:
2072 release_temp(emit, temp);
2073
2074 return ret;
2075 }
2076
2077
2078 /**
2079 * Translate/emit TGSI DDX, DDY instructions.
2080 */
2081 static bool
2082 emit_deriv(struct svga_shader_emitter *emit,
2083 const struct tgsi_full_instruction *insn)
2084 {
2085 if (emit->dynamic_branching_level > 0 &&
2086 insn->Src[0].Register.File == TGSI_FILE_TEMPORARY) {
2087 SVGA3dShaderDestToken dst =
2088 translate_dst_register(emit, insn, 0);
2089
2090 /* Deriv opcodes not valid inside dynamic branching, workaround
2091 * by zeroing out the destination.
2092 */
2093 if (!submit_op1(emit,
2094 inst_token(SVGA3DOP_MOV),
2095 dst,
2096 get_zero_immediate(emit)))
2097 return false;
2098
2099 return true;
2100 } else {
2101 SVGA3dShaderOpCodeType opcode;
2102 const struct tgsi_full_src_register *reg = &insn->Src[0];
2103 SVGA3dShaderInstToken inst;
2104 SVGA3dShaderDestToken dst;
2105 struct src_register src0;
2106
2107 switch (insn->Instruction.Opcode) {
2108 case TGSI_OPCODE_DDX:
2109 opcode = SVGA3DOP_DSX;
2110 break;
2111 case TGSI_OPCODE_DDY:
2112 opcode = SVGA3DOP_DSY;
2113 break;
2114 default:
2115 return false;
2116 }
2117
2118 inst = inst_token(opcode);
2119 dst = translate_dst_register(emit, insn, 0);
2120 src0 = translate_src_register(emit, reg);
2121
2122 /* We cannot use negate or abs on source to dsx/dsy instruction.
2123 */
2124 if (reg->Register.Absolute ||
2125 reg->Register.Negate) {
2126 SVGA3dShaderDestToken temp = get_temp(emit);
2127
2128 if (!emit_repl(emit, temp, &src0))
2129 return false;
2130 }
2131
2132 return submit_op1(emit, inst, dst, src0);
2133 }
2134 }
2135
2136
2137 /**
2138 * Translate/emit ARL (Address Register Load) instruction. Used to
2139 * move a value into the special 'address' register. Used to implement
2140 * indirect/variable indexing into arrays.
2141 */
2142 static bool
2143 emit_arl(struct svga_shader_emitter *emit,
2144 const struct tgsi_full_instruction *insn)
2145 {
2146 ++emit->current_arl;
2147 if (emit->unit == MESA_SHADER_FRAGMENT) {
2148 /* MOVA not present in pixel shader instruction set.
2149 * Ignore this instruction altogether since it is
2150 * only used for loop counters -- and for that
2151 * we reference aL directly.
2152 */
2153 return true;
2154 }
2155 if (svga_arl_needs_adjustment(emit)) {
2156 return emit_fake_arl(emit, insn);
2157 } else {
2158 /* no need to adjust, just emit straight arl */
2159 return emit_simple_instruction(emit, SVGA3DOP_MOVA, insn);
2160 }
2161 }
2162
2163
2164 static bool
2165 emit_pow(struct svga_shader_emitter *emit,
2166 const struct tgsi_full_instruction *insn)
2167 {
2168 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2169 struct src_register src0 = translate_src_register(
2170 emit, &insn->Src[0]);
2171 struct src_register src1 = translate_src_register(
2172 emit, &insn->Src[1]);
2173 bool need_tmp = false;
2174
2175 /* POW can only output to a temporary */
2176 if (insn->Dst[0].Register.File != TGSI_FILE_TEMPORARY)
2177 need_tmp = true;
2178
2179 /* POW src1 must not be the same register as dst */
2180 if (alias_src_dst(src1, dst))
2181 need_tmp = true;
2182
2183 /* it's a scalar op */
2184 src0 = scalar(src0, TGSI_SWIZZLE_X);
2185 src1 = scalar(src1, TGSI_SWIZZLE_X);
2186
2187 if (need_tmp) {
2188 SVGA3dShaderDestToken tmp =
2189 writemask(get_temp(emit), TGSI_WRITEMASK_X);
2190
2191 if (!submit_op2(emit, inst_token(SVGA3DOP_POW), tmp, src0, src1))
2192 return false;
2193
2194 return submit_op1(emit, inst_token(SVGA3DOP_MOV),
2195 dst, scalar(src(tmp), 0));
2196 } else {
2197 return submit_op2(emit, inst_token(SVGA3DOP_POW), dst, src0, src1);
2198 }
2199 }
2200
2201
2202 /**
2203 * Emit a LRP (linear interpolation) instruction.
2204 */
2205 static bool
2206 submit_lrp(struct svga_shader_emitter *emit,
2207 SVGA3dShaderDestToken dst,
2208 struct src_register src0,
2209 struct src_register src1,
2210 struct src_register src2)
2211 {
2212 SVGA3dShaderDestToken tmp;
2213 bool need_dst_tmp = false;
2214
2215 /* The dst reg must be a temporary, and not be the same as src0 or src2 */
2216 if (SVGA3dShaderGetRegType(dst.value) != SVGA3DREG_TEMP ||
2217 alias_src_dst(src0, dst) ||
2218 alias_src_dst(src2, dst))
2219 need_dst_tmp = true;
2220
2221 if (need_dst_tmp) {
2222 tmp = get_temp(emit);
2223 tmp.mask = dst.mask;
2224 } else {
2225 tmp = dst;
2226 }
2227
2228 if (!submit_op3(emit, inst_token(SVGA3DOP_LRP), tmp, src0, src1, src2))
2229 return false;
2230
2231 if (need_dst_tmp) {
2232 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst, src(tmp)))
2233 return false;
2234 }
2235
2236 return true;
2237 }
2238
2239
2240 /**
2241 * Translate/emit LRP (Linear Interpolation) instruction.
2242 */
2243 static bool
2244 emit_lrp(struct svga_shader_emitter *emit,
2245 const struct tgsi_full_instruction *insn)
2246 {
2247 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2248 const struct src_register src0 = translate_src_register(
2249 emit, &insn->Src[0]);
2250 const struct src_register src1 = translate_src_register(
2251 emit, &insn->Src[1]);
2252 const struct src_register src2 = translate_src_register(
2253 emit, &insn->Src[2]);
2254
2255 return submit_lrp(emit, dst, src0, src1, src2);
2256 }
2257
2258 /**
2259 * Translate/emit DST (Distance function) instruction.
2260 */
2261 static bool
2262 emit_dst_insn(struct svga_shader_emitter *emit,
2263 const struct tgsi_full_instruction *insn)
2264 {
2265 if (emit->unit == MESA_SHADER_VERTEX) {
2266 /* SVGA/DX9 has a DST instruction, but only for vertex shaders:
2267 */
2268 return emit_simple_instruction(emit, SVGA3DOP_DST, insn);
2269 } else {
2270 /* result[0] = 1 * 1;
2271 * result[1] = a[1] * b[1];
2272 * result[2] = a[2] * 1;
2273 * result[3] = 1 * b[3];
2274 */
2275 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2276 SVGA3dShaderDestToken tmp;
2277 const struct src_register src0 = translate_src_register(
2278 emit, &insn->Src[0]);
2279 const struct src_register src1 = translate_src_register(
2280 emit, &insn->Src[1]);
2281 bool need_tmp = false;
2282
2283 if (SVGA3dShaderGetRegType(dst.value) != SVGA3DREG_TEMP ||
2284 alias_src_dst(src0, dst) ||
2285 alias_src_dst(src1, dst))
2286 need_tmp = true;
2287
2288 if (need_tmp) {
2289 tmp = get_temp(emit);
2290 } else {
2291 tmp = dst;
2292 }
2293
2294 /* tmp.xw = 1.0
2295 */
2296 if (tmp.mask & TGSI_WRITEMASK_XW) {
2297 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2298 writemask(tmp, TGSI_WRITEMASK_XW),
2299 get_one_immediate(emit)))
2300 return false;
2301 }
2302
2303 /* tmp.yz = src0
2304 */
2305 if (tmp.mask & TGSI_WRITEMASK_YZ) {
2306 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2307 writemask(tmp, TGSI_WRITEMASK_YZ),
2308 src0))
2309 return false;
2310 }
2311
2312 /* tmp.yw = tmp * src1
2313 */
2314 if (tmp.mask & TGSI_WRITEMASK_YW) {
2315 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL),
2316 writemask(tmp, TGSI_WRITEMASK_YW),
2317 src(tmp),
2318 src1))
2319 return false;
2320 }
2321
2322 /* dst = tmp
2323 */
2324 if (need_tmp) {
2325 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2326 dst,
2327 src(tmp)))
2328 return false;
2329 }
2330 }
2331
2332 return true;
2333 }
2334
2335
2336 static bool
2337 emit_exp(struct svga_shader_emitter *emit,
2338 const struct tgsi_full_instruction *insn)
2339 {
2340 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2341 struct src_register src0 =
2342 translate_src_register(emit, &insn->Src[0]);
2343 SVGA3dShaderDestToken fraction;
2344
2345 if (dst.mask & TGSI_WRITEMASK_Y)
2346 fraction = dst;
2347 else if (dst.mask & TGSI_WRITEMASK_X)
2348 fraction = get_temp(emit);
2349 else
2350 fraction.value = 0;
2351
2352 /* If y is being written, fill it with src0 - floor(src0).
2353 */
2354 if (dst.mask & TGSI_WRITEMASK_XY) {
2355 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC),
2356 writemask(fraction, TGSI_WRITEMASK_Y),
2357 src0))
2358 return false;
2359 }
2360
2361 /* If x is being written, fill it with 2 ^ floor(src0).
2362 */
2363 if (dst.mask & TGSI_WRITEMASK_X) {
2364 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD),
2365 writemask(dst, TGSI_WRITEMASK_X),
2366 src0,
2367 scalar(negate(src(fraction)), TGSI_SWIZZLE_Y)))
2368 return false;
2369
2370 if (!submit_op1(emit, inst_token(SVGA3DOP_EXP),
2371 writemask(dst, TGSI_WRITEMASK_X),
2372 scalar(src(dst), TGSI_SWIZZLE_X)))
2373 return false;
2374
2375 if (!(dst.mask & TGSI_WRITEMASK_Y))
2376 release_temp(emit, fraction);
2377 }
2378
2379 /* If z is being written, fill it with 2 ^ src0 (partial precision).
2380 */
2381 if (dst.mask & TGSI_WRITEMASK_Z) {
2382 if (!submit_op1(emit, inst_token(SVGA3DOP_EXPP),
2383 writemask(dst, TGSI_WRITEMASK_Z),
2384 src0))
2385 return false;
2386 }
2387
2388 /* If w is being written, fill it with one.
2389 */
2390 if (dst.mask & TGSI_WRITEMASK_W) {
2391 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2392 writemask(dst, TGSI_WRITEMASK_W),
2393 get_one_immediate(emit)))
2394 return false;
2395 }
2396
2397 return true;
2398 }
2399
2400
2401 /**
2402 * Translate/emit LIT (Lighting helper) instruction.
2403 */
2404 static bool
2405 emit_lit(struct svga_shader_emitter *emit,
2406 const struct tgsi_full_instruction *insn)
2407 {
2408 if (emit->unit == MESA_SHADER_VERTEX) {
2409 /* SVGA/DX9 has a LIT instruction, but only for vertex shaders:
2410 */
2411 return emit_simple_instruction(emit, SVGA3DOP_LIT, insn);
2412 } else {
2413 /* D3D vs. GL semantics can be fairly easily accommodated by
2414 * variations on this sequence.
2415 *
2416 * GL:
2417 * tmp.y = src.x
2418 * tmp.z = pow(src.y,src.w)
2419 * p0 = src0.xxxx > 0
2420 * result = zero.wxxw
2421 * (p0) result.yz = tmp
2422 *
2423 * D3D:
2424 * tmp.y = src.x
2425 * tmp.z = pow(src.y,src.w)
2426 * p0 = src0.xxyy > 0
2427 * result = zero.wxxw
2428 * (p0) result.yz = tmp
2429 *
2430 * Will implement the GL version for now.
2431 */
2432 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2433 SVGA3dShaderDestToken tmp = get_temp(emit);
2434 const struct src_register src0 = translate_src_register(
2435 emit, &insn->Src[0]);
2436
2437 /* tmp = pow(src.y, src.w)
2438 */
2439 if (dst.mask & TGSI_WRITEMASK_Z) {
2440 if (!submit_op2(emit, inst_token(SVGA3DOP_POW),
2441 tmp,
2442 scalar(src0, 1),
2443 scalar(src0, 3)))
2444 return false;
2445 }
2446
2447 /* tmp.y = src.x
2448 */
2449 if (dst.mask & TGSI_WRITEMASK_Y) {
2450 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2451 writemask(tmp, TGSI_WRITEMASK_Y),
2452 scalar(src0, 0)))
2453 return false;
2454 }
2455
2456 /* Can't quite do this with emit conditional due to the extra
2457 * writemask on the predicated mov:
2458 */
2459 {
2460 SVGA3dShaderDestToken pred_reg = dst_register(SVGA3DREG_PREDICATE, 0);
2461 struct src_register predsrc;
2462
2463 /* D3D vs GL semantics:
2464 */
2465 if (0)
2466 predsrc = swizzle(src0, 0, 0, 1, 1); /* D3D */
2467 else
2468 predsrc = swizzle(src0, 0, 0, 0, 0); /* GL */
2469
2470 /* SETP src0.xxyy, GT, {0}.x */
2471 if (!submit_op2(emit,
2472 inst_token_setp(SVGA3DOPCOMP_GT),
2473 pred_reg,
2474 predsrc,
2475 get_zero_immediate(emit)))
2476 return false;
2477
2478 /* MOV dst, fail */
2479 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), dst,
2480 get_immediate(emit, 1.0f, 0.0f, 0.0f, 1.0f)))
2481 return false;
2482
2483 /* MOV dst.yz, tmp (predicated)
2484 *
2485 * Note that the predicate reg (and possible modifiers) is passed
2486 * as the first source argument.
2487 */
2488 if (dst.mask & TGSI_WRITEMASK_YZ) {
2489 if (!submit_op2(emit,
2490 inst_token_predicated(SVGA3DOP_MOV),
2491 writemask(dst, TGSI_WRITEMASK_YZ),
2492 src(pred_reg), src(tmp)))
2493 return false;
2494 }
2495 }
2496 }
2497
2498 return true;
2499 }
2500
2501
2502 static bool
2503 emit_ex2(struct svga_shader_emitter *emit,
2504 const struct tgsi_full_instruction *insn)
2505 {
2506 SVGA3dShaderInstToken inst;
2507 SVGA3dShaderDestToken dst;
2508 struct src_register src0;
2509
2510 inst = inst_token(SVGA3DOP_EXP);
2511 dst = translate_dst_register(emit, insn, 0);
2512 src0 = translate_src_register(emit, &insn->Src[0]);
2513 src0 = scalar(src0, TGSI_SWIZZLE_X);
2514
2515 if (dst.mask != TGSI_WRITEMASK_XYZW) {
2516 SVGA3dShaderDestToken tmp = get_temp(emit);
2517
2518 if (!submit_op1(emit, inst, tmp, src0))
2519 return false;
2520
2521 return submit_op1(emit, inst_token(SVGA3DOP_MOV),
2522 dst,
2523 scalar(src(tmp), TGSI_SWIZZLE_X));
2524 }
2525
2526 return submit_op1(emit, inst, dst, src0);
2527 }
2528
2529
2530 static bool
2531 emit_log(struct svga_shader_emitter *emit,
2532 const struct tgsi_full_instruction *insn)
2533 {
2534 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2535 struct src_register src0 =
2536 translate_src_register(emit, &insn->Src[0]);
2537 SVGA3dShaderDestToken abs_tmp;
2538 struct src_register abs_src0;
2539 SVGA3dShaderDestToken log2_abs;
2540
2541 abs_tmp.value = 0;
2542
2543 if (dst.mask & TGSI_WRITEMASK_Z)
2544 log2_abs = dst;
2545 else if (dst.mask & TGSI_WRITEMASK_XY)
2546 log2_abs = get_temp(emit);
2547 else
2548 log2_abs.value = 0;
2549
2550 /* If z is being written, fill it with log2(abs(src0)).
2551 */
2552 if (dst.mask & TGSI_WRITEMASK_XYZ) {
2553 if (!src0.base.srcMod || src0.base.srcMod == SVGA3DSRCMOD_ABS) {
2554 abs_src0 = src0;
2555 } else {
2556 abs_tmp = get_temp(emit);
2557
2558 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2559 abs_tmp,
2560 src0))
2561 return false;
2562
2563 abs_src0 = src(abs_tmp);
2564 }
2565
2566 abs_src0 = absolute(scalar(abs_src0, TGSI_SWIZZLE_X));
2567
2568 if (!submit_op1(emit, inst_token(SVGA3DOP_LOG),
2569 writemask(log2_abs, TGSI_WRITEMASK_Z),
2570 abs_src0))
2571 return false;
2572 }
2573
2574 if (dst.mask & TGSI_WRITEMASK_XY) {
2575 SVGA3dShaderDestToken floor_log2;
2576
2577 if (dst.mask & TGSI_WRITEMASK_X)
2578 floor_log2 = dst;
2579 else
2580 floor_log2 = get_temp(emit);
2581
2582 /* If x is being written, fill it with floor(log2(abs(src0))).
2583 */
2584 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC),
2585 writemask(floor_log2, TGSI_WRITEMASK_X),
2586 scalar(src(log2_abs), TGSI_SWIZZLE_Z)))
2587 return false;
2588
2589 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD),
2590 writemask(floor_log2, TGSI_WRITEMASK_X),
2591 scalar(src(log2_abs), TGSI_SWIZZLE_Z),
2592 negate(src(floor_log2))))
2593 return false;
2594
2595 /* If y is being written, fill it with
2596 * abs (src0) / (2 ^ floor(log2(abs(src0)))).
2597 */
2598 if (dst.mask & TGSI_WRITEMASK_Y) {
2599 if (!submit_op1(emit, inst_token(SVGA3DOP_EXP),
2600 writemask(dst, TGSI_WRITEMASK_Y),
2601 negate(scalar(src(floor_log2),
2602 TGSI_SWIZZLE_X))))
2603 return false;
2604
2605 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL),
2606 writemask(dst, TGSI_WRITEMASK_Y),
2607 src(dst),
2608 abs_src0))
2609 return false;
2610 }
2611
2612 if (!(dst.mask & TGSI_WRITEMASK_X))
2613 release_temp(emit, floor_log2);
2614
2615 if (!(dst.mask & TGSI_WRITEMASK_Z))
2616 release_temp(emit, log2_abs);
2617 }
2618
2619 if (dst.mask & TGSI_WRITEMASK_XYZ && src0.base.srcMod &&
2620 src0.base.srcMod != SVGA3DSRCMOD_ABS)
2621 release_temp(emit, abs_tmp);
2622
2623 /* If w is being written, fill it with one.
2624 */
2625 if (dst.mask & TGSI_WRITEMASK_W) {
2626 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
2627 writemask(dst, TGSI_WRITEMASK_W),
2628 get_one_immediate(emit)))
2629 return false;
2630 }
2631
2632 return true;
2633 }
2634
2635
2636 /**
2637 * Translate TGSI TRUNC or ROUND instruction.
2638 * We need to truncate toward zero. Ex: trunc(-1.9) = -1
2639 * Different approaches are needed for VS versus PS.
2640 */
2641 static bool
2642 emit_trunc_round(struct svga_shader_emitter *emit,
2643 const struct tgsi_full_instruction *insn,
2644 bool round)
2645 {
2646 SVGA3dShaderDestToken dst = translate_dst_register(emit, insn, 0);
2647 const struct src_register src0 =
2648 translate_src_register(emit, &insn->Src[0]);
2649 SVGA3dShaderDestToken t1 = get_temp(emit);
2650
2651 if (round) {
2652 SVGA3dShaderDestToken t0 = get_temp(emit);
2653 struct src_register half = get_half_immediate(emit);
2654
2655 /* t0 = abs(src0) + 0.5 */
2656 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), t0,
2657 absolute(src0), half))
2658 return false;
2659
2660 /* t1 = fract(t0) */
2661 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC), t1, src(t0)))
2662 return false;
2663
2664 /* t1 = t0 - t1 */
2665 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), t1, src(t0),
2666 negate(src(t1))))
2667 return false;
2668 } else {
2669 /* trunc */
2670
2671 /* t1 = fract(abs(src0)) */
2672 if (!submit_op1(emit, inst_token(SVGA3DOP_FRC), t1, absolute(src0)))
2673 return false;
2674
2675 /* t1 = abs(src0) - t1 */
2676 if (!submit_op2(emit, inst_token(SVGA3DOP_ADD), t1, absolute(src0),
2677 negate(src(t1))))
2678 return false;
2679 }
2680
2681 /*
2682 * Now we need to multiply t1 by the sign of the original value.
2683 */
2684 if (emit->unit == MESA_SHADER_VERTEX) {
2685 /* For VS: use SGN instruction */
2686 /* Need two extra/dummy registers: */
2687 SVGA3dShaderDestToken t2 = get_temp(emit), t3 = get_temp(emit),
2688 t4 = get_temp(emit);
2689
2690 /* t2 = sign(src0) */
2691 if (!submit_op3(emit, inst_token(SVGA3DOP_SGN), t2, src0,
2692 src(t3), src(t4)))
2693 return false;
2694
2695 /* dst = t1 * t2 */
2696 if (!submit_op2(emit, inst_token(SVGA3DOP_MUL), dst, src(t1), src(t2)))
2697 return false;
2698 } else {
2699 /* For FS: Use CMP instruction */
2700 return submit_op3(emit, inst_token(SVGA3DOP_CMP), dst,
2701 src0, src(t1), negate(src(t1)));
2702 }
2703
2704 return true;
2705 }
2706
2707
2708 /**
2709 * Translate/emit "begin subroutine" instruction/marker/label.
2710 */
2711 static bool
2712 emit_bgnsub(struct svga_shader_emitter *emit,
2713 unsigned position,
2714 const struct tgsi_full_instruction *insn)
2715 {
2716 /* Note that we've finished the main function and are now emitting
2717 * subroutines. This affects how we terminate the generated
2718 * shader.
2719 */
2720 emit->in_main_func = false;
2721
2722 for (unsigned i = 0; i < emit->nr_labels; i++) {
2723 if (emit->label[i] == position) {
2724 return (emit_instruction(emit, inst_token(SVGA3DOP_RET)) &&
2725 emit_instruction(emit, inst_token(SVGA3DOP_LABEL)) &&
2726 emit_src(emit, src_register(SVGA3DREG_LABEL, i)));
2727 }
2728 }
2729
2730 assert(0);
2731 return true;
2732 }
2733
2734
2735 /**
2736 * Translate/emit subroutine call instruction.
2737 */
2738 static bool
2739 emit_call(struct svga_shader_emitter *emit,
2740 const struct tgsi_full_instruction *insn)
2741 {
2742 unsigned position = insn->Label.Label;
2743
2744 unsigned i;
2745 for (i = 0; i < emit->nr_labels; i++) {
2746 if (emit->label[i] == position)
2747 break;
2748 }
2749
2750 if (emit->nr_labels == ARRAY_SIZE(emit->label))
2751 return false;
2752
2753 if (i == emit->nr_labels) {
2754 emit->label[i] = position;
2755 emit->nr_labels++;
2756 }
2757
2758 return (emit_instruction(emit, inst_token(SVGA3DOP_CALL)) &&
2759 emit_src(emit, src_register(SVGA3DREG_LABEL, i)));
2760 }
2761
2762
2763 /**
2764 * Called at the end of the shader. Actually, emit special "fix-up"
2765 * code for the vertex/fragment shader.
2766 */
2767 static bool
2768 emit_end(struct svga_shader_emitter *emit)
2769 {
2770 if (emit->unit == MESA_SHADER_VERTEX) {
2771 return emit_vs_postamble(emit);
2772 } else {
2773 return emit_ps_postamble(emit);
2774 }
2775 }
2776
2777
2778 /**
2779 * Translate any TGSI instruction to SVGA.
2780 */
2781 static bool
2782 svga_emit_instruction(struct svga_shader_emitter *emit,
2783 unsigned position,
2784 const struct tgsi_full_instruction *insn)
2785 {
2786 switch (insn->Instruction.Opcode) {
2787
2788 case TGSI_OPCODE_ARL:
2789 return emit_arl(emit, insn);
2790
2791 case TGSI_OPCODE_TEX:
2792 case TGSI_OPCODE_TXB:
2793 case TGSI_OPCODE_TXP:
2794 case TGSI_OPCODE_TXL:
2795 case TGSI_OPCODE_TXD:
2796 return emit_tex(emit, insn);
2797
2798 case TGSI_OPCODE_DDX:
2799 case TGSI_OPCODE_DDY:
2800 return emit_deriv(emit, insn);
2801
2802 case TGSI_OPCODE_BGNSUB:
2803 return emit_bgnsub(emit, position, insn);
2804
2805 case TGSI_OPCODE_ENDSUB:
2806 return true;
2807
2808 case TGSI_OPCODE_CAL:
2809 return emit_call(emit, insn);
2810
2811 case TGSI_OPCODE_FLR:
2812 return emit_floor(emit, insn);
2813
2814 case TGSI_OPCODE_TRUNC:
2815 return emit_trunc_round(emit, insn, false);
2816
2817 case TGSI_OPCODE_ROUND:
2818 return emit_trunc_round(emit, insn, true);
2819
2820 case TGSI_OPCODE_CEIL:
2821 return emit_ceil(emit, insn);
2822
2823 case TGSI_OPCODE_CMP:
2824 return emit_cmp(emit, insn);
2825
2826 case TGSI_OPCODE_DIV:
2827 return emit_div(emit, insn);
2828
2829 case TGSI_OPCODE_DP2:
2830 return emit_dp2(emit, insn);
2831
2832 case TGSI_OPCODE_COS:
2833 return emit_cos(emit, insn);
2834
2835 case TGSI_OPCODE_SIN:
2836 return emit_sin(emit, insn);
2837
2838 case TGSI_OPCODE_END:
2839 /* TGSI always finishes the main func with an END */
2840 return emit_end(emit);
2841
2842 case TGSI_OPCODE_KILL_IF:
2843 return emit_cond_discard(emit, insn);
2844
2845 /* Selection opcodes. The underlying language is fairly
2846 * non-orthogonal about these.
2847 */
2848 case TGSI_OPCODE_SEQ:
2849 return emit_select_op(emit, PIPE_FUNC_EQUAL, insn);
2850
2851 case TGSI_OPCODE_SNE:
2852 return emit_select_op(emit, PIPE_FUNC_NOTEQUAL, insn);
2853
2854 case TGSI_OPCODE_SGT:
2855 return emit_select_op(emit, PIPE_FUNC_GREATER, insn);
2856
2857 case TGSI_OPCODE_SGE:
2858 return emit_select_op(emit, PIPE_FUNC_GEQUAL, insn);
2859
2860 case TGSI_OPCODE_SLT:
2861 return emit_select_op(emit, PIPE_FUNC_LESS, insn);
2862
2863 case TGSI_OPCODE_SLE:
2864 return emit_select_op(emit, PIPE_FUNC_LEQUAL, insn);
2865
2866 case TGSI_OPCODE_POW:
2867 return emit_pow(emit, insn);
2868
2869 case TGSI_OPCODE_EX2:
2870 return emit_ex2(emit, insn);
2871
2872 case TGSI_OPCODE_EXP:
2873 return emit_exp(emit, insn);
2874
2875 case TGSI_OPCODE_LOG:
2876 return emit_log(emit, insn);
2877
2878 case TGSI_OPCODE_LG2:
2879 return emit_scalar_op1(emit, SVGA3DOP_LOG, insn);
2880
2881 case TGSI_OPCODE_RSQ:
2882 return emit_scalar_op1(emit, SVGA3DOP_RSQ, insn);
2883
2884 case TGSI_OPCODE_RCP:
2885 return emit_scalar_op1(emit, SVGA3DOP_RCP, insn);
2886
2887 case TGSI_OPCODE_CONT:
2888 /* not expected (we return pipe_shader_caps.cont_supported = 0) */
2889 return false;
2890
2891 case TGSI_OPCODE_RET:
2892 /* This is a noop -- we tell mesa that we can't support RET
2893 * within a function (early return), so this will always be
2894 * followed by an ENDSUB.
2895 */
2896 return true;
2897
2898 /* These aren't actually used by any of the frontends we care
2899 * about:
2900 */
2901 case TGSI_OPCODE_AND:
2902 case TGSI_OPCODE_OR:
2903 case TGSI_OPCODE_I2F:
2904 case TGSI_OPCODE_NOT:
2905 case TGSI_OPCODE_SHL:
2906 case TGSI_OPCODE_ISHR:
2907 case TGSI_OPCODE_XOR:
2908 return false;
2909
2910 case TGSI_OPCODE_IF:
2911 return emit_if(emit, insn);
2912 case TGSI_OPCODE_ELSE:
2913 return emit_else(emit, insn);
2914 case TGSI_OPCODE_ENDIF:
2915 return emit_endif(emit, insn);
2916
2917 case TGSI_OPCODE_BGNLOOP:
2918 return emit_bgnloop(emit, insn);
2919 case TGSI_OPCODE_ENDLOOP:
2920 return emit_endloop(emit, insn);
2921 case TGSI_OPCODE_BRK:
2922 return emit_brk(emit, insn);
2923
2924 case TGSI_OPCODE_KILL:
2925 return emit_discard(emit, insn);
2926
2927 case TGSI_OPCODE_DST:
2928 return emit_dst_insn(emit, insn);
2929
2930 case TGSI_OPCODE_LIT:
2931 return emit_lit(emit, insn);
2932
2933 case TGSI_OPCODE_LRP:
2934 return emit_lrp(emit, insn);
2935
2936 case TGSI_OPCODE_SSG:
2937 return emit_ssg(emit, insn);
2938
2939 case TGSI_OPCODE_MOV:
2940 return emit_mov(emit, insn);
2941
2942 case TGSI_OPCODE_SQRT:
2943 return emit_sqrt(emit, insn);
2944
2945 default:
2946 {
2947 SVGA3dShaderOpCodeType opcode =
2948 translate_opcode(insn->Instruction.Opcode);
2949
2950 if (opcode == SVGA3DOP_LAST_INST)
2951 return false;
2952
2953 if (!emit_simple_instruction(emit, opcode, insn))
2954 return false;
2955 }
2956 }
2957
2958 return true;
2959 }
2960
2961
2962 /**
2963 * Translate/emit a TGSI IMMEDIATE declaration.
2964 * An immediate vector is a constant that's hard-coded into the shader.
2965 */
2966 static bool
2967 svga_emit_immediate(struct svga_shader_emitter *emit,
2968 const struct tgsi_full_immediate *imm)
2969 {
2970 static const float id[4] = {0,0,0,1};
2971 float value[4];
2972 unsigned i;
2973
2974 assert(1 <= imm->Immediate.NrTokens && imm->Immediate.NrTokens <= 5);
2975 for (i = 0; i < 4 && i < imm->Immediate.NrTokens - 1; i++) {
2976 float f = imm->u[i].Float;
2977 value[i] = util_is_inf_or_nan(f) ? 0.0f : f;
2978 }
2979
2980 /* If the immediate has less than four values, fill in the remaining
2981 * positions from id={0,0,0,1}.
2982 */
2983 for (; i < 4; i++)
2984 value[i] = id[i];
2985
2986 return emit_def_const(emit, SVGA3D_CONST_TYPE_FLOAT,
2987 emit->imm_start + emit->internal_imm_count++,
2988 value[0], value[1], value[2], value[3]);
2989 }
2990
2991
2992 static bool
2993 make_immediate(struct svga_shader_emitter *emit,
2994 float a, float b, float c, float d,
2995 struct src_register *out)
2996 {
2997 unsigned idx = emit->nr_hw_float_const++;
2998
2999 if (!emit_def_const(emit, SVGA3D_CONST_TYPE_FLOAT,
3000 idx, a, b, c, d))
3001 return false;
3002
3003 *out = src_register(SVGA3DREG_CONST, idx);
3004
3005 return true;
3006 }
3007
3008
3009 /**
3010 * Emit special VS instructions at top of shader.
3011 */
3012 static bool
3013 emit_vs_preamble(struct svga_shader_emitter *emit)
3014 {
3015 if (!emit->key.vs.need_prescale) {
3016 if (!make_immediate(emit, 0, 0, .5, .5,
3017 &emit->imm_0055))
3018 return false;
3019 }
3020
3021 return true;
3022 }
3023
3024
3025 /**
3026 * Emit special PS instructions at top of shader.
3027 */
3028 static bool
3029 emit_ps_preamble(struct svga_shader_emitter *emit)
3030 {
3031 if (emit->ps_reads_pos && emit->info.reads_z) {
3032 /*
3033 * Assemble the position from various bits of inputs. Depth and W are
3034 * passed in a texcoord this is due to D3D's vPos not hold Z or W.
3035 * Also fixup the perspective interpolation.
3036 *
3037 * temp_pos.xy = vPos.xy
3038 * temp_pos.w = rcp(texcoord1.w);
3039 * temp_pos.z = texcoord1.z * temp_pos.w;
3040 */
3041 if (!submit_op1(emit,
3042 inst_token(SVGA3DOP_MOV),
3043 writemask(emit->ps_temp_pos, TGSI_WRITEMASK_XY),
3044 emit->ps_true_pos))
3045 return false;
3046
3047 if (!submit_op1(emit,
3048 inst_token(SVGA3DOP_RCP),
3049 writemask(emit->ps_temp_pos, TGSI_WRITEMASK_W),
3050 scalar(emit->ps_depth_pos, TGSI_SWIZZLE_W)))
3051 return false;
3052
3053 if (!submit_op2(emit,
3054 inst_token(SVGA3DOP_MUL),
3055 writemask(emit->ps_temp_pos, TGSI_WRITEMASK_Z),
3056 scalar(emit->ps_depth_pos, TGSI_SWIZZLE_Z),
3057 scalar(src(emit->ps_temp_pos), TGSI_SWIZZLE_W)))
3058 return false;
3059 }
3060
3061 return true;
3062 }
3063
3064
3065 /**
3066 * Emit special PS instructions at end of shader.
3067 */
3068 static bool
3069 emit_ps_postamble(struct svga_shader_emitter *emit)
3070 {
3071 /* PS oDepth is incredibly fragile and it's very hard to catch the
3072 * types of usage that break it during shader emit. Easier just to
3073 * redirect the main program to a temporary and then only touch
3074 * oDepth with a hand-crafted MOV below.
3075 */
3076 if (SVGA3dShaderGetRegType(emit->true_pos.value) != 0) {
3077 if (!submit_op1(emit,
3078 inst_token(SVGA3DOP_MOV),
3079 emit->true_pos,
3080 scalar(src(emit->temp_pos), TGSI_SWIZZLE_Z)))
3081 return false;
3082 }
3083
3084 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
3085 if (SVGA3dShaderGetRegType(emit->true_color_output[i].value) != 0) {
3086 /* Potentially override output colors with white for XOR
3087 * logicop workaround.
3088 */
3089 if (emit->unit == MESA_SHADER_FRAGMENT &&
3090 emit->key.fs.white_fragments) {
3091 struct src_register one = get_one_immediate(emit);
3092
3093 if (!submit_op1(emit,
3094 inst_token(SVGA3DOP_MOV),
3095 emit->true_color_output[i],
3096 one))
3097 return false;
3098 } else if (emit->unit == MESA_SHADER_FRAGMENT &&
3099 i < emit->key.fs.write_color0_to_n_cbufs) {
3100 /* Write temp color output [0] to true output [i] */
3101 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV),
3102 emit->true_color_output[i],
3103 src(emit->temp_color_output[0]))) {
3104 return false;
3105 }
3106 } else {
3107 if (!submit_op1(emit,
3108 inst_token(SVGA3DOP_MOV),
3109 emit->true_color_output[i],
3110 src(emit->temp_color_output[i])))
3111 return false;
3112 }
3113 }
3114 }
3115
3116 return true;
3117 }
3118
3119
3120 /**
3121 * Emit special VS instructions at end of shader.
3122 */
3123 static bool
3124 emit_vs_postamble(struct svga_shader_emitter *emit)
3125 {
3126 /* PSIZ output is incredibly fragile and it's very hard to catch
3127 * the types of usage that break it during shader emit. Easier
3128 * just to redirect the main program to a temporary and then only
3129 * touch PSIZ with a hand-crafted MOV below.
3130 */
3131 if (SVGA3dShaderGetRegType(emit->true_psiz.value) != 0) {
3132 if (!submit_op1(emit,
3133 inst_token(SVGA3DOP_MOV),
3134 emit->true_psiz,
3135 scalar(src(emit->temp_psiz), TGSI_SWIZZLE_X)))
3136 return false;
3137 }
3138
3139 /* Need to perform various manipulations on vertex position to cope
3140 * with the different GL and D3D clip spaces.
3141 */
3142 if (emit->key.vs.need_prescale) {
3143 SVGA3dShaderDestToken temp_pos = emit->temp_pos;
3144 SVGA3dShaderDestToken depth = emit->depth_pos;
3145 SVGA3dShaderDestToken pos = emit->true_pos;
3146 unsigned offset = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
3147 struct src_register prescale_scale = src_register(SVGA3DREG_CONST,
3148 offset + 0);
3149 struct src_register prescale_trans = src_register(SVGA3DREG_CONST,
3150 offset + 1);
3151
3152 if (!submit_op1(emit,
3153 inst_token(SVGA3DOP_MOV),
3154 writemask(depth, TGSI_WRITEMASK_W),
3155 scalar(src(temp_pos), TGSI_SWIZZLE_W)))
3156 return false;
3157
3158 /* MUL temp_pos.xyz, temp_pos, prescale.scale
3159 * MAD result.position, temp_pos.wwww, prescale.trans, temp_pos
3160 * --> Note that prescale.trans.w == 0
3161 */
3162 if (!submit_op2(emit,
3163 inst_token(SVGA3DOP_MUL),
3164 writemask(temp_pos, TGSI_WRITEMASK_XYZ),
3165 src(temp_pos),
3166 prescale_scale))
3167 return false;
3168
3169 if (!submit_op3(emit,
3170 inst_token(SVGA3DOP_MAD),
3171 pos,
3172 swizzle(src(temp_pos), 3, 3, 3, 3),
3173 prescale_trans,
3174 src(temp_pos)))
3175 return false;
3176
3177 /* Also write to depth value */
3178 if (!submit_op3(emit,
3179 inst_token(SVGA3DOP_MAD),
3180 writemask(depth, TGSI_WRITEMASK_Z),
3181 swizzle(src(temp_pos), 3, 3, 3, 3),
3182 prescale_trans,
3183 src(temp_pos)))
3184 return false;
3185 } else {
3186 SVGA3dShaderDestToken temp_pos = emit->temp_pos;
3187 SVGA3dShaderDestToken depth = emit->depth_pos;
3188 SVGA3dShaderDestToken pos = emit->true_pos;
3189 struct src_register imm_0055 = emit->imm_0055;
3190
3191 /* Adjust GL clipping coordinate space to hardware (D3D-style):
3192 *
3193 * DP4 temp_pos.z, {0,0,.5,.5}, temp_pos
3194 * MOV result.position, temp_pos
3195 */
3196 if (!submit_op2(emit,
3197 inst_token(SVGA3DOP_DP4),
3198 writemask(temp_pos, TGSI_WRITEMASK_Z),
3199 imm_0055,
3200 src(temp_pos)))
3201 return false;
3202
3203 if (!submit_op1(emit,
3204 inst_token(SVGA3DOP_MOV),
3205 pos,
3206 src(temp_pos)))
3207 return false;
3208
3209 /* Move the manipulated depth into the extra texcoord reg */
3210 if (!submit_op1(emit,
3211 inst_token(SVGA3DOP_MOV),
3212 writemask(depth, TGSI_WRITEMASK_ZW),
3213 src(temp_pos)))
3214 return false;
3215 }
3216
3217 return true;
3218 }
3219
3220
3221 /**
3222 * For the pixel shader: emit the code which chooses the front
3223 * or back face color depending on triangle orientation.
3224 * This happens at the top of the fragment shader.
3225 *
3226 * 0: IF VFACE :4
3227 * 1: COLOR = FrontColor;
3228 * 2: ELSE
3229 * 3: COLOR = BackColor;
3230 * 4: ENDIF
3231 */
3232 static bool
3233 emit_light_twoside(struct svga_shader_emitter *emit)
3234 {
3235 struct src_register front[2];
3236 struct src_register back[2];
3237 SVGA3dShaderDestToken color[2];
3238 int count = emit->internal_color_count;
3239
3240 if (count == 0)
3241 return true;
3242
3243 struct src_register vface = get_vface(emit);
3244 struct src_register zero = get_zero_immediate(emit);
3245
3246 /* Can't use get_temp() to allocate the color reg as such
3247 * temporaries will be reclaimed after each instruction by the call
3248 * to reset_temp_regs().
3249 */
3250 for (unsigned i = 0; i < count; i++) {
3251 color[i] = dst_register(SVGA3DREG_TEMP, emit->nr_hw_temp++);
3252 front[i] = emit->input_map[emit->internal_color_idx[i]];
3253
3254 /* Back is always the next input:
3255 */
3256 back[i] = front[i];
3257 back[i].base.num = front[i].base.num + 1;
3258
3259 /* Reassign the input_map to the actual front-face color:
3260 */
3261 emit->input_map[emit->internal_color_idx[i]] = src(color[i]);
3262 }
3263
3264 SVGA3dShaderInstToken if_token = inst_token(SVGA3DOP_IFC);
3265
3266 if (emit->key.fs.front_ccw)
3267 if_token.control = SVGA3DOPCOMP_LT;
3268 else
3269 if_token.control = SVGA3DOPCOMP_GT;
3270
3271 if (!(emit_instruction(emit, if_token) &&
3272 emit_src(emit, vface) &&
3273 emit_src(emit, zero)))
3274 return false;
3275
3276 for (unsigned i = 0; i < count; i++) {
3277 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), color[i], front[i]))
3278 return false;
3279 }
3280
3281 if (!(emit_instruction(emit, inst_token(SVGA3DOP_ELSE))))
3282 return false;
3283
3284 for (unsigned i = 0; i < count; i++) {
3285 if (!submit_op1(emit, inst_token(SVGA3DOP_MOV), color[i], back[i]))
3286 return false;
3287 }
3288
3289 if (!emit_instruction(emit, inst_token(SVGA3DOP_ENDIF)))
3290 return false;
3291
3292 return true;
3293 }
3294
3295
3296 /**
3297 * Emit special setup code for the front/back face register in the FS.
3298 * 0: SETP_GT TEMP, VFACE, 0
3299 * where TEMP is a fake frontface register
3300 */
3301 static bool
3302 emit_frontface(struct svga_shader_emitter *emit)
3303 {
3304 struct src_register vface;
3305 SVGA3dShaderDestToken temp;
3306 struct src_register pass, fail;
3307
3308 vface = get_vface(emit);
3309
3310 /* Can't use get_temp() to allocate the fake frontface reg as such
3311 * temporaries will be reclaimed after each instruction by the call
3312 * to reset_temp_regs().
3313 */
3314 temp = dst_register(SVGA3DREG_TEMP,
3315 emit->nr_hw_temp++);
3316
3317 if (emit->key.fs.front_ccw) {
3318 pass = get_zero_immediate(emit);
3319 fail = get_one_immediate(emit);
3320 } else {
3321 pass = get_one_immediate(emit);
3322 fail = get_zero_immediate(emit);
3323 }
3324
3325 if (!emit_conditional(emit, PIPE_FUNC_GREATER,
3326 temp, vface, get_zero_immediate(emit),
3327 pass, fail))
3328 return false;
3329
3330 /* Reassign the input_map to the actual front-face color:
3331 */
3332 emit->input_map[emit->internal_frontface_idx] = src(temp);
3333
3334 return true;
3335 }
3336
3337
3338 /**
3339 * Emit code to invert the T component of the incoming texture coordinate.
3340 * This is used for drawing point sprites when
3341 * pipe_rasterizer_state::sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT.
3342 */
3343 static bool
3344 emit_inverted_texcoords(struct svga_shader_emitter *emit)
3345 {
3346 unsigned inverted_texcoords = emit->inverted_texcoords;
3347
3348 while (inverted_texcoords) {
3349 const unsigned unit = ffs(inverted_texcoords) - 1;
3350
3351 assert(emit->inverted_texcoords & (1 << unit));
3352
3353 assert(unit < ARRAY_SIZE(emit->ps_true_texcoord));
3354
3355 assert(unit < ARRAY_SIZE(emit->ps_inverted_texcoord_input));
3356
3357 assert(emit->ps_inverted_texcoord_input[unit]
3358 < ARRAY_SIZE(emit->input_map));
3359
3360 /* inverted = coord * (1, -1, 1, 1) + (0, 1, 0, 0) */
3361 if (!submit_op3(emit,
3362 inst_token(SVGA3DOP_MAD),
3363 dst(emit->ps_inverted_texcoord[unit]),
3364 emit->ps_true_texcoord[unit],
3365 get_immediate(emit, 1.0f, -1.0f, 1.0f, 1.0f),
3366 get_immediate(emit, 0.0f, 1.0f, 0.0f, 0.0f)))
3367 return false;
3368
3369 /* Reassign the input_map entry to the new texcoord register */
3370 emit->input_map[emit->ps_inverted_texcoord_input[unit]] =
3371 emit->ps_inverted_texcoord[unit];
3372
3373 inverted_texcoords &= ~(1 << unit);
3374 }
3375
3376 return true;
3377 }
3378
3379
3380 /**
3381 * Emit code to adjust vertex shader inputs/attributes:
3382 * - Change range from [0,1] to [-1,1] (for normalized byte/short attribs).
3383 * - Set attrib W component = 1.
3384 */
3385 static bool
3386 emit_adjusted_vertex_attribs(struct svga_shader_emitter *emit)
3387 {
3388 unsigned adjust_mask = (emit->key.vs.adjust_attrib_range |
3389 emit->key.vs.adjust_attrib_w_1);
3390
3391 while (adjust_mask) {
3392 /* Adjust vertex attrib range and/or set W component = 1 */
3393 const unsigned index = u_bit_scan(&adjust_mask);
3394 struct src_register tmp;
3395
3396 /* allocate a temp reg */
3397 tmp = src_register(SVGA3DREG_TEMP, emit->nr_hw_temp);
3398 emit->nr_hw_temp++;
3399
3400 if (emit->key.vs.adjust_attrib_range & (1 << index)) {
3401 /* The vertex input/attribute is supposed to be a signed value in
3402 * the range [-1,1] but we actually fetched/converted it to the
3403 * range [0,1]. This most likely happens when the app specifies a
3404 * signed byte attribute but we interpreted it as unsigned bytes.
3405 * See also svga_translate_vertex_format().
3406 *
3407 * Here, we emit some extra instructions to adjust
3408 * the attribute values from [0,1] to [-1,1].
3409 *
3410 * The adjustment we implement is:
3411 * new_attrib = attrib * 2.0;
3412 * if (attrib >= 0.5)
3413 * new_attrib = new_attrib - 2.0;
3414 * This isn't exactly right (it's off by a bit or so) but close enough.
3415 */
3416 SVGA3dShaderDestToken pred_reg = dst_register(SVGA3DREG_PREDICATE, 0);
3417
3418 /* tmp = attrib * 2.0 */
3419 if (!submit_op2(emit,
3420 inst_token(SVGA3DOP_MUL),
3421 dst(tmp),
3422 emit->input_map[index],
3423 get_two_immediate(emit)))
3424 return false;
3425
3426 /* pred = (attrib >= 0.5) */
3427 if (!submit_op2(emit,
3428 inst_token_setp(SVGA3DOPCOMP_GE),
3429 pred_reg,
3430 emit->input_map[index], /* vert attrib */
3431 get_half_immediate(emit))) /* 0.5 */
3432 return false;
3433
3434 /* sub(pred) tmp, tmp, 2.0 */
3435 if (!submit_op3(emit,
3436 inst_token_predicated(SVGA3DOP_SUB),
3437 dst(tmp),
3438 src(pred_reg),
3439 tmp,
3440 get_two_immediate(emit)))
3441 return false;
3442 } else {
3443 /* just copy the vertex input attrib to the temp register */
3444 if (!submit_op1(emit,
3445 inst_token(SVGA3DOP_MOV),
3446 dst(tmp),
3447 emit->input_map[index]))
3448 return false;
3449 }
3450
3451 if (emit->key.vs.adjust_attrib_w_1 & (1 << index)) {
3452 /* move 1 into W position of tmp */
3453 if (!submit_op1(emit,
3454 inst_token(SVGA3DOP_MOV),
3455 writemask(dst(tmp), TGSI_WRITEMASK_W),
3456 get_one_immediate(emit)))
3457 return false;
3458 }
3459
3460 /* Reassign the input_map entry to the new tmp register */
3461 emit->input_map[index] = tmp;
3462 }
3463
3464 return true;
3465 }
3466
3467
3468 /**
3469 * Determine if we need to create the "common" immediate value which is
3470 * used for generating useful vector constants such as {0,0,0,0} and
3471 * {1,1,1,1}.
3472 * We could just do this all the time except that we want to conserve
3473 * registers whenever possible.
3474 */
3475 static bool
3476 needs_to_create_common_immediate(const struct svga_shader_emitter *emit)
3477 {
3478 if (emit->unit == MESA_SHADER_FRAGMENT) {
3479 if (emit->key.fs.light_twoside)
3480 return true;
3481
3482 if (emit->key.fs.white_fragments)
3483 return true;
3484
3485 if (emit->emit_frontface)
3486 return true;
3487
3488 if (emit->info.opcode_count[TGSI_OPCODE_DST] >= 1 ||
3489 emit->info.opcode_count[TGSI_OPCODE_SSG] >= 1 ||
3490 emit->info.opcode_count[TGSI_OPCODE_LIT] >= 1)
3491 return true;
3492
3493 if (emit->inverted_texcoords)
3494 return true;
3495
3496 /* look for any PIPE_SWIZZLE_0/ONE terms */
3497 for (unsigned i = 0; i < emit->key.num_textures; i++) {
3498 if (emit->key.tex[i].swizzle_r > PIPE_SWIZZLE_W ||
3499 emit->key.tex[i].swizzle_g > PIPE_SWIZZLE_W ||
3500 emit->key.tex[i].swizzle_b > PIPE_SWIZZLE_W ||
3501 emit->key.tex[i].swizzle_a > PIPE_SWIZZLE_W)
3502 return true;
3503 }
3504
3505 for (unsigned i = 0; i < emit->key.num_textures; i++) {
3506 if (emit->key.tex[i].compare_mode
3507 == PIPE_TEX_COMPARE_R_TO_TEXTURE)
3508 return true;
3509 }
3510 } else if (emit->unit == MESA_SHADER_VERTEX) {
3511 if (emit->info.opcode_count[TGSI_OPCODE_CMP] >= 1)
3512 return true;
3513 if (emit->key.vs.adjust_attrib_range ||
3514 emit->key.vs.adjust_attrib_w_1)
3515 return true;
3516 }
3517
3518 if (emit->info.opcode_count[TGSI_OPCODE_IF] >= 1 ||
3519 emit->info.opcode_count[TGSI_OPCODE_BGNLOOP] >= 1 ||
3520 emit->info.opcode_count[TGSI_OPCODE_DDX] >= 1 ||
3521 emit->info.opcode_count[TGSI_OPCODE_DDY] >= 1 ||
3522 emit->info.opcode_count[TGSI_OPCODE_ROUND] >= 1 ||
3523 emit->info.opcode_count[TGSI_OPCODE_SGE] >= 1 ||
3524 emit->info.opcode_count[TGSI_OPCODE_SGT] >= 1 ||
3525 emit->info.opcode_count[TGSI_OPCODE_SLE] >= 1 ||
3526 emit->info.opcode_count[TGSI_OPCODE_SLT] >= 1 ||
3527 emit->info.opcode_count[TGSI_OPCODE_SNE] >= 1 ||
3528 emit->info.opcode_count[TGSI_OPCODE_SEQ] >= 1 ||
3529 emit->info.opcode_count[TGSI_OPCODE_EXP] >= 1 ||
3530 emit->info.opcode_count[TGSI_OPCODE_LOG] >= 1 ||
3531 emit->info.opcode_count[TGSI_OPCODE_KILL] >= 1 ||
3532 emit->info.opcode_count[TGSI_OPCODE_SQRT] >= 1)
3533 return true;
3534
3535 return false;
3536 }
3537
3538
3539 /**
3540 * Do we need to create a looping constant?
3541 */
3542 static bool
3543 needs_to_create_loop_const(const struct svga_shader_emitter *emit)
3544 {
3545 return (emit->info.opcode_count[TGSI_OPCODE_BGNLOOP] >= 1);
3546 }
3547
3548
3549 static bool
3550 needs_to_create_arl_consts(const struct svga_shader_emitter *emit)
3551 {
3552 return (emit->num_arl_consts > 0);
3553 }
3554
3555
3556 static bool
3557 pre_parse_add_indirect(struct svga_shader_emitter *emit,
3558 int num, int current_arl)
3559 {
3560 assert(num < 0);
3561
3562 unsigned i;
3563 for (i = 0; i < emit->num_arl_consts; ++i) {
3564 if (emit->arl_consts[i].arl_num == current_arl)
3565 break;
3566 }
3567 /* new entry */
3568 if (emit->num_arl_consts == i) {
3569 ++emit->num_arl_consts;
3570 }
3571 emit->arl_consts[i].number = (emit->arl_consts[i].number > num) ?
3572 num :
3573 emit->arl_consts[i].number;
3574 emit->arl_consts[i].arl_num = current_arl;
3575 return true;
3576 }
3577
3578
3579 static bool
3580 pre_parse_instruction(struct svga_shader_emitter *emit,
3581 const struct tgsi_full_instruction *insn,
3582 int current_arl)
3583 {
3584 if (insn->Src[0].Register.Indirect &&
3585 insn->Src[0].Indirect.File == TGSI_FILE_ADDRESS) {
3586 const struct tgsi_full_src_register *reg = &insn->Src[0];
3587 if (reg->Register.Index < 0) {
3588 pre_parse_add_indirect(emit, reg->Register.Index, current_arl);
3589 }
3590 }
3591
3592 if (insn->Src[1].Register.Indirect &&
3593 insn->Src[1].Indirect.File == TGSI_FILE_ADDRESS) {
3594 const struct tgsi_full_src_register *reg = &insn->Src[1];
3595 if (reg->Register.Index < 0) {
3596 pre_parse_add_indirect(emit, reg->Register.Index, current_arl);
3597 }
3598 }
3599
3600 if (insn->Src[2].Register.Indirect &&
3601 insn->Src[2].Indirect.File == TGSI_FILE_ADDRESS) {
3602 const struct tgsi_full_src_register *reg = &insn->Src[2];
3603 if (reg->Register.Index < 0) {
3604 pre_parse_add_indirect(emit, reg->Register.Index, current_arl);
3605 }
3606 }
3607
3608 return true;
3609 }
3610
3611
3612 static bool
3613 pre_parse_tokens(struct svga_shader_emitter *emit,
3614 const struct tgsi_token *tokens)
3615 {
3616 struct tgsi_parse_context parse;
3617 int current_arl = 0;
3618
3619 tgsi_parse_init(&parse, tokens);
3620
3621 while (!tgsi_parse_end_of_tokens(&parse)) {
3622 tgsi_parse_token(&parse);
3623 switch (parse.FullToken.Token.Type) {
3624 case TGSI_TOKEN_TYPE_IMMEDIATE:
3625 case TGSI_TOKEN_TYPE_DECLARATION:
3626 break;
3627 case TGSI_TOKEN_TYPE_INSTRUCTION:
3628 if (parse.FullToken.FullInstruction.Instruction.Opcode ==
3629 TGSI_OPCODE_ARL) {
3630 ++current_arl;
3631 }
3632 if (!pre_parse_instruction(emit, &parse.FullToken.FullInstruction,
3633 current_arl))
3634 return false;
3635 break;
3636 default:
3637 break;
3638 }
3639
3640 }
3641 return true;
3642 }
3643
3644
3645 static bool
3646 svga_shader_emit_helpers(struct svga_shader_emitter *emit)
3647 {
3648 if (needs_to_create_common_immediate(emit)) {
3649 create_common_immediate(emit);
3650 }
3651 if (needs_to_create_loop_const(emit)) {
3652 create_loop_const(emit);
3653 }
3654 if (needs_to_create_arl_consts(emit)) {
3655 create_arl_consts(emit);
3656 }
3657
3658 if (emit->unit == MESA_SHADER_FRAGMENT) {
3659 if (!svga_shader_emit_samplers_decl(emit))
3660 return false;
3661
3662 if (!emit_ps_preamble(emit))
3663 return false;
3664
3665 if (emit->key.fs.light_twoside) {
3666 if (!emit_light_twoside(emit))
3667 return false;
3668 }
3669 if (emit->emit_frontface) {
3670 if (!emit_frontface(emit))
3671 return false;
3672 }
3673 if (emit->inverted_texcoords) {
3674 if (!emit_inverted_texcoords(emit))
3675 return false;
3676 }
3677 } else {
3678 assert(emit->unit == MESA_SHADER_VERTEX);
3679 if (emit->key.vs.adjust_attrib_range) {
3680 if (!emit_adjusted_vertex_attribs(emit) ||
3681 emit->key.vs.adjust_attrib_w_1) {
3682 return false;
3683 }
3684 }
3685 }
3686
3687 return true;
3688 }
3689
3690
3691 /**
3692 * This is the main entrypoint into the TGSI instruction translater.
3693 * Translate TGSI shader tokens into an SVGA shader.
3694 */
3695 bool
3696 svga_shader_emit_instructions(struct svga_shader_emitter *emit,
3697 const struct tgsi_token *tokens)
3698 {
3699 struct tgsi_parse_context parse;
3700 const struct tgsi_token *new_tokens = NULL;
3701 bool ret = true;
3702 bool helpers_emitted = false;
3703 unsigned line_nr = 0;
3704
3705 if (emit->unit == MESA_SHADER_FRAGMENT && emit->key.fs.pstipple) {
3706 unsigned unit;
3707
3708 new_tokens = util_pstipple_create_fragment_shader(tokens, &unit, 0,
3709 TGSI_FILE_INPUT);
3710
3711 if (new_tokens) {
3712 /* Setup texture state for stipple */
3713 emit->sampler_target[unit] = TGSI_TEXTURE_2D;
3714 emit->key.tex[unit].swizzle_r = TGSI_SWIZZLE_X;
3715 emit->key.tex[unit].swizzle_g = TGSI_SWIZZLE_Y;
3716 emit->key.tex[unit].swizzle_b = TGSI_SWIZZLE_Z;
3717 emit->key.tex[unit].swizzle_a = TGSI_SWIZZLE_W;
3718
3719 emit->pstipple_sampler_unit = unit;
3720
3721 tokens = new_tokens;
3722 }
3723 }
3724
3725 tgsi_parse_init(&parse, tokens);
3726 emit->internal_imm_count = 0;
3727
3728 if (emit->unit == MESA_SHADER_VERTEX) {
3729 ret = emit_vs_preamble(emit);
3730 if (!ret)
3731 goto done;
3732 }
3733
3734 pre_parse_tokens(emit, tokens);
3735
3736 while (!tgsi_parse_end_of_tokens(&parse)) {
3737 tgsi_parse_token(&parse);
3738
3739 switch (parse.FullToken.Token.Type) {
3740 case TGSI_TOKEN_TYPE_IMMEDIATE:
3741 ret = svga_emit_immediate(emit, &parse.FullToken.FullImmediate);
3742 if (!ret)
3743 goto done;
3744 break;
3745
3746 case TGSI_TOKEN_TYPE_DECLARATION:
3747 ret = svga_translate_decl_sm30(emit, &parse.FullToken.FullDeclaration);
3748 if (!ret)
3749 goto done;
3750 break;
3751
3752 case TGSI_TOKEN_TYPE_INSTRUCTION:
3753 if (!helpers_emitted) {
3754 if (!svga_shader_emit_helpers(emit))
3755 goto done;
3756 helpers_emitted = true;
3757 }
3758 ret = svga_emit_instruction(emit,
3759 line_nr++,
3760 &parse.FullToken.FullInstruction);
3761 if (!ret)
3762 goto done;
3763 break;
3764 default:
3765 break;
3766 }
3767
3768 reset_temp_regs(emit);
3769 }
3770
3771 /* Need to terminate the current subroutine. Note that the
3772 * hardware doesn't tolerate shaders without sub-routines
3773 * terminating with RET+END.
3774 */
3775 if (!emit->in_main_func) {
3776 ret = emit_instruction(emit, inst_token(SVGA3DOP_RET));
3777 if (!ret)
3778 goto done;
3779 }
3780
3781 assert(emit->dynamic_branching_level == 0);
3782
3783 /* Need to terminate the whole shader:
3784 */
3785 ret = emit_instruction(emit, inst_token(SVGA3DOP_END));
3786 if (!ret)
3787 goto done;
3788
3789 done:
3790 tgsi_parse_free(&parse);
3791 if (new_tokens) {
3792 tgsi_free_tokens(new_tokens);
3793 }
3794
3795 return ret;
3796 }