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_SU_MAX_ADDR 7
36 //#define NV50_PROGRAM_DUMP
38 /* ARL - gallium craps itself on progs/vp/arl.txt
40 * MSB - Like MAD, but MUL+SUB
41 * - Fuck it off, introduce a way to negate args for ops that
44 * Look into inlining IMMD for ops other than MOV (make it general?)
45 * - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
46 * but can emit to P_TEMP first - then MOV later. NVIDIA does this
48 * In ops such as ADD it's possible to construct a bad opcode in the !is_long()
49 * case, if the emit_src() causes the inst to suddenly become long.
51 * Verify half-insns work where expected - and force disable them where they
52 * don't work - MUL has it forcibly disabled atm as it fixes POW..
54 * FUCK! watch dst==src vectors, can overwrite components that are needed.
55 * ie. SUB R0, R0.yzxw, R0
57 * Things to check with renouveau:
58 * FP attr/result assignment - how?
60 * - 0x16bc maps vp output onto fp hpos
61 * - 0x16c0 maps vp output onto fp col0
65 * 0x16bc->0x16e8 --> some binding between vp/fp regs
66 * 0x16b8 --> VP output count
68 * 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
69 * "MOV rcol.x, fcol.y" = 0x00000004
70 * 0x19a8 --> as above but 0x00000100 and 0x00000000
71 * - 0x00100000 used when KIL used
72 * 0x196c --> as above but 0x00000011 and 0x00000000
74 * 0x1988 --> 0xXXNNNNNN
75 * - XX == FP high something
91 int rhw
; /* result hw for FP outputs, or interpolant index */
92 int acc
; /* instruction where this reg is last read (first insn == 1) */
95 /* arbitrary limits */
96 #define MAX_IF_DEPTH 4
97 #define MAX_LOOP_DEPTH 4
100 struct nv50_program
*p
;
103 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
104 struct nv50_reg r_addr
[NV50_SU_MAX_ADDR
];
107 struct nv50_reg
*temp
;
109 struct nv50_reg
*attr
;
111 struct nv50_reg
*result
;
113 struct nv50_reg
*param
;
115 struct nv50_reg
*immd
;
118 struct nv50_reg
**addr
;
121 struct nv50_reg
*temp_temp
[16];
122 unsigned temp_temp_nr
;
124 /* broadcast and destination replacement regs */
125 struct nv50_reg
*r_brdc
;
126 struct nv50_reg
*r_dst
[4];
128 unsigned interp_mode
[32];
129 /* perspective interpolation registers */
130 struct nv50_reg
*iv_p
;
131 struct nv50_reg
*iv_c
;
133 struct nv50_program_exec
*if_cond
;
134 struct nv50_program_exec
*if_insn
[MAX_IF_DEPTH
];
135 struct nv50_program_exec
*br_join
[MAX_IF_DEPTH
];
136 struct nv50_program_exec
*br_loop
[MAX_LOOP_DEPTH
]; /* for BRK branch */
137 int if_lvl
, loop_lvl
;
138 unsigned loop_pos
[MAX_LOOP_DEPTH
];
140 /* current instruction and total number of insns */
148 ctor_reg(struct nv50_reg
*reg
, unsigned type
, int index
, int hw
)
158 static INLINE
unsigned
159 popcnt4(uint32_t val
)
161 static const unsigned cnt
[16]
162 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
163 return cnt
[val
& 0xf];
167 terminate_mbb(struct nv50_pc
*pc
)
171 /* remove records of temporary address register values */
172 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
173 if (pc
->r_addr
[i
].index
< 0)
174 pc
->r_addr
[i
].rhw
= -1;
178 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
182 if (reg
->type
== P_RESULT
) {
183 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
184 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
187 if (reg
->type
!= P_TEMP
)
191 /*XXX: do this here too to catch FP temp-as-attr usage..
192 * not clean, but works */
193 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
194 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
198 if (reg
->rhw
!= -1) {
199 /* try to allocate temporary with index rhw first */
200 if (!(pc
->r_temp
[reg
->rhw
])) {
201 pc
->r_temp
[reg
->rhw
] = reg
;
203 if (pc
->p
->cfg
.high_temp
< (reg
->rhw
+ 1))
204 pc
->p
->cfg
.high_temp
= reg
->rhw
+ 1;
207 /* make sure we don't get things like $r0 needs to go
208 * in $r1 and $r1 in $r0
210 i
= pc
->result_nr
* 4;
213 for (; i
< NV50_SU_MAX_TEMP
; i
++) {
214 if (!(pc
->r_temp
[i
])) {
217 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
218 pc
->p
->cfg
.high_temp
= i
+ 1;
226 /* XXX: For shaders that aren't executed linearly (e.g. shaders that
227 * contain loops), we need to assign all hw regs to TGSI TEMPs early,
228 * lest we risk temp_temps overwriting regs alloc'd "later".
230 static struct nv50_reg
*
231 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
236 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
239 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
240 if (!pc
->r_temp
[i
]) {
241 r
= MALLOC_STRUCT(nv50_reg
);
242 ctor_reg(r
, P_TEMP
, -1, i
);
252 /* Assign the hw of the discarded temporary register src
253 * to the tgsi register dst and free src.
256 assimilate_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
258 assert(src
->index
== -1 && src
->hw
!= -1);
261 pc
->r_temp
[dst
->hw
] = NULL
;
262 pc
->r_temp
[src
->hw
] = dst
;
268 /* release the hardware resource held by r */
270 release_hw(struct nv50_pc
*pc
, struct nv50_reg
*r
)
272 assert(r
->type
== P_TEMP
);
276 assert(pc
->r_temp
[r
->hw
] == r
);
277 pc
->r_temp
[r
->hw
] = NULL
;
285 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
287 if (r
->index
== -1) {
290 FREE(pc
->r_temp
[hw
]);
291 pc
->r_temp
[hw
] = NULL
;
296 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
300 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
303 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
304 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
305 return alloc_temp4(pc
, dst
, idx
+ 4);
307 for (i
= 0; i
< 4; i
++) {
308 dst
[i
] = MALLOC_STRUCT(nv50_reg
);
309 ctor_reg(dst
[i
], P_TEMP
, -1, idx
+ i
);
310 pc
->r_temp
[idx
+ i
] = dst
[i
];
317 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
321 for (i
= 0; i
< 4; i
++)
322 free_temp(pc
, reg
[i
]);
325 static struct nv50_reg
*
326 temp_temp(struct nv50_pc
*pc
)
328 if (pc
->temp_temp_nr
>= 16)
331 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
332 return pc
->temp_temp
[pc
->temp_temp_nr
++];
336 kill_temp_temp(struct nv50_pc
*pc
)
340 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
341 free_temp(pc
, pc
->temp_temp
[i
]);
342 pc
->temp_temp_nr
= 0;
346 ctor_immd(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
348 pc
->immd_buf
= REALLOC(pc
->immd_buf
, (pc
->immd_nr
* 4 * sizeof(float)),
349 (pc
->immd_nr
+ 1) * 4 * sizeof(float));
350 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
351 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
352 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
353 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
355 return pc
->immd_nr
++;
358 static struct nv50_reg
*
359 alloc_immd(struct nv50_pc
*pc
, float f
)
361 struct nv50_reg
*r
= MALLOC_STRUCT(nv50_reg
);
364 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
365 if (pc
->immd_buf
[hw
] == f
)
368 if (hw
== pc
->immd_nr
* 4)
369 hw
= ctor_immd(pc
, f
, -f
, 0.5 * f
, 0) * 4;
371 ctor_reg(r
, P_IMMD
, -1, hw
);
375 static struct nv50_program_exec
*
376 exec(struct nv50_pc
*pc
)
378 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
385 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
387 struct nv50_program
*p
= pc
->p
;
390 p
->exec_tail
->next
= e
;
394 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
397 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
400 is_long(struct nv50_program_exec
*e
)
408 is_immd(struct nv50_program_exec
*e
)
410 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
416 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
417 struct nv50_program_exec
*e
)
420 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
421 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
425 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
426 struct nv50_program_exec
*e
)
429 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
430 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
434 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
440 set_pred(pc
, 0xf, 0, e
);
441 set_pred_wr(pc
, 0, 0, e
);
445 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
447 if (dst
->type
== P_RESULT
) {
449 e
->inst
[1] |= 0x00000008;
453 e
->inst
[0] |= (dst
->hw
<< 2);
457 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
459 float f
= pc
->immd_buf
[imm
->hw
];
460 unsigned val
= fui(imm
->neg
? -f
: f
);
463 /*XXX: can't be predicated - bits overlap.. catch cases where both
464 * are required and avoid them. */
465 set_pred(pc
, 0, 0, e
);
466 set_pred_wr(pc
, 0, 0, e
);
468 e
->inst
[1] |= 0x00000002 | 0x00000001;
469 e
->inst
[0] |= (val
& 0x3f) << 16;
470 e
->inst
[1] |= (val
>> 6) << 2;
474 emit_set_addr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, unsigned val
)
476 struct nv50_program_exec
*e
= exec(pc
);
478 assert(val
<= 0xffff);
479 e
->inst
[0] = 0xd0000000 | ((val
& 0xffff) << 9);
480 e
->inst
[1] = 0x20000000;
481 e
->inst
[0] |= dst
->hw
<< 2;
487 static struct nv50_reg
*
488 alloc_addr(struct nv50_pc
*pc
, struct nv50_reg
*ref
)
491 struct nv50_reg
*a
= NULL
;
494 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
) {
495 if (pc
->r_addr
[i
].index
>= 0)
497 if (pc
->r_addr
[i
].rhw
>= 0 &&
498 pc
->r_addr
[i
].acc
== pc
->insn_cur
)
501 pc
->r_addr
[i
].rhw
= -1;
502 pc
->r_addr
[i
].index
= i
;
503 return &pc
->r_addr
[i
];
509 for (i
= NV50_SU_MAX_ADDR
- 1; i
>= 0; --i
) {
510 if (pc
->r_addr
[i
].index
>= 0) /* occupied for TGSI */
512 if (pc
->r_addr
[i
].rhw
< 0) { /* unused */
516 if (!a
&& pc
->r_addr
[i
].acc
!= pc
->insn_cur
)
519 if (ref
->hw
- pc
->r_addr
[i
].rhw
< 128) {
520 /* alloc'd & suitable */
521 pc
->r_addr
[i
].acc
= pc
->insn_cur
;
522 return &pc
->r_addr
[i
];
526 emit_set_addr(pc
, a
, ref
->hw
* 4);
528 a
->rhw
= ref
->hw
% 128;
529 a
->acc
= pc
->insn_cur
;
533 #define INTERP_LINEAR 0
534 #define INTERP_FLAT 1
535 #define INTERP_PERSPECTIVE 2
536 #define INTERP_CENTROID 4
538 /* interpolant index has been stored in dst->rhw */
540 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*iv
,
543 assert(dst
->rhw
!= -1);
544 struct nv50_program_exec
*e
= exec(pc
);
546 e
->inst
[0] |= 0x80000000;
548 e
->inst
[0] |= (dst
->rhw
<< 16);
550 if (mode
& INTERP_FLAT
) {
551 e
->inst
[0] |= (1 << 8);
553 if (mode
& INTERP_PERSPECTIVE
) {
554 e
->inst
[0] |= (1 << 25);
556 e
->inst
[0] |= (iv
->hw
<< 9);
559 if (mode
& INTERP_CENTROID
)
560 e
->inst
[0] |= (1 << 24);
567 set_addr(struct nv50_program_exec
*e
, struct nv50_reg
*a
)
569 assert(!(e
->inst
[0] & 0x0c000000));
570 assert(!(e
->inst
[1] & 0x00000004));
572 e
->inst
[0] |= (a
->hw
& 3) << 26;
573 e
->inst
[1] |= (a
->hw
>> 2) << 2;
577 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
578 struct nv50_program_exec
*e
)
582 e
->param
.index
= src
->hw
;
584 e
->param
.mask
= m
<< (s
% 32);
587 set_addr(e
, alloc_addr(pc
, src
));
590 assert(src
->type
== P_CONST
);
591 set_addr(e
, pc
->addr
[src
->index
]);
594 e
->inst
[1] |= (((src
->type
== P_IMMD
) ? 0 : 1) << 22);
598 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
600 struct nv50_program_exec
*e
= exec(pc
);
602 e
->inst
[0] = 0x10000000;
608 if (!is_long(e
) && src
->type
== P_IMMD
) {
609 set_immd(pc
, src
, e
);
610 /*XXX: 32-bit, but steals part of "half" reg space - need to
611 * catch and handle this case if/when we do half-regs
614 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
616 set_data(pc
, src
, 0x7f, 9, e
);
617 e
->inst
[1] |= 0x20000000; /* src0 const? */
619 if (src
->type
== P_ATTR
) {
621 e
->inst
[1] |= 0x00200000;
625 e
->inst
[0] |= (src
->hw
<< 9);
628 if (is_long(e
) && !is_immd(e
)) {
629 e
->inst
[1] |= 0x04000000; /* 32-bit */
630 e
->inst
[1] |= 0x0000c000; /* "subsubop" 0x3 */
631 if (!(e
->inst
[1] & 0x20000000))
632 e
->inst
[1] |= 0x00030000; /* "subsubop" 0xf */
634 e
->inst
[0] |= 0x00008000;
640 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
642 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
643 emit_mov(pc
, dst
, imm
);
648 check_swap_src_0_1(struct nv50_pc
*pc
,
649 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
651 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
653 if (src0
->type
== P_CONST
) {
654 if (src1
->type
!= P_CONST
) {
660 if (src1
->type
== P_ATTR
) {
661 if (src0
->type
!= P_ATTR
) {
672 set_src_0_restricted(struct nv50_pc
*pc
, struct nv50_reg
*src
,
673 struct nv50_program_exec
*e
)
675 struct nv50_reg
*temp
;
677 if (src
->type
!= P_TEMP
) {
678 temp
= temp_temp(pc
);
679 emit_mov(pc
, temp
, src
);
684 e
->inst
[0] |= (src
->hw
<< 9);
688 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
690 if (src
->type
== P_ATTR
) {
692 e
->inst
[1] |= 0x00200000;
694 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
695 struct nv50_reg
*temp
= temp_temp(pc
);
697 emit_mov(pc
, temp
, src
);
702 e
->inst
[0] |= (src
->hw
<< 9);
706 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
708 if (src
->type
== P_ATTR
) {
709 struct nv50_reg
*temp
= temp_temp(pc
);
711 emit_mov(pc
, temp
, src
);
714 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
715 assert(!(e
->inst
[0] & 0x00800000));
716 if (e
->inst
[0] & 0x01000000) {
717 struct nv50_reg
*temp
= temp_temp(pc
);
719 emit_mov(pc
, temp
, src
);
722 set_data(pc
, src
, 0x7f, 16, e
);
723 e
->inst
[0] |= 0x00800000;
728 e
->inst
[0] |= ((src
->hw
& 127) << 16);
732 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
736 if (src
->type
== P_ATTR
) {
737 struct nv50_reg
*temp
= temp_temp(pc
);
739 emit_mov(pc
, temp
, src
);
742 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
743 assert(!(e
->inst
[0] & 0x01000000));
744 if (e
->inst
[0] & 0x00800000) {
745 struct nv50_reg
*temp
= temp_temp(pc
);
747 emit_mov(pc
, temp
, src
);
750 set_data(pc
, src
, 0x7f, 32+14, e
);
751 e
->inst
[0] |= 0x01000000;
756 e
->inst
[1] |= ((src
->hw
& 127) << 14);
760 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
761 struct nv50_reg
*src1
)
763 struct nv50_program_exec
*e
= exec(pc
);
765 e
->inst
[0] |= 0xc0000000;
770 check_swap_src_0_1(pc
, &src0
, &src1
);
772 set_src_0(pc
, src0
, e
);
773 if (src1
->type
== P_IMMD
&& !is_long(e
)) {
775 e
->inst
[0] |= 0x00008000;
776 set_immd(pc
, src1
, e
);
778 set_src_1(pc
, src1
, e
);
779 if (src0
->neg
^ src1
->neg
) {
781 e
->inst
[1] |= 0x08000000;
783 e
->inst
[0] |= 0x00008000;
791 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
792 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
794 struct nv50_program_exec
*e
= exec(pc
);
796 e
->inst
[0] |= 0xb0000000;
798 check_swap_src_0_1(pc
, &src0
, &src1
);
800 if (!pc
->allow32
|| src0
->neg
|| src1
->neg
) {
802 e
->inst
[1] |= (src0
->neg
<< 26) | (src1
->neg
<< 27);
806 set_src_0(pc
, src0
, e
);
807 if (src1
->type
== P_CONST
|| src1
->type
== P_ATTR
|| is_long(e
))
808 set_src_2(pc
, src1
, e
);
810 if (src1
->type
== P_IMMD
)
811 set_immd(pc
, src1
, e
);
813 set_src_1(pc
, src1
, e
);
819 emit_arl(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
822 struct nv50_program_exec
*e
= exec(pc
);
825 e
->inst
[1] |= 0xc0000000;
827 e
->inst
[0] |= dst
->hw
<< 2;
828 e
->inst
[0] |= s
<< 16; /* shift left */
829 set_src_0_restricted(pc
, src
, e
);
835 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
836 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
838 struct nv50_program_exec
*e
= exec(pc
);
841 e
->inst
[0] |= 0xb0000000;
842 e
->inst
[1] |= (sub
<< 29);
844 check_swap_src_0_1(pc
, &src0
, &src1
);
846 set_src_0(pc
, src0
, e
);
847 set_src_1(pc
, src1
, e
);
853 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
854 struct nv50_reg
*src1
)
857 emit_add(pc
, dst
, src0
, src1
);
862 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
863 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
865 struct nv50_program_exec
*e
= exec(pc
);
867 e
->inst
[0] |= 0xe0000000;
869 check_swap_src_0_1(pc
, &src0
, &src1
);
871 set_src_0(pc
, src0
, e
);
872 set_src_1(pc
, src1
, e
);
873 set_src_2(pc
, src2
, e
);
875 if (src0
->neg
^ src1
->neg
)
876 e
->inst
[1] |= 0x04000000;
878 e
->inst
[1] |= 0x08000000;
884 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
885 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
888 emit_mad(pc
, dst
, src0
, src1
, src2
);
893 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
894 struct nv50_reg
*dst
, struct nv50_reg
*src
)
896 struct nv50_program_exec
*e
= exec(pc
);
898 e
->inst
[0] |= 0x90000000;
901 e
->inst
[1] |= (sub
<< 29);
906 if (sub
== 0 || sub
== 2)
907 set_src_0_restricted(pc
, src
, e
);
909 set_src_0(pc
, src
, e
);
915 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
917 struct nv50_program_exec
*e
= exec(pc
);
919 e
->inst
[0] |= 0xb0000000;
922 set_src_0(pc
, src
, e
);
924 e
->inst
[1] |= (6 << 29) | 0x00004000;
930 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
932 struct nv50_program_exec
*e
= exec(pc
);
934 e
->inst
[0] |= 0xb0000000;
937 set_src_0(pc
, src
, e
);
939 e
->inst
[1] |= (6 << 29);
944 #define CVTOP_RN 0x01
945 #define CVTOP_FLOOR 0x03
946 #define CVTOP_CEIL 0x05
947 #define CVTOP_TRUNC 0x07
948 #define CVTOP_SAT 0x08
949 #define CVTOP_ABS 0x10
951 /* 0x04 == 32 bit dst */
952 /* 0x40 == dst is float */
953 /* 0x80 == src is float */
954 #define CVT_F32_F32 0xc4
955 #define CVT_F32_S32 0x44
956 #define CVT_F32_U32 0x64
957 #define CVT_S32_F32 0x8c
958 #define CVT_S32_S32 0x0c
963 emit_cvt(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
964 int wp
, unsigned cvn
, unsigned fmt
)
966 struct nv50_program_exec
*e
;
971 e
->inst
[0] |= 0xa0000000;
972 e
->inst
[1] |= 0x00004000; /* 32 bit src */
973 e
->inst
[1] |= (cvn
<< 16);
974 e
->inst
[1] |= (fmt
<< 24);
975 set_src_0(pc
, src
, e
);
978 set_pred_wr(pc
, 1, wp
, e
);
983 e
->inst
[0] |= 0x000001fc;
984 e
->inst
[1] |= 0x00000008;
990 /* nv50 Condition codes:
997 * 0x7 = set condition code ? (used before bra.lt/le/gt/ge)
998 * 0x8 = unordered bit (allows NaN)
1001 emit_set(struct nv50_pc
*pc
, unsigned ccode
, struct nv50_reg
*dst
, int wp
,
1002 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
1004 static const unsigned cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
1006 struct nv50_program_exec
*e
= exec(pc
);
1007 struct nv50_reg
*rdst
;
1010 if (check_swap_src_0_1(pc
, &src0
, &src1
))
1011 ccode
= cc_swapped
[ccode
& 7] | (ccode
& 8);
1014 if (dst
&& dst
->type
!= P_TEMP
)
1015 dst
= alloc_temp(pc
, NULL
);
1019 e
->inst
[0] |= 0xb0000000;
1020 e
->inst
[1] |= 0x60000000 | (ccode
<< 14);
1022 /* XXX: decuda will disasm as .u16 and use .lo/.hi regs, but
1023 * that doesn't seem to match what the hw actually does
1024 e->inst[1] |= 0x04000000; << breaks things, u32 by default ?
1028 set_pred_wr(pc
, 1, wp
, e
);
1030 set_dst(pc
, dst
, e
);
1032 e
->inst
[0] |= 0x000001fc;
1033 e
->inst
[1] |= 0x00000008;
1036 set_src_0(pc
, src0
, e
);
1037 set_src_1(pc
, src1
, e
);
1040 pc
->if_cond
= pc
->p
->exec_tail
; /* record for OPCODE_IF */
1042 /* cvt.f32.u32/s32 (?) if we didn't only write the predicate */
1044 emit_cvt(pc
, rdst
, dst
, -1, CVTOP_ABS
| CVTOP_RN
, CVT_F32_S32
);
1045 if (rdst
&& rdst
!= dst
)
1049 static INLINE
unsigned
1050 map_tgsi_setop_cc(unsigned op
)
1053 case TGSI_OPCODE_SLT
: return 0x1;
1054 case TGSI_OPCODE_SGE
: return 0x6;
1055 case TGSI_OPCODE_SEQ
: return 0x2;
1056 case TGSI_OPCODE_SGT
: return 0x4;
1057 case TGSI_OPCODE_SLE
: return 0x3;
1058 case TGSI_OPCODE_SNE
: return 0xd;
1066 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1068 emit_cvt(pc
, dst
, src
, -1, CVTOP_FLOOR
, CVT_F32_F32
| CVT_RI
);
1072 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1073 struct nv50_reg
*v
, struct nv50_reg
*e
)
1075 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
1077 emit_flop(pc
, 3, temp
, v
);
1078 emit_mul(pc
, temp
, temp
, e
);
1079 emit_preex2(pc
, temp
, temp
);
1080 emit_flop(pc
, 6, dst
, temp
);
1082 free_temp(pc
, temp
);
1086 emit_abs(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1088 emit_cvt(pc
, dst
, src
, -1, CVTOP_ABS
, CVT_F32_F32
);
1092 emit_sat(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1094 emit_cvt(pc
, dst
, src
, -1, CVTOP_SAT
, CVT_F32_F32
);
1098 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1099 struct nv50_reg
**src
)
1101 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1102 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
1103 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
1104 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
1105 struct nv50_reg
*tmp
[4];
1106 boolean allow32
= pc
->allow32
;
1108 pc
->allow32
= FALSE
;
1110 if (mask
& (3 << 1)) {
1111 tmp
[0] = alloc_temp(pc
, NULL
);
1112 emit_minmax(pc
, 4, tmp
[0], src
[0], zero
);
1115 if (mask
& (1 << 2)) {
1116 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
1118 tmp
[1] = temp_temp(pc
);
1119 emit_minmax(pc
, 4, tmp
[1], src
[1], zero
);
1121 tmp
[3] = temp_temp(pc
);
1122 emit_minmax(pc
, 4, tmp
[3], src
[3], neg128
);
1123 emit_minmax(pc
, 5, tmp
[3], tmp
[3], pos128
);
1125 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
1126 emit_mov(pc
, dst
[2], zero
);
1127 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
1130 if (mask
& (1 << 1))
1131 assimilate_temp(pc
, dst
[1], tmp
[0]);
1133 if (mask
& (1 << 2))
1134 free_temp(pc
, tmp
[0]);
1136 pc
->allow32
= allow32
;
1138 /* do this last, in case src[i,j] == dst[0,3] */
1139 if (mask
& (1 << 0))
1140 emit_mov(pc
, dst
[0], one
);
1142 if (mask
& (1 << 3))
1143 emit_mov(pc
, dst
[3], one
);
1152 emit_neg(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1154 emit_cvt(pc
, dst
, src
, -1, CVTOP_RN
, CVT_F32_F32
| CVT_NEG
);
1158 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
1160 struct nv50_program_exec
*e
;
1161 const int r_pred
= 1;
1162 unsigned cvn
= CVT_F32_F32
;
1166 /* write predicate reg */
1167 emit_cvt(pc
, NULL
, src
, r_pred
, CVTOP_RN
, cvn
);
1169 /* conditional discard */
1171 e
->inst
[0] = 0x00000002;
1173 set_pred(pc
, 0x1 /* LT */, r_pred
, e
);
1178 emit_tex(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1179 struct nv50_reg
**src
, unsigned unit
, unsigned type
, boolean proj
)
1181 struct nv50_reg
*temp
, *t
[4];
1182 struct nv50_program_exec
*e
;
1184 unsigned c
, mode
, dim
;
1187 case TGSI_TEXTURE_1D
:
1190 case TGSI_TEXTURE_UNKNOWN
:
1191 case TGSI_TEXTURE_2D
:
1192 case TGSI_TEXTURE_SHADOW1D
: /* XXX: x, z */
1193 case TGSI_TEXTURE_RECT
:
1196 case TGSI_TEXTURE_3D
:
1197 case TGSI_TEXTURE_CUBE
:
1198 case TGSI_TEXTURE_SHADOW2D
:
1199 case TGSI_TEXTURE_SHADOWRECT
: /* XXX */
1207 /* some cards need t[0]'s hw index to be a multiple of 4 */
1208 alloc_temp4(pc
, t
, 0);
1211 if (src
[0]->type
== P_TEMP
&& src
[0]->rhw
!= -1) {
1212 mode
= pc
->interp_mode
[src
[0]->index
];
1214 t
[3]->rhw
= src
[3]->rhw
;
1215 emit_interp(pc
, t
[3], NULL
, (mode
& INTERP_CENTROID
));
1216 emit_flop(pc
, 0, t
[3], t
[3]);
1218 for (c
= 0; c
< dim
; c
++) {
1219 t
[c
]->rhw
= src
[c
]->rhw
;
1220 emit_interp(pc
, t
[c
], t
[3],
1221 (mode
| INTERP_PERSPECTIVE
));
1224 emit_flop(pc
, 0, t
[3], src
[3]);
1225 for (c
= 0; c
< dim
; c
++)
1226 emit_mul(pc
, t
[c
], src
[c
], t
[3]);
1228 /* XXX: for some reason the blob sometimes uses MAD:
1229 * emit_mad(pc, t[c], src[0][c], t[3], t[3])
1230 * pc->p->exec_tail->inst[1] |= 0x080fc000;
1234 if (type
== TGSI_TEXTURE_CUBE
) {
1235 temp
= temp_temp(pc
);
1236 emit_minmax(pc
, 4, temp
, src
[0], src
[1]);
1237 emit_minmax(pc
, 4, temp
, temp
, src
[2]);
1238 emit_flop(pc
, 0, temp
, temp
);
1239 for (c
= 0; c
< 3; c
++)
1240 emit_mul(pc
, t
[c
], src
[c
], temp
);
1242 for (c
= 0; c
< dim
; c
++)
1243 emit_mov(pc
, t
[c
], src
[c
]);
1249 e
->inst
[0] |= 0xf0000000;
1250 e
->inst
[1] |= 0x00000004;
1251 set_dst(pc
, t
[0], e
);
1252 e
->inst
[0] |= (unit
<< 9);
1255 e
->inst
[0] |= 0x00400000;
1258 e
->inst
[0] |= 0x00800000;
1260 e
->inst
[0] |= (mask
& 0x3) << 25;
1261 e
->inst
[1] |= (mask
& 0xc) << 12;
1267 if (mask
& 1) emit_mov(pc
, dst
[0], t
[c
++]);
1268 if (mask
& 2) emit_mov(pc
, dst
[1], t
[c
++]);
1269 if (mask
& 4) emit_mov(pc
, dst
[2], t
[c
++]);
1270 if (mask
& 8) emit_mov(pc
, dst
[3], t
[c
]);
1274 /* XXX: if p.e. MUL is used directly after TEX, it would still use
1275 * the texture coordinates, not the fetched values: latency ? */
1277 for (c
= 0; c
< 4; c
++) {
1278 if (mask
& (1 << c
))
1279 assimilate_temp(pc
, dst
[c
], t
[c
]);
1281 free_temp(pc
, t
[c
]);
1287 emit_branch(struct nv50_pc
*pc
, int pred
, unsigned cc
,
1288 struct nv50_program_exec
**join
)
1290 struct nv50_program_exec
*e
= exec(pc
);
1294 e
->inst
[0] |= 0xa0000002;
1301 e
->inst
[0] |= 0x10000002;
1303 set_pred(pc
, cc
, pred
, e
);
1308 emit_nop(struct nv50_pc
*pc
)
1310 struct nv50_program_exec
*e
= exec(pc
);
1312 e
->inst
[0] = 0xf0000000;
1314 e
->inst
[1] = 0xe0000000;
1319 emit_ddx(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1321 struct nv50_program_exec
*e
= exec(pc
);
1323 assert(src
->type
== P_TEMP
);
1325 e
->inst
[0] = 0xc0140000;
1326 e
->inst
[1] = 0x89800000;
1328 set_dst(pc
, dst
, e
);
1329 set_src_0(pc
, src
, e
);
1330 set_src_2(pc
, src
, e
);
1336 emit_ddy(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1338 struct nv50_program_exec
*e
= exec(pc
);
1340 assert(src
->type
== P_TEMP
);
1342 if (!src
->neg
) /* ! double negation */
1343 emit_neg(pc
, src
, src
);
1345 e
->inst
[0] = 0xc0150000;
1346 e
->inst
[1] = 0x8a400000;
1348 set_dst(pc
, dst
, e
);
1349 set_src_0(pc
, src
, e
);
1350 set_src_2(pc
, src
, e
);
1356 convert_to_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
1358 unsigned q
= 0, m
= ~0;
1360 assert(!is_long(e
));
1362 switch (e
->inst
[0] >> 28) {
1369 /* INTERP (move centroid, perspective and flat bits) */
1371 q
= (e
->inst
[0] & (3 << 24)) >> (24 - 16);
1372 q
|= (e
->inst
[0] & (1 << 8)) << (18 - 8);
1380 q
= ((e
->inst
[0] & (~m
)) >> 2);
1385 q
= ((e
->inst
[0] & (~m
)) << 12);
1388 /* MAD (if src2 == dst) */
1389 q
= ((e
->inst
[0] & 0x1fc) << 12);
1403 /* Some operations support an optional negation flag. */
1405 negate_supported(const struct tgsi_full_instruction
*insn
, int i
)
1409 switch (insn
->Instruction
.Opcode
) {
1410 case TGSI_OPCODE_DDY
:
1411 case TGSI_OPCODE_DP3
:
1412 case TGSI_OPCODE_DP4
:
1413 case TGSI_OPCODE_MUL
:
1414 case TGSI_OPCODE_KIL
:
1415 case TGSI_OPCODE_ADD
:
1416 case TGSI_OPCODE_SUB
:
1417 case TGSI_OPCODE_MAD
:
1419 case TGSI_OPCODE_POW
:
1427 /* Watch out for possible multiple uses of an nv50_reg, we
1428 * can't use nv50_reg::neg in these cases.
1430 for (s
= 0; s
< insn
->Instruction
.NumSrcRegs
; ++s
) {
1433 if ((insn
->FullSrcRegisters
[s
].SrcRegister
.Index
==
1434 insn
->FullSrcRegisters
[i
].SrcRegister
.Index
) &&
1435 (insn
->FullSrcRegisters
[s
].SrcRegister
.File
==
1436 insn
->FullSrcRegisters
[i
].SrcRegister
.File
))
1443 /* Return a read mask for source registers deduced from opcode & write mask. */
1445 nv50_tgsi_src_mask(const struct tgsi_full_instruction
*insn
, int c
)
1447 unsigned x
, mask
= insn
->FullDstRegisters
[0].DstRegister
.WriteMask
;
1449 switch (insn
->Instruction
.Opcode
) {
1450 case TGSI_OPCODE_COS
:
1451 case TGSI_OPCODE_SIN
:
1452 return (mask
& 0x8) | ((mask
& 0x7) ? 0x1 : 0x0);
1453 case TGSI_OPCODE_DP3
:
1455 case TGSI_OPCODE_DP4
:
1456 case TGSI_OPCODE_DPH
:
1457 case TGSI_OPCODE_KIL
: /* WriteMask ignored */
1459 case TGSI_OPCODE_DST
:
1460 return mask
& (c
? 0xa : 0x6);
1461 case TGSI_OPCODE_EX2
:
1462 case TGSI_OPCODE_LG2
:
1463 case TGSI_OPCODE_POW
:
1464 case TGSI_OPCODE_RCP
:
1465 case TGSI_OPCODE_RSQ
:
1466 case TGSI_OPCODE_SCS
:
1468 case TGSI_OPCODE_LIT
:
1470 case TGSI_OPCODE_TEX
:
1471 case TGSI_OPCODE_TXP
:
1473 const struct tgsi_instruction_ext_texture
*tex
;
1475 assert(insn
->Instruction
.Extended
);
1476 tex
= &insn
->InstructionExtTexture
;
1479 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1482 switch (tex
->Texture
) {
1483 case TGSI_TEXTURE_1D
:
1486 case TGSI_TEXTURE_2D
:
1494 case TGSI_OPCODE_XPD
:
1496 if (mask
& 1) x
|= 0x6;
1497 if (mask
& 2) x
|= 0x5;
1498 if (mask
& 4) x
|= 0x3;
1507 static struct nv50_reg
*
1508 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
1510 switch (dst
->DstRegister
.File
) {
1511 case TGSI_FILE_TEMPORARY
:
1512 return &pc
->temp
[dst
->DstRegister
.Index
* 4 + c
];
1513 case TGSI_FILE_OUTPUT
:
1514 return &pc
->result
[dst
->DstRegister
.Index
* 4 + c
];
1515 case TGSI_FILE_ADDRESS
:
1517 struct nv50_reg
*r
= pc
->addr
[dst
->DstRegister
.Index
* 4 + c
];
1519 r
= alloc_addr(pc
, NULL
);
1520 pc
->addr
[dst
->DstRegister
.Index
* 4 + c
] = r
;
1525 case TGSI_FILE_NULL
:
1534 static struct nv50_reg
*
1535 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
,
1538 struct nv50_reg
*r
= NULL
;
1539 struct nv50_reg
*temp
;
1540 unsigned sgn
, c
, swz
;
1542 if (src
->SrcRegister
.File
!= TGSI_FILE_CONSTANT
)
1543 assert(!src
->SrcRegister
.Indirect
);
1545 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1547 c
= tgsi_util_get_full_src_register_extswizzle(src
, chan
);
1549 case TGSI_EXTSWIZZLE_X
:
1550 case TGSI_EXTSWIZZLE_Y
:
1551 case TGSI_EXTSWIZZLE_Z
:
1552 case TGSI_EXTSWIZZLE_W
:
1553 switch (src
->SrcRegister
.File
) {
1554 case TGSI_FILE_INPUT
:
1555 r
= &pc
->attr
[src
->SrcRegister
.Index
* 4 + c
];
1557 case TGSI_FILE_TEMPORARY
:
1558 r
= &pc
->temp
[src
->SrcRegister
.Index
* 4 + c
];
1560 case TGSI_FILE_CONSTANT
:
1561 if (!src
->SrcRegister
.Indirect
) {
1562 r
= &pc
->param
[src
->SrcRegister
.Index
* 4 + c
];
1565 /* Indicate indirection by setting r->acc < 0 and
1566 * use the index field to select the address reg.
1568 r
= MALLOC_STRUCT(nv50_reg
);
1569 swz
= tgsi_util_get_src_register_swizzle(
1570 &src
->SrcRegisterInd
, 0);
1571 ctor_reg(r
, P_CONST
,
1572 src
->SrcRegisterInd
.Index
* 4 + swz
, c
);
1575 case TGSI_FILE_IMMEDIATE
:
1576 r
= &pc
->immd
[src
->SrcRegister
.Index
* 4 + c
];
1578 case TGSI_FILE_SAMPLER
:
1580 case TGSI_FILE_ADDRESS
:
1581 r
= pc
->addr
[src
->SrcRegister
.Index
* 4 + c
];
1589 case TGSI_EXTSWIZZLE_ZERO
:
1590 r
= alloc_immd(pc
, 0.0);
1592 case TGSI_EXTSWIZZLE_ONE
:
1593 if (sgn
== TGSI_UTIL_SIGN_TOGGLE
|| sgn
== TGSI_UTIL_SIGN_SET
)
1594 return alloc_immd(pc
, -1.0);
1595 return alloc_immd(pc
, 1.0);
1602 case TGSI_UTIL_SIGN_KEEP
:
1604 case TGSI_UTIL_SIGN_CLEAR
:
1605 temp
= temp_temp(pc
);
1606 emit_abs(pc
, temp
, r
);
1609 case TGSI_UTIL_SIGN_TOGGLE
:
1613 temp
= temp_temp(pc
);
1614 emit_neg(pc
, temp
, r
);
1618 case TGSI_UTIL_SIGN_SET
:
1619 temp
= temp_temp(pc
);
1620 emit_abs(pc
, temp
, r
);
1624 emit_neg(pc
, temp
, temp
);
1635 /* return TRUE for ops that produce only a single result */
1637 is_scalar_op(unsigned op
)
1640 case TGSI_OPCODE_COS
:
1641 case TGSI_OPCODE_DP2
:
1642 case TGSI_OPCODE_DP3
:
1643 case TGSI_OPCODE_DP4
:
1644 case TGSI_OPCODE_DPH
:
1645 case TGSI_OPCODE_EX2
:
1646 case TGSI_OPCODE_LG2
:
1647 case TGSI_OPCODE_POW
:
1648 case TGSI_OPCODE_RCP
:
1649 case TGSI_OPCODE_RSQ
:
1650 case TGSI_OPCODE_SIN
:
1652 case TGSI_OPCODE_KIL:
1653 case TGSI_OPCODE_LIT:
1654 case TGSI_OPCODE_SCS:
1662 /* Returns a bitmask indicating which dst components depend
1663 * on source s, component c (reverse of nv50_tgsi_src_mask).
1666 nv50_tgsi_dst_revdep(unsigned op
, int s
, int c
)
1668 if (is_scalar_op(op
))
1672 case TGSI_OPCODE_DST
:
1673 return (1 << c
) & (s
? 0xa : 0x6);
1674 case TGSI_OPCODE_XPD
:
1684 case TGSI_OPCODE_LIT
:
1685 case TGSI_OPCODE_SCS
:
1686 case TGSI_OPCODE_TEX
:
1687 case TGSI_OPCODE_TXP
:
1688 /* these take care of dangerous swizzles themselves */
1690 case TGSI_OPCODE_IF
:
1691 case TGSI_OPCODE_KIL
:
1692 /* don't call this function for these ops */
1696 /* linear vector instruction */
1701 static INLINE boolean
1702 has_pred(struct nv50_program_exec
*e
, unsigned cc
)
1704 if (!is_long(e
) || is_immd(e
))
1706 return ((e
->inst
[1] & 0x780) == (cc
<< 7));
1709 /* on ENDIF see if we can do "@p0.neu single_op" instead of:
1716 nv50_kill_branch(struct nv50_pc
*pc
)
1718 int lvl
= pc
->if_lvl
;
1720 if (pc
->if_insn
[lvl
]->next
!= pc
->p
->exec_tail
)
1723 /* if ccode == 'true', the BRA is from an ELSE and the predicate
1724 * reg may no longer be valid, since we currently always use $p0
1726 if (has_pred(pc
->if_insn
[lvl
], 0xf))
1728 assert(pc
->if_insn
[lvl
] && pc
->br_join
[lvl
]);
1730 /* We'll use the exec allocated for JOIN_AT (as we can't easily
1731 * update prev's next); if exec_tail is BRK, update the pointer.
1733 if (pc
->loop_lvl
&& pc
->br_loop
[pc
->loop_lvl
- 1] == pc
->p
->exec_tail
)
1734 pc
->br_loop
[pc
->loop_lvl
- 1] = pc
->br_join
[lvl
];
1736 pc
->p
->exec_size
-= 4; /* remove JOIN_AT and BRA */
1738 *pc
->br_join
[lvl
] = *pc
->p
->exec_tail
;
1740 FREE(pc
->if_insn
[lvl
]);
1741 FREE(pc
->p
->exec_tail
);
1743 pc
->p
->exec_tail
= pc
->br_join
[lvl
];
1744 pc
->p
->exec_tail
->next
= NULL
;
1745 set_pred(pc
, 0xd, 0, pc
->p
->exec_tail
);
1751 nv50_program_tx_insn(struct nv50_pc
*pc
,
1752 const struct tgsi_full_instruction
*inst
)
1754 struct nv50_reg
*rdst
[4], *dst
[4], *brdc
, *src
[3][4], *temp
;
1755 unsigned mask
, sat
, unit
;
1758 mask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
1759 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
1761 memset(src
, 0, sizeof(src
));
1763 for (c
= 0; c
< 4; c
++) {
1764 if ((mask
& (1 << c
)) && !pc
->r_dst
[c
])
1765 dst
[c
] = tgsi_dst(pc
, c
, &inst
->FullDstRegisters
[0]);
1767 dst
[c
] = pc
->r_dst
[c
];
1771 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1772 const struct tgsi_full_src_register
*fs
= &inst
->FullSrcRegisters
[i
];
1776 src_mask
= nv50_tgsi_src_mask(inst
, i
);
1777 neg_supp
= negate_supported(inst
, i
);
1779 if (fs
->SrcRegister
.File
== TGSI_FILE_SAMPLER
)
1780 unit
= fs
->SrcRegister
.Index
;
1782 for (c
= 0; c
< 4; c
++)
1783 if (src_mask
& (1 << c
))
1784 src
[i
][c
] = tgsi_src(pc
, c
, fs
, neg_supp
);
1787 brdc
= temp
= pc
->r_brdc
;
1788 if (brdc
&& brdc
->type
!= P_TEMP
) {
1789 temp
= temp_temp(pc
);
1794 for (c
= 0; c
< 4; c
++) {
1795 if (!(mask
& (1 << c
)) || dst
[c
]->type
== P_TEMP
)
1798 dst
[c
] = temp_temp(pc
);
1802 assert(brdc
|| !is_scalar_op(inst
->Instruction
.Opcode
));
1804 switch (inst
->Instruction
.Opcode
) {
1805 case TGSI_OPCODE_ABS
:
1806 for (c
= 0; c
< 4; c
++) {
1807 if (!(mask
& (1 << c
)))
1809 emit_abs(pc
, dst
[c
], src
[0][c
]);
1812 case TGSI_OPCODE_ADD
:
1813 for (c
= 0; c
< 4; c
++) {
1814 if (!(mask
& (1 << c
)))
1816 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1819 case TGSI_OPCODE_ARL
:
1821 temp
= temp_temp(pc
);
1822 emit_cvt(pc
, temp
, src
[0][0], -1, CVTOP_FLOOR
, CVT_S32_F32
);
1823 emit_arl(pc
, dst
[0], temp
, 4);
1825 case TGSI_OPCODE_BGNLOOP
:
1826 pc
->loop_pos
[pc
->loop_lvl
++] = pc
->p
->exec_size
;
1829 case TGSI_OPCODE_BRK
:
1830 emit_branch(pc
, -1, 0, NULL
);
1831 assert(pc
->loop_lvl
> 0);
1832 pc
->br_loop
[pc
->loop_lvl
- 1] = pc
->p
->exec_tail
;
1834 case TGSI_OPCODE_CEIL
:
1835 for (c
= 0; c
< 4; c
++) {
1836 if (!(mask
& (1 << c
)))
1838 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
1839 CVTOP_CEIL
, CVT_F32_F32
| CVT_RI
);
1842 case TGSI_OPCODE_CMP
:
1843 pc
->allow32
= FALSE
;
1844 for (c
= 0; c
< 4; c
++) {
1845 if (!(mask
& (1 << c
)))
1847 emit_cvt(pc
, NULL
, src
[0][c
], 1, CVTOP_RN
, CVT_F32_F32
);
1848 emit_mov(pc
, dst
[c
], src
[1][c
]);
1849 set_pred(pc
, 0x1, 1, pc
->p
->exec_tail
); /* @SF */
1850 emit_mov(pc
, dst
[c
], src
[2][c
]);
1851 set_pred(pc
, 0x6, 1, pc
->p
->exec_tail
); /* @NSF */
1854 case TGSI_OPCODE_COS
:
1856 emit_precossin(pc
, temp
, src
[0][3]);
1857 emit_flop(pc
, 5, dst
[3], temp
);
1861 temp
= brdc
= temp_temp(pc
);
1863 emit_precossin(pc
, temp
, src
[0][0]);
1864 emit_flop(pc
, 5, brdc
, temp
);
1866 case TGSI_OPCODE_DDX
:
1867 for (c
= 0; c
< 4; c
++) {
1868 if (!(mask
& (1 << c
)))
1870 emit_ddx(pc
, dst
[c
], src
[0][c
]);
1873 case TGSI_OPCODE_DDY
:
1874 for (c
= 0; c
< 4; c
++) {
1875 if (!(mask
& (1 << c
)))
1877 emit_ddy(pc
, dst
[c
], src
[0][c
]);
1880 case TGSI_OPCODE_DP3
:
1881 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1882 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1883 emit_mad(pc
, brdc
, src
[0][2], src
[1][2], temp
);
1885 case TGSI_OPCODE_DP4
:
1886 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1887 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1888 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1889 emit_mad(pc
, brdc
, src
[0][3], src
[1][3], temp
);
1891 case TGSI_OPCODE_DPH
:
1892 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1893 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1894 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1895 emit_add(pc
, brdc
, src
[1][3], temp
);
1897 case TGSI_OPCODE_DST
:
1898 if (mask
& (1 << 1))
1899 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
1900 if (mask
& (1 << 2))
1901 emit_mov(pc
, dst
[2], src
[0][2]);
1902 if (mask
& (1 << 3))
1903 emit_mov(pc
, dst
[3], src
[1][3]);
1904 if (mask
& (1 << 0))
1905 emit_mov_immdval(pc
, dst
[0], 1.0f
);
1907 case TGSI_OPCODE_ELSE
:
1908 emit_branch(pc
, -1, 0, NULL
);
1909 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
1910 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
1913 case TGSI_OPCODE_ENDIF
:
1914 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
1916 /* try to replace branch over 1 insn with a predicated insn */
1917 if (nv50_kill_branch(pc
) == TRUE
)
1920 if (pc
->br_join
[pc
->if_lvl
]) {
1921 pc
->br_join
[pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
1922 pc
->br_join
[pc
->if_lvl
] = NULL
;
1925 /* emit a NOP as join point, we could set it on the next
1926 * one, but would have to make sure it is long and !immd
1929 pc
->p
->exec_tail
->inst
[1] |= 2;
1931 case TGSI_OPCODE_ENDLOOP
:
1932 emit_branch(pc
, -1, 0, NULL
);
1933 pc
->p
->exec_tail
->param
.index
= pc
->loop_pos
[--pc
->loop_lvl
];
1934 pc
->br_loop
[pc
->loop_lvl
]->param
.index
= pc
->p
->exec_size
;
1937 case TGSI_OPCODE_EX2
:
1938 emit_preex2(pc
, temp
, src
[0][0]);
1939 emit_flop(pc
, 6, brdc
, temp
);
1941 case TGSI_OPCODE_FLR
:
1942 for (c
= 0; c
< 4; c
++) {
1943 if (!(mask
& (1 << c
)))
1945 emit_flr(pc
, dst
[c
], src
[0][c
]);
1948 case TGSI_OPCODE_FRC
:
1949 temp
= temp_temp(pc
);
1950 for (c
= 0; c
< 4; c
++) {
1951 if (!(mask
& (1 << c
)))
1953 emit_flr(pc
, temp
, src
[0][c
]);
1954 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
1957 case TGSI_OPCODE_IF
:
1958 /* emitting a join_at may not be necessary */
1959 assert(pc
->if_lvl
< MAX_IF_DEPTH
);
1960 set_pred_wr(pc
, 1, 0, pc
->if_cond
);
1961 emit_branch(pc
, 0, 2, &pc
->br_join
[pc
->if_lvl
]);
1962 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
1965 case TGSI_OPCODE_KIL
:
1966 emit_kil(pc
, src
[0][0]);
1967 emit_kil(pc
, src
[0][1]);
1968 emit_kil(pc
, src
[0][2]);
1969 emit_kil(pc
, src
[0][3]);
1971 case TGSI_OPCODE_LIT
:
1972 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
1974 case TGSI_OPCODE_LG2
:
1975 emit_flop(pc
, 3, brdc
, src
[0][0]);
1977 case TGSI_OPCODE_LRP
:
1978 temp
= temp_temp(pc
);
1979 for (c
= 0; c
< 4; c
++) {
1980 if (!(mask
& (1 << c
)))
1982 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
1983 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
1986 case TGSI_OPCODE_MAD
:
1987 for (c
= 0; c
< 4; c
++) {
1988 if (!(mask
& (1 << c
)))
1990 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
1993 case TGSI_OPCODE_MAX
:
1994 for (c
= 0; c
< 4; c
++) {
1995 if (!(mask
& (1 << c
)))
1997 emit_minmax(pc
, 4, dst
[c
], src
[0][c
], src
[1][c
]);
2000 case TGSI_OPCODE_MIN
:
2001 for (c
= 0; c
< 4; c
++) {
2002 if (!(mask
& (1 << c
)))
2004 emit_minmax(pc
, 5, dst
[c
], src
[0][c
], src
[1][c
]);
2007 case TGSI_OPCODE_MOV
:
2008 case TGSI_OPCODE_SWZ
:
2009 for (c
= 0; c
< 4; c
++) {
2010 if (!(mask
& (1 << c
)))
2012 emit_mov(pc
, dst
[c
], src
[0][c
]);
2015 case TGSI_OPCODE_MUL
:
2016 for (c
= 0; c
< 4; c
++) {
2017 if (!(mask
& (1 << c
)))
2019 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2022 case TGSI_OPCODE_POW
:
2023 emit_pow(pc
, brdc
, src
[0][0], src
[1][0]);
2025 case TGSI_OPCODE_RCP
:
2026 emit_flop(pc
, 0, brdc
, src
[0][0]);
2028 case TGSI_OPCODE_RSQ
:
2029 emit_flop(pc
, 2, brdc
, src
[0][0]);
2031 case TGSI_OPCODE_SCS
:
2032 temp
= temp_temp(pc
);
2034 emit_precossin(pc
, temp
, src
[0][0]);
2035 if (mask
& (1 << 0))
2036 emit_flop(pc
, 5, dst
[0], temp
);
2037 if (mask
& (1 << 1))
2038 emit_flop(pc
, 4, dst
[1], temp
);
2039 if (mask
& (1 << 2))
2040 emit_mov_immdval(pc
, dst
[2], 0.0);
2041 if (mask
& (1 << 3))
2042 emit_mov_immdval(pc
, dst
[3], 1.0);
2044 case TGSI_OPCODE_SIN
:
2046 emit_precossin(pc
, temp
, src
[0][3]);
2047 emit_flop(pc
, 4, dst
[3], temp
);
2051 temp
= brdc
= temp_temp(pc
);
2053 emit_precossin(pc
, temp
, src
[0][0]);
2054 emit_flop(pc
, 4, brdc
, temp
);
2056 case TGSI_OPCODE_SLT
:
2057 case TGSI_OPCODE_SGE
:
2058 case TGSI_OPCODE_SEQ
:
2059 case TGSI_OPCODE_SGT
:
2060 case TGSI_OPCODE_SLE
:
2061 case TGSI_OPCODE_SNE
:
2062 i
= map_tgsi_setop_cc(inst
->Instruction
.Opcode
);
2063 for (c
= 0; c
< 4; c
++) {
2064 if (!(mask
& (1 << c
)))
2066 emit_set(pc
, i
, dst
[c
], -1, src
[0][c
], src
[1][c
]);
2069 case TGSI_OPCODE_SUB
:
2070 for (c
= 0; c
< 4; c
++) {
2071 if (!(mask
& (1 << c
)))
2073 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2076 case TGSI_OPCODE_TEX
:
2077 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2078 inst
->InstructionExtTexture
.Texture
, FALSE
);
2080 case TGSI_OPCODE_TXP
:
2081 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2082 inst
->InstructionExtTexture
.Texture
, TRUE
);
2084 case TGSI_OPCODE_TRUNC
:
2085 for (c
= 0; c
< 4; c
++) {
2086 if (!(mask
& (1 << c
)))
2088 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2089 CVTOP_TRUNC
, CVT_F32_F32
| CVT_RI
);
2092 case TGSI_OPCODE_XPD
:
2093 temp
= temp_temp(pc
);
2094 if (mask
& (1 << 0)) {
2095 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
2096 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
2098 if (mask
& (1 << 1)) {
2099 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
2100 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
2102 if (mask
& (1 << 2)) {
2103 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
2104 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
2106 if (mask
& (1 << 3))
2107 emit_mov_immdval(pc
, dst
[3], 1.0);
2109 case TGSI_OPCODE_END
:
2112 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
2118 emit_sat(pc
, brdc
, brdc
);
2119 for (c
= 0; c
< 4; c
++)
2120 if ((mask
& (1 << c
)) && dst
[c
] != brdc
)
2121 emit_mov(pc
, dst
[c
], brdc
);
2124 for (c
= 0; c
< 4; c
++) {
2125 if (!(mask
& (1 << c
)))
2127 /* in this case we saturate later */
2128 if (dst
[c
]->type
== P_TEMP
&& dst
[c
]->index
< 0)
2130 emit_sat(pc
, rdst
[c
], dst
[c
]);
2134 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
2135 for (c
= 0; c
< 4; c
++) {
2139 if (src
[i
][c
]->index
== -1 && src
[i
][c
]->type
== P_IMMD
)
2142 if (src
[i
][c
]->acc
< 0 && src
[i
][c
]->type
== P_CONST
)
2143 FREE(src
[i
][c
]); /* indirect constant */
2152 prep_inspect_insn(struct nv50_pc
*pc
, const struct tgsi_full_instruction
*insn
)
2154 struct nv50_reg
*reg
= NULL
;
2155 const struct tgsi_full_src_register
*src
;
2156 const struct tgsi_dst_register
*dst
;
2157 unsigned i
, c
, k
, mask
;
2159 dst
= &insn
->FullDstRegisters
[0].DstRegister
;
2160 mask
= dst
->WriteMask
;
2162 if (dst
->File
== TGSI_FILE_TEMPORARY
)
2165 if (dst
->File
== TGSI_FILE_OUTPUT
)
2169 for (c
= 0; c
< 4; c
++) {
2170 if (!(mask
& (1 << c
)))
2172 reg
[dst
->Index
* 4 + c
].acc
= pc
->insn_nr
;
2176 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2177 src
= &insn
->FullSrcRegisters
[i
];
2179 if (src
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
)
2182 if (src
->SrcRegister
.File
== TGSI_FILE_INPUT
)
2187 mask
= nv50_tgsi_src_mask(insn
, i
);
2189 for (c
= 0; c
< 4; c
++) {
2190 if (!(mask
& (1 << c
)))
2192 k
= tgsi_util_get_full_src_register_extswizzle(src
, c
);
2194 if (k
> TGSI_EXTSWIZZLE_W
)
2197 reg
[src
->SrcRegister
.Index
* 4 + k
].acc
= pc
->insn_nr
;
2202 /* Returns a bitmask indicating which dst components need to be
2203 * written to temporaries first to avoid 'corrupting' sources.
2205 * m[i] (out) indicate component to write in the i-th position
2206 * rdep[c] (in) bitmasks of dst[i] that require dst[c] as source
2209 nv50_revdep_reorder(unsigned m
[4], unsigned rdep
[4])
2211 unsigned i
, c
, x
, unsafe
;
2213 for (c
= 0; c
< 4; c
++)
2216 /* Swap as long as a dst component written earlier is depended on
2217 * by one written later, but the next one isn't depended on by it.
2219 for (c
= 0; c
< 3; c
++) {
2220 if (rdep
[m
[c
+ 1]] & (1 << m
[c
]))
2221 continue; /* if next one is depended on by us */
2222 for (i
= c
+ 1; i
< 4; i
++)
2223 /* if we are depended on by a later one */
2224 if (rdep
[m
[c
]] & (1 << m
[i
]))
2237 /* mark dependencies that could not be resolved by reordering */
2238 for (i
= 0; i
< 3; ++i
)
2239 for (c
= i
+ 1; c
< 4; ++c
)
2240 if (rdep
[m
[i
]] & (1 << m
[c
]))
2243 /* NOTE: $unsafe is with respect to order, not component */
2247 /* Select a suitable dst register for broadcasting scalar results,
2248 * or return NULL if we have to allocate an extra TEMP.
2250 * If e.g. only 1 component is written, we may also emit the final
2251 * result to a write-only register.
2253 static struct nv50_reg
*
2254 tgsi_broadcast_dst(struct nv50_pc
*pc
,
2255 const struct tgsi_full_dst_register
*fd
, unsigned mask
)
2257 if (fd
->DstRegister
.File
== TGSI_FILE_TEMPORARY
) {
2258 int c
= ffs(~mask
& fd
->DstRegister
.WriteMask
);
2260 return tgsi_dst(pc
, c
- 1, fd
);
2262 int c
= ffs(fd
->DstRegister
.WriteMask
) - 1;
2263 if ((1 << c
) == fd
->DstRegister
.WriteMask
)
2264 return tgsi_dst(pc
, c
, fd
);
2270 /* Scan source swizzles and return a bitmask indicating dst regs that
2271 * also occur among the src regs, and fill rdep for nv50_revdep_reoder.
2274 nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction
*insn
,
2277 const struct tgsi_full_dst_register
*fd
= &insn
->FullDstRegisters
[0];
2278 const struct tgsi_full_src_register
*fs
;
2279 unsigned i
, deqs
= 0;
2281 for (i
= 0; i
< 4; ++i
)
2284 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2285 unsigned chn
, mask
= nv50_tgsi_src_mask(insn
, i
);
2286 boolean neg_supp
= negate_supported(insn
, i
);
2288 fs
= &insn
->FullSrcRegisters
[i
];
2289 if (fs
->SrcRegister
.File
!= fd
->DstRegister
.File
||
2290 fs
->SrcRegister
.Index
!= fd
->DstRegister
.Index
)
2293 for (chn
= 0; chn
< 4; ++chn
) {
2296 if (!(mask
& (1 << chn
))) /* src is not read */
2298 c
= tgsi_util_get_full_src_register_extswizzle(fs
, chn
);
2299 s
= tgsi_util_get_full_src_register_sign_mode(fs
, chn
);
2301 if (c
> TGSI_EXTSWIZZLE_W
||
2302 !(fd
->DstRegister
.WriteMask
& (1 << c
)))
2305 /* no danger if src is copied to TEMP first */
2306 if ((s
!= TGSI_UTIL_SIGN_KEEP
) &&
2307 (s
!= TGSI_UTIL_SIGN_TOGGLE
|| !neg_supp
))
2310 rdep
[c
] |= nv50_tgsi_dst_revdep(
2311 insn
->Instruction
.Opcode
, i
, chn
);
2320 nv50_tgsi_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
2322 struct tgsi_full_instruction insn
= tok
->FullInstruction
;
2323 const struct tgsi_full_dst_register
*fd
;
2324 unsigned i
, deqs
, rdep
[4], m
[4];
2326 fd
= &tok
->FullInstruction
.FullDstRegisters
[0];
2327 deqs
= nv50_tgsi_scan_swizzle(&insn
, rdep
);
2329 if (is_scalar_op(insn
.Instruction
.Opcode
)) {
2330 pc
->r_brdc
= tgsi_broadcast_dst(pc
, fd
, deqs
);
2332 pc
->r_brdc
= temp_temp(pc
);
2333 return nv50_program_tx_insn(pc
, &insn
);
2338 return nv50_program_tx_insn(pc
, &insn
);
2340 deqs
= nv50_revdep_reorder(m
, rdep
);
2342 for (i
= 0; i
< 4; ++i
) {
2343 assert(pc
->r_dst
[m
[i
]] == NULL
);
2345 insn
.FullDstRegisters
[0].DstRegister
.WriteMask
=
2346 fd
->DstRegister
.WriteMask
& (1 << m
[i
]);
2348 if (!insn
.FullDstRegisters
[0].DstRegister
.WriteMask
)
2351 if (deqs
& (1 << i
))
2352 pc
->r_dst
[m
[i
]] = alloc_temp(pc
, NULL
);
2354 if (!nv50_program_tx_insn(pc
, &insn
))
2358 for (i
= 0; i
< 4; i
++) {
2359 struct nv50_reg
*reg
= pc
->r_dst
[i
];
2362 pc
->r_dst
[i
] = NULL
;
2364 if (insn
.Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
2365 emit_sat(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2367 emit_mov(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2375 load_interpolant(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
2377 struct nv50_reg
*iv
, **ppiv
;
2378 unsigned mode
= pc
->interp_mode
[reg
->index
];
2380 ppiv
= (mode
& INTERP_CENTROID
) ? &pc
->iv_c
: &pc
->iv_p
;
2383 if ((mode
& INTERP_PERSPECTIVE
) && !iv
) {
2384 iv
= *ppiv
= alloc_temp(pc
, NULL
);
2385 iv
->rhw
= popcnt4(pc
->p
->cfg
.regs
[1] >> 24) - 1;
2387 emit_interp(pc
, iv
, NULL
, mode
& INTERP_CENTROID
);
2388 emit_flop(pc
, 0, iv
, iv
);
2390 /* XXX: when loading interpolants dynamically, move these
2391 * to the program head, or make sure it can't be skipped.
2395 emit_interp(pc
, reg
, iv
, mode
);
2399 nv50_program_tx_prep(struct nv50_pc
*pc
)
2401 struct tgsi_parse_context tp
;
2402 struct nv50_program
*p
= pc
->p
;
2403 boolean ret
= FALSE
;
2404 unsigned i
, c
, flat_nr
= 0;
2406 tgsi_parse_init(&tp
, pc
->p
->pipe
.tokens
);
2407 while (!tgsi_parse_end_of_tokens(&tp
)) {
2408 const union tgsi_full_token
*tok
= &tp
.FullToken
;
2410 tgsi_parse_token(&tp
);
2411 switch (tok
->Token
.Type
) {
2412 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2414 const struct tgsi_full_immediate
*imm
=
2415 &tp
.FullToken
.FullImmediate
;
2417 ctor_immd(pc
, imm
->u
[0].Float
,
2423 case TGSI_TOKEN_TYPE_DECLARATION
:
2425 const struct tgsi_full_declaration
*d
;
2426 unsigned si
, last
, first
, mode
;
2428 d
= &tp
.FullToken
.FullDeclaration
;
2429 first
= d
->DeclarationRange
.First
;
2430 last
= d
->DeclarationRange
.Last
;
2432 switch (d
->Declaration
.File
) {
2433 case TGSI_FILE_TEMPORARY
:
2435 case TGSI_FILE_OUTPUT
:
2436 if (!d
->Declaration
.Semantic
||
2437 p
->type
== PIPE_SHADER_FRAGMENT
)
2440 si
= d
->Semantic
.SemanticIndex
;
2441 switch (d
->Semantic
.SemanticName
) {
2442 case TGSI_SEMANTIC_BCOLOR
:
2443 p
->cfg
.two_side
[si
].hw
= first
;
2444 if (p
->cfg
.io_nr
> first
)
2445 p
->cfg
.io_nr
= first
;
2447 case TGSI_SEMANTIC_PSIZE
:
2448 p
->cfg
.psiz
= first
;
2449 if (p
->cfg
.io_nr
> first
)
2450 p
->cfg
.io_nr
= first
;
2453 case TGSI_SEMANTIC_CLIP_DISTANCE:
2454 p->cfg.clpd = MIN2(p->cfg.clpd, first);
2461 case TGSI_FILE_INPUT
:
2463 if (p
->type
!= PIPE_SHADER_FRAGMENT
)
2466 switch (d
->Declaration
.Interpolate
) {
2467 case TGSI_INTERPOLATE_CONSTANT
:
2471 case TGSI_INTERPOLATE_PERSPECTIVE
:
2472 mode
= INTERP_PERSPECTIVE
;
2473 p
->cfg
.regs
[1] |= 0x08 << 24;
2476 mode
= INTERP_LINEAR
;
2479 if (d
->Declaration
.Centroid
)
2480 mode
|= INTERP_CENTROID
;
2483 for (i
= first
; i
<= last
; i
++)
2484 pc
->interp_mode
[i
] = mode
;
2487 case TGSI_FILE_ADDRESS
:
2488 case TGSI_FILE_CONSTANT
:
2489 case TGSI_FILE_SAMPLER
:
2492 NOUVEAU_ERR("bad decl file %d\n",
2493 d
->Declaration
.File
);
2498 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2500 prep_inspect_insn(pc
, &tok
->FullInstruction
);
2507 if (p
->type
== PIPE_SHADER_VERTEX
) {
2510 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
) {
2511 if (pc
->attr
[i
].acc
) {
2512 pc
->attr
[i
].hw
= rid
++;
2513 p
->cfg
.attr
[i
/ 32] |= 1 << (i
% 32);
2517 for (i
= 0, rid
= 0; i
< pc
->result_nr
; ++i
) {
2518 p
->cfg
.io
[i
].hw
= rid
;
2519 p
->cfg
.io
[i
].id_vp
= i
;
2521 for (c
= 0; c
< 4; ++c
) {
2523 if (!pc
->result
[n
].acc
)
2525 pc
->result
[n
].hw
= rid
++;
2526 p
->cfg
.io
[i
].mask
|= 1 << c
;
2530 for (c
= 0; c
< 2; ++c
)
2531 if (p
->cfg
.two_side
[c
].hw
< 0x40)
2532 p
->cfg
.two_side
[c
] = p
->cfg
.io
[
2533 p
->cfg
.two_side
[c
].hw
];
2535 if (p
->cfg
.psiz
< 0x40)
2536 p
->cfg
.psiz
= p
->cfg
.io
[p
->cfg
.psiz
].hw
;
2538 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
2540 unsigned n
= 0, m
= pc
->attr_nr
- flat_nr
;
2542 int base
= (TGSI_SEMANTIC_POSITION
==
2543 p
->info
.input_semantic_name
[0]) ? 0 : 1;
2545 /* non-flat interpolants have to be mapped to
2546 * the lower hardware IDs, so sort them:
2548 for (i
= 0; i
< pc
->attr_nr
; i
++) {
2549 if (pc
->interp_mode
[i
] == INTERP_FLAT
) {
2550 p
->cfg
.io
[m
].id_vp
= i
+ base
;
2551 p
->cfg
.io
[m
++].id_fp
= i
;
2553 if (!(pc
->interp_mode
[i
] & INTERP_PERSPECTIVE
))
2554 p
->cfg
.io
[n
].linear
= TRUE
;
2555 p
->cfg
.io
[n
].id_vp
= i
+ base
;
2556 p
->cfg
.io
[n
++].id_fp
= i
;
2560 if (!base
) /* set w-coordinate mask from perspective interp */
2561 p
->cfg
.io
[0].mask
|= p
->cfg
.regs
[1] >> 24;
2563 aid
= popcnt4( /* if fcrd isn't contained in cfg.io */
2564 base
? (p
->cfg
.regs
[1] >> 24) : p
->cfg
.io
[0].mask
);
2566 for (n
= 0; n
< pc
->attr_nr
; ++n
) {
2567 p
->cfg
.io
[n
].hw
= rid
= aid
;
2568 i
= p
->cfg
.io
[n
].id_fp
;
2570 for (c
= 0; c
< 4; ++c
) {
2571 if (!pc
->attr
[i
* 4 + c
].acc
)
2573 pc
->attr
[i
* 4 + c
].rhw
= rid
++;
2574 p
->cfg
.io
[n
].mask
|= 1 << c
;
2576 load_interpolant(pc
, &pc
->attr
[i
* 4 + c
]);
2578 aid
+= popcnt4(p
->cfg
.io
[n
].mask
);
2582 p
->cfg
.regs
[1] |= p
->cfg
.io
[0].mask
<< 24;
2584 m
= popcnt4(p
->cfg
.regs
[1] >> 24);
2586 /* set count of non-position inputs and of non-flat
2587 * non-position inputs for FP_INTERPOLANT_CTRL
2589 p
->cfg
.regs
[1] |= aid
- m
;
2592 i
= p
->cfg
.io
[pc
->attr_nr
- flat_nr
].hw
;
2593 p
->cfg
.regs
[1] |= (i
- m
) << 16;
2595 p
->cfg
.regs
[1] |= p
->cfg
.regs
[1] << 16;
2597 /* mark color semantic for light-twoside */
2599 for (i
= 0; i
< pc
->attr_nr
; i
++) {
2602 sn
= p
->info
.input_semantic_name
[p
->cfg
.io
[i
].id_fp
];
2603 si
= p
->info
.input_semantic_index
[p
->cfg
.io
[i
].id_fp
];
2605 if (sn
== TGSI_SEMANTIC_COLOR
) {
2606 p
->cfg
.two_side
[si
] = p
->cfg
.io
[i
];
2608 /* increase colour count */
2609 p
->cfg
.regs
[0] += popcnt4(
2610 p
->cfg
.two_side
[si
].mask
) << 16;
2612 n
= MIN2(n
, p
->cfg
.io
[i
].hw
- m
);
2616 p
->cfg
.regs
[0] += n
;
2618 /* Initialize FP results:
2619 * FragDepth is always first TGSI and last hw output
2621 i
= p
->info
.writes_z
? 4 : 0;
2622 for (rid
= 0; i
< pc
->result_nr
* 4; i
++)
2623 pc
->result
[i
].rhw
= rid
++;
2624 if (p
->info
.writes_z
)
2625 pc
->result
[2].rhw
= rid
;
2627 p
->cfg
.high_result
= rid
;
2633 pc
->immd
= MALLOC(pc
->immd_nr
* 4 * sizeof(struct nv50_reg
));
2637 for (i
= 0; i
< pc
->immd_nr
; i
++) {
2638 for (c
= 0; c
< 4; c
++, rid
++)
2639 ctor_reg(&pc
->immd
[rid
], P_IMMD
, i
, rid
);
2646 free_temp(pc
, pc
->iv_p
);
2648 free_temp(pc
, pc
->iv_c
);
2650 tgsi_parse_free(&tp
);
2655 free_nv50_pc(struct nv50_pc
*pc
)
2672 ctor_nv50_pc(struct nv50_pc
*pc
, struct nv50_program
*p
)
2675 unsigned rtype
[2] = { P_ATTR
, P_RESULT
};
2678 pc
->temp_nr
= p
->info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
2679 pc
->attr_nr
= p
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2680 pc
->result_nr
= p
->info
.file_max
[TGSI_FILE_OUTPUT
] + 1;
2681 pc
->param_nr
= p
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2682 pc
->addr_nr
= p
->info
.file_max
[TGSI_FILE_ADDRESS
] + 1;
2683 assert(pc
->addr_nr
<= 2);
2685 p
->cfg
.high_temp
= 4;
2687 p
->cfg
.two_side
[0].hw
= 0x40;
2688 p
->cfg
.two_side
[1].hw
= 0x40;
2691 case PIPE_SHADER_VERTEX
:
2694 p
->cfg
.io_nr
= pc
->result_nr
;
2696 case PIPE_SHADER_FRAGMENT
:
2697 rtype
[0] = rtype
[1] = P_TEMP
;
2699 p
->cfg
.regs
[0] = 0x01000004;
2700 p
->cfg
.io_nr
= pc
->attr_nr
;
2702 if (p
->info
.writes_z
) {
2703 p
->cfg
.regs
[2] |= 0x00000100;
2704 p
->cfg
.regs
[3] |= 0x00000011;
2706 if (p
->info
.uses_kill
)
2707 p
->cfg
.regs
[2] |= 0x00100000;
2712 pc
->temp
= MALLOC(pc
->temp_nr
* 4 * sizeof(struct nv50_reg
));
2716 for (i
= 0; i
< pc
->temp_nr
* 4; ++i
)
2717 ctor_reg(&pc
->temp
[i
], P_TEMP
, i
/ 4, -1);
2721 pc
->attr
= MALLOC(pc
->attr_nr
* 4 * sizeof(struct nv50_reg
));
2725 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
)
2726 ctor_reg(&pc
->attr
[i
], rtype
[0], i
/ 4, -1);
2729 if (pc
->result_nr
) {
2730 unsigned nr
= pc
->result_nr
* 4;
2732 pc
->result
= MALLOC(nr
* sizeof(struct nv50_reg
));
2736 for (i
= 0; i
< nr
; ++i
)
2737 ctor_reg(&pc
->result
[i
], rtype
[1], i
/ 4, -1);
2743 pc
->param
= MALLOC(pc
->param_nr
* 4 * sizeof(struct nv50_reg
));
2747 for (i
= 0; i
< pc
->param_nr
; ++i
)
2748 for (c
= 0; c
< 4; ++c
, ++rid
)
2749 ctor_reg(&pc
->param
[rid
], P_CONST
, i
, rid
);
2753 pc
->addr
= CALLOC(pc
->addr_nr
* 4, sizeof(struct nv50_reg
*));
2757 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
2758 ctor_reg(&pc
->r_addr
[i
], P_ADDR
, -1, i
+ 1);
2764 nv50_fp_move_results(struct nv50_pc
*pc
)
2766 struct nv50_reg reg
;
2769 ctor_reg(®
, P_TEMP
, -1, -1);
2771 for (i
= 0; i
< pc
->result_nr
* 4; ++i
) {
2772 if (pc
->result
[i
].rhw
< 0 || pc
->result
[i
].hw
< 0)
2774 if (pc
->result
[i
].rhw
!= pc
->result
[i
].hw
) {
2775 reg
.hw
= pc
->result
[i
].rhw
;
2776 emit_mov(pc
, ®
, &pc
->result
[i
]);
2782 nv50_program_fixup_insns(struct nv50_pc
*pc
)
2784 struct nv50_program_exec
*e
, *prev
= NULL
, **bra_list
;
2787 bra_list
= CALLOC(pc
->p
->exec_size
, sizeof(struct nv50_program_exec
*));
2789 /* Collect branch instructions, we need to adjust their offsets
2790 * when converting 32 bit instructions to 64 bit ones
2792 for (n
= 0, e
= pc
->p
->exec_head
; e
; e
= e
->next
)
2793 if (e
->param
.index
>= 0 && !e
->param
.mask
)
2796 /* Make sure we don't have any single 32 bit instructions. */
2797 for (e
= pc
->p
->exec_head
, pos
= 0; e
; e
= e
->next
) {
2798 pos
+= is_long(e
) ? 2 : 1;
2800 if ((pos
& 1) && (!e
->next
|| is_long(e
->next
))) {
2801 for (i
= 0; i
< n
; ++i
)
2802 if (bra_list
[i
]->param
.index
>= pos
)
2803 bra_list
[i
]->param
.index
+= 1;
2804 convert_to_long(pc
, e
);
2811 assert(!is_immd(pc
->p
->exec_head
));
2812 assert(!is_immd(pc
->p
->exec_tail
));
2814 /* last instruction must be long so it can have the end bit set */
2815 if (!is_long(pc
->p
->exec_tail
)) {
2816 convert_to_long(pc
, pc
->p
->exec_tail
);
2818 convert_to_long(pc
, prev
);
2820 assert(!(pc
->p
->exec_tail
->inst
[1] & 2));
2821 /* set the end-bit */
2822 pc
->p
->exec_tail
->inst
[1] |= 1;
2828 nv50_program_tx(struct nv50_program
*p
)
2830 struct tgsi_parse_context parse
;
2834 pc
= CALLOC_STRUCT(nv50_pc
);
2838 ret
= ctor_nv50_pc(pc
, p
);
2842 ret
= nv50_program_tx_prep(pc
);
2846 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
2847 while (!tgsi_parse_end_of_tokens(&parse
)) {
2848 const union tgsi_full_token
*tok
= &parse
.FullToken
;
2850 /* don't allow half insn/immd on first and last instruction */
2852 if (pc
->insn_cur
== 0 || pc
->insn_cur
+ 2 == pc
->insn_nr
)
2853 pc
->allow32
= FALSE
;
2855 tgsi_parse_token(&parse
);
2857 switch (tok
->Token
.Type
) {
2858 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2860 ret
= nv50_tgsi_insn(pc
, tok
);
2869 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
)
2870 nv50_fp_move_results(pc
);
2872 nv50_program_fixup_insns(pc
);
2874 p
->param_nr
= pc
->param_nr
* 4;
2875 p
->immd_nr
= pc
->immd_nr
* 4;
2876 p
->immd
= pc
->immd_buf
;
2879 tgsi_parse_free(&parse
);
2887 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
2889 if (nv50_program_tx(p
) == FALSE
)
2891 p
->translated
= TRUE
;
2895 nv50_program_upload_data(struct nv50_context
*nv50
, float *map
,
2896 unsigned start
, unsigned count
, unsigned cbuf
)
2898 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
2899 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2902 unsigned nr
= count
> 2047 ? 2047 : count
;
2904 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
2905 OUT_RING (chan
, (cbuf
<< 0) | (start
<< 8));
2906 BEGIN_RING(chan
, tesla
, NV50TCL_CB_DATA(0) | 0x40000000, nr
);
2907 OUT_RINGp (chan
, map
, nr
);
2916 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
2918 struct pipe_screen
*pscreen
= nv50
->pipe
.screen
;
2920 if (!p
->data
[0] && p
->immd_nr
) {
2921 struct nouveau_resource
*heap
= nv50
->screen
->immd_heap
[0];
2923 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
, &p
->data
[0])) {
2924 while (heap
->next
&& heap
->size
< p
->immd_nr
) {
2925 struct nv50_program
*evict
= heap
->next
->priv
;
2926 nouveau_resource_free(&evict
->data
[0]);
2929 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
,
2934 /* immediates only need to be uploaded again when freed */
2935 nv50_program_upload_data(nv50
, p
->immd
, p
->data
[0]->start
,
2936 p
->immd_nr
, NV50_CB_PMISC
);
2939 assert(p
->param_nr
<= 512);
2943 float *map
= pipe_buffer_map(pscreen
, nv50
->constbuf
[p
->type
],
2944 PIPE_BUFFER_USAGE_CPU_READ
);
2946 if (p
->type
== PIPE_SHADER_VERTEX
)
2951 nv50_program_upload_data(nv50
, map
, 0, p
->param_nr
, cb
);
2952 pipe_buffer_unmap(pscreen
, nv50
->constbuf
[p
->type
]);
2957 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
2959 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
2960 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
2961 struct nv50_program_exec
*e
;
2962 struct nouveau_stateobj
*so
;
2963 const unsigned flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
;
2964 unsigned start
, count
, *up
, *ptr
;
2965 boolean upload
= FALSE
;
2968 nouveau_bo_new(chan
->device
, NOUVEAU_BO_VRAM
, 0x100,
2969 p
->exec_size
* 4, &p
->bo
);
2973 if (p
->data
[0] && p
->data
[0]->start
!= p
->data_start
[0])
2979 for (e
= p
->exec_head
; e
; e
= e
->next
) {
2980 unsigned ei
, ci
, bs
;
2982 if (e
->param
.index
< 0)
2985 if (e
->param
.mask
== 0) {
2986 assert(!(e
->param
.index
& 1));
2987 /* seem to be 8 byte steps */
2988 ei
= (e
->param
.index
>> 1) + 0 /* START_ID */;
2990 e
->inst
[0] &= 0xf0000fff;
2991 e
->inst
[0] |= ei
<< 12;
2995 bs
= (e
->inst
[1] >> 22) & 0x07;
2997 ei
= e
->param
.shift
>> 5;
2998 ci
= e
->param
.index
;
3000 ci
+= p
->data
[bs
]->start
;
3002 e
->inst
[ei
] &= ~e
->param
.mask
;
3003 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
3007 p
->data_start
[0] = p
->data
[0]->start
;
3009 #ifdef NV50_PROGRAM_DUMP
3010 NOUVEAU_ERR("-------\n");
3011 for (e
= p
->exec_head
; e
; e
= e
->next
) {
3012 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
3014 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
3018 up
= ptr
= MALLOC(p
->exec_size
* 4);
3019 for (e
= p
->exec_head
; e
; e
= e
->next
) {
3020 *(ptr
++) = e
->inst
[0];
3022 *(ptr
++) = e
->inst
[1];
3026 so_method(so
, nv50
->screen
->tesla
, NV50TCL_CB_DEF_ADDRESS_HIGH
, 3);
3027 so_reloc (so
, p
->bo
, 0, flags
| NOUVEAU_BO_HIGH
, 0, 0);
3028 so_reloc (so
, p
->bo
, 0, flags
| NOUVEAU_BO_LOW
, 0, 0);
3029 so_data (so
, (NV50_CB_PUPLOAD
<< 16) | 0x0800); //(p->exec_size * 4));
3031 start
= 0; count
= p
->exec_size
;
3033 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
3038 nr
= MIN2(count
, 2047);
3039 nr
= MIN2(chan
->pushbuf
->remaining
, nr
);
3040 if (chan
->pushbuf
->remaining
< (nr
+ 3)) {
3045 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
3046 OUT_RING (chan
, (start
<< 8) | NV50_CB_PUPLOAD
);
3047 BEGIN_RING(chan
, tesla
, NV50TCL_CB_DATA(0) | 0x40000000, nr
);
3048 OUT_RINGp (chan
, up
+ start
, nr
);
3059 nv50_vertprog_validate(struct nv50_context
*nv50
)
3061 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3062 struct nv50_program
*p
= nv50
->vertprog
;
3063 struct nouveau_stateobj
*so
;
3065 if (!p
->translated
) {
3066 nv50_program_validate(nv50
, p
);
3071 nv50_program_validate_data(nv50
, p
);
3072 nv50_program_validate_code(nv50
, p
);
3075 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
3076 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3077 NOUVEAU_BO_HIGH
, 0, 0);
3078 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3079 NOUVEAU_BO_LOW
, 0, 0);
3080 so_method(so
, tesla
, NV50TCL_VP_ATTR_EN_0
, 2);
3081 so_data (so
, p
->cfg
.attr
[0]);
3082 so_data (so
, p
->cfg
.attr
[1]);
3083 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_RESULT
, 1);
3084 so_data (so
, p
->cfg
.high_result
);
3085 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 2);
3086 so_data (so
, p
->cfg
.high_result
); //8);
3087 so_data (so
, p
->cfg
.high_temp
);
3088 so_method(so
, tesla
, NV50TCL_VP_START_ID
, 1);
3089 so_data (so
, 0); /* program start offset */
3090 so_ref(so
, &nv50
->state
.vertprog
);
3095 nv50_fragprog_validate(struct nv50_context
*nv50
)
3097 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3098 struct nv50_program
*p
= nv50
->fragprog
;
3099 struct nouveau_stateobj
*so
;
3101 if (!p
->translated
) {
3102 nv50_program_validate(nv50
, p
);
3107 nv50_program_validate_data(nv50
, p
);
3108 nv50_program_validate_code(nv50
, p
);
3111 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
3112 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3113 NOUVEAU_BO_HIGH
, 0, 0);
3114 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3115 NOUVEAU_BO_LOW
, 0, 0);
3116 so_method(so
, tesla
, NV50TCL_FP_REG_ALLOC_TEMP
, 1);
3117 so_data (so
, p
->cfg
.high_temp
);
3118 so_method(so
, tesla
, NV50TCL_FP_RESULT_COUNT
, 1);
3119 so_data (so
, p
->cfg
.high_result
);
3120 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK19A8
, 1);
3121 so_data (so
, p
->cfg
.regs
[2]);
3122 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK196C
, 1);
3123 so_data (so
, p
->cfg
.regs
[3]);
3124 so_method(so
, tesla
, NV50TCL_FP_START_ID
, 1);
3125 so_data (so
, 0); /* program start offset */
3126 so_ref(so
, &nv50
->state
.fragprog
);
3131 nv50_pntc_replace(struct nv50_context
*nv50
, uint32_t pntc
[8], unsigned base
)
3133 struct nv50_program
*fp
= nv50
->fragprog
;
3134 struct nv50_program
*vp
= nv50
->vertprog
;
3135 unsigned i
, c
, m
= base
;
3137 /* XXX: This can't work correctly in all cases yet, we either
3138 * have to create TGSI_SEMANTIC_PNTC or sprite_coord_mode has
3139 * to be per FP input instead of per VP output
3141 memset(pntc
, 0, 8 * sizeof(uint32_t));
3143 for (i
= 0; i
< fp
->cfg
.io_nr
; i
++) {
3145 uint8_t j
= fp
->cfg
.io
[i
].id_vp
, k
= fp
->cfg
.io
[i
].id_fp
;
3146 unsigned n
= popcnt4(fp
->cfg
.io
[i
].mask
);
3148 if (fp
->info
.input_semantic_name
[k
] != TGSI_SEMANTIC_GENERIC
) {
3153 sn
= vp
->info
.input_semantic_name
[j
];
3154 si
= vp
->info
.input_semantic_index
[j
];
3156 if (j
< fp
->cfg
.io_nr
&& sn
== TGSI_SEMANTIC_GENERIC
) {
3158 nv50
->rasterizer
->pipe
.sprite_coord_mode
[si
];
3160 if (mode
== PIPE_SPRITE_COORD_NONE
) {
3166 /* this is either PointCoord or replaced by sprite coords */
3167 for (c
= 0; c
< 4; c
++) {
3168 if (!(fp
->cfg
.io
[i
].mask
& (1 << c
)))
3170 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
3177 nv50_sreg4_map(uint32_t *p_map
, int mid
, uint32_t lin
[4],
3178 struct nv50_sreg4
*fpi
, struct nv50_sreg4
*vpo
)
3181 uint8_t mv
= vpo
->mask
, mf
= fpi
->mask
, oid
= vpo
->hw
;
3182 uint8_t *map
= (uint8_t *)p_map
;
3184 for (c
= 0; c
< 4; ++c
) {
3186 if (fpi
->linear
== TRUE
)
3187 lin
[mid
/ 32] |= 1 << (mid
% 32);
3188 map
[mid
++] = (mv
& 1) ? oid
: ((c
== 3) ? 0x41 : 0x40);
3200 nv50_linkage_validate(struct nv50_context
*nv50
)
3202 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3203 struct nv50_program
*vp
= nv50
->vertprog
;
3204 struct nv50_program
*fp
= nv50
->fragprog
;
3205 struct nouveau_stateobj
*so
;
3206 struct nv50_sreg4 dummy
, *vpo
;
3208 uint32_t map
[16], lin
[4], reg
[5], pcrd
[8];
3210 memset(map
, 0, sizeof(map
));
3211 memset(lin
, 0, sizeof(lin
));
3213 reg
[1] = 0x00000004; /* low and high clip distance map ids */
3214 reg
[2] = 0x00000000; /* layer index map id (disabled, GP only) */
3215 reg
[3] = 0x00000000; /* point size map id & enable */
3216 reg
[0] = fp
->cfg
.regs
[0]; /* colour semantic reg */
3217 reg
[4] = fp
->cfg
.regs
[1]; /* interpolant info */
3219 dummy
.linear
= FALSE
;
3220 dummy
.mask
= 0xf; /* map all components of HPOS */
3221 m
= nv50_sreg4_map(map
, m
, lin
, &dummy
, &vp
->cfg
.io
[0]);
3225 if (vp
->cfg
.clpd
< 0x40) {
3226 for (c
= 0; c
< vp
->cfg
.clpd_nr
; ++c
)
3227 map
[m
++] = vp
->cfg
.clpd
+ c
;
3231 reg
[0] |= m
<< 8; /* adjust BFC0 id */
3233 /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
3234 if (nv50
->rasterizer
->pipe
.light_twoside
) {
3235 vpo
= &vp
->cfg
.two_side
[0];
3237 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[0], &vpo
[0]);
3238 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[1], &vpo
[1]);
3241 reg
[0] += m
- 4; /* adjust FFC0 id */
3242 reg
[4] |= m
<< 8; /* set mid where 'normal' FP inputs start */
3245 if (fp
->info
.input_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
3247 for (; i
< fp
->cfg
.io_nr
; i
++) {
3248 ubyte sn
= fp
->info
.input_semantic_name
[fp
->cfg
.io
[i
].id_fp
];
3249 ubyte si
= fp
->info
.input_semantic_index
[fp
->cfg
.io
[i
].id_fp
];
3251 n
= fp
->cfg
.io
[i
].id_vp
;
3252 if (n
>= vp
->cfg
.io_nr
||
3253 vp
->info
.output_semantic_name
[n
] != sn
||
3254 vp
->info
.output_semantic_index
[n
] != si
)
3257 vpo
= &vp
->cfg
.io
[n
];
3259 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.io
[i
], vpo
);
3262 if (nv50
->rasterizer
->pipe
.point_size_per_vertex
) {
3263 map
[m
/ 4] |= vp
->cfg
.psiz
<< ((m
% 4) * 8);
3264 reg
[3] = (m
++ << 4) | 1;
3267 /* now fill the stateobj */
3271 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
3273 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), n
);
3274 so_datap (so
, map
, n
);
3276 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_0
, 4);
3277 so_datap (so
, reg
, 4);
3279 so_method(so
, tesla
, NV50TCL_FP_INTERPOLANT_CTRL
, 1);
3280 so_data (so
, reg
[4]);
3282 so_method(so
, tesla
, 0x1540, 4);
3283 so_datap (so
, lin
, 4);
3285 if (nv50
->rasterizer
->pipe
.point_sprite
) {
3286 nv50_pntc_replace(nv50
, pcrd
, (reg
[4] >> 8) & 0xff);
3288 so_method(so
, tesla
, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
3289 so_datap (so
, pcrd
, 8);
3292 so_ref(so
, &nv50
->state
.programs
);
3297 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
3299 while (p
->exec_head
) {
3300 struct nv50_program_exec
*e
= p
->exec_head
;
3302 p
->exec_head
= e
->next
;
3305 p
->exec_tail
= NULL
;
3308 nouveau_bo_ref(NULL
, &p
->bo
);
3310 nouveau_resource_free(&p
->data
[0]);