2 * Copyright 2008 Ben Skeggs
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_context.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_state.h"
26 #include "pipe/p_inlines.h"
28 #include "pipe/p_shader_tokens.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_util.h"
32 #include "nv50_context.h"
34 #define NV50_SU_MAX_TEMP 64
35 //#define NV50_PROGRAM_DUMP
37 /* ARL - gallium craps itself on progs/vp/arl.txt
39 * MSB - Like MAD, but MUL+SUB
40 * - Fuck it off, introduce a way to negate args for ops that
43 * Look into inlining IMMD for ops other than MOV (make it general?)
44 * - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
45 * but can emit to P_TEMP first - then MOV later. NVIDIA does this
47 * In ops such as ADD it's possible to construct a bad opcode in the !is_long()
48 * case, if the emit_src() causes the inst to suddenly become long.
50 * Verify half-insns work where expected - and force disable them where they
51 * don't work - MUL has it forcibly disabled atm as it fixes POW..
53 * FUCK! watch dst==src vectors, can overwrite components that are needed.
54 * ie. SUB R0, R0.yzxw, R0
56 * Things to check with renouveau:
57 * FP attr/result assignment - how?
59 * - 0x16bc maps vp output onto fp hpos
60 * - 0x16c0 maps vp output onto fp col0
64 * 0x16bc->0x16e8 --> some binding between vp/fp regs
65 * 0x16b8 --> VP output count
67 * 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
68 * "MOV rcol.x, fcol.y" = 0x00000004
69 * 0x19a8 --> as above but 0x00000100 and 0x00000000
70 * - 0x00100000 used when KIL used
71 * 0x196c --> as above but 0x00000011 and 0x00000000
73 * 0x1988 --> 0xXXNNNNNN
74 * - XX == FP high something
89 int rhw
; /* result hw for FP outputs, or interpolant index */
90 int acc
; /* instruction where this reg is last read (first insn == 1) */
94 struct nv50_program
*p
;
97 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
100 struct nv50_reg
*temp
;
102 struct nv50_reg
*attr
;
104 struct nv50_reg
*result
;
106 struct nv50_reg
*param
;
108 struct nv50_reg
*immd
;
112 struct nv50_reg
*temp_temp
[16];
113 unsigned temp_temp_nr
;
115 unsigned interp_mode
[32];
116 /* perspective interpolation registers */
117 struct nv50_reg
*iv_p
;
118 struct nv50_reg
*iv_c
;
120 /* current instruction and total number of insns */
128 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
132 if (reg
->type
== P_RESULT
) {
133 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
134 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
137 if (reg
->type
!= P_TEMP
)
141 /*XXX: do this here too to catch FP temp-as-attr usage..
142 * not clean, but works */
143 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
144 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
148 if (reg
->rhw
!= -1) {
149 /* try to allocate temporary with index rhw first */
150 if (!(pc
->r_temp
[reg
->rhw
])) {
151 pc
->r_temp
[reg
->rhw
] = reg
;
153 if (pc
->p
->cfg
.high_temp
< (reg
->rhw
+ 1))
154 pc
->p
->cfg
.high_temp
= reg
->rhw
+ 1;
157 /* make sure we don't get things like $r0 needs to go
158 * in $r1 and $r1 in $r0
160 i
= pc
->result_nr
* 4;
163 for (; i
< NV50_SU_MAX_TEMP
; i
++) {
164 if (!(pc
->r_temp
[i
])) {
167 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
168 pc
->p
->cfg
.high_temp
= i
+ 1;
176 static struct nv50_reg
*
177 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
182 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
185 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
186 if (!pc
->r_temp
[i
]) {
187 r
= CALLOC_STRUCT(nv50_reg
);
201 /* Assign the hw of the discarded temporary register src
202 * to the tgsi register dst and free src.
205 assimilate_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
207 assert(src
->index
== -1 && src
->hw
!= -1);
210 pc
->r_temp
[dst
->hw
] = NULL
;
211 pc
->r_temp
[src
->hw
] = dst
;
218 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
220 if (r
->index
== -1) {
223 FREE(pc
->r_temp
[hw
]);
224 pc
->r_temp
[hw
] = NULL
;
229 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
233 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
236 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
237 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
238 return alloc_temp4(pc
, dst
, idx
+ 1);
240 for (i
= 0; i
< 4; i
++) {
241 dst
[i
] = CALLOC_STRUCT(nv50_reg
);
242 dst
[i
]->type
= P_TEMP
;
244 dst
[i
]->hw
= idx
+ i
;
245 pc
->r_temp
[idx
+ i
] = dst
[i
];
252 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
256 for (i
= 0; i
< 4; i
++)
257 free_temp(pc
, reg
[i
]);
260 static struct nv50_reg
*
261 temp_temp(struct nv50_pc
*pc
)
263 if (pc
->temp_temp_nr
>= 16)
266 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
267 return pc
->temp_temp
[pc
->temp_temp_nr
++];
271 kill_temp_temp(struct nv50_pc
*pc
)
275 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
276 free_temp(pc
, pc
->temp_temp
[i
]);
277 pc
->temp_temp_nr
= 0;
281 ctor_immd(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
283 pc
->immd_buf
= REALLOC(pc
->immd_buf
, (pc
->immd_nr
* r
* sizeof(float)),
284 (pc
->immd_nr
+ 1) * 4 * sizeof(float));
285 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
286 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
287 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
288 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
290 return pc
->immd_nr
++;
293 static struct nv50_reg
*
294 alloc_immd(struct nv50_pc
*pc
, float f
)
296 struct nv50_reg
*r
= CALLOC_STRUCT(nv50_reg
);
299 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
300 if (pc
->immd_buf
[hw
] == f
)
303 if (hw
== pc
->immd_nr
* 4)
304 hw
= ctor_immd(pc
, f
, -f
, 0.5 * f
, 0) * 4;
312 static struct nv50_program_exec
*
313 exec(struct nv50_pc
*pc
)
315 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
322 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
324 struct nv50_program
*p
= pc
->p
;
327 p
->exec_tail
->next
= e
;
331 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
334 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
337 is_long(struct nv50_program_exec
*e
)
345 is_immd(struct nv50_program_exec
*e
)
347 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
353 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
354 struct nv50_program_exec
*e
)
357 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
358 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
362 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
363 struct nv50_program_exec
*e
)
366 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
367 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
371 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
377 set_pred(pc
, 0xf, 0, e
);
378 set_pred_wr(pc
, 0, 0, e
);
382 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
384 if (dst
->type
== P_RESULT
) {
386 e
->inst
[1] |= 0x00000008;
390 e
->inst
[0] |= (dst
->hw
<< 2);
394 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
396 unsigned val
= fui(pc
->immd_buf
[imm
->hw
]); /* XXX */
399 /*XXX: can't be predicated - bits overlap.. catch cases where both
400 * are required and avoid them. */
401 set_pred(pc
, 0, 0, e
);
402 set_pred_wr(pc
, 0, 0, e
);
404 e
->inst
[1] |= 0x00000002 | 0x00000001;
405 e
->inst
[0] |= (val
& 0x3f) << 16;
406 e
->inst
[1] |= (val
>> 6) << 2;
410 #define INTERP_LINEAR 0
411 #define INTERP_FLAT 1
412 #define INTERP_PERSPECTIVE 2
413 #define INTERP_CENTROID 4
415 /* interpolant index has been stored in dst->rhw */
417 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*iv
,
420 assert(dst
->rhw
!= -1);
421 struct nv50_program_exec
*e
= exec(pc
);
423 e
->inst
[0] |= 0x80000000;
425 e
->inst
[0] |= (dst
->rhw
<< 16);
427 if (mode
& INTERP_FLAT
) {
428 e
->inst
[0] |= (1 << 8);
430 if (mode
& INTERP_PERSPECTIVE
) {
431 e
->inst
[0] |= (1 << 25);
433 e
->inst
[0] |= (iv
->hw
<< 9);
436 if (mode
& INTERP_CENTROID
)
437 e
->inst
[0] |= (1 << 24);
444 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
445 struct nv50_program_exec
*e
)
449 e
->inst
[1] |= (1 << 22);
451 if (src
->type
== P_IMMD
) {
452 e
->inst
[1] |= (NV50_CB_PMISC
<< 22);
454 if (pc
->p
->type
== PIPE_SHADER_VERTEX
)
455 e
->inst
[1] |= (NV50_CB_PVP
<< 22);
457 e
->inst
[1] |= (NV50_CB_PFP
<< 22);
461 e
->param
.index
= src
->hw
;
463 e
->param
.mask
= m
<< (s
% 32);
467 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
469 struct nv50_program_exec
*e
= exec(pc
);
471 e
->inst
[0] |= 0x10000000;
475 if (0 && dst
->type
!= P_RESULT
&& src
->type
== P_IMMD
) {
476 set_immd(pc
, src
, e
);
477 /*XXX: 32-bit, but steals part of "half" reg space - need to
478 * catch and handle this case if/when we do half-regs
480 e
->inst
[0] |= 0x00008000;
482 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
484 set_data(pc
, src
, 0x7f, 9, e
);
485 e
->inst
[1] |= 0x20000000; /* src0 const? */
487 if (src
->type
== P_ATTR
) {
489 e
->inst
[1] |= 0x00200000;
493 e
->inst
[0] |= (src
->hw
<< 9);
496 if (is_long(e
) && !is_immd(e
)) {
497 e
->inst
[1] |= 0x04000000; /* 32-bit */
498 e
->inst
[1] |= 0x0000c000; /* "subsubop" 0x3 */
499 if (!(e
->inst
[1] & 0x20000000))
500 e
->inst
[1] |= 0x00030000; /* "subsubop" 0xf */
502 e
->inst
[0] |= 0x00008000;
508 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
510 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
511 emit_mov(pc
, dst
, imm
);
516 check_swap_src_0_1(struct nv50_pc
*pc
,
517 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
519 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
521 if (src0
->type
== P_CONST
) {
522 if (src1
->type
!= P_CONST
) {
528 if (src1
->type
== P_ATTR
) {
529 if (src0
->type
!= P_ATTR
) {
540 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
542 if (src
->type
== P_ATTR
) {
544 e
->inst
[1] |= 0x00200000;
546 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
547 struct nv50_reg
*temp
= temp_temp(pc
);
549 emit_mov(pc
, temp
, src
);
554 e
->inst
[0] |= (src
->hw
<< 9);
558 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
560 if (src
->type
== P_ATTR
) {
561 struct nv50_reg
*temp
= temp_temp(pc
);
563 emit_mov(pc
, temp
, src
);
566 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
567 assert(!(e
->inst
[0] & 0x00800000));
568 if (e
->inst
[0] & 0x01000000) {
569 struct nv50_reg
*temp
= temp_temp(pc
);
571 emit_mov(pc
, temp
, src
);
574 set_data(pc
, src
, 0x7f, 16, e
);
575 e
->inst
[0] |= 0x00800000;
580 e
->inst
[0] |= (src
->hw
<< 16);
584 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
588 if (src
->type
== P_ATTR
) {
589 struct nv50_reg
*temp
= temp_temp(pc
);
591 emit_mov(pc
, temp
, src
);
594 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
595 assert(!(e
->inst
[0] & 0x01000000));
596 if (e
->inst
[0] & 0x00800000) {
597 struct nv50_reg
*temp
= temp_temp(pc
);
599 emit_mov(pc
, temp
, src
);
602 set_data(pc
, src
, 0x7f, 32+14, e
);
603 e
->inst
[0] |= 0x01000000;
608 e
->inst
[1] |= (src
->hw
<< 14);
612 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
613 struct nv50_reg
*src1
)
615 struct nv50_program_exec
*e
= exec(pc
);
617 e
->inst
[0] |= 0xc0000000;
619 check_swap_src_0_1(pc
, &src0
, &src1
);
621 set_src_0(pc
, src0
, e
);
622 set_src_1(pc
, src1
, e
);
628 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
629 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
631 struct nv50_program_exec
*e
= exec(pc
);
633 e
->inst
[0] |= 0xb0000000;
635 check_swap_src_0_1(pc
, &src0
, &src1
);
637 set_src_0(pc
, src0
, e
);
639 set_src_2(pc
, src1
, e
);
641 set_src_1(pc
, src1
, e
);
647 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
648 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
650 struct nv50_program_exec
*e
= exec(pc
);
653 e
->inst
[0] |= 0xb0000000;
654 e
->inst
[1] |= (sub
<< 29);
656 check_swap_src_0_1(pc
, &src0
, &src1
);
658 set_src_0(pc
, src0
, e
);
659 set_src_1(pc
, src1
, e
);
665 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
666 struct nv50_reg
*src1
)
668 struct nv50_program_exec
*e
= exec(pc
);
670 e
->inst
[0] |= 0xb0000000;
673 if (check_swap_src_0_1(pc
, &src0
, &src1
))
674 e
->inst
[1] |= 0x04000000;
676 e
->inst
[1] |= 0x08000000;
679 set_src_0(pc
, src0
, e
);
680 set_src_2(pc
, src1
, e
);
686 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
687 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
689 struct nv50_program_exec
*e
= exec(pc
);
691 e
->inst
[0] |= 0xe0000000;
693 check_swap_src_0_1(pc
, &src0
, &src1
);
695 set_src_0(pc
, src0
, e
);
696 set_src_1(pc
, src1
, e
);
697 set_src_2(pc
, src2
, e
);
703 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
704 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
706 struct nv50_program_exec
*e
= exec(pc
);
708 e
->inst
[0] |= 0xe0000000;
710 e
->inst
[1] |= 0x08000000; /* src0 * src1 - src2 */
712 check_swap_src_0_1(pc
, &src0
, &src1
);
714 set_src_0(pc
, src0
, e
);
715 set_src_1(pc
, src1
, e
);
716 set_src_2(pc
, src2
, e
);
722 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
723 struct nv50_reg
*dst
, struct nv50_reg
*src
)
725 struct nv50_program_exec
*e
= exec(pc
);
727 e
->inst
[0] |= 0x90000000;
730 e
->inst
[1] |= (sub
<< 29);
734 set_src_0(pc
, src
, e
);
740 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
742 struct nv50_program_exec
*e
= exec(pc
);
744 e
->inst
[0] |= 0xb0000000;
747 set_src_0(pc
, src
, e
);
749 e
->inst
[1] |= (6 << 29) | 0x00004000;
755 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
757 struct nv50_program_exec
*e
= exec(pc
);
759 e
->inst
[0] |= 0xb0000000;
762 set_src_0(pc
, src
, e
);
764 e
->inst
[1] |= (6 << 29);
770 emit_set(struct nv50_pc
*pc
, unsigned c_op
, struct nv50_reg
*dst
,
771 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
773 struct nv50_program_exec
*e
= exec(pc
);
774 unsigned inv_cop
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
775 struct nv50_reg
*rdst
;
778 if (check_swap_src_0_1(pc
, &src0
, &src1
))
779 c_op
= inv_cop
[c_op
];
782 if (dst
->type
!= P_TEMP
)
783 dst
= alloc_temp(pc
, NULL
);
787 e
->inst
[0] |= 0xb0000000;
788 e
->inst
[1] |= (3 << 29);
789 e
->inst
[1] |= (c_op
<< 14);
790 /*XXX: breaks things, .u32 by default?
791 * decuda will disasm as .u16 and use .lo/.hi regs, but this
792 * doesn't seem to match what the hw actually does.
793 inst[1] |= 0x04000000; << breaks things.. .u32 by default?
796 set_src_0(pc
, src0
, e
);
797 set_src_1(pc
, src1
, e
);
802 e
->inst
[0] = 0xa0000001;
803 e
->inst
[1] = 0x64014780;
804 set_dst(pc
, rdst
, e
);
805 set_src_0(pc
, dst
, e
);
813 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
815 struct nv50_program_exec
*e
= exec(pc
);
817 e
->inst
[0] = 0xa0000000; /* cvt */
819 e
->inst
[1] |= (6 << 29); /* cvt */
820 e
->inst
[1] |= 0x08000000; /* integer mode */
821 e
->inst
[1] |= 0x04000000; /* 32 bit */
822 e
->inst
[1] |= ((0x1 << 3)) << 14; /* .rn */
823 e
->inst
[1] |= (1 << 14); /* src .f32 */
825 set_src_0(pc
, src
, e
);
831 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
832 struct nv50_reg
*v
, struct nv50_reg
*e
)
834 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
836 emit_flop(pc
, 3, temp
, v
);
837 emit_mul(pc
, temp
, temp
, e
);
838 emit_preex2(pc
, temp
, temp
);
839 emit_flop(pc
, 6, dst
, temp
);
845 emit_abs(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
847 struct nv50_program_exec
*e
= exec(pc
);
849 e
->inst
[0] = 0xa0000000; /* cvt */
851 e
->inst
[1] |= (6 << 29); /* cvt */
852 e
->inst
[1] |= 0x04000000; /* 32 bit */
853 e
->inst
[1] |= (1 << 14); /* src .f32 */
854 e
->inst
[1] |= ((1 << 6) << 14); /* .abs */
856 set_src_0(pc
, src
, e
);
862 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
863 struct nv50_reg
**src
)
865 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
866 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
867 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
868 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
869 struct nv50_reg
*tmp
[4];
871 if (mask
& (3 << 1)) {
872 tmp
[0] = alloc_temp(pc
, NULL
);
873 emit_minmax(pc
, 4, tmp
[0], src
[0], zero
);
876 if (mask
& (1 << 2)) {
877 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
879 tmp
[1] = temp_temp(pc
);
880 emit_minmax(pc
, 4, tmp
[1], src
[1], zero
);
882 tmp
[3] = temp_temp(pc
);
883 emit_minmax(pc
, 4, tmp
[3], src
[3], neg128
);
884 emit_minmax(pc
, 5, tmp
[3], tmp
[3], pos128
);
886 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
887 emit_mov(pc
, dst
[2], zero
);
888 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
892 assimilate_temp(pc
, dst
[1], tmp
[0]);
895 free_temp(pc
, tmp
[0]);
897 /* do this last, in case src[i,j] == dst[0,3] */
899 emit_mov(pc
, dst
[0], one
);
902 emit_mov(pc
, dst
[3], one
);
911 emit_neg(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
913 struct nv50_program_exec
*e
= exec(pc
);
916 e
->inst
[0] |= 0xa0000000; /* delta */
917 e
->inst
[1] |= (7 << 29); /* delta */
918 e
->inst
[1] |= 0x04000000; /* negate arg0? probably not */
919 e
->inst
[1] |= (1 << 14); /* src .f32 */
921 set_src_0(pc
, src
, e
);
927 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
929 struct nv50_program_exec
*e
;
930 const int r_pred
= 1;
932 /* Sets predicate reg ? */
934 e
->inst
[0] = 0xa00001fd;
935 e
->inst
[1] = 0xc4014788;
936 set_src_0(pc
, src
, e
);
937 set_pred_wr(pc
, 1, r_pred
, e
);
940 /* This is probably KILP */
942 e
->inst
[0] = 0x000001fe;
944 set_pred(pc
, 1 /* LT? */, r_pred
, e
);
949 convert_to_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
951 unsigned q
= 0, m
= ~0;
955 switch (e
->inst
[0] >> 28) {
964 if (e
->inst
[0] & 0x02000000)
973 q
= ((e
->inst
[0] & (~m
)) >> 2);
978 q
= ((e
->inst
[0] & (~m
)) << 12);
981 /* MAD (if src2 == dst) */
982 q
= ((e
->inst
[0] & 0x1fc) << 12);
996 static struct nv50_reg
*
997 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
999 switch (dst
->DstRegister
.File
) {
1000 case TGSI_FILE_TEMPORARY
:
1001 return &pc
->temp
[dst
->DstRegister
.Index
* 4 + c
];
1002 case TGSI_FILE_OUTPUT
:
1003 return &pc
->result
[dst
->DstRegister
.Index
* 4 + c
];
1004 case TGSI_FILE_NULL
:
1013 static struct nv50_reg
*
1014 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
)
1016 struct nv50_reg
*r
= NULL
;
1017 struct nv50_reg
*temp
;
1020 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1022 c
= tgsi_util_get_full_src_register_extswizzle(src
, chan
);
1024 case TGSI_EXTSWIZZLE_X
:
1025 case TGSI_EXTSWIZZLE_Y
:
1026 case TGSI_EXTSWIZZLE_Z
:
1027 case TGSI_EXTSWIZZLE_W
:
1028 switch (src
->SrcRegister
.File
) {
1029 case TGSI_FILE_INPUT
:
1030 r
= &pc
->attr
[src
->SrcRegister
.Index
* 4 + c
];
1032 case TGSI_FILE_TEMPORARY
:
1033 r
= &pc
->temp
[src
->SrcRegister
.Index
* 4 + c
];
1035 case TGSI_FILE_CONSTANT
:
1036 r
= &pc
->param
[src
->SrcRegister
.Index
* 4 + c
];
1038 case TGSI_FILE_IMMEDIATE
:
1039 r
= &pc
->immd
[src
->SrcRegister
.Index
* 4 + c
];
1041 case TGSI_FILE_SAMPLER
:
1048 case TGSI_EXTSWIZZLE_ZERO
:
1049 r
= alloc_immd(pc
, 0.0);
1051 case TGSI_EXTSWIZZLE_ONE
:
1052 if (sgn
== TGSI_UTIL_SIGN_TOGGLE
|| sgn
== TGSI_UTIL_SIGN_SET
)
1053 return alloc_immd(pc
, -1.0);
1054 return alloc_immd(pc
, 1.0);
1061 case TGSI_UTIL_SIGN_KEEP
:
1063 case TGSI_UTIL_SIGN_CLEAR
:
1064 temp
= temp_temp(pc
);
1065 emit_abs(pc
, temp
, r
);
1068 case TGSI_UTIL_SIGN_TOGGLE
:
1069 temp
= temp_temp(pc
);
1070 emit_neg(pc
, temp
, r
);
1073 case TGSI_UTIL_SIGN_SET
:
1074 temp
= temp_temp(pc
);
1075 emit_abs(pc
, temp
, r
);
1076 emit_neg(pc
, temp
, temp
);
1087 /* returns TRUE if instruction can overwrite sources before they're read */
1089 direct2dest_op(const struct tgsi_full_instruction
*insn
)
1091 if (insn
->Instruction
.Saturate
)
1094 switch (insn
->Instruction
.Opcode
) {
1095 case TGSI_OPCODE_COS
:
1096 case TGSI_OPCODE_DP3
:
1097 case TGSI_OPCODE_DP4
:
1098 case TGSI_OPCODE_DPH
:
1099 case TGSI_OPCODE_KIL
:
1100 case TGSI_OPCODE_LIT
:
1101 case TGSI_OPCODE_POW
:
1102 case TGSI_OPCODE_RCP
:
1103 case TGSI_OPCODE_RSQ
:
1104 case TGSI_OPCODE_SCS
:
1105 case TGSI_OPCODE_SIN
:
1106 case TGSI_OPCODE_TEX
:
1107 case TGSI_OPCODE_TXP
:
1115 nv50_program_tx_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
1117 const struct tgsi_full_instruction
*inst
= &tok
->FullInstruction
;
1118 struct nv50_reg
*rdst
[4], *dst
[4], *src
[3][4], *temp
;
1119 unsigned mask
, sat
, unit
;
1120 boolean assimilate
= FALSE
;
1123 mask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
1124 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
1126 for (c
= 0; c
< 4; c
++) {
1127 if (mask
& (1 << c
))
1128 dst
[c
] = tgsi_dst(pc
, c
, &inst
->FullDstRegisters
[0]);
1137 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1138 const struct tgsi_full_src_register
*fs
= &inst
->FullSrcRegisters
[i
];
1140 if (fs
->SrcRegister
.File
== TGSI_FILE_SAMPLER
)
1141 unit
= fs
->SrcRegister
.Index
;
1143 for (c
= 0; c
< 4; c
++)
1144 src
[i
][c
] = tgsi_src(pc
, c
, fs
);
1148 for (c
= 0; c
< 4; c
++) {
1150 dst
[c
] = temp_temp(pc
);
1153 if (direct2dest_op(inst
)) {
1154 for (c
= 0; c
< 4; c
++) {
1155 if (!dst
[c
] || dst
[c
]->type
!= P_TEMP
)
1158 for (i
= c
+ 1; i
< 4; i
++) {
1159 if (dst
[c
] == src
[0][i
] ||
1160 dst
[c
] == src
[1][i
] ||
1161 dst
[c
] == src
[2][i
])
1169 dst
[c
] = alloc_temp(pc
, NULL
);
1173 switch (inst
->Instruction
.Opcode
) {
1174 case TGSI_OPCODE_ABS
:
1175 for (c
= 0; c
< 4; c
++) {
1176 if (!(mask
& (1 << c
)))
1178 emit_abs(pc
, dst
[c
], src
[0][c
]);
1181 case TGSI_OPCODE_ADD
:
1182 for (c
= 0; c
< 4; c
++) {
1183 if (!(mask
& (1 << c
)))
1185 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1188 case TGSI_OPCODE_COS
:
1189 temp
= temp_temp(pc
);
1190 emit_precossin(pc
, temp
, src
[0][0]);
1191 emit_flop(pc
, 5, temp
, temp
);
1192 for (c
= 0; c
< 4; c
++) {
1193 if (!(mask
& (1 << c
)))
1195 emit_mov(pc
, dst
[c
], temp
);
1198 case TGSI_OPCODE_DP3
:
1199 temp
= temp_temp(pc
);
1200 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1201 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1202 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1203 for (c
= 0; c
< 4; c
++) {
1204 if (!(mask
& (1 << c
)))
1206 emit_mov(pc
, dst
[c
], temp
);
1209 case TGSI_OPCODE_DP4
:
1210 temp
= temp_temp(pc
);
1211 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1212 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1213 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1214 emit_mad(pc
, temp
, src
[0][3], src
[1][3], temp
);
1215 for (c
= 0; c
< 4; c
++) {
1216 if (!(mask
& (1 << c
)))
1218 emit_mov(pc
, dst
[c
], temp
);
1221 case TGSI_OPCODE_DPH
:
1222 temp
= temp_temp(pc
);
1223 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1224 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1225 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1226 emit_add(pc
, temp
, src
[1][3], temp
);
1227 for (c
= 0; c
< 4; c
++) {
1228 if (!(mask
& (1 << c
)))
1230 emit_mov(pc
, dst
[c
], temp
);
1233 case TGSI_OPCODE_DST
:
1235 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1236 if (mask
& (1 << 0))
1237 emit_mov(pc
, dst
[0], one
);
1238 if (mask
& (1 << 1))
1239 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
1240 if (mask
& (1 << 2))
1241 emit_mov(pc
, dst
[2], src
[0][2]);
1242 if (mask
& (1 << 3))
1243 emit_mov(pc
, dst
[3], src
[1][3]);
1247 case TGSI_OPCODE_EX2
:
1248 temp
= temp_temp(pc
);
1249 emit_preex2(pc
, temp
, src
[0][0]);
1250 emit_flop(pc
, 6, temp
, temp
);
1251 for (c
= 0; c
< 4; c
++) {
1252 if (!(mask
& (1 << c
)))
1254 emit_mov(pc
, dst
[c
], temp
);
1257 case TGSI_OPCODE_FLR
:
1258 for (c
= 0; c
< 4; c
++) {
1259 if (!(mask
& (1 << c
)))
1261 emit_flr(pc
, dst
[c
], src
[0][c
]);
1264 case TGSI_OPCODE_FRC
:
1265 temp
= temp_temp(pc
);
1266 for (c
= 0; c
< 4; c
++) {
1267 if (!(mask
& (1 << c
)))
1269 emit_flr(pc
, temp
, src
[0][c
]);
1270 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
1273 case TGSI_OPCODE_KIL
:
1274 emit_kil(pc
, src
[0][0]);
1275 emit_kil(pc
, src
[0][1]);
1276 emit_kil(pc
, src
[0][2]);
1277 emit_kil(pc
, src
[0][3]);
1278 pc
->p
->cfg
.fp
.regs
[2] |= 0x00100000;
1280 case TGSI_OPCODE_LIT
:
1281 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
1283 case TGSI_OPCODE_LG2
:
1284 temp
= temp_temp(pc
);
1285 emit_flop(pc
, 3, temp
, src
[0][0]);
1286 for (c
= 0; c
< 4; c
++) {
1287 if (!(mask
& (1 << c
)))
1289 emit_mov(pc
, dst
[c
], temp
);
1292 case TGSI_OPCODE_LRP
:
1293 temp
= temp_temp(pc
);
1294 for (c
= 0; c
< 4; c
++) {
1295 if (!(mask
& (1 << c
)))
1297 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
1298 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
1301 case TGSI_OPCODE_MAD
:
1302 for (c
= 0; c
< 4; c
++) {
1303 if (!(mask
& (1 << c
)))
1305 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
1308 case TGSI_OPCODE_MAX
:
1309 for (c
= 0; c
< 4; c
++) {
1310 if (!(mask
& (1 << c
)))
1312 emit_minmax(pc
, 4, dst
[c
], src
[0][c
], src
[1][c
]);
1315 case TGSI_OPCODE_MIN
:
1316 for (c
= 0; c
< 4; c
++) {
1317 if (!(mask
& (1 << c
)))
1319 emit_minmax(pc
, 5, dst
[c
], src
[0][c
], src
[1][c
]);
1322 case TGSI_OPCODE_MOV
:
1323 for (c
= 0; c
< 4; c
++) {
1324 if (!(mask
& (1 << c
)))
1326 emit_mov(pc
, dst
[c
], src
[0][c
]);
1329 case TGSI_OPCODE_MUL
:
1330 for (c
= 0; c
< 4; c
++) {
1331 if (!(mask
& (1 << c
)))
1333 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1336 case TGSI_OPCODE_POW
:
1337 temp
= temp_temp(pc
);
1338 emit_pow(pc
, temp
, src
[0][0], src
[1][0]);
1339 for (c
= 0; c
< 4; c
++) {
1340 if (!(mask
& (1 << c
)))
1342 emit_mov(pc
, dst
[c
], temp
);
1345 case TGSI_OPCODE_RCP
:
1346 for (c
= 3; c
>= 0; c
--) {
1347 if (!(mask
& (1 << c
)))
1349 emit_flop(pc
, 0, dst
[c
], src
[0][0]);
1352 case TGSI_OPCODE_RSQ
:
1353 for (c
= 3; c
>= 0; c
--) {
1354 if (!(mask
& (1 << c
)))
1356 emit_flop(pc
, 2, dst
[c
], src
[0][0]);
1359 case TGSI_OPCODE_SCS
:
1360 temp
= temp_temp(pc
);
1361 emit_precossin(pc
, temp
, src
[0][0]);
1362 if (mask
& (1 << 0))
1363 emit_flop(pc
, 5, dst
[0], temp
);
1364 if (mask
& (1 << 1))
1365 emit_flop(pc
, 4, dst
[1], temp
);
1366 if (mask
& (1 << 2))
1367 emit_mov_immdval(pc
, dst
[2], 0.0);
1368 if (mask
& (1 << 3))
1369 emit_mov_immdval(pc
, dst
[3], 1.0);
1371 case TGSI_OPCODE_SGE
:
1372 for (c
= 0; c
< 4; c
++) {
1373 if (!(mask
& (1 << c
)))
1375 emit_set(pc
, 6, dst
[c
], src
[0][c
], src
[1][c
]);
1378 case TGSI_OPCODE_SIN
:
1379 temp
= temp_temp(pc
);
1380 emit_precossin(pc
, temp
, src
[0][0]);
1381 emit_flop(pc
, 4, temp
, temp
);
1382 for (c
= 0; c
< 4; c
++) {
1383 if (!(mask
& (1 << c
)))
1385 emit_mov(pc
, dst
[c
], temp
);
1388 case TGSI_OPCODE_SLT
:
1389 for (c
= 0; c
< 4; c
++) {
1390 if (!(mask
& (1 << c
)))
1392 emit_set(pc
, 1, dst
[c
], src
[0][c
], src
[1][c
]);
1395 case TGSI_OPCODE_SUB
:
1396 for (c
= 0; c
< 4; c
++) {
1397 if (!(mask
& (1 << c
)))
1399 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1402 case TGSI_OPCODE_TEX
:
1403 case TGSI_OPCODE_TXP
:
1405 struct nv50_reg
*t
[4];
1406 struct nv50_program_exec
*e
;
1408 alloc_temp4(pc
, t
, 0);
1409 emit_mov(pc
, t
[0], src
[0][0]);
1410 emit_mov(pc
, t
[1], src
[0][1]);
1413 e
->inst
[0] = 0xf6400000;
1414 e
->inst
[0] |= (unit
<< 9);
1416 e
->inst
[1] |= 0x0000c004;
1417 set_dst(pc
, t
[0], e
);
1420 if (mask
& (1 << 0)) emit_mov(pc
, dst
[0], t
[0]);
1421 if (mask
& (1 << 1)) emit_mov(pc
, dst
[1], t
[1]);
1422 if (mask
& (1 << 2)) emit_mov(pc
, dst
[2], t
[2]);
1423 if (mask
& (1 << 3)) emit_mov(pc
, dst
[3], t
[3]);
1428 case TGSI_OPCODE_XPD
:
1429 temp
= temp_temp(pc
);
1430 if (mask
& (1 << 0)) {
1431 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
1432 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
1434 if (mask
& (1 << 1)) {
1435 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
1436 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
1438 if (mask
& (1 << 2)) {
1439 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
1440 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
1442 if (mask
& (1 << 3))
1443 emit_mov_immdval(pc
, dst
[3], 1.0);
1445 case TGSI_OPCODE_END
:
1448 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
1453 for (c
= 0; c
< 4; c
++) {
1454 struct nv50_program_exec
*e
;
1456 if (!(mask
& (1 << c
)))
1460 e
->inst
[0] = 0xa0000000; /* cvt */
1462 e
->inst
[1] |= (6 << 29); /* cvt */
1463 e
->inst
[1] |= 0x04000000; /* 32 bit */
1464 e
->inst
[1] |= (1 << 14); /* src .f32 */
1465 e
->inst
[1] |= ((1 << 5) << 14); /* .sat */
1466 set_dst(pc
, rdst
[c
], e
);
1467 set_src_0(pc
, dst
[c
], e
);
1470 } else if (assimilate
) {
1471 for (c
= 0; c
< 4; c
++)
1473 assimilate_temp(pc
, rdst
[c
], dst
[c
]);
1476 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1477 for (c
= 0; c
< 4; c
++) {
1480 if (src
[i
][c
]->index
== -1 && src
[i
][c
]->type
== P_IMMD
)
1489 /* Adjust a bitmask that indicates what components of a source are used,
1490 * we use this in tx_prep so we only load interpolants that are needed.
1493 insn_adjust_mask(const struct tgsi_full_instruction
*insn
, unsigned *mask
)
1495 const struct tgsi_instruction_ext_texture
*tex
;
1497 switch (insn
->Instruction
.Opcode
) {
1498 case TGSI_OPCODE_DP3
:
1501 case TGSI_OPCODE_DP4
:
1502 case TGSI_OPCODE_DPH
:
1505 case TGSI_OPCODE_LIT
:
1508 case TGSI_OPCODE_RCP
:
1509 case TGSI_OPCODE_RSQ
:
1512 case TGSI_OPCODE_TEX
:
1513 case TGSI_OPCODE_TXP
:
1514 assert(insn
->Instruction
.Extended
);
1515 tex
= &insn
->InstructionExtTexture
;
1518 if (tex
->Texture
== TGSI_TEXTURE_1D
)
1521 if (tex
->Texture
== TGSI_TEXTURE_2D
)
1524 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1533 prep_inspect_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
,
1534 unsigned *r_usage
[2])
1536 const struct tgsi_full_instruction
*insn
;
1537 const struct tgsi_full_src_register
*src
;
1538 const struct tgsi_dst_register
*dst
;
1540 unsigned i
, c
, k
, n
, mask
, *acc_p
;
1542 insn
= &tok
->FullInstruction
;
1543 dst
= &insn
->FullDstRegisters
[0].DstRegister
;
1544 mask
= dst
->WriteMask
;
1547 r_usage
[0] = CALLOC(pc
->temp_nr
* 4, sizeof(unsigned));
1549 r_usage
[1] = CALLOC(pc
->attr_nr
* 4, sizeof(unsigned));
1551 if (dst
->File
== TGSI_FILE_TEMPORARY
) {
1552 for (c
= 0; c
< 4; c
++) {
1553 if (!(mask
& (1 << c
)))
1555 r_usage
[0][dst
->Index
* 4 + c
] = pc
->insn_nr
;
1559 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
1560 src
= &insn
->FullSrcRegisters
[i
];
1562 switch (src
->SrcRegister
.File
) {
1563 case TGSI_FILE_TEMPORARY
:
1566 case TGSI_FILE_INPUT
:
1573 insn_adjust_mask(insn
, &mask
);
1575 for (c
= 0; c
< 4; c
++) {
1576 if (!(mask
& (1 << c
)))
1579 k
= tgsi_util_get_full_src_register_extswizzle(src
, c
);
1581 case TGSI_EXTSWIZZLE_X
:
1582 case TGSI_EXTSWIZZLE_Y
:
1583 case TGSI_EXTSWIZZLE_Z
:
1584 case TGSI_EXTSWIZZLE_W
:
1585 n
= src
->SrcRegister
.Index
* 4 + k
;
1586 acc_p
[n
] = pc
->insn_nr
;
1596 load_fp_attrib(struct nv50_pc
*pc
, int i
, unsigned *acc
, int *mid
,
1597 int *aid
, int *p_oid
)
1599 struct nv50_reg
*iv
;
1603 iv
= (pc
->interp_mode
[i
] & INTERP_CENTROID
) ? pc
->iv_c
: pc
->iv_p
;
1605 for (c
= 0, n
= i
* 4; c
< 4; c
++, n
++) {
1607 pc
->attr
[n
].type
= P_TEMP
;
1608 pc
->attr
[n
].index
= i
;
1610 if (pc
->attr
[n
].acc
== acc
[n
])
1614 pc
->attr
[n
].acc
= acc
[n
];
1615 pc
->attr
[n
].rhw
= pc
->attr
[n
].hw
= -1;
1616 alloc_reg(pc
, &pc
->attr
[n
]);
1618 pc
->attr
[n
].rhw
= (*aid
)++;
1619 emit_interp(pc
, &pc
->attr
[n
], iv
, pc
->interp_mode
[i
]);
1621 pc
->p
->cfg
.fp
.map
[(*mid
) / 4] |= oid
<< (8 * ((*mid
) % 4));
1623 pc
->p
->cfg
.fp
.regs
[1] += 0x00010001;
1630 nv50_program_tx_prep(struct nv50_pc
*pc
)
1632 struct tgsi_parse_context p
;
1633 boolean ret
= FALSE
;
1635 unsigned fcol
, bcol
, fcrd
, depr
;
1637 /* count (centroid) perspective interpolations */
1638 unsigned centroid_loads
= 0;
1639 unsigned perspect_loads
= 0;
1641 /* track register access for temps and attrs */
1642 unsigned *r_usage
[2];
1646 depr
= fcol
= bcol
= fcrd
= 0xffff;
1648 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1649 pc
->p
->cfg
.fp
.regs
[0] = 0x01000404;
1650 pc
->p
->cfg
.fp
.regs
[1] = 0x00000400;
1653 tgsi_parse_init(&p
, pc
->p
->pipe
.tokens
);
1654 while (!tgsi_parse_end_of_tokens(&p
)) {
1655 const union tgsi_full_token
*tok
= &p
.FullToken
;
1657 tgsi_parse_token(&p
);
1658 switch (tok
->Token
.Type
) {
1659 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1661 const struct tgsi_full_immediate
*imm
=
1662 &p
.FullToken
.FullImmediate
;
1664 ctor_immd(pc
, imm
->u
.ImmediateFloat32
[0].Float
,
1665 imm
->u
.ImmediateFloat32
[1].Float
,
1666 imm
->u
.ImmediateFloat32
[2].Float
,
1667 imm
->u
.ImmediateFloat32
[3].Float
);
1670 case TGSI_TOKEN_TYPE_DECLARATION
:
1672 const struct tgsi_full_declaration
*d
;
1673 unsigned last
, first
, mode
;
1675 d
= &p
.FullToken
.FullDeclaration
;
1676 first
= d
->DeclarationRange
.First
;
1677 last
= d
->DeclarationRange
.Last
;
1679 switch (d
->Declaration
.File
) {
1680 case TGSI_FILE_TEMPORARY
:
1681 if (pc
->temp_nr
< (last
+ 1))
1682 pc
->temp_nr
= last
+ 1;
1684 case TGSI_FILE_OUTPUT
:
1685 if (pc
->result_nr
< (last
+ 1))
1686 pc
->result_nr
= last
+ 1;
1688 if (!d
->Declaration
.Semantic
)
1691 switch (d
->Semantic
.SemanticName
) {
1692 case TGSI_SEMANTIC_POSITION
:
1694 pc
->p
->cfg
.fp
.regs
[2] |= 0x00000100;
1695 pc
->p
->cfg
.fp
.regs
[3] |= 0x00000011;
1702 case TGSI_FILE_INPUT
:
1704 if (pc
->attr_nr
< (last
+ 1))
1705 pc
->attr_nr
= last
+ 1;
1707 if (pc
->p
->type
!= PIPE_SHADER_FRAGMENT
)
1710 switch (d
->Declaration
.Interpolate
) {
1711 case TGSI_INTERPOLATE_CONSTANT
:
1714 case TGSI_INTERPOLATE_PERSPECTIVE
:
1715 mode
= INTERP_PERSPECTIVE
;
1718 mode
= INTERP_LINEAR
;
1722 if (d
->Declaration
.Semantic
) {
1723 switch (d
->Semantic
.SemanticName
) {
1724 case TGSI_SEMANTIC_POSITION
:
1727 case TGSI_SEMANTIC_COLOR
:
1729 mode
= INTERP_PERSPECTIVE
;
1731 case TGSI_SEMANTIC_BCOLOR
:
1733 mode
= INTERP_PERSPECTIVE
;
1738 if (d
->Declaration
.Centroid
) {
1739 mode
|= INTERP_CENTROID
;
1740 if (mode
& INTERP_PERSPECTIVE
)
1743 if (mode
& INTERP_PERSPECTIVE
)
1747 for (i
= first
; i
<= last
; i
++)
1748 pc
->interp_mode
[i
] = mode
;
1751 case TGSI_FILE_CONSTANT
:
1752 if (pc
->param_nr
< (last
+ 1))
1753 pc
->param_nr
= last
+ 1;
1755 case TGSI_FILE_SAMPLER
:
1758 NOUVEAU_ERR("bad decl file %d\n",
1759 d
->Declaration
.File
);
1764 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1766 prep_inspect_insn(pc
, tok
, r_usage
);
1774 pc
->temp
= CALLOC(pc
->temp_nr
* 4, sizeof(struct nv50_reg
));
1778 for (i
= 0; i
< pc
->temp_nr
; i
++) {
1779 for (c
= 0; c
< 4; c
++) {
1780 pc
->temp
[i
*4+c
].type
= P_TEMP
;
1781 pc
->temp
[i
*4+c
].hw
= -1;
1782 pc
->temp
[i
*4+c
].rhw
= -1;
1783 pc
->temp
[i
*4+c
].index
= i
;
1784 pc
->temp
[i
*4+c
].acc
= r_usage
[0][i
*4+c
];
1790 int oid
= 4, mid
= 4, aid
= 0;
1791 /* oid = VP output id
1792 * aid = FP attribute/interpolant id
1793 * mid = VP output mapping field ID
1796 pc
->attr
= CALLOC(pc
->attr_nr
* 4, sizeof(struct nv50_reg
));
1800 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1801 /* position should be loaded first */
1802 if (fcrd
!= 0xffff) {
1805 mask
= load_fp_attrib(pc
, fcrd
, r_usage
[1],
1808 pc
->p
->cfg
.fp
.regs
[1] |= (mask
<< 24);
1809 pc
->p
->cfg
.fp
.map
[0] = 0x04040404 * fcrd
;
1811 pc
->p
->cfg
.fp
.map
[0] += 0x03020100;
1813 /* should do MAD fcrd.xy, fcrd, SOME_CONST, fcrd */
1815 if (perspect_loads
) {
1816 pc
->iv_p
= alloc_temp(pc
, NULL
);
1818 if (!(pc
->p
->cfg
.fp
.regs
[1] & 0x08000000)) {
1819 pc
->p
->cfg
.fp
.regs
[1] |= 0x08000000;
1820 pc
->iv_p
->rhw
= aid
++;
1821 emit_interp(pc
, pc
->iv_p
, NULL
,
1823 emit_flop(pc
, 0, pc
->iv_p
, pc
->iv_p
);
1825 pc
->iv_p
->rhw
= aid
- 1;
1826 emit_flop(pc
, 0, pc
->iv_p
,
1827 &pc
->attr
[fcrd
* 4 + 3]);
1831 if (centroid_loads
) {
1832 pc
->iv_c
= alloc_temp(pc
, NULL
);
1833 pc
->iv_c
->rhw
= pc
->iv_p
? aid
- 1 : aid
++;
1834 emit_interp(pc
, pc
->iv_c
, NULL
,
1836 emit_flop(pc
, 0, pc
->iv_c
, pc
->iv_c
);
1837 pc
->p
->cfg
.fp
.regs
[1] |= 0x08000000;
1840 for (c
= 0; c
< 4; c
++) {
1841 /* I don't know what these values do, but
1842 * let's set them like the blob does:
1844 if (fcol
!= 0xffff && r_usage
[1][fcol
* 4 + c
])
1845 pc
->p
->cfg
.fp
.regs
[0] += 0x00010000;
1846 if (bcol
!= 0xffff && r_usage
[1][bcol
* 4 + c
])
1847 pc
->p
->cfg
.fp
.regs
[0] += 0x00010000;
1850 for (i
= 0; i
< pc
->attr_nr
; i
++)
1851 load_fp_attrib(pc
, i
, r_usage
[1],
1855 free_temp(pc
, pc
->iv_p
);
1857 free_temp(pc
, pc
->iv_c
);
1859 pc
->p
->cfg
.fp
.high_map
= (mid
/ 4);
1860 pc
->p
->cfg
.fp
.high_map
+= ((mid
% 4) ? 1 : 0);
1862 /* vertex program */
1863 for (i
= 0; i
< pc
->attr_nr
* 4; i
++) {
1864 pc
->p
->cfg
.vp
.attr
[aid
/ 32] |=
1866 pc
->attr
[i
].type
= P_ATTR
;
1867 pc
->attr
[i
].hw
= aid
++;
1868 pc
->attr
[i
].index
= i
/ 4;
1873 if (pc
->result_nr
) {
1876 pc
->result
= CALLOC(pc
->result_nr
* 4, sizeof(struct nv50_reg
));
1880 for (i
= 0; i
< pc
->result_nr
; i
++) {
1881 for (c
= 0; c
< 4; c
++) {
1882 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1883 pc
->result
[i
*4+c
].type
= P_TEMP
;
1884 pc
->result
[i
*4+c
].hw
= -1;
1885 pc
->result
[i
*4+c
].rhw
= (i
== depr
) ?
1888 pc
->result
[i
*4+c
].type
= P_RESULT
;
1889 pc
->result
[i
*4+c
].hw
= rid
++;
1891 pc
->result
[i
*4+c
].index
= i
;
1894 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
&&
1896 pc
->result
[depr
* 4 + 2].rhw
=
1897 (pc
->result_nr
- 1) * 4;
1905 pc
->param
= CALLOC(pc
->param_nr
* 4, sizeof(struct nv50_reg
));
1909 for (i
= 0; i
< pc
->param_nr
; i
++) {
1910 for (c
= 0; c
< 4; c
++) {
1911 pc
->param
[i
*4+c
].type
= P_CONST
;
1912 pc
->param
[i
*4+c
].hw
= rid
++;
1913 pc
->param
[i
*4+c
].index
= i
;
1919 int rid
= pc
->param_nr
* 4;
1921 pc
->immd
= CALLOC(pc
->immd_nr
* 4, sizeof(struct nv50_reg
));
1925 for (i
= 0; i
< pc
->immd_nr
; i
++) {
1926 for (c
= 0; c
< 4; c
++) {
1927 pc
->immd
[i
*4+c
].type
= P_IMMD
;
1928 pc
->immd
[i
*4+c
].hw
= rid
++;
1929 pc
->immd
[i
*4+c
].index
= i
;
1941 tgsi_parse_free(&p
);
1946 free_nv50_pc(struct nv50_pc
*pc
)
1961 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
1962 /* deallocate fragment program attributes */
1963 if (pc
->r_temp
[i
] && pc
->r_temp
[i
]->index
== -1)
1964 FREE(pc
->r_temp
[i
]);
1971 nv50_program_tx(struct nv50_program
*p
)
1973 struct tgsi_parse_context parse
;
1978 pc
= CALLOC_STRUCT(nv50_pc
);
1982 pc
->p
->cfg
.high_temp
= 4;
1984 ret
= nv50_program_tx_prep(pc
);
1988 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
1989 while (!tgsi_parse_end_of_tokens(&parse
)) {
1990 const union tgsi_full_token
*tok
= &parse
.FullToken
;
1992 /* don't allow half insn/immd on first and last instruction */
1994 if (pc
->insn_cur
== 0 || pc
->insn_cur
+ 2 == pc
->insn_nr
)
1995 pc
->allow32
= FALSE
;
1997 tgsi_parse_token(&parse
);
1999 switch (tok
->Token
.Type
) {
2000 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2002 ret
= nv50_program_tx_insn(pc
, tok
);
2011 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
2012 struct nv50_reg out
;
2015 for (k
= 0; k
< pc
->result_nr
* 4; k
++) {
2016 if (pc
->result
[k
].rhw
== -1)
2018 if (pc
->result
[k
].hw
!= pc
->result
[k
].rhw
) {
2019 out
.hw
= pc
->result
[k
].rhw
;
2020 emit_mov(pc
, &out
, &pc
->result
[k
]);
2022 if (pc
->p
->cfg
.high_result
< (pc
->result
[k
].rhw
+ 1))
2023 pc
->p
->cfg
.high_result
= pc
->result
[k
].rhw
+ 1;
2027 /* look for single half instructions and make them long */
2028 struct nv50_program_exec
*e
, *e_prev
;
2030 for (k
= 0, e
= pc
->p
->exec_head
, e_prev
= NULL
; e
; e
= e
->next
) {
2034 if (!e
->next
|| is_long(e
->next
)) {
2036 convert_to_long(pc
, e
);
2044 if (!is_long(pc
->p
->exec_tail
)) {
2045 /* this may occur if moving FP results */
2046 assert(e_prev
&& !is_long(e_prev
));
2047 convert_to_long(pc
, e_prev
);
2048 convert_to_long(pc
, pc
->p
->exec_tail
);
2051 assert(is_long(pc
->p
->exec_tail
) && !is_immd(pc
->p
->exec_head
));
2052 pc
->p
->exec_tail
->inst
[1] |= 0x00000001;
2054 p
->param_nr
= pc
->param_nr
* 4;
2055 p
->immd_nr
= pc
->immd_nr
* 4;
2056 p
->immd
= pc
->immd_buf
;
2059 tgsi_parse_free(&parse
);
2067 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
2069 if (nv50_program_tx(p
) == FALSE
)
2071 p
->translated
= TRUE
;
2075 nv50_program_upload_data(struct nv50_context
*nv50
, float *map
,
2076 unsigned start
, unsigned count
)
2078 struct nouveau_channel
*chan
= nv50
->screen
->nvws
->channel
;
2079 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2082 unsigned nr
= count
> 2047 ? 2047 : count
;
2084 BEGIN_RING(chan
, tesla
, 0x00000f00, 1);
2085 OUT_RING (chan
, (NV50_CB_PMISC
<< 0) | (start
<< 8));
2086 BEGIN_RING(chan
, tesla
, 0x40000f04, nr
);
2087 OUT_RINGp (chan
, map
, nr
);
2096 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
2098 struct nouveau_winsys
*nvws
= nv50
->screen
->nvws
;
2099 struct pipe_winsys
*ws
= nv50
->pipe
.winsys
;
2100 unsigned nr
= p
->param_nr
+ p
->immd_nr
;
2102 if (!p
->data
&& nr
) {
2103 struct nouveau_resource
*heap
= nv50
->screen
->vp_data_heap
;
2105 if (nvws
->res_alloc(heap
, nr
, p
, &p
->data
)) {
2106 while (heap
->next
&& heap
->size
< nr
) {
2107 struct nv50_program
*evict
= heap
->next
->priv
;
2108 nvws
->res_free(&evict
->data
);
2111 if (nvws
->res_alloc(heap
, nr
, p
, &p
->data
))
2117 float *map
= ws
->buffer_map(ws
, nv50
->constbuf
[p
->type
],
2118 PIPE_BUFFER_USAGE_CPU_READ
);
2119 nv50_program_upload_data(nv50
, map
, p
->data
->start
,
2121 ws
->buffer_unmap(ws
, nv50
->constbuf
[p
->type
]);
2125 nv50_program_upload_data(nv50
, p
->immd
,
2126 p
->data
->start
+ p
->param_nr
,
2132 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
2134 struct nouveau_channel
*chan
= nv50
->screen
->nvws
->channel
;
2135 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2136 struct pipe_screen
*screen
= nv50
->pipe
.screen
;
2137 struct nv50_program_exec
*e
;
2138 struct nouveau_stateobj
*so
;
2139 const unsigned flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
;
2140 unsigned start
, count
, *up
, *ptr
;
2141 boolean upload
= FALSE
;
2144 p
->buffer
= screen
->buffer_create(screen
, 0x100, 0, p
->exec_size
* 4);
2148 if (p
->data
&& p
->data
->start
!= p
->data_start
) {
2149 for (e
= p
->exec_head
; e
; e
= e
->next
) {
2152 if (e
->param
.index
< 0)
2154 ei
= e
->param
.shift
>> 5;
2155 ci
= e
->param
.index
+ p
->data
->start
;
2157 e
->inst
[ei
] &= ~e
->param
.mask
;
2158 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
2161 p
->data_start
= p
->data
->start
;
2168 #ifdef NV50_PROGRAM_DUMP
2169 NOUVEAU_ERR("-------\n");
2170 for (e
= p
->exec_head
; e
; e
= e
->next
) {
2171 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
2173 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
2177 up
= ptr
= MALLOC(p
->exec_size
* 4);
2178 for (e
= p
->exec_head
; e
; e
= e
->next
) {
2179 *(ptr
++) = e
->inst
[0];
2181 *(ptr
++) = e
->inst
[1];
2185 so_method(so
, nv50
->screen
->tesla
, 0x1280, 3);
2186 so_reloc (so
, p
->buffer
, 0, flags
| NOUVEAU_BO_HIGH
, 0, 0);
2187 so_reloc (so
, p
->buffer
, 0, flags
| NOUVEAU_BO_LOW
, 0, 0);
2188 so_data (so
, (NV50_CB_PUPLOAD
<< 16) | 0x0800); //(p->exec_size * 4));
2190 start
= 0; count
= p
->exec_size
;
2192 struct nouveau_winsys
*nvws
= nv50
->screen
->nvws
;
2197 nr
= MIN2(count
, 2047);
2198 nr
= MIN2(nvws
->channel
->pushbuf
->remaining
, nr
);
2199 if (nvws
->channel
->pushbuf
->remaining
< (nr
+ 3)) {
2204 BEGIN_RING(chan
, tesla
, 0x0f00, 1);
2205 OUT_RING (chan
, (start
<< 8) | NV50_CB_PUPLOAD
);
2206 BEGIN_RING(chan
, tesla
, 0x40000f04, nr
);
2207 OUT_RINGp (chan
, up
+ start
, nr
);
2218 nv50_vertprog_validate(struct nv50_context
*nv50
)
2220 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2221 struct nv50_program
*p
= nv50
->vertprog
;
2222 struct nouveau_stateobj
*so
;
2224 if (!p
->translated
) {
2225 nv50_program_validate(nv50
, p
);
2230 nv50_program_validate_data(nv50
, p
);
2231 nv50_program_validate_code(nv50
, p
);
2234 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
2235 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
2236 NOUVEAU_BO_HIGH
, 0, 0);
2237 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
2238 NOUVEAU_BO_LOW
, 0, 0);
2239 so_method(so
, tesla
, 0x1650, 2);
2240 so_data (so
, p
->cfg
.vp
.attr
[0]);
2241 so_data (so
, p
->cfg
.vp
.attr
[1]);
2242 so_method(so
, tesla
, 0x16b8, 1);
2243 so_data (so
, p
->cfg
.high_result
);
2244 so_method(so
, tesla
, 0x16ac, 2);
2245 so_data (so
, p
->cfg
.high_result
); //8);
2246 so_data (so
, p
->cfg
.high_temp
);
2247 so_method(so
, tesla
, 0x140c, 1);
2248 so_data (so
, 0); /* program start offset */
2249 so_ref(so
, &nv50
->state
.vertprog
);
2254 nv50_fragprog_validate(struct nv50_context
*nv50
)
2256 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2257 struct nv50_program
*p
= nv50
->fragprog
;
2258 struct nouveau_stateobj
*so
;
2261 if (!p
->translated
) {
2262 nv50_program_validate(nv50
, p
);
2267 nv50_program_validate_data(nv50
, p
);
2268 nv50_program_validate_code(nv50
, p
);
2271 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
2272 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
2273 NOUVEAU_BO_HIGH
, 0, 0);
2274 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
2275 NOUVEAU_BO_LOW
, 0, 0);
2276 so_method(so
, tesla
, 0x1904, 4);
2277 so_data (so
, p
->cfg
.fp
.regs
[0]); /* 0x01000404 / 0x00040404 */
2278 so_data (so
, 0x00000004);
2279 so_data (so
, 0x00000000);
2280 so_data (so
, 0x00000000);
2281 so_method(so
, tesla
, 0x16bc, p
->cfg
.fp
.high_map
);
2282 for (i
= 0; i
< p
->cfg
.fp
.high_map
; i
++)
2283 so_data(so
, p
->cfg
.fp
.map
[i
]);
2284 so_method(so
, tesla
, 0x1988, 2);
2285 so_data (so
, p
->cfg
.fp
.regs
[1]); /* 0x08040404 / 0x0f000401 */
2286 so_data (so
, p
->cfg
.high_temp
);
2287 so_method(so
, tesla
, 0x1298, 1);
2288 so_data (so
, p
->cfg
.high_result
);
2289 so_method(so
, tesla
, 0x19a8, 1);
2290 so_data (so
, p
->cfg
.fp
.regs
[2]);
2291 so_method(so
, tesla
, 0x196c, 1);
2292 so_data (so
, p
->cfg
.fp
.regs
[3]);
2293 so_method(so
, tesla
, 0x1414, 1);
2294 so_data (so
, 0); /* program start offset */
2295 so_ref(so
, &nv50
->state
.fragprog
);
2300 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
2302 struct pipe_screen
*pscreen
= nv50
->pipe
.screen
;
2304 while (p
->exec_head
) {
2305 struct nv50_program_exec
*e
= p
->exec_head
;
2307 p
->exec_head
= e
->next
;
2310 p
->exec_tail
= NULL
;
2314 pipe_buffer_reference(&p
->buffer
, NULL
);
2316 nv50
->screen
->nvws
->res_free(&p
->data
);