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 127
35 #define NV50_SU_MAX_ADDR 4
36 //#define NV50_PROGRAM_DUMP
38 /* $a5 and $a6 always seem to be 0, and using $a7 gives you noise */
40 /* ARL - gallium craps itself on progs/vp/arl.txt
42 * MSB - Like MAD, but MUL+SUB
43 * - Fuck it off, introduce a way to negate args for ops that
46 * Look into inlining IMMD for ops other than MOV (make it general?)
47 * - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
48 * but can emit to P_TEMP first - then MOV later. NVIDIA does this
50 * In ops such as ADD it's possible to construct a bad opcode in the !is_long()
51 * case, if the emit_src() causes the inst to suddenly become long.
53 * Verify half-insns work where expected - and force disable them where they
54 * don't work - MUL has it forcibly disabled atm as it fixes POW..
56 * FUCK! watch dst==src vectors, can overwrite components that are needed.
57 * ie. SUB R0, R0.yzxw, R0
59 * Things to check with renouveau:
60 * FP attr/result assignment - how?
62 * - 0x16bc maps vp output onto fp hpos
63 * - 0x16c0 maps vp output onto fp col0
67 * 0x16bc->0x16e8 --> some binding between vp/fp regs
68 * 0x16b8 --> VP output count
70 * 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
71 * "MOV rcol.x, fcol.y" = 0x00000004
72 * 0x19a8 --> as above but 0x00000100 and 0x00000000
73 * - 0x00100000 used when KIL used
74 * 0x196c --> as above but 0x00000011 and 0x00000000
76 * 0x1988 --> 0xXXNNNNNN
77 * - XX == FP high something
93 int rhw
; /* result hw for FP outputs, or interpolant index */
94 int acc
; /* instruction where this reg is last read (first insn == 1) */
97 #define NV50_MOD_NEG 1
98 #define NV50_MOD_ABS 2
99 #define NV50_MOD_SAT 4
101 /* arbitrary limits */
102 #define MAX_IF_DEPTH 4
103 #define MAX_LOOP_DEPTH 4
106 struct nv50_program
*p
;
109 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
110 struct nv50_reg r_addr
[NV50_SU_MAX_ADDR
];
113 struct nv50_reg
*temp
;
115 struct nv50_reg
*attr
;
117 struct nv50_reg
*result
;
119 struct nv50_reg
*param
;
121 struct nv50_reg
*immd
;
124 struct nv50_reg
**addr
;
127 struct nv50_reg
*temp_temp
[16];
128 unsigned temp_temp_nr
;
130 /* broadcast and destination replacement regs */
131 struct nv50_reg
*r_brdc
;
132 struct nv50_reg
*r_dst
[4];
134 struct nv50_reg reg_instances
[16];
135 unsigned reg_instance_nr
;
137 unsigned interp_mode
[32];
138 /* perspective interpolation registers */
139 struct nv50_reg
*iv_p
;
140 struct nv50_reg
*iv_c
;
142 struct nv50_program_exec
*if_cond
;
143 struct nv50_program_exec
*if_insn
[MAX_IF_DEPTH
];
144 struct nv50_program_exec
*br_join
[MAX_IF_DEPTH
];
145 struct nv50_program_exec
*br_loop
[MAX_LOOP_DEPTH
]; /* for BRK branch */
146 int if_lvl
, loop_lvl
;
147 unsigned loop_pos
[MAX_LOOP_DEPTH
];
149 /* current instruction and total number of insns */
156 static INLINE
struct nv50_reg
*
157 reg_instance(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
159 struct nv50_reg
*dup
= NULL
;
161 assert(pc
->reg_instance_nr
< 16);
162 dup
= &pc
->reg_instances
[pc
->reg_instance_nr
++];
170 ctor_reg(struct nv50_reg
*reg
, unsigned type
, int index
, int hw
)
180 static INLINE
unsigned
181 popcnt4(uint32_t val
)
183 static const unsigned cnt
[16]
184 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
185 return cnt
[val
& 0xf];
189 terminate_mbb(struct nv50_pc
*pc
)
193 /* remove records of temporary address register values */
194 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
195 if (pc
->r_addr
[i
].index
< 0)
196 pc
->r_addr
[i
].rhw
= -1;
200 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
204 if (reg
->type
== P_RESULT
) {
205 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
206 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
209 if (reg
->type
!= P_TEMP
)
213 /*XXX: do this here too to catch FP temp-as-attr usage..
214 * not clean, but works */
215 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
216 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
220 if (reg
->rhw
!= -1) {
221 /* try to allocate temporary with index rhw first */
222 if (!(pc
->r_temp
[reg
->rhw
])) {
223 pc
->r_temp
[reg
->rhw
] = reg
;
225 if (pc
->p
->cfg
.high_temp
< (reg
->rhw
+ 1))
226 pc
->p
->cfg
.high_temp
= reg
->rhw
+ 1;
229 /* make sure we don't get things like $r0 needs to go
230 * in $r1 and $r1 in $r0
232 i
= pc
->result_nr
* 4;
235 for (; i
< NV50_SU_MAX_TEMP
; i
++) {
236 if (!(pc
->r_temp
[i
])) {
239 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
240 pc
->p
->cfg
.high_temp
= i
+ 1;
248 /* XXX: For shaders that aren't executed linearly (e.g. shaders that
249 * contain loops), we need to assign all hw regs to TGSI TEMPs early,
250 * lest we risk temp_temps overwriting regs alloc'd "later".
252 static struct nv50_reg
*
253 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
258 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
261 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
262 if (!pc
->r_temp
[i
]) {
263 r
= MALLOC_STRUCT(nv50_reg
);
264 ctor_reg(r
, P_TEMP
, -1, i
);
274 /* Assign the hw of the discarded temporary register src
275 * to the tgsi register dst and free src.
278 assimilate_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
280 assert(src
->index
== -1 && src
->hw
!= -1);
283 pc
->r_temp
[dst
->hw
] = NULL
;
284 pc
->r_temp
[src
->hw
] = dst
;
290 /* release the hardware resource held by r */
292 release_hw(struct nv50_pc
*pc
, struct nv50_reg
*r
)
294 assert(r
->type
== P_TEMP
);
298 assert(pc
->r_temp
[r
->hw
] == r
);
299 pc
->r_temp
[r
->hw
] = NULL
;
307 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
309 if (r
->index
== -1) {
312 FREE(pc
->r_temp
[hw
]);
313 pc
->r_temp
[hw
] = NULL
;
318 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
322 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
325 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
326 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
327 return alloc_temp4(pc
, dst
, idx
+ 4);
329 for (i
= 0; i
< 4; i
++) {
330 dst
[i
] = MALLOC_STRUCT(nv50_reg
);
331 ctor_reg(dst
[i
], P_TEMP
, -1, idx
+ i
);
332 pc
->r_temp
[idx
+ i
] = dst
[i
];
339 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
343 for (i
= 0; i
< 4; i
++)
344 free_temp(pc
, reg
[i
]);
347 static struct nv50_reg
*
348 temp_temp(struct nv50_pc
*pc
)
350 if (pc
->temp_temp_nr
>= 16)
353 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
354 return pc
->temp_temp
[pc
->temp_temp_nr
++];
358 kill_temp_temp(struct nv50_pc
*pc
)
362 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
363 free_temp(pc
, pc
->temp_temp
[i
]);
364 pc
->temp_temp_nr
= 0;
368 ctor_immd(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
370 pc
->immd_buf
= REALLOC(pc
->immd_buf
, (pc
->immd_nr
* 4 * sizeof(float)),
371 (pc
->immd_nr
+ 1) * 4 * sizeof(float));
372 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
373 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
374 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
375 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
377 return pc
->immd_nr
++;
380 static struct nv50_reg
*
381 alloc_immd(struct nv50_pc
*pc
, float f
)
383 struct nv50_reg
*r
= MALLOC_STRUCT(nv50_reg
);
386 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
387 if (pc
->immd_buf
[hw
] == f
)
390 if (hw
== pc
->immd_nr
* 4)
391 hw
= ctor_immd(pc
, f
, -f
, 0.5 * f
, 0) * 4;
393 ctor_reg(r
, P_IMMD
, -1, hw
);
397 static struct nv50_program_exec
*
398 exec(struct nv50_pc
*pc
)
400 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
407 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
409 struct nv50_program
*p
= pc
->p
;
412 p
->exec_tail
->next
= e
;
416 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
419 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
422 is_long(struct nv50_program_exec
*e
)
430 is_immd(struct nv50_program_exec
*e
)
432 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
438 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
439 struct nv50_program_exec
*e
)
442 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
443 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
447 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
448 struct nv50_program_exec
*e
)
451 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
452 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
456 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
462 set_pred(pc
, 0xf, 0, e
);
463 set_pred_wr(pc
, 0, 0, e
);
467 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
469 if (dst
->type
== P_RESULT
) {
471 e
->inst
[1] |= 0x00000008;
477 e
->inst
[0] |= (dst
->hw
<< 2);
481 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
484 float f
= pc
->immd_buf
[imm
->hw
];
486 if (imm
->mod
& NV50_MOD_ABS
)
488 val
= fui((imm
->mod
& NV50_MOD_NEG
) ? -f
: f
);
491 /*XXX: can't be predicated - bits overlap.. catch cases where both
492 * are required and avoid them. */
493 set_pred(pc
, 0, 0, e
);
494 set_pred_wr(pc
, 0, 0, e
);
496 e
->inst
[1] |= 0x00000002 | 0x00000001;
497 e
->inst
[0] |= (val
& 0x3f) << 16;
498 e
->inst
[1] |= (val
>> 6) << 2;
502 set_addr(struct nv50_program_exec
*e
, struct nv50_reg
*a
)
504 assert(!(e
->inst
[0] & 0x0c000000));
505 assert(!(e
->inst
[1] & 0x00000004));
507 e
->inst
[0] |= (a
->hw
& 3) << 26;
508 e
->inst
[1] |= (a
->hw
>> 2) << 2;
512 emit_add_addr_imm(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
513 struct nv50_reg
*src0
, uint16_t src1_val
)
515 struct nv50_program_exec
*e
= exec(pc
);
517 e
->inst
[0] = 0xd0000000 | (src1_val
<< 9);
518 e
->inst
[1] = 0x20000000;
520 e
->inst
[0] |= dst
->hw
<< 2;
521 if (src0
) /* otherwise will add to $a0, which is always 0 */
527 static struct nv50_reg
*
528 alloc_addr(struct nv50_pc
*pc
, struct nv50_reg
*ref
)
531 struct nv50_reg
*a_tgsi
= NULL
, *a
= NULL
;
534 /* allocate for TGSI address reg */
535 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
) {
536 if (pc
->r_addr
[i
].index
>= 0)
538 if (pc
->r_addr
[i
].rhw
>= 0 &&
539 pc
->r_addr
[i
].acc
== pc
->insn_cur
)
542 pc
->r_addr
[i
].rhw
= -1;
543 pc
->r_addr
[i
].index
= i
;
544 return &pc
->r_addr
[i
];
550 /* Allocate and set an address reg so we can access 'ref'.
552 * If and r_addr has index < 0, it is not reserved for TGSI,
553 * and index will be the negative of the TGSI addr index the
554 * value in rhw is relative to, or -256 if rhw is an offset
555 * from 0. If rhw < 0, the reg has not been initialized.
557 for (i
= NV50_SU_MAX_ADDR
- 1; i
>= 0; --i
) {
558 if (pc
->r_addr
[i
].index
>= 0) /* occupied for TGSI */
560 if (pc
->r_addr
[i
].rhw
< 0) { /* unused */
564 if (!a
&& pc
->r_addr
[i
].acc
!= pc
->insn_cur
)
567 if (ref
->hw
- pc
->r_addr
[i
].rhw
>= 128)
570 if ((ref
->acc
>= 0 && pc
->r_addr
[i
].index
== -256) ||
571 (ref
->acc
< 0 && -pc
->r_addr
[i
].index
== ref
->index
)) {
572 pc
->r_addr
[i
].acc
= pc
->insn_cur
;
573 return &pc
->r_addr
[i
];
579 a_tgsi
= pc
->addr
[ref
->index
];
581 emit_add_addr_imm(pc
, a
, a_tgsi
, (ref
->hw
& ~0x7f) * 4);
583 a
->rhw
= ref
->hw
& ~0x7f;
584 a
->acc
= pc
->insn_cur
;
585 a
->index
= a_tgsi
? -ref
->index
: -256;
589 #define INTERP_LINEAR 0
590 #define INTERP_FLAT 1
591 #define INTERP_PERSPECTIVE 2
592 #define INTERP_CENTROID 4
594 /* interpolant index has been stored in dst->rhw */
596 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*iv
,
599 assert(dst
->rhw
!= -1);
600 struct nv50_program_exec
*e
= exec(pc
);
602 e
->inst
[0] |= 0x80000000;
604 e
->inst
[0] |= (dst
->rhw
<< 16);
606 if (mode
& INTERP_FLAT
) {
607 e
->inst
[0] |= (1 << 8);
609 if (mode
& INTERP_PERSPECTIVE
) {
610 e
->inst
[0] |= (1 << 25);
612 e
->inst
[0] |= (iv
->hw
<< 9);
615 if (mode
& INTERP_CENTROID
)
616 e
->inst
[0] |= (1 << 24);
623 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
624 struct nv50_program_exec
*e
)
628 e
->param
.index
= src
->hw
& 127;
630 e
->param
.mask
= m
<< (s
% 32);
633 set_addr(e
, alloc_addr(pc
, src
));
636 assert(src
->type
== P_CONST
);
637 set_addr(e
, pc
->addr
[src
->index
]);
640 e
->inst
[1] |= (((src
->type
== P_IMMD
) ? 0 : 1) << 22);
644 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
646 struct nv50_program_exec
*e
= exec(pc
);
648 e
->inst
[0] = 0x10000000;
654 if (!is_long(e
) && src
->type
== P_IMMD
) {
655 set_immd(pc
, src
, e
);
656 /*XXX: 32-bit, but steals part of "half" reg space - need to
657 * catch and handle this case if/when we do half-regs
660 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
662 set_data(pc
, src
, 0x7f, 9, e
);
663 e
->inst
[1] |= 0x20000000; /* src0 const? */
665 if (src
->type
== P_ATTR
) {
667 e
->inst
[1] |= 0x00200000;
673 e
->inst
[0] |= (src
->hw
<< 9);
676 if (is_long(e
) && !is_immd(e
)) {
677 e
->inst
[1] |= 0x04000000; /* 32-bit */
678 e
->inst
[1] |= 0x0000c000; /* "subsubop" 0x3 */
679 if (!(e
->inst
[1] & 0x20000000))
680 e
->inst
[1] |= 0x00030000; /* "subsubop" 0xf */
682 e
->inst
[0] |= 0x00008000;
688 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
690 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
691 emit_mov(pc
, dst
, imm
);
696 check_swap_src_0_1(struct nv50_pc
*pc
,
697 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
699 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
701 if (src0
->type
== P_CONST
) {
702 if (src1
->type
!= P_CONST
) {
708 if (src1
->type
== P_ATTR
) {
709 if (src0
->type
!= P_ATTR
) {
720 set_src_0_restricted(struct nv50_pc
*pc
, struct nv50_reg
*src
,
721 struct nv50_program_exec
*e
)
723 struct nv50_reg
*temp
;
725 if (src
->type
!= P_TEMP
) {
726 temp
= temp_temp(pc
);
727 emit_mov(pc
, temp
, src
);
734 e
->inst
[0] |= (src
->hw
<< 9);
738 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
740 if (src
->type
== P_ATTR
) {
742 e
->inst
[1] |= 0x00200000;
744 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
745 struct nv50_reg
*temp
= temp_temp(pc
);
747 emit_mov(pc
, temp
, src
);
754 e
->inst
[0] |= (src
->hw
<< 9);
758 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
760 if (src
->type
== P_ATTR
) {
761 struct nv50_reg
*temp
= temp_temp(pc
);
763 emit_mov(pc
, temp
, src
);
766 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
767 assert(!(e
->inst
[0] & 0x00800000));
768 if (e
->inst
[0] & 0x01000000) {
769 struct nv50_reg
*temp
= temp_temp(pc
);
771 emit_mov(pc
, temp
, src
);
774 set_data(pc
, src
, 0x7f, 16, e
);
775 e
->inst
[0] |= 0x00800000;
782 e
->inst
[0] |= ((src
->hw
& 127) << 16);
786 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
790 if (src
->type
== P_ATTR
) {
791 struct nv50_reg
*temp
= temp_temp(pc
);
793 emit_mov(pc
, temp
, src
);
796 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
797 assert(!(e
->inst
[0] & 0x01000000));
798 if (e
->inst
[0] & 0x00800000) {
799 struct nv50_reg
*temp
= temp_temp(pc
);
801 emit_mov(pc
, temp
, src
);
804 set_data(pc
, src
, 0x7f, 32+14, e
);
805 e
->inst
[0] |= 0x01000000;
810 e
->inst
[1] |= ((src
->hw
& 127) << 14);
814 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
815 struct nv50_reg
*src1
)
817 struct nv50_program_exec
*e
= exec(pc
);
819 e
->inst
[0] |= 0xc0000000;
824 check_swap_src_0_1(pc
, &src0
, &src1
);
826 set_src_0(pc
, src0
, e
);
827 if (src1
->type
== P_IMMD
&& !is_long(e
)) {
828 if (src0
->mod
& NV50_MOD_NEG
)
829 e
->inst
[0] |= 0x00008000;
830 set_immd(pc
, src1
, e
);
832 set_src_1(pc
, src1
, e
);
833 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
) {
835 e
->inst
[1] |= 0x08000000;
837 e
->inst
[0] |= 0x00008000;
845 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
846 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
848 struct nv50_program_exec
*e
= exec(pc
);
850 e
->inst
[0] = 0xb0000000;
853 check_swap_src_0_1(pc
, &src0
, &src1
);
855 if (!pc
->allow32
|| (src0
->mod
| src1
->mod
) || src1
->hw
> 63) {
857 e
->inst
[1] |= ((src0
->mod
& NV50_MOD_NEG
) << 26) |
858 ((src1
->mod
& NV50_MOD_NEG
) << 27);
862 set_src_0(pc
, src0
, e
);
863 if (src1
->type
== P_CONST
|| src1
->type
== P_ATTR
|| is_long(e
))
864 set_src_2(pc
, src1
, e
);
866 if (src1
->type
== P_IMMD
)
867 set_immd(pc
, src1
, e
);
869 set_src_1(pc
, src1
, e
);
875 emit_arl(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
878 struct nv50_program_exec
*e
= exec(pc
);
881 e
->inst
[1] |= 0xc0000000;
883 e
->inst
[0] |= dst
->hw
<< 2;
884 e
->inst
[0] |= s
<< 16; /* shift left */
885 set_src_0_restricted(pc
, src
, e
);
891 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
892 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
894 struct nv50_program_exec
*e
= exec(pc
);
897 e
->inst
[0] |= 0xb0000000;
898 e
->inst
[1] |= (sub
<< 29);
900 check_swap_src_0_1(pc
, &src0
, &src1
);
902 set_src_0(pc
, src0
, e
);
903 set_src_1(pc
, src1
, e
);
905 if (src0
->mod
& NV50_MOD_ABS
)
906 e
->inst
[1] |= 0x00100000;
907 if (src1
->mod
& NV50_MOD_ABS
)
908 e
->inst
[1] |= 0x00080000;
914 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
915 struct nv50_reg
*src1
)
917 src1
->mod
^= NV50_MOD_NEG
;
918 emit_add(pc
, dst
, src0
, src1
);
919 src1
->mod
^= NV50_MOD_NEG
;
923 emit_bitop2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
924 struct nv50_reg
*src1
, unsigned op
)
926 struct nv50_program_exec
*e
= exec(pc
);
928 e
->inst
[0] = 0xd0000000;
931 check_swap_src_0_1(pc
, &src0
, &src1
);
933 set_src_0(pc
, src0
, e
);
935 if (op
!= TGSI_OPCODE_AND
&& op
!= TGSI_OPCODE_OR
&&
936 op
!= TGSI_OPCODE_XOR
)
937 assert(!"invalid bit op");
939 if (src1
->type
== P_IMMD
&& src0
->type
== P_TEMP
&& pc
->allow32
) {
940 set_immd(pc
, src1
, e
);
941 if (op
== TGSI_OPCODE_OR
)
942 e
->inst
[0] |= 0x0100;
944 if (op
== TGSI_OPCODE_XOR
)
945 e
->inst
[0] |= 0x8000;
947 set_src_1(pc
, src1
, e
);
948 e
->inst
[1] |= 0x04000000; /* 32 bit */
949 if (op
== TGSI_OPCODE_OR
)
950 e
->inst
[1] |= 0x4000;
952 if (op
== TGSI_OPCODE_XOR
)
953 e
->inst
[1] |= 0x8000;
960 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
961 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
963 struct nv50_program_exec
*e
= exec(pc
);
965 e
->inst
[0] |= 0xe0000000;
967 check_swap_src_0_1(pc
, &src0
, &src1
);
969 set_src_0(pc
, src0
, e
);
970 set_src_1(pc
, src1
, e
);
971 set_src_2(pc
, src2
, e
);
973 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
)
974 e
->inst
[1] |= 0x04000000;
975 if (src2
->mod
& NV50_MOD_NEG
)
976 e
->inst
[1] |= 0x08000000;
982 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
983 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
985 src2
->mod
^= NV50_MOD_NEG
;
986 emit_mad(pc
, dst
, src0
, src1
, src2
);
987 src2
->mod
^= NV50_MOD_NEG
;
991 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
992 struct nv50_reg
*dst
, struct nv50_reg
*src
)
994 struct nv50_program_exec
*e
= exec(pc
);
996 e
->inst
[0] |= 0x90000000;
999 e
->inst
[1] |= (sub
<< 29);
1002 set_dst(pc
, dst
, e
);
1004 if (sub
== 0 || sub
== 2)
1005 set_src_0_restricted(pc
, src
, e
);
1007 set_src_0(pc
, src
, e
);
1013 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1015 struct nv50_program_exec
*e
= exec(pc
);
1017 e
->inst
[0] |= 0xb0000000;
1019 set_dst(pc
, dst
, e
);
1020 set_src_0(pc
, src
, e
);
1022 e
->inst
[1] |= (6 << 29) | 0x00004000;
1028 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1030 struct nv50_program_exec
*e
= exec(pc
);
1032 e
->inst
[0] |= 0xb0000000;
1034 set_dst(pc
, dst
, e
);
1035 set_src_0(pc
, src
, e
);
1037 e
->inst
[1] |= (6 << 29);
1042 #define CVTOP_RN 0x01
1043 #define CVTOP_FLOOR 0x03
1044 #define CVTOP_CEIL 0x05
1045 #define CVTOP_TRUNC 0x07
1046 #define CVTOP_SAT 0x08
1047 #define CVTOP_ABS 0x10
1049 /* 0x04 == 32 bit dst */
1050 /* 0x40 == dst is float */
1051 /* 0x80 == src is float */
1052 #define CVT_F32_F32 0xc4
1053 #define CVT_F32_S32 0x44
1054 #define CVT_S32_F32 0x8c
1055 #define CVT_S32_S32 0x0c
1056 #define CVT_NEG 0x20
1060 emit_cvt(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
1061 int wp
, unsigned cvn
, unsigned fmt
)
1063 struct nv50_program_exec
*e
;
1068 e
->inst
[0] |= 0xa0000000;
1069 e
->inst
[1] |= 0x00004000; /* 32 bit src */
1070 e
->inst
[1] |= (cvn
<< 16);
1071 e
->inst
[1] |= (fmt
<< 24);
1072 set_src_0(pc
, src
, e
);
1075 set_pred_wr(pc
, 1, wp
, e
);
1078 set_dst(pc
, dst
, e
);
1080 e
->inst
[0] |= 0x000001fc;
1081 e
->inst
[1] |= 0x00000008;
1087 /* nv50 Condition codes:
1094 * 0x7 = set condition code ? (used before bra.lt/le/gt/ge)
1095 * 0x8 = unordered bit (allows NaN)
1098 emit_set(struct nv50_pc
*pc
, unsigned ccode
, struct nv50_reg
*dst
, int wp
,
1099 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
1101 static const unsigned cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
1103 struct nv50_program_exec
*e
= exec(pc
);
1104 struct nv50_reg
*rdst
;
1107 if (check_swap_src_0_1(pc
, &src0
, &src1
))
1108 ccode
= cc_swapped
[ccode
& 7] | (ccode
& 8);
1111 if (dst
&& dst
->type
!= P_TEMP
)
1112 dst
= alloc_temp(pc
, NULL
);
1116 e
->inst
[0] |= 0xb0000000;
1117 e
->inst
[1] |= 0x60000000 | (ccode
<< 14);
1119 /* XXX: decuda will disasm as .u16 and use .lo/.hi regs, but
1120 * that doesn't seem to match what the hw actually does
1121 e->inst[1] |= 0x04000000; << breaks things, u32 by default ?
1125 set_pred_wr(pc
, 1, wp
, e
);
1127 set_dst(pc
, dst
, e
);
1129 e
->inst
[0] |= 0x000001fc;
1130 e
->inst
[1] |= 0x00000008;
1133 set_src_0(pc
, src0
, e
);
1134 set_src_1(pc
, src1
, e
);
1137 pc
->if_cond
= pc
->p
->exec_tail
; /* record for OPCODE_IF */
1139 /* cvt.f32.u32/s32 (?) if we didn't only write the predicate */
1141 emit_cvt(pc
, rdst
, dst
, -1, CVTOP_ABS
| CVTOP_RN
, CVT_F32_S32
);
1142 if (rdst
&& rdst
!= dst
)
1146 static INLINE
unsigned
1147 map_tgsi_setop_cc(unsigned op
)
1150 case TGSI_OPCODE_SLT
: return 0x1;
1151 case TGSI_OPCODE_SGE
: return 0x6;
1152 case TGSI_OPCODE_SEQ
: return 0x2;
1153 case TGSI_OPCODE_SGT
: return 0x4;
1154 case TGSI_OPCODE_SLE
: return 0x3;
1155 case TGSI_OPCODE_SNE
: return 0xd;
1163 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1165 emit_cvt(pc
, dst
, src
, -1, CVTOP_FLOOR
, CVT_F32_F32
| CVT_RI
);
1169 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1170 struct nv50_reg
*v
, struct nv50_reg
*e
)
1172 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
1174 emit_flop(pc
, 3, temp
, v
);
1175 emit_mul(pc
, temp
, temp
, e
);
1176 emit_preex2(pc
, temp
, temp
);
1177 emit_flop(pc
, 6, dst
, temp
);
1179 free_temp(pc
, temp
);
1183 emit_abs(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1185 emit_cvt(pc
, dst
, src
, -1, CVTOP_ABS
, CVT_F32_F32
);
1189 emit_sat(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1191 emit_cvt(pc
, dst
, src
, -1, CVTOP_SAT
, CVT_F32_F32
);
1195 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1196 struct nv50_reg
**src
)
1198 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1199 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
1200 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
1201 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
1202 struct nv50_reg
*tmp
[4];
1203 boolean allow32
= pc
->allow32
;
1205 pc
->allow32
= FALSE
;
1207 if (mask
& (3 << 1)) {
1208 tmp
[0] = alloc_temp(pc
, NULL
);
1209 emit_minmax(pc
, 4, tmp
[0], src
[0], zero
);
1212 if (mask
& (1 << 2)) {
1213 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
1215 tmp
[1] = temp_temp(pc
);
1216 emit_minmax(pc
, 4, tmp
[1], src
[1], zero
);
1218 tmp
[3] = temp_temp(pc
);
1219 emit_minmax(pc
, 4, tmp
[3], src
[3], neg128
);
1220 emit_minmax(pc
, 5, tmp
[3], tmp
[3], pos128
);
1222 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
1223 emit_mov(pc
, dst
[2], zero
);
1224 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
1227 if (mask
& (1 << 1))
1228 assimilate_temp(pc
, dst
[1], tmp
[0]);
1230 if (mask
& (1 << 2))
1231 free_temp(pc
, tmp
[0]);
1233 pc
->allow32
= allow32
;
1235 /* do this last, in case src[i,j] == dst[0,3] */
1236 if (mask
& (1 << 0))
1237 emit_mov(pc
, dst
[0], one
);
1239 if (mask
& (1 << 3))
1240 emit_mov(pc
, dst
[3], one
);
1249 emit_neg(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1251 emit_cvt(pc
, dst
, src
, -1, CVTOP_RN
, CVT_F32_F32
| CVT_NEG
);
1255 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
1257 struct nv50_program_exec
*e
;
1258 const int r_pred
= 1;
1259 unsigned cvn
= CVT_F32_F32
;
1261 if (src
->mod
& NV50_MOD_NEG
)
1263 /* write predicate reg */
1264 emit_cvt(pc
, NULL
, src
, r_pred
, CVTOP_RN
, cvn
);
1266 /* conditional discard */
1268 e
->inst
[0] = 0x00000002;
1270 set_pred(pc
, 0x1 /* LT */, r_pred
, e
);
1275 load_cube_tex_coords(struct nv50_pc
*pc
, struct nv50_reg
*t
[4],
1276 struct nv50_reg
**src
, boolean proj
)
1278 int mod
[3] = { src
[0]->mod
, src
[1]->mod
, src
[2]->mod
};
1280 src
[0]->mod
|= NV50_MOD_ABS
;
1281 src
[1]->mod
|= NV50_MOD_ABS
;
1282 src
[2]->mod
|= NV50_MOD_ABS
;
1284 emit_minmax(pc
, 4, t
[2], src
[0], src
[1]);
1285 emit_minmax(pc
, 4, t
[2], src
[2], t
[2]);
1287 src
[0]->mod
= mod
[0];
1288 src
[1]->mod
= mod
[1];
1289 src
[2]->mod
= mod
[2];
1291 if (proj
&& 0 /* looks more correct without this */)
1292 emit_mul(pc
, t
[2], t
[2], src
[3]);
1293 emit_flop(pc
, 0, t
[2], t
[2]);
1295 emit_mul(pc
, t
[0], src
[0], t
[2]);
1296 emit_mul(pc
, t
[1], src
[1], t
[2]);
1297 emit_mul(pc
, t
[2], src
[2], t
[2]);
1301 emit_tex(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1302 struct nv50_reg
**src
, unsigned unit
, unsigned type
, boolean proj
)
1304 struct nv50_reg
*t
[4];
1305 struct nv50_program_exec
*e
;
1307 unsigned c
, mode
, dim
;
1310 case TGSI_TEXTURE_1D
:
1313 case TGSI_TEXTURE_UNKNOWN
:
1314 case TGSI_TEXTURE_2D
:
1315 case TGSI_TEXTURE_SHADOW1D
: /* XXX: x, z */
1316 case TGSI_TEXTURE_RECT
:
1319 case TGSI_TEXTURE_3D
:
1320 case TGSI_TEXTURE_CUBE
:
1321 case TGSI_TEXTURE_SHADOW2D
:
1322 case TGSI_TEXTURE_SHADOWRECT
: /* XXX */
1330 /* some cards need t[0]'s hw index to be a multiple of 4 */
1331 alloc_temp4(pc
, t
, 0);
1333 if (type
== TGSI_TEXTURE_CUBE
) {
1334 load_cube_tex_coords(pc
, t
, src
, proj
);
1337 if (src
[0]->type
== P_TEMP
&& src
[0]->rhw
!= -1) {
1338 mode
= pc
->interp_mode
[src
[0]->index
];
1340 t
[3]->rhw
= src
[3]->rhw
;
1341 emit_interp(pc
, t
[3], NULL
, (mode
& INTERP_CENTROID
));
1342 emit_flop(pc
, 0, t
[3], t
[3]);
1344 for (c
= 0; c
< dim
; c
++) {
1345 t
[c
]->rhw
= src
[c
]->rhw
;
1346 emit_interp(pc
, t
[c
], t
[3],
1347 (mode
| INTERP_PERSPECTIVE
));
1350 emit_flop(pc
, 0, t
[3], src
[3]);
1351 for (c
= 0; c
< dim
; c
++)
1352 emit_mul(pc
, t
[c
], src
[c
], t
[3]);
1354 /* XXX: for some reason the blob sometimes uses MAD:
1355 * emit_mad(pc, t[c], src[0][c], t[3], t[3])
1356 * pc->p->exec_tail->inst[1] |= 0x080fc000;
1360 for (c
= 0; c
< dim
; c
++)
1361 emit_mov(pc
, t
[c
], src
[c
]);
1366 e
->inst
[0] |= 0xf0000000;
1367 e
->inst
[1] |= 0x00000004;
1368 set_dst(pc
, t
[0], e
);
1369 e
->inst
[0] |= (unit
<< 9);
1372 e
->inst
[0] |= 0x00400000;
1375 e
->inst
[0] |= 0x00800000;
1376 if (type
== TGSI_TEXTURE_CUBE
)
1377 e
->inst
[0] |= 0x08000000;
1380 e
->inst
[0] |= (mask
& 0x3) << 25;
1381 e
->inst
[1] |= (mask
& 0xc) << 12;
1386 if (mask
& 1) emit_mov(pc
, dst
[0], t
[c
++]);
1387 if (mask
& 2) emit_mov(pc
, dst
[1], t
[c
++]);
1388 if (mask
& 4) emit_mov(pc
, dst
[2], t
[c
++]);
1389 if (mask
& 8) emit_mov(pc
, dst
[3], t
[c
]);
1393 /* XXX: if p.e. MUL is used directly after TEX, it would still use
1394 * the texture coordinates, not the fetched values: latency ? */
1396 for (c
= 0; c
< 4; c
++) {
1397 if (mask
& (1 << c
))
1398 assimilate_temp(pc
, dst
[c
], t
[c
]);
1400 free_temp(pc
, t
[c
]);
1406 emit_branch(struct nv50_pc
*pc
, int pred
, unsigned cc
,
1407 struct nv50_program_exec
**join
)
1409 struct nv50_program_exec
*e
= exec(pc
);
1413 e
->inst
[0] |= 0xa0000002;
1420 e
->inst
[0] |= 0x10000002;
1422 set_pred(pc
, cc
, pred
, e
);
1427 emit_nop(struct nv50_pc
*pc
)
1429 struct nv50_program_exec
*e
= exec(pc
);
1431 e
->inst
[0] = 0xf0000000;
1433 e
->inst
[1] = 0xe0000000;
1438 emit_ddx(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1440 struct nv50_program_exec
*e
= exec(pc
);
1442 assert(src
->type
== P_TEMP
);
1444 e
->inst
[0] = 0xc0140000;
1445 e
->inst
[1] = 0x89800000;
1447 set_dst(pc
, dst
, e
);
1448 set_src_0(pc
, src
, e
);
1449 set_src_2(pc
, src
, e
);
1455 emit_ddy(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1457 struct nv50_reg
*r
= src
;
1458 struct nv50_program_exec
*e
= exec(pc
);
1460 assert(src
->type
== P_TEMP
);
1462 if (!(src
->mod
& NV50_MOD_NEG
)) { /* ! double negation */
1463 r
= alloc_temp(pc
, NULL
);
1464 emit_neg(pc
, r
, src
);
1467 e
->inst
[0] = 0xc0150000;
1468 e
->inst
[1] = 0x8a400000;
1470 set_dst(pc
, dst
, e
);
1471 set_src_0(pc
, r
, e
);
1472 set_src_2(pc
, r
, e
);
1481 convert_to_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
1483 unsigned q
= 0, m
= ~0;
1485 assert(!is_long(e
));
1487 switch (e
->inst
[0] >> 28) {
1494 /* INTERP (move centroid, perspective and flat bits) */
1496 q
= (e
->inst
[0] & (3 << 24)) >> (24 - 16);
1497 q
|= (e
->inst
[0] & (1 << 8)) << (18 - 8);
1505 q
= ((e
->inst
[0] & (~m
)) >> 2);
1510 q
= ((e
->inst
[0] & (~m
)) << 12);
1513 /* MAD (if src2 == dst) */
1514 q
= ((e
->inst
[0] & 0x1fc) << 12);
1528 /* Some operations support an optional negation flag. */
1530 negate_supported(const struct tgsi_full_instruction
*insn
, int i
)
1532 switch (insn
->Instruction
.Opcode
) {
1533 case TGSI_OPCODE_DDY
:
1534 case TGSI_OPCODE_DP3
:
1535 case TGSI_OPCODE_DP4
:
1536 case TGSI_OPCODE_MUL
:
1537 case TGSI_OPCODE_KIL
:
1538 case TGSI_OPCODE_ADD
:
1539 case TGSI_OPCODE_SUB
:
1540 case TGSI_OPCODE_MAD
:
1542 case TGSI_OPCODE_POW
:
1551 /* Return a read mask for source registers deduced from opcode & write mask. */
1553 nv50_tgsi_src_mask(const struct tgsi_full_instruction
*insn
, int c
)
1555 unsigned x
, mask
= insn
->Dst
[0].Register
.WriteMask
;
1557 switch (insn
->Instruction
.Opcode
) {
1558 case TGSI_OPCODE_COS
:
1559 case TGSI_OPCODE_SIN
:
1560 return (mask
& 0x8) | ((mask
& 0x7) ? 0x1 : 0x0);
1561 case TGSI_OPCODE_DP3
:
1563 case TGSI_OPCODE_DP4
:
1564 case TGSI_OPCODE_DPH
:
1565 case TGSI_OPCODE_KIL
: /* WriteMask ignored */
1567 case TGSI_OPCODE_DST
:
1568 return mask
& (c
? 0xa : 0x6);
1569 case TGSI_OPCODE_EX2
:
1570 case TGSI_OPCODE_LG2
:
1571 case TGSI_OPCODE_POW
:
1572 case TGSI_OPCODE_RCP
:
1573 case TGSI_OPCODE_RSQ
:
1574 case TGSI_OPCODE_SCS
:
1576 case TGSI_OPCODE_IF
:
1578 case TGSI_OPCODE_LIT
:
1580 case TGSI_OPCODE_TEX
:
1581 case TGSI_OPCODE_TXP
:
1583 const struct tgsi_instruction_texture
*tex
;
1585 assert(insn
->Instruction
.Texture
);
1586 tex
= &insn
->Texture
;
1589 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1592 switch (tex
->Texture
) {
1593 case TGSI_TEXTURE_1D
:
1596 case TGSI_TEXTURE_2D
:
1604 case TGSI_OPCODE_XPD
:
1606 if (mask
& 1) x
|= 0x6;
1607 if (mask
& 2) x
|= 0x5;
1608 if (mask
& 4) x
|= 0x3;
1617 static struct nv50_reg
*
1618 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
1620 switch (dst
->Register
.File
) {
1621 case TGSI_FILE_TEMPORARY
:
1622 return &pc
->temp
[dst
->Register
.Index
* 4 + c
];
1623 case TGSI_FILE_OUTPUT
:
1624 return &pc
->result
[dst
->Register
.Index
* 4 + c
];
1625 case TGSI_FILE_ADDRESS
:
1627 struct nv50_reg
*r
= pc
->addr
[dst
->Register
.Index
* 4 + c
];
1629 r
= alloc_addr(pc
, NULL
);
1630 pc
->addr
[dst
->Register
.Index
* 4 + c
] = r
;
1635 case TGSI_FILE_NULL
:
1644 static struct nv50_reg
*
1645 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
,
1648 struct nv50_reg
*r
= NULL
;
1649 struct nv50_reg
*temp
;
1650 unsigned sgn
, c
, swz
;
1652 if (src
->Register
.File
!= TGSI_FILE_CONSTANT
)
1653 assert(!src
->Register
.Indirect
);
1655 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1657 c
= tgsi_util_get_full_src_register_swizzle(src
, chan
);
1659 case TGSI_SWIZZLE_X
:
1660 case TGSI_SWIZZLE_Y
:
1661 case TGSI_SWIZZLE_Z
:
1662 case TGSI_SWIZZLE_W
:
1663 switch (src
->Register
.File
) {
1664 case TGSI_FILE_INPUT
:
1665 r
= &pc
->attr
[src
->Register
.Index
* 4 + c
];
1667 case TGSI_FILE_TEMPORARY
:
1668 r
= &pc
->temp
[src
->Register
.Index
* 4 + c
];
1670 case TGSI_FILE_CONSTANT
:
1671 if (!src
->Register
.Indirect
) {
1672 r
= &pc
->param
[src
->Register
.Index
* 4 + c
];
1675 /* Indicate indirection by setting r->acc < 0 and
1676 * use the index field to select the address reg.
1678 r
= MALLOC_STRUCT(nv50_reg
);
1679 swz
= tgsi_util_get_src_register_swizzle(
1681 ctor_reg(r
, P_CONST
,
1682 src
->Indirect
.Index
* 4 + swz
,
1683 src
->Register
.Index
* 4 + c
);
1686 case TGSI_FILE_IMMEDIATE
:
1687 r
= &pc
->immd
[src
->Register
.Index
* 4 + c
];
1689 case TGSI_FILE_SAMPLER
:
1691 case TGSI_FILE_ADDRESS
:
1692 r
= pc
->addr
[src
->Register
.Index
* 4 + c
];
1706 case TGSI_UTIL_SIGN_KEEP
:
1708 case TGSI_UTIL_SIGN_CLEAR
:
1709 temp
= temp_temp(pc
);
1710 emit_abs(pc
, temp
, r
);
1713 case TGSI_UTIL_SIGN_TOGGLE
:
1715 r
->mod
= NV50_MOD_NEG
;
1717 temp
= temp_temp(pc
);
1718 emit_neg(pc
, temp
, r
);
1722 case TGSI_UTIL_SIGN_SET
:
1723 temp
= temp_temp(pc
);
1724 emit_cvt(pc
, temp
, r
, -1, CVTOP_ABS
, CVT_F32_F32
| CVT_NEG
);
1735 /* return TRUE for ops that produce only a single result */
1737 is_scalar_op(unsigned op
)
1740 case TGSI_OPCODE_COS
:
1741 case TGSI_OPCODE_DP2
:
1742 case TGSI_OPCODE_DP3
:
1743 case TGSI_OPCODE_DP4
:
1744 case TGSI_OPCODE_DPH
:
1745 case TGSI_OPCODE_EX2
:
1746 case TGSI_OPCODE_LG2
:
1747 case TGSI_OPCODE_POW
:
1748 case TGSI_OPCODE_RCP
:
1749 case TGSI_OPCODE_RSQ
:
1750 case TGSI_OPCODE_SIN
:
1752 case TGSI_OPCODE_KIL:
1753 case TGSI_OPCODE_LIT:
1754 case TGSI_OPCODE_SCS:
1762 /* Returns a bitmask indicating which dst components depend
1763 * on source s, component c (reverse of nv50_tgsi_src_mask).
1766 nv50_tgsi_dst_revdep(unsigned op
, int s
, int c
)
1768 if (is_scalar_op(op
))
1772 case TGSI_OPCODE_DST
:
1773 return (1 << c
) & (s
? 0xa : 0x6);
1774 case TGSI_OPCODE_XPD
:
1784 case TGSI_OPCODE_LIT
:
1785 case TGSI_OPCODE_SCS
:
1786 case TGSI_OPCODE_TEX
:
1787 case TGSI_OPCODE_TXP
:
1788 /* these take care of dangerous swizzles themselves */
1790 case TGSI_OPCODE_IF
:
1791 case TGSI_OPCODE_KIL
:
1792 /* don't call this function for these ops */
1796 /* linear vector instruction */
1801 static INLINE boolean
1802 has_pred(struct nv50_program_exec
*e
, unsigned cc
)
1804 if (!is_long(e
) || is_immd(e
))
1806 return ((e
->inst
[1] & 0x780) == (cc
<< 7));
1809 /* on ENDIF see if we can do "@p0.neu single_op" instead of:
1816 nv50_kill_branch(struct nv50_pc
*pc
)
1818 int lvl
= pc
->if_lvl
;
1820 if (pc
->if_insn
[lvl
]->next
!= pc
->p
->exec_tail
)
1823 /* if ccode == 'true', the BRA is from an ELSE and the predicate
1824 * reg may no longer be valid, since we currently always use $p0
1826 if (has_pred(pc
->if_insn
[lvl
], 0xf))
1828 assert(pc
->if_insn
[lvl
] && pc
->br_join
[lvl
]);
1830 /* We'll use the exec allocated for JOIN_AT (as we can't easily
1831 * update prev's next); if exec_tail is BRK, update the pointer.
1833 if (pc
->loop_lvl
&& pc
->br_loop
[pc
->loop_lvl
- 1] == pc
->p
->exec_tail
)
1834 pc
->br_loop
[pc
->loop_lvl
- 1] = pc
->br_join
[lvl
];
1836 pc
->p
->exec_size
-= 4; /* remove JOIN_AT and BRA */
1838 *pc
->br_join
[lvl
] = *pc
->p
->exec_tail
;
1840 FREE(pc
->if_insn
[lvl
]);
1841 FREE(pc
->p
->exec_tail
);
1843 pc
->p
->exec_tail
= pc
->br_join
[lvl
];
1844 pc
->p
->exec_tail
->next
= NULL
;
1845 set_pred(pc
, 0xd, 0, pc
->p
->exec_tail
);
1851 nv50_program_tx_insn(struct nv50_pc
*pc
,
1852 const struct tgsi_full_instruction
*inst
)
1854 struct nv50_reg
*rdst
[4], *dst
[4], *brdc
, *src
[3][4], *temp
;
1855 unsigned mask
, sat
, unit
;
1858 mask
= inst
->Dst
[0].Register
.WriteMask
;
1859 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
1861 memset(src
, 0, sizeof(src
));
1863 for (c
= 0; c
< 4; c
++) {
1864 if ((mask
& (1 << c
)) && !pc
->r_dst
[c
])
1865 dst
[c
] = tgsi_dst(pc
, c
, &inst
->Dst
[0]);
1867 dst
[c
] = pc
->r_dst
[c
];
1871 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1872 const struct tgsi_full_src_register
*fs
= &inst
->Src
[i
];
1876 src_mask
= nv50_tgsi_src_mask(inst
, i
);
1877 neg_supp
= negate_supported(inst
, i
);
1879 if (fs
->Register
.File
== TGSI_FILE_SAMPLER
)
1880 unit
= fs
->Register
.Index
;
1882 for (c
= 0; c
< 4; c
++)
1883 if (src_mask
& (1 << c
))
1884 src
[i
][c
] = reg_instance(pc
,
1885 tgsi_src(pc
, c
, fs
, neg_supp
));
1888 brdc
= temp
= pc
->r_brdc
;
1889 if (brdc
&& brdc
->type
!= P_TEMP
) {
1890 temp
= temp_temp(pc
);
1895 for (c
= 0; c
< 4; c
++) {
1896 if (!(mask
& (1 << c
)) || dst
[c
]->type
== P_TEMP
)
1898 /* rdst[c] = dst[c]; */ /* done above */
1899 dst
[c
] = temp_temp(pc
);
1903 assert(brdc
|| !is_scalar_op(inst
->Instruction
.Opcode
));
1905 switch (inst
->Instruction
.Opcode
) {
1906 case TGSI_OPCODE_ABS
:
1907 for (c
= 0; c
< 4; c
++) {
1908 if (!(mask
& (1 << c
)))
1910 emit_abs(pc
, dst
[c
], src
[0][c
]);
1913 case TGSI_OPCODE_ADD
:
1914 for (c
= 0; c
< 4; c
++) {
1915 if (!(mask
& (1 << c
)))
1917 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1920 case TGSI_OPCODE_AND
:
1921 case TGSI_OPCODE_XOR
:
1922 case TGSI_OPCODE_OR
:
1923 for (c
= 0; c
< 4; c
++) {
1924 if (!(mask
& (1 << c
)))
1926 emit_bitop2(pc
, dst
[c
], src
[0][c
], src
[1][c
],
1927 inst
->Instruction
.Opcode
);
1930 case TGSI_OPCODE_ARL
:
1932 temp
= temp_temp(pc
);
1933 emit_cvt(pc
, temp
, src
[0][0], -1, CVTOP_FLOOR
, CVT_S32_F32
);
1934 emit_arl(pc
, dst
[0], temp
, 4);
1936 case TGSI_OPCODE_BGNLOOP
:
1937 pc
->loop_pos
[pc
->loop_lvl
++] = pc
->p
->exec_size
;
1940 case TGSI_OPCODE_BRK
:
1941 emit_branch(pc
, -1, 0, NULL
);
1942 assert(pc
->loop_lvl
> 0);
1943 pc
->br_loop
[pc
->loop_lvl
- 1] = pc
->p
->exec_tail
;
1945 case TGSI_OPCODE_CEIL
:
1946 for (c
= 0; c
< 4; c
++) {
1947 if (!(mask
& (1 << c
)))
1949 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
1950 CVTOP_CEIL
, CVT_F32_F32
| CVT_RI
);
1953 case TGSI_OPCODE_CMP
:
1954 pc
->allow32
= FALSE
;
1955 for (c
= 0; c
< 4; c
++) {
1956 if (!(mask
& (1 << c
)))
1958 emit_cvt(pc
, NULL
, src
[0][c
], 1, CVTOP_RN
, CVT_F32_F32
);
1959 emit_mov(pc
, dst
[c
], src
[1][c
]);
1960 set_pred(pc
, 0x1, 1, pc
->p
->exec_tail
); /* @SF */
1961 emit_mov(pc
, dst
[c
], src
[2][c
]);
1962 set_pred(pc
, 0x6, 1, pc
->p
->exec_tail
); /* @NSF */
1965 case TGSI_OPCODE_COS
:
1967 emit_precossin(pc
, temp
, src
[0][3]);
1968 emit_flop(pc
, 5, dst
[3], temp
);
1972 temp
= brdc
= temp_temp(pc
);
1974 emit_precossin(pc
, temp
, src
[0][0]);
1975 emit_flop(pc
, 5, brdc
, temp
);
1977 case TGSI_OPCODE_DDX
:
1978 for (c
= 0; c
< 4; c
++) {
1979 if (!(mask
& (1 << c
)))
1981 emit_ddx(pc
, dst
[c
], src
[0][c
]);
1984 case TGSI_OPCODE_DDY
:
1985 for (c
= 0; c
< 4; c
++) {
1986 if (!(mask
& (1 << c
)))
1988 emit_ddy(pc
, dst
[c
], src
[0][c
]);
1991 case TGSI_OPCODE_DP3
:
1992 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1993 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1994 emit_mad(pc
, brdc
, src
[0][2], src
[1][2], temp
);
1996 case TGSI_OPCODE_DP4
:
1997 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1998 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1999 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2000 emit_mad(pc
, brdc
, src
[0][3], src
[1][3], temp
);
2002 case TGSI_OPCODE_DPH
:
2003 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2004 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2005 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2006 emit_add(pc
, brdc
, src
[1][3], temp
);
2008 case TGSI_OPCODE_DST
:
2009 if (mask
& (1 << 1))
2010 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
2011 if (mask
& (1 << 2))
2012 emit_mov(pc
, dst
[2], src
[0][2]);
2013 if (mask
& (1 << 3))
2014 emit_mov(pc
, dst
[3], src
[1][3]);
2015 if (mask
& (1 << 0))
2016 emit_mov_immdval(pc
, dst
[0], 1.0f
);
2018 case TGSI_OPCODE_ELSE
:
2019 emit_branch(pc
, -1, 0, NULL
);
2020 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2021 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
2024 case TGSI_OPCODE_ENDIF
:
2025 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2027 /* try to replace branch over 1 insn with a predicated insn */
2028 if (nv50_kill_branch(pc
) == TRUE
)
2031 if (pc
->br_join
[pc
->if_lvl
]) {
2032 pc
->br_join
[pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2033 pc
->br_join
[pc
->if_lvl
] = NULL
;
2036 /* emit a NOP as join point, we could set it on the next
2037 * one, but would have to make sure it is long and !immd
2040 pc
->p
->exec_tail
->inst
[1] |= 2;
2042 case TGSI_OPCODE_ENDLOOP
:
2043 emit_branch(pc
, -1, 0, NULL
);
2044 pc
->p
->exec_tail
->param
.index
= pc
->loop_pos
[--pc
->loop_lvl
];
2045 pc
->br_loop
[pc
->loop_lvl
]->param
.index
= pc
->p
->exec_size
;
2048 case TGSI_OPCODE_EX2
:
2049 emit_preex2(pc
, temp
, src
[0][0]);
2050 emit_flop(pc
, 6, brdc
, temp
);
2052 case TGSI_OPCODE_FLR
:
2053 for (c
= 0; c
< 4; c
++) {
2054 if (!(mask
& (1 << c
)))
2056 emit_flr(pc
, dst
[c
], src
[0][c
]);
2059 case TGSI_OPCODE_FRC
:
2060 temp
= temp_temp(pc
);
2061 for (c
= 0; c
< 4; c
++) {
2062 if (!(mask
& (1 << c
)))
2064 emit_flr(pc
, temp
, src
[0][c
]);
2065 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
2068 case TGSI_OPCODE_IF
:
2069 /* emitting a join_at may not be necessary */
2070 assert(pc
->if_lvl
< MAX_IF_DEPTH
);
2071 /* set_pred_wr(pc, 1, 0, pc->if_cond); */
2072 emit_cvt(pc
, NULL
, src
[0][0], 0, CVTOP_ABS
| CVTOP_RN
,
2074 emit_branch(pc
, 0, 2, &pc
->br_join
[pc
->if_lvl
]);
2075 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
2078 case TGSI_OPCODE_KIL
:
2079 emit_kil(pc
, src
[0][0]);
2080 emit_kil(pc
, src
[0][1]);
2081 emit_kil(pc
, src
[0][2]);
2082 emit_kil(pc
, src
[0][3]);
2084 case TGSI_OPCODE_LIT
:
2085 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
2087 case TGSI_OPCODE_LG2
:
2088 emit_flop(pc
, 3, brdc
, src
[0][0]);
2090 case TGSI_OPCODE_LRP
:
2091 temp
= temp_temp(pc
);
2092 for (c
= 0; c
< 4; c
++) {
2093 if (!(mask
& (1 << c
)))
2095 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
2096 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
2099 case TGSI_OPCODE_MAD
:
2100 for (c
= 0; c
< 4; c
++) {
2101 if (!(mask
& (1 << c
)))
2103 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
2106 case TGSI_OPCODE_MAX
:
2107 for (c
= 0; c
< 4; c
++) {
2108 if (!(mask
& (1 << c
)))
2110 emit_minmax(pc
, 4, dst
[c
], src
[0][c
], src
[1][c
]);
2113 case TGSI_OPCODE_MIN
:
2114 for (c
= 0; c
< 4; c
++) {
2115 if (!(mask
& (1 << c
)))
2117 emit_minmax(pc
, 5, dst
[c
], src
[0][c
], src
[1][c
]);
2120 case TGSI_OPCODE_MOV
:
2121 for (c
= 0; c
< 4; c
++) {
2122 if (!(mask
& (1 << c
)))
2124 emit_mov(pc
, dst
[c
], src
[0][c
]);
2127 case TGSI_OPCODE_MUL
:
2128 for (c
= 0; c
< 4; c
++) {
2129 if (!(mask
& (1 << c
)))
2131 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2134 case TGSI_OPCODE_POW
:
2135 emit_pow(pc
, brdc
, src
[0][0], src
[1][0]);
2137 case TGSI_OPCODE_RCP
:
2138 emit_flop(pc
, 0, brdc
, src
[0][0]);
2140 case TGSI_OPCODE_RSQ
:
2141 emit_flop(pc
, 2, brdc
, src
[0][0]);
2143 case TGSI_OPCODE_SCS
:
2144 temp
= temp_temp(pc
);
2146 emit_precossin(pc
, temp
, src
[0][0]);
2147 if (mask
& (1 << 0))
2148 emit_flop(pc
, 5, dst
[0], temp
);
2149 if (mask
& (1 << 1))
2150 emit_flop(pc
, 4, dst
[1], temp
);
2151 if (mask
& (1 << 2))
2152 emit_mov_immdval(pc
, dst
[2], 0.0);
2153 if (mask
& (1 << 3))
2154 emit_mov_immdval(pc
, dst
[3], 1.0);
2156 case TGSI_OPCODE_SIN
:
2158 emit_precossin(pc
, temp
, src
[0][3]);
2159 emit_flop(pc
, 4, dst
[3], temp
);
2163 temp
= brdc
= temp_temp(pc
);
2165 emit_precossin(pc
, temp
, src
[0][0]);
2166 emit_flop(pc
, 4, brdc
, temp
);
2168 case TGSI_OPCODE_SLT
:
2169 case TGSI_OPCODE_SGE
:
2170 case TGSI_OPCODE_SEQ
:
2171 case TGSI_OPCODE_SGT
:
2172 case TGSI_OPCODE_SLE
:
2173 case TGSI_OPCODE_SNE
:
2174 i
= map_tgsi_setop_cc(inst
->Instruction
.Opcode
);
2175 for (c
= 0; c
< 4; c
++) {
2176 if (!(mask
& (1 << c
)))
2178 emit_set(pc
, i
, dst
[c
], -1, src
[0][c
], src
[1][c
]);
2181 case TGSI_OPCODE_SUB
:
2182 for (c
= 0; c
< 4; c
++) {
2183 if (!(mask
& (1 << c
)))
2185 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2188 case TGSI_OPCODE_TEX
:
2189 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2190 inst
->Texture
.Texture
, FALSE
);
2192 case TGSI_OPCODE_TXP
:
2193 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2194 inst
->Texture
.Texture
, TRUE
);
2196 case TGSI_OPCODE_TRUNC
:
2197 for (c
= 0; c
< 4; c
++) {
2198 if (!(mask
& (1 << c
)))
2200 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2201 CVTOP_TRUNC
, CVT_F32_F32
| CVT_RI
);
2204 case TGSI_OPCODE_XPD
:
2205 temp
= temp_temp(pc
);
2206 if (mask
& (1 << 0)) {
2207 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
2208 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
2210 if (mask
& (1 << 1)) {
2211 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
2212 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
2214 if (mask
& (1 << 2)) {
2215 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
2216 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
2218 if (mask
& (1 << 3))
2219 emit_mov_immdval(pc
, dst
[3], 1.0);
2221 case TGSI_OPCODE_END
:
2224 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
2230 emit_sat(pc
, brdc
, brdc
);
2231 for (c
= 0; c
< 4; c
++)
2232 if ((mask
& (1 << c
)) && dst
[c
] != brdc
)
2233 emit_mov(pc
, dst
[c
], brdc
);
2236 for (c
= 0; c
< 4; c
++) {
2237 if (!(mask
& (1 << c
)))
2239 /* In this case we saturate later, and dst[c] won't
2240 * be another temp_temp (and thus lost), since rdst
2241 * already is TEMP (see above). */
2242 if (rdst
[c
]->type
== P_TEMP
&& rdst
[c
]->index
< 0)
2244 emit_sat(pc
, rdst
[c
], dst
[c
]);
2248 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
2249 for (c
= 0; c
< 4; c
++) {
2252 if (src
[i
][c
]->acc
< 0 && src
[i
][c
]->type
== P_CONST
)
2253 FREE(src
[i
][c
]); /* indirect constant */
2258 pc
->reg_instance_nr
= 0;
2264 prep_inspect_insn(struct nv50_pc
*pc
, const struct tgsi_full_instruction
*insn
)
2266 struct nv50_reg
*reg
= NULL
;
2267 const struct tgsi_full_src_register
*src
;
2268 const struct tgsi_dst_register
*dst
;
2269 unsigned i
, c
, k
, mask
;
2271 dst
= &insn
->Dst
[0].Register
;
2272 mask
= dst
->WriteMask
;
2274 if (dst
->File
== TGSI_FILE_TEMPORARY
)
2277 if (dst
->File
== TGSI_FILE_OUTPUT
)
2281 for (c
= 0; c
< 4; c
++) {
2282 if (!(mask
& (1 << c
)))
2284 reg
[dst
->Index
* 4 + c
].acc
= pc
->insn_nr
;
2288 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2289 src
= &insn
->Src
[i
];
2291 if (src
->Register
.File
== TGSI_FILE_TEMPORARY
)
2294 if (src
->Register
.File
== TGSI_FILE_INPUT
)
2299 mask
= nv50_tgsi_src_mask(insn
, i
);
2301 for (c
= 0; c
< 4; c
++) {
2302 if (!(mask
& (1 << c
)))
2304 k
= tgsi_util_get_full_src_register_swizzle(src
, c
);
2306 reg
[src
->Register
.Index
* 4 + k
].acc
= pc
->insn_nr
;
2311 /* Returns a bitmask indicating which dst components need to be
2312 * written to temporaries first to avoid 'corrupting' sources.
2314 * m[i] (out) indicate component to write in the i-th position
2315 * rdep[c] (in) bitmasks of dst[i] that require dst[c] as source
2318 nv50_revdep_reorder(unsigned m
[4], unsigned rdep
[4])
2320 unsigned i
, c
, x
, unsafe
;
2322 for (c
= 0; c
< 4; c
++)
2325 /* Swap as long as a dst component written earlier is depended on
2326 * by one written later, but the next one isn't depended on by it.
2328 for (c
= 0; c
< 3; c
++) {
2329 if (rdep
[m
[c
+ 1]] & (1 << m
[c
]))
2330 continue; /* if next one is depended on by us */
2331 for (i
= c
+ 1; i
< 4; i
++)
2332 /* if we are depended on by a later one */
2333 if (rdep
[m
[c
]] & (1 << m
[i
]))
2346 /* mark dependencies that could not be resolved by reordering */
2347 for (i
= 0; i
< 3; ++i
)
2348 for (c
= i
+ 1; c
< 4; ++c
)
2349 if (rdep
[m
[i
]] & (1 << m
[c
]))
2352 /* NOTE: $unsafe is with respect to order, not component */
2356 /* Select a suitable dst register for broadcasting scalar results,
2357 * or return NULL if we have to allocate an extra TEMP.
2359 * If e.g. only 1 component is written, we may also emit the final
2360 * result to a write-only register.
2362 static struct nv50_reg
*
2363 tgsi_broadcast_dst(struct nv50_pc
*pc
,
2364 const struct tgsi_full_dst_register
*fd
, unsigned mask
)
2366 if (fd
->Register
.File
== TGSI_FILE_TEMPORARY
) {
2367 int c
= ffs(~mask
& fd
->Register
.WriteMask
);
2369 return tgsi_dst(pc
, c
- 1, fd
);
2371 int c
= ffs(fd
->Register
.WriteMask
) - 1;
2372 if ((1 << c
) == fd
->Register
.WriteMask
)
2373 return tgsi_dst(pc
, c
, fd
);
2379 /* Scan source swizzles and return a bitmask indicating dst regs that
2380 * also occur among the src regs, and fill rdep for nv50_revdep_reoder.
2383 nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction
*insn
,
2386 const struct tgsi_full_dst_register
*fd
= &insn
->Dst
[0];
2387 const struct tgsi_full_src_register
*fs
;
2388 unsigned i
, deqs
= 0;
2390 for (i
= 0; i
< 4; ++i
)
2393 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2394 unsigned chn
, mask
= nv50_tgsi_src_mask(insn
, i
);
2395 boolean neg_supp
= negate_supported(insn
, i
);
2398 if (fs
->Register
.File
!= fd
->Register
.File
||
2399 fs
->Register
.Index
!= fd
->Register
.Index
)
2402 for (chn
= 0; chn
< 4; ++chn
) {
2405 if (!(mask
& (1 << chn
))) /* src is not read */
2407 c
= tgsi_util_get_full_src_register_swizzle(fs
, chn
);
2408 s
= tgsi_util_get_full_src_register_sign_mode(fs
, chn
);
2410 if (!(fd
->Register
.WriteMask
& (1 << c
)))
2413 /* no danger if src is copied to TEMP first */
2414 if ((s
!= TGSI_UTIL_SIGN_KEEP
) &&
2415 (s
!= TGSI_UTIL_SIGN_TOGGLE
|| !neg_supp
))
2418 rdep
[c
] |= nv50_tgsi_dst_revdep(
2419 insn
->Instruction
.Opcode
, i
, chn
);
2428 nv50_tgsi_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
2430 struct tgsi_full_instruction insn
= tok
->FullInstruction
;
2431 const struct tgsi_full_dst_register
*fd
;
2432 unsigned i
, deqs
, rdep
[4], m
[4];
2434 fd
= &tok
->FullInstruction
.Dst
[0];
2435 deqs
= nv50_tgsi_scan_swizzle(&insn
, rdep
);
2437 if (is_scalar_op(insn
.Instruction
.Opcode
)) {
2438 pc
->r_brdc
= tgsi_broadcast_dst(pc
, fd
, deqs
);
2440 pc
->r_brdc
= temp_temp(pc
);
2441 return nv50_program_tx_insn(pc
, &insn
);
2446 return nv50_program_tx_insn(pc
, &insn
);
2448 deqs
= nv50_revdep_reorder(m
, rdep
);
2450 for (i
= 0; i
< 4; ++i
) {
2451 assert(pc
->r_dst
[m
[i
]] == NULL
);
2453 insn
.Dst
[0].Register
.WriteMask
=
2454 fd
->Register
.WriteMask
& (1 << m
[i
]);
2456 if (!insn
.Dst
[0].Register
.WriteMask
)
2459 if (deqs
& (1 << i
))
2460 pc
->r_dst
[m
[i
]] = alloc_temp(pc
, NULL
);
2462 if (!nv50_program_tx_insn(pc
, &insn
))
2466 for (i
= 0; i
< 4; i
++) {
2467 struct nv50_reg
*reg
= pc
->r_dst
[i
];
2470 pc
->r_dst
[i
] = NULL
;
2472 if (insn
.Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
2473 emit_sat(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2475 emit_mov(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2483 load_interpolant(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
2485 struct nv50_reg
*iv
, **ppiv
;
2486 unsigned mode
= pc
->interp_mode
[reg
->index
];
2488 ppiv
= (mode
& INTERP_CENTROID
) ? &pc
->iv_c
: &pc
->iv_p
;
2491 if ((mode
& INTERP_PERSPECTIVE
) && !iv
) {
2492 iv
= *ppiv
= alloc_temp(pc
, NULL
);
2493 iv
->rhw
= popcnt4(pc
->p
->cfg
.regs
[1] >> 24) - 1;
2495 emit_interp(pc
, iv
, NULL
, mode
& INTERP_CENTROID
);
2496 emit_flop(pc
, 0, iv
, iv
);
2498 /* XXX: when loading interpolants dynamically, move these
2499 * to the program head, or make sure it can't be skipped.
2503 emit_interp(pc
, reg
, iv
, mode
);
2506 /* The face input is always at v[255] (varying space), with a
2507 * value of 0 for back-facing, and 0xffffffff for front-facing.
2510 load_frontfacing(struct nv50_pc
*pc
, struct nv50_reg
*a
)
2512 struct nv50_reg
*one
= alloc_immd(pc
, 1.0f
);
2514 assert(a
->rhw
== -1);
2515 alloc_reg(pc
, a
); /* do this before rhw is set */
2517 load_interpolant(pc
, a
);
2518 emit_bitop2(pc
, a
, a
, one
, TGSI_OPCODE_AND
);
2524 nv50_program_tx_prep(struct nv50_pc
*pc
)
2526 struct tgsi_parse_context tp
;
2527 struct nv50_program
*p
= pc
->p
;
2528 boolean ret
= FALSE
;
2529 unsigned i
, c
, flat_nr
= 0;
2531 tgsi_parse_init(&tp
, pc
->p
->pipe
.tokens
);
2532 while (!tgsi_parse_end_of_tokens(&tp
)) {
2533 const union tgsi_full_token
*tok
= &tp
.FullToken
;
2535 tgsi_parse_token(&tp
);
2536 switch (tok
->Token
.Type
) {
2537 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2539 const struct tgsi_full_immediate
*imm
=
2540 &tp
.FullToken
.FullImmediate
;
2542 ctor_immd(pc
, imm
->u
[0].Float
,
2548 case TGSI_TOKEN_TYPE_DECLARATION
:
2550 const struct tgsi_full_declaration
*d
;
2551 unsigned si
, last
, first
, mode
;
2553 d
= &tp
.FullToken
.FullDeclaration
;
2554 first
= d
->Range
.First
;
2555 last
= d
->Range
.Last
;
2557 switch (d
->Declaration
.File
) {
2558 case TGSI_FILE_TEMPORARY
:
2560 case TGSI_FILE_OUTPUT
:
2561 if (!d
->Declaration
.Semantic
||
2562 p
->type
== PIPE_SHADER_FRAGMENT
)
2565 si
= d
->Semantic
.Index
;
2566 switch (d
->Semantic
.Name
) {
2567 case TGSI_SEMANTIC_BCOLOR
:
2568 p
->cfg
.two_side
[si
].hw
= first
;
2569 if (p
->cfg
.io_nr
> first
)
2570 p
->cfg
.io_nr
= first
;
2572 case TGSI_SEMANTIC_PSIZE
:
2573 p
->cfg
.psiz
= first
;
2574 if (p
->cfg
.io_nr
> first
)
2575 p
->cfg
.io_nr
= first
;
2578 case TGSI_SEMANTIC_CLIP_DISTANCE:
2579 p->cfg.clpd = MIN2(p->cfg.clpd, first);
2586 case TGSI_FILE_INPUT
:
2588 if (p
->type
!= PIPE_SHADER_FRAGMENT
)
2591 switch (d
->Declaration
.Interpolate
) {
2592 case TGSI_INTERPOLATE_CONSTANT
:
2596 case TGSI_INTERPOLATE_PERSPECTIVE
:
2597 mode
= INTERP_PERSPECTIVE
;
2598 p
->cfg
.regs
[1] |= 0x08 << 24;
2601 mode
= INTERP_LINEAR
;
2604 if (d
->Declaration
.Centroid
)
2605 mode
|= INTERP_CENTROID
;
2608 for (i
= first
; i
<= last
; i
++)
2609 pc
->interp_mode
[i
] = mode
;
2612 case TGSI_FILE_ADDRESS
:
2613 case TGSI_FILE_CONSTANT
:
2614 case TGSI_FILE_SAMPLER
:
2617 NOUVEAU_ERR("bad decl file %d\n",
2618 d
->Declaration
.File
);
2623 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2625 prep_inspect_insn(pc
, &tok
->FullInstruction
);
2632 if (p
->type
== PIPE_SHADER_VERTEX
) {
2635 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
) {
2636 if (pc
->attr
[i
].acc
) {
2637 pc
->attr
[i
].hw
= rid
++;
2638 p
->cfg
.attr
[i
/ 32] |= 1 << (i
% 32);
2642 for (i
= 0, rid
= 0; i
< pc
->result_nr
; ++i
) {
2643 p
->cfg
.io
[i
].hw
= rid
;
2644 p
->cfg
.io
[i
].id
= i
;
2646 for (c
= 0; c
< 4; ++c
) {
2648 if (!pc
->result
[n
].acc
)
2650 pc
->result
[n
].hw
= rid
++;
2651 p
->cfg
.io
[i
].mask
|= 1 << c
;
2655 for (c
= 0; c
< 2; ++c
)
2656 if (p
->cfg
.two_side
[c
].hw
< 0x40)
2657 p
->cfg
.two_side
[c
] = p
->cfg
.io
[
2658 p
->cfg
.two_side
[c
].hw
];
2660 if (p
->cfg
.psiz
< 0x40)
2661 p
->cfg
.psiz
= p
->cfg
.io
[p
->cfg
.psiz
].hw
;
2663 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
2665 unsigned n
= 0, m
= pc
->attr_nr
- flat_nr
;
2669 int base
= (TGSI_SEMANTIC_POSITION
==
2670 p
->info
.input_semantic_name
[0]) ? 0 : 1;
2672 /* non-flat interpolants have to be mapped to
2673 * the lower hardware IDs, so sort them:
2675 for (i
= 0; i
< pc
->attr_nr
; i
++) {
2676 if (pc
->interp_mode
[i
] == INTERP_FLAT
)
2677 p
->cfg
.io
[m
++].id
= i
;
2679 if (!(pc
->interp_mode
[i
] & INTERP_PERSPECTIVE
))
2680 p
->cfg
.io
[n
].linear
= TRUE
;
2681 p
->cfg
.io
[n
++].id
= i
;
2685 if (!base
) /* set w-coordinate mask from perspective interp */
2686 p
->cfg
.io
[0].mask
|= p
->cfg
.regs
[1] >> 24;
2688 aid
= popcnt4( /* if fcrd isn't contained in cfg.io */
2689 base
? (p
->cfg
.regs
[1] >> 24) : p
->cfg
.io
[0].mask
);
2691 for (n
= 0; n
< pc
->attr_nr
; ++n
) {
2692 p
->cfg
.io
[n
].hw
= rid
= aid
;
2693 i
= p
->cfg
.io
[n
].id
;
2695 if (p
->info
.input_semantic_name
[n
] ==
2696 TGSI_SEMANTIC_FACE
) {
2697 load_frontfacing(pc
, &pc
->attr
[i
* 4]);
2701 for (c
= 0; c
< 4; ++c
) {
2702 if (!pc
->attr
[i
* 4 + c
].acc
)
2704 pc
->attr
[i
* 4 + c
].rhw
= rid
++;
2705 p
->cfg
.io
[n
].mask
|= 1 << c
;
2707 load_interpolant(pc
, &pc
->attr
[i
* 4 + c
]);
2709 aid
+= popcnt4(p
->cfg
.io
[n
].mask
);
2713 p
->cfg
.regs
[1] |= p
->cfg
.io
[0].mask
<< 24;
2715 m
= popcnt4(p
->cfg
.regs
[1] >> 24);
2717 /* set count of non-position inputs and of non-flat
2718 * non-position inputs for FP_INTERPOLANT_CTRL
2720 p
->cfg
.regs
[1] |= aid
- m
;
2723 i
= p
->cfg
.io
[pc
->attr_nr
- flat_nr
].hw
;
2724 p
->cfg
.regs
[1] |= (i
- m
) << 16;
2726 p
->cfg
.regs
[1] |= p
->cfg
.regs
[1] << 16;
2728 /* mark color semantic for light-twoside */
2730 for (i
= 0; i
< pc
->attr_nr
; i
++) {
2733 sn
= p
->info
.input_semantic_name
[p
->cfg
.io
[i
].id
];
2734 si
= p
->info
.input_semantic_index
[p
->cfg
.io
[i
].id
];
2736 if (sn
== TGSI_SEMANTIC_COLOR
) {
2737 p
->cfg
.two_side
[si
] = p
->cfg
.io
[i
];
2739 /* increase colour count */
2740 p
->cfg
.regs
[0] += popcnt4(
2741 p
->cfg
.two_side
[si
].mask
) << 16;
2743 n
= MIN2(n
, p
->cfg
.io
[i
].hw
- m
);
2747 p
->cfg
.regs
[0] += n
;
2749 /* Initialize FP results:
2750 * FragDepth is always first TGSI and last hw output
2752 i
= p
->info
.writes_z
? 4 : 0;
2753 for (rid
= 0; i
< pc
->result_nr
* 4; i
++)
2754 pc
->result
[i
].rhw
= rid
++;
2755 if (p
->info
.writes_z
)
2756 pc
->result
[2].rhw
= rid
;
2758 p
->cfg
.high_result
= rid
;
2760 /* separate/different colour results for MRTs ? */
2761 if (pc
->result_nr
- (p
->info
.writes_z
? 1 : 0) > 1)
2762 p
->cfg
.regs
[2] |= 1;
2768 pc
->immd
= MALLOC(pc
->immd_nr
* 4 * sizeof(struct nv50_reg
));
2772 for (i
= 0; i
< pc
->immd_nr
; i
++) {
2773 for (c
= 0; c
< 4; c
++, rid
++)
2774 ctor_reg(&pc
->immd
[rid
], P_IMMD
, i
, rid
);
2781 free_temp(pc
, pc
->iv_p
);
2783 free_temp(pc
, pc
->iv_c
);
2785 tgsi_parse_free(&tp
);
2790 free_nv50_pc(struct nv50_pc
*pc
)
2807 ctor_nv50_pc(struct nv50_pc
*pc
, struct nv50_program
*p
)
2810 unsigned rtype
[2] = { P_ATTR
, P_RESULT
};
2813 pc
->temp_nr
= p
->info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
2814 pc
->attr_nr
= p
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
2815 pc
->result_nr
= p
->info
.file_max
[TGSI_FILE_OUTPUT
] + 1;
2816 pc
->param_nr
= p
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
2817 pc
->addr_nr
= p
->info
.file_max
[TGSI_FILE_ADDRESS
] + 1;
2818 assert(pc
->addr_nr
<= 2);
2820 p
->cfg
.high_temp
= 4;
2822 p
->cfg
.two_side
[0].hw
= 0x40;
2823 p
->cfg
.two_side
[1].hw
= 0x40;
2826 case PIPE_SHADER_VERTEX
:
2829 p
->cfg
.io_nr
= pc
->result_nr
;
2831 case PIPE_SHADER_FRAGMENT
:
2832 rtype
[0] = rtype
[1] = P_TEMP
;
2834 p
->cfg
.regs
[0] = 0x01000004;
2835 p
->cfg
.io_nr
= pc
->attr_nr
;
2837 if (p
->info
.writes_z
) {
2838 p
->cfg
.regs
[2] |= 0x00000100;
2839 p
->cfg
.regs
[3] |= 0x00000011;
2841 if (p
->info
.uses_kill
)
2842 p
->cfg
.regs
[2] |= 0x00100000;
2847 pc
->temp
= MALLOC(pc
->temp_nr
* 4 * sizeof(struct nv50_reg
));
2851 for (i
= 0; i
< pc
->temp_nr
* 4; ++i
)
2852 ctor_reg(&pc
->temp
[i
], P_TEMP
, i
/ 4, -1);
2856 pc
->attr
= MALLOC(pc
->attr_nr
* 4 * sizeof(struct nv50_reg
));
2860 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
)
2861 ctor_reg(&pc
->attr
[i
], rtype
[0], i
/ 4, -1);
2864 if (pc
->result_nr
) {
2865 unsigned nr
= pc
->result_nr
* 4;
2867 pc
->result
= MALLOC(nr
* sizeof(struct nv50_reg
));
2871 for (i
= 0; i
< nr
; ++i
)
2872 ctor_reg(&pc
->result
[i
], rtype
[1], i
/ 4, -1);
2878 pc
->param
= MALLOC(pc
->param_nr
* 4 * sizeof(struct nv50_reg
));
2882 for (i
= 0; i
< pc
->param_nr
; ++i
)
2883 for (c
= 0; c
< 4; ++c
, ++rid
)
2884 ctor_reg(&pc
->param
[rid
], P_CONST
, i
, rid
);
2888 pc
->addr
= CALLOC(pc
->addr_nr
* 4, sizeof(struct nv50_reg
*));
2892 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
2893 ctor_reg(&pc
->r_addr
[i
], P_ADDR
, -256, i
+ 1);
2899 nv50_fp_move_results(struct nv50_pc
*pc
)
2901 struct nv50_reg reg
;
2904 ctor_reg(®
, P_TEMP
, -1, -1);
2906 for (i
= 0; i
< pc
->result_nr
* 4; ++i
) {
2907 if (pc
->result
[i
].rhw
< 0 || pc
->result
[i
].hw
< 0)
2909 if (pc
->result
[i
].rhw
!= pc
->result
[i
].hw
) {
2910 reg
.hw
= pc
->result
[i
].rhw
;
2911 emit_mov(pc
, ®
, &pc
->result
[i
]);
2917 nv50_program_fixup_insns(struct nv50_pc
*pc
)
2919 struct nv50_program_exec
*e
, **bra_list
;
2922 bra_list
= CALLOC(pc
->p
->exec_size
, sizeof(struct nv50_program_exec
*));
2924 /* Collect branch instructions, we need to adjust their offsets
2925 * when converting 32 bit instructions to 64 bit ones
2927 for (n
= 0, e
= pc
->p
->exec_head
; e
; e
= e
->next
)
2928 if (e
->param
.index
>= 0 && !e
->param
.mask
)
2931 /* last instruction must be long so it can have the exit bit set */
2932 if (!is_long(pc
->p
->exec_tail
))
2933 convert_to_long(pc
, pc
->p
->exec_tail
);
2935 pc
->p
->exec_tail
->inst
[1] |= 1;
2937 /* !immd on exit insn simultaneously means !join */
2938 assert(!is_immd(pc
->p
->exec_head
));
2939 assert(!is_immd(pc
->p
->exec_tail
));
2941 /* Make sure we don't have any single 32 bit instructions. */
2942 for (e
= pc
->p
->exec_head
, pos
= 0; e
; e
= e
->next
) {
2943 pos
+= is_long(e
) ? 2 : 1;
2945 if ((pos
& 1) && (!e
->next
|| is_long(e
->next
))) {
2946 for (i
= 0; i
< n
; ++i
)
2947 if (bra_list
[i
]->param
.index
>= pos
)
2948 bra_list
[i
]->param
.index
+= 1;
2949 convert_to_long(pc
, e
);
2958 nv50_program_tx(struct nv50_program
*p
)
2960 struct tgsi_parse_context parse
;
2964 pc
= CALLOC_STRUCT(nv50_pc
);
2968 ret
= ctor_nv50_pc(pc
, p
);
2972 ret
= nv50_program_tx_prep(pc
);
2976 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
2977 while (!tgsi_parse_end_of_tokens(&parse
)) {
2978 const union tgsi_full_token
*tok
= &parse
.FullToken
;
2980 /* don't allow half insn/immd on first and last instruction */
2982 if (pc
->insn_cur
== 0 || pc
->insn_cur
+ 2 == pc
->insn_nr
)
2983 pc
->allow32
= FALSE
;
2985 tgsi_parse_token(&parse
);
2987 switch (tok
->Token
.Type
) {
2988 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2990 ret
= nv50_tgsi_insn(pc
, tok
);
2999 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
)
3000 nv50_fp_move_results(pc
);
3002 nv50_program_fixup_insns(pc
);
3004 p
->param_nr
= pc
->param_nr
* 4;
3005 p
->immd_nr
= pc
->immd_nr
* 4;
3006 p
->immd
= pc
->immd_buf
;
3009 tgsi_parse_free(&parse
);
3017 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
3019 if (nv50_program_tx(p
) == FALSE
)
3021 p
->translated
= TRUE
;
3025 nv50_program_upload_data(struct nv50_context
*nv50
, float *map
,
3026 unsigned start
, unsigned count
, unsigned cbuf
)
3028 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
3029 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3032 unsigned nr
= count
> 2047 ? 2047 : count
;
3034 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
3035 OUT_RING (chan
, (cbuf
<< 0) | (start
<< 8));
3036 BEGIN_RING(chan
, tesla
, NV50TCL_CB_DATA(0) | 0x40000000, nr
);
3037 OUT_RINGp (chan
, map
, nr
);
3046 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
3048 struct pipe_screen
*pscreen
= nv50
->pipe
.screen
;
3050 if (!p
->data
[0] && p
->immd_nr
) {
3051 struct nouveau_resource
*heap
= nv50
->screen
->immd_heap
[0];
3053 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
, &p
->data
[0])) {
3054 while (heap
->next
&& heap
->size
< p
->immd_nr
) {
3055 struct nv50_program
*evict
= heap
->next
->priv
;
3056 nouveau_resource_free(&evict
->data
[0]);
3059 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
,
3064 /* immediates only need to be uploaded again when freed */
3065 nv50_program_upload_data(nv50
, p
->immd
, p
->data
[0]->start
,
3066 p
->immd_nr
, NV50_CB_PMISC
);
3069 assert(p
->param_nr
<= 512);
3073 float *map
= pipe_buffer_map(pscreen
, nv50
->constbuf
[p
->type
],
3074 PIPE_BUFFER_USAGE_CPU_READ
);
3076 if (p
->type
== PIPE_SHADER_VERTEX
)
3081 nv50_program_upload_data(nv50
, map
, 0, p
->param_nr
, cb
);
3082 pipe_buffer_unmap(pscreen
, nv50
->constbuf
[p
->type
]);
3087 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
3089 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
3090 struct nv50_program_exec
*e
;
3092 boolean upload
= FALSE
;
3095 nouveau_bo_new(chan
->device
, NOUVEAU_BO_VRAM
, 0x100,
3096 p
->exec_size
* 4, &p
->bo
);
3100 if (p
->data
[0] && p
->data
[0]->start
!= p
->data_start
[0])
3106 up
= MALLOC(p
->exec_size
* 4);
3108 for (i
= 0, e
= p
->exec_head
; e
; e
= e
->next
) {
3109 unsigned ei
, ci
, bs
;
3111 if (e
->param
.index
>= 0 && e
->param
.mask
) {
3112 bs
= (e
->inst
[1] >> 22) & 0x07;
3114 ei
= e
->param
.shift
>> 5;
3115 ci
= e
->param
.index
;
3117 ci
+= p
->data
[bs
]->start
;
3119 e
->inst
[ei
] &= ~e
->param
.mask
;
3120 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
3122 if (e
->param
.index
>= 0) {
3123 /* zero mask means param is a jump/branch offset */
3124 assert(!(e
->param
.index
& 1));
3125 /* seem to be 8 byte steps */
3126 ei
= (e
->param
.index
>> 1) + 0 /* START_ID */;
3128 e
->inst
[0] &= 0xf0000fff;
3129 e
->inst
[0] |= ei
<< 12;
3132 up
[i
++] = e
->inst
[0];
3134 up
[i
++] = e
->inst
[1];
3136 assert(i
== p
->exec_size
);
3139 p
->data_start
[0] = p
->data
[0]->start
;
3141 #ifdef NV50_PROGRAM_DUMP
3142 NOUVEAU_ERR("-------\n");
3143 for (e
= p
->exec_head
; e
; e
= e
->next
) {
3144 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
3146 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
3149 nv50_upload_sifc(nv50
, p
->bo
, 0, NOUVEAU_BO_VRAM
,
3150 NV50_2D_DST_FORMAT_R8_UNORM
, 65536, 1, 262144,
3151 up
, NV50_2D_SIFC_FORMAT_R8_UNORM
, 0,
3152 0, 0, p
->exec_size
* 4, 1, 1);
3158 nv50_vertprog_validate(struct nv50_context
*nv50
)
3160 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3161 struct nv50_program
*p
= nv50
->vertprog
;
3162 struct nouveau_stateobj
*so
;
3164 if (!p
->translated
) {
3165 nv50_program_validate(nv50
, p
);
3170 nv50_program_validate_data(nv50
, p
);
3171 nv50_program_validate_code(nv50
, p
);
3174 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
3175 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3176 NOUVEAU_BO_HIGH
, 0, 0);
3177 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3178 NOUVEAU_BO_LOW
, 0, 0);
3179 so_method(so
, tesla
, NV50TCL_VP_ATTR_EN_0
, 2);
3180 so_data (so
, p
->cfg
.attr
[0]);
3181 so_data (so
, p
->cfg
.attr
[1]);
3182 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_RESULT
, 1);
3183 so_data (so
, p
->cfg
.high_result
);
3184 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 2);
3185 so_data (so
, p
->cfg
.high_result
); //8);
3186 so_data (so
, p
->cfg
.high_temp
);
3187 so_method(so
, tesla
, NV50TCL_VP_START_ID
, 1);
3188 so_data (so
, 0); /* program start offset */
3189 so_ref(so
, &nv50
->state
.vertprog
);
3194 nv50_fragprog_validate(struct nv50_context
*nv50
)
3196 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3197 struct nv50_program
*p
= nv50
->fragprog
;
3198 struct nouveau_stateobj
*so
;
3200 if (!p
->translated
) {
3201 nv50_program_validate(nv50
, p
);
3206 nv50_program_validate_data(nv50
, p
);
3207 nv50_program_validate_code(nv50
, p
);
3210 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
3211 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3212 NOUVEAU_BO_HIGH
, 0, 0);
3213 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3214 NOUVEAU_BO_LOW
, 0, 0);
3215 so_method(so
, tesla
, NV50TCL_FP_REG_ALLOC_TEMP
, 1);
3216 so_data (so
, p
->cfg
.high_temp
);
3217 so_method(so
, tesla
, NV50TCL_FP_RESULT_COUNT
, 1);
3218 so_data (so
, p
->cfg
.high_result
);
3219 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK19A8
, 1);
3220 so_data (so
, p
->cfg
.regs
[2]);
3221 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK196C
, 1);
3222 so_data (so
, p
->cfg
.regs
[3]);
3223 so_method(so
, tesla
, NV50TCL_FP_START_ID
, 1);
3224 so_data (so
, 0); /* program start offset */
3225 so_ref(so
, &nv50
->state
.fragprog
);
3230 nv50_pntc_replace(struct nv50_context
*nv50
, uint32_t pntc
[8], unsigned base
)
3232 struct nv50_program
*fp
= nv50
->fragprog
;
3233 struct nv50_program
*vp
= nv50
->vertprog
;
3234 unsigned i
, c
, m
= base
;
3236 /* XXX: this might not work correctly in all cases yet - we'll
3237 * just assume that an FP generic input that is not written in
3238 * the VP is PointCoord.
3240 memset(pntc
, 0, 8 * sizeof(uint32_t));
3242 for (i
= 0; i
< fp
->cfg
.io_nr
; i
++) {
3244 uint8_t j
, k
= fp
->cfg
.io
[i
].id
;
3245 unsigned n
= popcnt4(fp
->cfg
.io
[i
].mask
);
3247 if (fp
->info
.input_semantic_name
[k
] != TGSI_SEMANTIC_GENERIC
) {
3252 for (j
= 0; j
< vp
->info
.num_outputs
; ++j
) {
3253 sn
= vp
->info
.output_semantic_name
[j
];
3254 si
= vp
->info
.output_semantic_index
[j
];
3256 if (sn
== fp
->info
.input_semantic_name
[k
] &&
3257 si
== fp
->info
.input_semantic_index
[k
])
3261 if (j
< vp
->info
.num_outputs
) {
3263 nv50
->rasterizer
->pipe
.sprite_coord_mode
[si
];
3265 if (mode
== PIPE_SPRITE_COORD_NONE
) {
3271 /* this is either PointCoord or replaced by sprite coords */
3272 for (c
= 0; c
< 4; c
++) {
3273 if (!(fp
->cfg
.io
[i
].mask
& (1 << c
)))
3275 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
3282 nv50_sreg4_map(uint32_t *p_map
, int mid
, uint32_t lin
[4],
3283 struct nv50_sreg4
*fpi
, struct nv50_sreg4
*vpo
)
3286 uint8_t mv
= vpo
->mask
, mf
= fpi
->mask
, oid
= vpo
->hw
;
3287 uint8_t *map
= (uint8_t *)p_map
;
3289 for (c
= 0; c
< 4; ++c
) {
3291 if (fpi
->linear
== TRUE
)
3292 lin
[mid
/ 32] |= 1 << (mid
% 32);
3293 map
[mid
++] = (mv
& 1) ? oid
: ((c
== 3) ? 0x41 : 0x40);
3305 nv50_linkage_validate(struct nv50_context
*nv50
)
3307 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3308 struct nv50_program
*vp
= nv50
->vertprog
;
3309 struct nv50_program
*fp
= nv50
->fragprog
;
3310 struct nouveau_stateobj
*so
;
3311 struct nv50_sreg4 dummy
, *vpo
;
3313 uint32_t map
[16], lin
[4], reg
[5], pcrd
[8];
3315 memset(map
, 0, sizeof(map
));
3316 memset(lin
, 0, sizeof(lin
));
3318 reg
[1] = 0x00000004; /* low and high clip distance map ids */
3319 reg
[2] = 0x00000000; /* layer index map id (disabled, GP only) */
3320 reg
[3] = 0x00000000; /* point size map id & enable */
3321 reg
[0] = fp
->cfg
.regs
[0]; /* colour semantic reg */
3322 reg
[4] = fp
->cfg
.regs
[1]; /* interpolant info */
3324 dummy
.linear
= FALSE
;
3325 dummy
.mask
= 0xf; /* map all components of HPOS */
3326 m
= nv50_sreg4_map(map
, m
, lin
, &dummy
, &vp
->cfg
.io
[0]);
3330 if (vp
->cfg
.clpd
< 0x40) {
3331 for (c
= 0; c
< vp
->cfg
.clpd_nr
; ++c
)
3332 map
[m
++] = vp
->cfg
.clpd
+ c
;
3336 reg
[0] |= m
<< 8; /* adjust BFC0 id */
3338 /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
3339 if (nv50
->rasterizer
->pipe
.light_twoside
) {
3340 vpo
= &vp
->cfg
.two_side
[0];
3342 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[0], &vpo
[0]);
3343 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[1], &vpo
[1]);
3346 reg
[0] += m
- 4; /* adjust FFC0 id */
3347 reg
[4] |= m
<< 8; /* set mid where 'normal' FP inputs start */
3349 for (i
= 0; i
< fp
->cfg
.io_nr
; i
++) {
3350 ubyte sn
= fp
->info
.input_semantic_name
[fp
->cfg
.io
[i
].id
];
3351 ubyte si
= fp
->info
.input_semantic_index
[fp
->cfg
.io
[i
].id
];
3353 /* position must be mapped first */
3354 assert(i
== 0 || sn
!= TGSI_SEMANTIC_POSITION
);
3356 /* maybe even remove these from cfg.io */
3357 if (sn
== TGSI_SEMANTIC_POSITION
|| sn
== TGSI_SEMANTIC_FACE
)
3360 /* VP outputs and vp->cfg.io are in the same order */
3361 for (n
= 0; n
< vp
->info
.num_outputs
; ++n
) {
3362 if (vp
->info
.output_semantic_name
[n
] == sn
&&
3363 vp
->info
.output_semantic_index
[n
] == si
)
3366 vpo
= (n
< vp
->info
.num_outputs
) ? &vp
->cfg
.io
[n
] : &dummy
;
3368 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.io
[i
], vpo
);
3371 if (nv50
->rasterizer
->pipe
.point_size_per_vertex
) {
3372 map
[m
/ 4] |= vp
->cfg
.psiz
<< ((m
% 4) * 8);
3373 reg
[3] = (m
++ << 4) | 1;
3376 /* now fill the stateobj */
3380 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
3382 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), n
);
3383 so_datap (so
, map
, n
);
3385 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_0
, 4);
3386 so_datap (so
, reg
, 4);
3388 so_method(so
, tesla
, NV50TCL_FP_INTERPOLANT_CTRL
, 1);
3389 so_data (so
, reg
[4]);
3391 so_method(so
, tesla
, 0x1540, 4);
3392 so_datap (so
, lin
, 4);
3394 if (nv50
->rasterizer
->pipe
.point_sprite
) {
3395 nv50_pntc_replace(nv50
, pcrd
, (reg
[4] >> 8) & 0xff);
3397 so_method(so
, tesla
, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
3398 so_datap (so
, pcrd
, 8);
3401 so_ref(so
, &nv50
->state
.programs
);
3406 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
3408 while (p
->exec_head
) {
3409 struct nv50_program_exec
*e
= p
->exec_head
;
3411 p
->exec_head
= e
->next
;
3414 p
->exec_tail
= NULL
;
3417 nouveau_bo_ref(NULL
, &p
->bo
);
3419 nouveau_resource_free(&p
->data
[0]);