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 /* STACK: Conditionals and loops have to use the (per warp) stack.
102 * Stack entries consist of an entry type (divergent path, join at),
103 * a mask indicating the active threads of the warp, and an address.
104 * MPs can store 12 stack entries internally, if we need more (and
105 * we probably do), we have to create a stack buffer in VRAM.
107 /* impose low limits for now */
108 #define NV50_MAX_COND_NESTING 4
109 #define NV50_MAX_LOOP_NESTING 3
111 #define JOIN_ON(e) e; pc->p->exec_tail->inst[1] |= 2
114 struct nv50_program
*p
;
117 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
118 struct nv50_reg r_addr
[NV50_SU_MAX_ADDR
];
121 struct nv50_reg
*temp
;
123 struct nv50_reg
*attr
;
125 struct nv50_reg
*result
;
127 struct nv50_reg
*param
;
129 struct nv50_reg
*immd
;
132 struct nv50_reg
**addr
;
134 uint8_t addr_alloc
; /* set bit indicates used for TGSI_FILE_ADDRESS */
136 struct nv50_reg
*temp_temp
[16];
137 unsigned temp_temp_nr
;
139 /* broadcast and destination replacement regs */
140 struct nv50_reg
*r_brdc
;
141 struct nv50_reg
*r_dst
[4];
143 struct nv50_reg reg_instances
[16];
144 unsigned reg_instance_nr
;
146 unsigned interp_mode
[32];
147 /* perspective interpolation registers */
148 struct nv50_reg
*iv_p
;
149 struct nv50_reg
*iv_c
;
151 struct nv50_program_exec
*if_insn
[NV50_MAX_COND_NESTING
];
152 struct nv50_program_exec
*if_join
[NV50_MAX_COND_NESTING
];
153 struct nv50_program_exec
*loop_brka
[NV50_MAX_LOOP_NESTING
];
154 int if_lvl
, loop_lvl
;
155 unsigned loop_pos
[NV50_MAX_LOOP_NESTING
];
157 /* current instruction and total number of insns */
164 static INLINE
struct nv50_reg
*
165 reg_instance(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
169 assert(pc
->reg_instance_nr
< 16);
170 ri
= &pc
->reg_instances
[pc
->reg_instance_nr
++];
179 ctor_reg(struct nv50_reg
*reg
, unsigned type
, int index
, int hw
)
189 static INLINE
unsigned
190 popcnt4(uint32_t val
)
192 static const unsigned cnt
[16]
193 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
194 return cnt
[val
& 0xf];
198 terminate_mbb(struct nv50_pc
*pc
)
202 /* remove records of temporary address register values */
203 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
204 pc
->r_addr
[i
].rhw
= -1;
208 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
212 if (reg
->type
== P_RESULT
) {
213 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
214 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
217 if (reg
->type
!= P_TEMP
)
221 /*XXX: do this here too to catch FP temp-as-attr usage..
222 * not clean, but works */
223 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
224 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
228 if (reg
->rhw
!= -1) {
229 /* try to allocate temporary with index rhw first */
230 if (!(pc
->r_temp
[reg
->rhw
])) {
231 pc
->r_temp
[reg
->rhw
] = reg
;
233 if (pc
->p
->cfg
.high_temp
< (reg
->rhw
+ 1))
234 pc
->p
->cfg
.high_temp
= reg
->rhw
+ 1;
237 /* make sure we don't get things like $r0 needs to go
238 * in $r1 and $r1 in $r0
240 i
= pc
->result_nr
* 4;
243 for (; i
< NV50_SU_MAX_TEMP
; i
++) {
244 if (!(pc
->r_temp
[i
])) {
247 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
248 pc
->p
->cfg
.high_temp
= i
+ 1;
256 /* XXX: For shaders that aren't executed linearly (e.g. shaders that
257 * contain loops), we need to assign all hw regs to TGSI TEMPs early,
258 * lest we risk temp_temps overwriting regs alloc'd "later".
260 static struct nv50_reg
*
261 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
266 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
269 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
270 if (!pc
->r_temp
[i
]) {
271 r
= MALLOC_STRUCT(nv50_reg
);
272 ctor_reg(r
, P_TEMP
, -1, i
);
282 /* Assign the hw of the discarded temporary register src
283 * to the tgsi register dst and free src.
286 assimilate_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
288 assert(src
->index
== -1 && src
->hw
!= -1);
291 pc
->r_temp
[dst
->hw
] = NULL
;
292 pc
->r_temp
[src
->hw
] = dst
;
298 /* release the hardware resource held by r */
300 release_hw(struct nv50_pc
*pc
, struct nv50_reg
*r
)
302 assert(r
->type
== P_TEMP
);
306 assert(pc
->r_temp
[r
->hw
] == r
);
307 pc
->r_temp
[r
->hw
] = NULL
;
315 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
317 if (r
->index
== -1) {
320 FREE(pc
->r_temp
[hw
]);
321 pc
->r_temp
[hw
] = NULL
;
326 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
330 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
333 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
334 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
335 return alloc_temp4(pc
, dst
, idx
+ 4);
337 for (i
= 0; i
< 4; i
++) {
338 dst
[i
] = MALLOC_STRUCT(nv50_reg
);
339 ctor_reg(dst
[i
], P_TEMP
, -1, idx
+ i
);
340 pc
->r_temp
[idx
+ i
] = dst
[i
];
347 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
351 for (i
= 0; i
< 4; i
++)
352 free_temp(pc
, reg
[i
]);
355 static struct nv50_reg
*
356 temp_temp(struct nv50_pc
*pc
)
358 if (pc
->temp_temp_nr
>= 16)
361 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
362 return pc
->temp_temp
[pc
->temp_temp_nr
++];
366 kill_temp_temp(struct nv50_pc
*pc
)
370 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
371 free_temp(pc
, pc
->temp_temp
[i
]);
372 pc
->temp_temp_nr
= 0;
376 ctor_immd_4u32(struct nv50_pc
*pc
,
377 uint32_t x
, uint32_t y
, uint32_t z
, uint32_t w
)
379 unsigned size
= pc
->immd_nr
* 4 * sizeof(uint32_t);
381 pc
->immd_buf
= REALLOC(pc
->immd_buf
, size
, size
+ 4 * sizeof(uint32_t));
383 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
384 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
385 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
386 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
388 return pc
->immd_nr
++;
392 ctor_immd_4f32(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
394 return ctor_immd_4u32(pc
, fui(x
), fui(y
), fui(z
), fui(w
));
397 static struct nv50_reg
*
398 alloc_immd(struct nv50_pc
*pc
, float f
)
400 struct nv50_reg
*r
= MALLOC_STRUCT(nv50_reg
);
403 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
404 if (pc
->immd_buf
[hw
] == fui(f
))
407 if (hw
== pc
->immd_nr
* 4)
408 hw
= ctor_immd_4f32(pc
, f
, -f
, 0.5 * f
, 0) * 4;
410 ctor_reg(r
, P_IMMD
, -1, hw
);
414 static struct nv50_program_exec
*
415 exec(struct nv50_pc
*pc
)
417 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
424 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
426 struct nv50_program
*p
= pc
->p
;
429 p
->exec_tail
->next
= e
;
433 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
436 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
439 is_long(struct nv50_program_exec
*e
)
447 is_immd(struct nv50_program_exec
*e
)
449 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
455 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
456 struct nv50_program_exec
*e
)
459 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
460 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
464 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
465 struct nv50_program_exec
*e
)
468 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
469 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
473 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
479 set_pred(pc
, 0xf, 0, e
);
480 set_pred_wr(pc
, 0, 0, e
);
484 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
486 if (dst
->type
== P_RESULT
) {
488 e
->inst
[1] |= 0x00000008;
494 e
->inst
[0] |= (dst
->hw
<< 2);
498 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
504 u
.ui
= pc
->immd_buf
[imm
->hw
];
506 u
.f
= (imm
->mod
& NV50_MOD_ABS
) ? fabsf(u
.f
) : u
.f
;
507 u
.f
= (imm
->mod
& NV50_MOD_NEG
) ? -u
.f
: u
.f
;
510 /* XXX: can't be predicated - bits overlap; cases where both
511 * are required should be avoided by using pc->allow32 */
512 set_pred(pc
, 0, 0, e
);
513 set_pred_wr(pc
, 0, 0, e
);
515 e
->inst
[1] |= 0x00000002 | 0x00000001;
516 e
->inst
[0] |= (u
.ui
& 0x3f) << 16;
517 e
->inst
[1] |= (u
.ui
>> 6) << 2;
521 set_addr(struct nv50_program_exec
*e
, struct nv50_reg
*a
)
523 assert(!(e
->inst
[0] & 0x0c000000));
524 assert(!(e
->inst
[1] & 0x00000004));
526 e
->inst
[0] |= (a
->hw
& 3) << 26;
527 e
->inst
[1] |= (a
->hw
>> 2) << 2;
531 emit_add_addr_imm(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
532 struct nv50_reg
*src0
, uint16_t src1_val
)
534 struct nv50_program_exec
*e
= exec(pc
);
536 e
->inst
[0] = 0xd0000000 | (src1_val
<< 9);
537 e
->inst
[1] = 0x20000000;
539 e
->inst
[0] |= dst
->hw
<< 2;
540 if (src0
) /* otherwise will add to $a0, which is always 0 */
546 static struct nv50_reg
*
547 alloc_addr(struct nv50_pc
*pc
, struct nv50_reg
*ref
)
549 struct nv50_reg
*a_tgsi
= NULL
, *a
= NULL
;
551 uint8_t avail
= ~pc
->addr_alloc
;
554 /* allocate for TGSI_FILE_ADDRESS */
558 if (pc
->r_addr
[i
].rhw
< 0 ||
559 pc
->r_addr
[i
].acc
!= pc
->insn_cur
) {
560 pc
->addr_alloc
|= (1 << i
);
562 pc
->r_addr
[i
].rhw
= -1;
563 pc
->r_addr
[i
].index
= i
;
564 return &pc
->r_addr
[i
];
572 /* Allocate and set an address reg so we can access 'ref'.
574 * If and r_addr->index will be -1 or the hw index the value
575 * value in rhw is relative to. If rhw < 0, the reg has not
576 * been initialized or is in use for TGSI_FILE_ADDRESS.
578 while (avail
) { /* only consider regs that are not TGSI */
582 if ((!a
|| a
->rhw
>= 0) && pc
->r_addr
[i
].rhw
< 0) {
583 /* prefer an usused reg with low hw index */
587 if (!a
&& pc
->r_addr
[i
].acc
!= pc
->insn_cur
)
590 if (ref
->hw
- pc
->r_addr
[i
].rhw
>= 128)
593 if ((ref
->acc
>= 0 && pc
->r_addr
[i
].index
< 0) ||
594 (ref
->acc
< 0 && pc
->r_addr
[i
].index
== ref
->index
)) {
595 pc
->r_addr
[i
].acc
= pc
->insn_cur
;
596 return &pc
->r_addr
[i
];
602 a_tgsi
= pc
->addr
[ref
->index
];
604 emit_add_addr_imm(pc
, a
, a_tgsi
, (ref
->hw
& ~0x7f) * 4);
606 a
->rhw
= ref
->hw
& ~0x7f;
607 a
->acc
= pc
->insn_cur
;
608 a
->index
= a_tgsi
? ref
->index
: -1;
612 #define INTERP_LINEAR 0
613 #define INTERP_FLAT 1
614 #define INTERP_PERSPECTIVE 2
615 #define INTERP_CENTROID 4
617 /* interpolant index has been stored in dst->rhw */
619 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*iv
,
622 assert(dst
->rhw
!= -1);
623 struct nv50_program_exec
*e
= exec(pc
);
625 e
->inst
[0] |= 0x80000000;
627 e
->inst
[0] |= (dst
->rhw
<< 16);
629 if (mode
& INTERP_FLAT
) {
630 e
->inst
[0] |= (1 << 8);
632 if (mode
& INTERP_PERSPECTIVE
) {
633 e
->inst
[0] |= (1 << 25);
635 e
->inst
[0] |= (iv
->hw
<< 9);
638 if (mode
& INTERP_CENTROID
)
639 e
->inst
[0] |= (1 << 24);
646 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
647 struct nv50_program_exec
*e
)
651 e
->param
.index
= src
->hw
& 127;
653 e
->param
.mask
= m
<< (s
% 32);
656 set_addr(e
, alloc_addr(pc
, src
));
659 assert(src
->type
== P_CONST
);
660 set_addr(e
, pc
->addr
[src
->index
]);
663 e
->inst
[1] |= (((src
->type
== P_IMMD
) ? 0 : 1) << 22);
667 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
669 struct nv50_program_exec
*e
= exec(pc
);
671 e
->inst
[0] = 0x10000000;
677 if (!is_long(e
) && src
->type
== P_IMMD
) {
678 set_immd(pc
, src
, e
);
679 /*XXX: 32-bit, but steals part of "half" reg space - need to
680 * catch and handle this case if/when we do half-regs
683 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
685 set_data(pc
, src
, 0x7f, 9, e
);
686 e
->inst
[1] |= 0x20000000; /* mov from c[] */
688 if (src
->type
== P_ATTR
) {
690 e
->inst
[1] |= 0x00200000;
696 e
->inst
[0] |= (src
->hw
<< 9);
699 if (is_long(e
) && !is_immd(e
)) {
700 e
->inst
[1] |= 0x04000000; /* 32-bit */
701 e
->inst
[1] |= 0x0000c000; /* 32-bit c[] load / lane mask 0:1 */
702 if (!(e
->inst
[1] & 0x20000000))
703 e
->inst
[1] |= 0x00030000; /* lane mask 2:3 */
705 e
->inst
[0] |= 0x00008000;
711 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
713 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
714 emit_mov(pc
, dst
, imm
);
719 emit_nop(struct nv50_pc
*pc
)
721 struct nv50_program_exec
*e
= exec(pc
);
723 e
->inst
[0] = 0xf0000000;
725 e
->inst
[1] = 0xe0000000;
730 check_swap_src_0_1(struct nv50_pc
*pc
,
731 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
733 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
735 if (src0
->type
== P_CONST
) {
736 if (src1
->type
!= P_CONST
) {
742 if (src1
->type
== P_ATTR
) {
743 if (src0
->type
!= P_ATTR
) {
754 set_src_0_restricted(struct nv50_pc
*pc
, struct nv50_reg
*src
,
755 struct nv50_program_exec
*e
)
757 struct nv50_reg
*temp
;
759 if (src
->type
!= P_TEMP
) {
760 temp
= temp_temp(pc
);
761 emit_mov(pc
, temp
, src
);
768 e
->inst
[0] |= (src
->hw
<< 9);
772 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
774 if (src
->type
== P_ATTR
) {
776 e
->inst
[1] |= 0x00200000;
778 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
779 struct nv50_reg
*temp
= temp_temp(pc
);
781 emit_mov(pc
, temp
, src
);
788 e
->inst
[0] |= (src
->hw
<< 9);
792 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
794 if (src
->type
== P_ATTR
) {
795 struct nv50_reg
*temp
= temp_temp(pc
);
797 emit_mov(pc
, temp
, src
);
800 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
801 assert(!(e
->inst
[0] & 0x00800000));
802 if (e
->inst
[0] & 0x01000000) {
803 struct nv50_reg
*temp
= temp_temp(pc
);
805 emit_mov(pc
, temp
, src
);
808 set_data(pc
, src
, 0x7f, 16, e
);
809 e
->inst
[0] |= 0x00800000;
816 e
->inst
[0] |= ((src
->hw
& 127) << 16);
820 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
824 if (src
->type
== P_ATTR
) {
825 struct nv50_reg
*temp
= temp_temp(pc
);
827 emit_mov(pc
, temp
, src
);
830 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
831 assert(!(e
->inst
[0] & 0x01000000));
832 if (e
->inst
[0] & 0x00800000) {
833 struct nv50_reg
*temp
= temp_temp(pc
);
835 emit_mov(pc
, temp
, src
);
838 set_data(pc
, src
, 0x7f, 32+14, e
);
839 e
->inst
[0] |= 0x01000000;
844 e
->inst
[1] |= ((src
->hw
& 127) << 14);
848 emit_mov_from_pred(struct nv50_pc
*pc
, struct nv50_reg
*dst
, int pred
)
850 struct nv50_program_exec
*e
= exec(pc
);
852 assert(dst
->type
== P_TEMP
);
853 e
->inst
[1] = 0x20000000 | (pred
<< 12);
861 emit_mov_to_pred(struct nv50_pc
*pc
, int pred
, struct nv50_reg
*src
)
863 struct nv50_program_exec
*e
= exec(pc
);
865 e
->inst
[0] = 0x000001fc;
866 e
->inst
[1] = 0xa0000008;
868 set_pred_wr(pc
, 1, pred
, e
);
869 set_src_0_restricted(pc
, src
, e
);
875 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
876 struct nv50_reg
*src1
)
878 struct nv50_program_exec
*e
= exec(pc
);
880 e
->inst
[0] |= 0xc0000000;
885 check_swap_src_0_1(pc
, &src0
, &src1
);
887 set_src_0(pc
, src0
, e
);
888 if (src1
->type
== P_IMMD
&& !is_long(e
)) {
889 if (src0
->mod
& NV50_MOD_NEG
)
890 e
->inst
[0] |= 0x00008000;
891 set_immd(pc
, src1
, e
);
893 set_src_1(pc
, src1
, e
);
894 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
) {
896 e
->inst
[1] |= 0x08000000;
898 e
->inst
[0] |= 0x00008000;
906 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
907 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
909 struct nv50_program_exec
*e
= exec(pc
);
911 e
->inst
[0] = 0xb0000000;
914 check_swap_src_0_1(pc
, &src0
, &src1
);
916 if (!pc
->allow32
|| (src0
->mod
| src1
->mod
) || src1
->hw
> 63) {
918 e
->inst
[1] |= ((src0
->mod
& NV50_MOD_NEG
) << 26) |
919 ((src1
->mod
& NV50_MOD_NEG
) << 27);
923 set_src_0(pc
, src0
, e
);
924 if (src1
->type
== P_CONST
|| src1
->type
== P_ATTR
|| is_long(e
))
925 set_src_2(pc
, src1
, e
);
927 if (src1
->type
== P_IMMD
)
928 set_immd(pc
, src1
, e
);
930 set_src_1(pc
, src1
, e
);
936 emit_arl(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
939 struct nv50_program_exec
*e
= exec(pc
);
942 e
->inst
[1] |= 0xc0000000;
944 e
->inst
[0] |= dst
->hw
<< 2;
945 e
->inst
[0] |= s
<< 16; /* shift left */
946 set_src_0_restricted(pc
, src
, e
);
952 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
953 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
955 struct nv50_program_exec
*e
= exec(pc
);
958 e
->inst
[0] |= 0xb0000000;
959 e
->inst
[1] |= (sub
<< 29);
961 check_swap_src_0_1(pc
, &src0
, &src1
);
963 set_src_0(pc
, src0
, e
);
964 set_src_1(pc
, src1
, e
);
966 if (src0
->mod
& NV50_MOD_ABS
)
967 e
->inst
[1] |= 0x00100000;
968 if (src1
->mod
& NV50_MOD_ABS
)
969 e
->inst
[1] |= 0x00080000;
975 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
976 struct nv50_reg
*src1
)
978 src1
->mod
^= NV50_MOD_NEG
;
979 emit_add(pc
, dst
, src0
, src1
);
980 src1
->mod
^= NV50_MOD_NEG
;
984 emit_bitop2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
985 struct nv50_reg
*src1
, unsigned op
)
987 struct nv50_program_exec
*e
= exec(pc
);
989 e
->inst
[0] = 0xd0000000;
992 check_swap_src_0_1(pc
, &src0
, &src1
);
994 set_src_0(pc
, src0
, e
);
996 if (op
!= TGSI_OPCODE_AND
&& op
!= TGSI_OPCODE_OR
&&
997 op
!= TGSI_OPCODE_XOR
)
998 assert(!"invalid bit op");
1000 if (src1
->type
== P_IMMD
&& src0
->type
== P_TEMP
&& pc
->allow32
) {
1001 set_immd(pc
, src1
, e
);
1002 if (op
== TGSI_OPCODE_OR
)
1003 e
->inst
[0] |= 0x0100;
1005 if (op
== TGSI_OPCODE_XOR
)
1006 e
->inst
[0] |= 0x8000;
1008 set_src_1(pc
, src1
, e
);
1009 e
->inst
[1] |= 0x04000000; /* 32 bit */
1010 if (op
== TGSI_OPCODE_OR
)
1011 e
->inst
[1] |= 0x4000;
1013 if (op
== TGSI_OPCODE_XOR
)
1014 e
->inst
[1] |= 0x8000;
1021 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1022 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
1024 struct nv50_program_exec
*e
= exec(pc
);
1026 e
->inst
[0] |= 0xe0000000;
1028 check_swap_src_0_1(pc
, &src0
, &src1
);
1029 set_dst(pc
, dst
, e
);
1030 set_src_0(pc
, src0
, e
);
1031 set_src_1(pc
, src1
, e
);
1032 set_src_2(pc
, src2
, e
);
1034 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
)
1035 e
->inst
[1] |= 0x04000000;
1036 if (src2
->mod
& NV50_MOD_NEG
)
1037 e
->inst
[1] |= 0x08000000;
1043 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1044 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
1046 src2
->mod
^= NV50_MOD_NEG
;
1047 emit_mad(pc
, dst
, src0
, src1
, src2
);
1048 src2
->mod
^= NV50_MOD_NEG
;
1052 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
1053 struct nv50_reg
*dst
, struct nv50_reg
*src
)
1055 struct nv50_program_exec
*e
= exec(pc
);
1057 e
->inst
[0] |= 0x90000000;
1060 e
->inst
[1] |= (sub
<< 29);
1063 set_dst(pc
, dst
, e
);
1065 if (sub
== 0 || sub
== 2)
1066 set_src_0_restricted(pc
, src
, e
);
1068 set_src_0(pc
, src
, e
);
1074 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1076 struct nv50_program_exec
*e
= exec(pc
);
1078 e
->inst
[0] |= 0xb0000000;
1080 set_dst(pc
, dst
, e
);
1081 set_src_0(pc
, src
, e
);
1083 e
->inst
[1] |= (6 << 29) | 0x00004000;
1089 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1091 struct nv50_program_exec
*e
= exec(pc
);
1093 e
->inst
[0] |= 0xb0000000;
1095 set_dst(pc
, dst
, e
);
1096 set_src_0(pc
, src
, e
);
1098 e
->inst
[1] |= (6 << 29);
1103 #define CVTOP_RN 0x01
1104 #define CVTOP_FLOOR 0x03
1105 #define CVTOP_CEIL 0x05
1106 #define CVTOP_TRUNC 0x07
1107 #define CVTOP_SAT 0x08
1108 #define CVTOP_ABS 0x10
1110 /* 0x04 == 32 bit dst */
1111 /* 0x40 == dst is float */
1112 /* 0x80 == src is float */
1113 #define CVT_F32_F32 0xc4
1114 #define CVT_F32_S32 0x44
1115 #define CVT_S32_F32 0x8c
1116 #define CVT_S32_S32 0x0c
1117 #define CVT_NEG 0x20
1121 emit_cvt(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
1122 int wp
, unsigned cvn
, unsigned fmt
)
1124 struct nv50_program_exec
*e
;
1129 e
->inst
[0] |= 0xa0000000;
1130 e
->inst
[1] |= 0x00004000; /* 32 bit src */
1131 e
->inst
[1] |= (cvn
<< 16);
1132 e
->inst
[1] |= (fmt
<< 24);
1133 set_src_0(pc
, src
, e
);
1136 set_pred_wr(pc
, 1, wp
, e
);
1139 set_dst(pc
, dst
, e
);
1141 e
->inst
[0] |= 0x000001fc;
1142 e
->inst
[1] |= 0x00000008;
1148 /* nv50 Condition codes:
1155 * 0x7 = set condition code ? (used before bra.lt/le/gt/ge)
1156 * 0x8 = unordered bit (allows NaN)
1159 emit_set(struct nv50_pc
*pc
, unsigned ccode
, struct nv50_reg
*dst
, int wp
,
1160 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
1162 static const unsigned cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
1164 struct nv50_program_exec
*e
= exec(pc
);
1165 struct nv50_reg
*rdst
;
1168 if (check_swap_src_0_1(pc
, &src0
, &src1
))
1169 ccode
= cc_swapped
[ccode
& 7] | (ccode
& 8);
1172 if (dst
&& dst
->type
!= P_TEMP
)
1173 dst
= alloc_temp(pc
, NULL
);
1177 e
->inst
[0] |= 0xb0000000;
1178 e
->inst
[1] |= 0x60000000 | (ccode
<< 14);
1180 /* XXX: decuda will disasm as .u16 and use .lo/.hi regs, but
1181 * that doesn't seem to match what the hw actually does
1182 e->inst[1] |= 0x04000000; << breaks things, u32 by default ?
1186 set_pred_wr(pc
, 1, wp
, e
);
1188 set_dst(pc
, dst
, e
);
1190 e
->inst
[0] |= 0x000001fc;
1191 e
->inst
[1] |= 0x00000008;
1194 set_src_0(pc
, src0
, e
);
1195 set_src_1(pc
, src1
, e
);
1199 /* cvt.f32.u32/s32 (?) if we didn't only write the predicate */
1201 emit_cvt(pc
, rdst
, dst
, -1, CVTOP_ABS
| CVTOP_RN
, CVT_F32_S32
);
1202 if (rdst
&& rdst
!= dst
)
1206 static INLINE
unsigned
1207 map_tgsi_setop_cc(unsigned op
)
1210 case TGSI_OPCODE_SLT
: return 0x1;
1211 case TGSI_OPCODE_SGE
: return 0x6;
1212 case TGSI_OPCODE_SEQ
: return 0x2;
1213 case TGSI_OPCODE_SGT
: return 0x4;
1214 case TGSI_OPCODE_SLE
: return 0x3;
1215 case TGSI_OPCODE_SNE
: return 0xd;
1223 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1225 emit_cvt(pc
, dst
, src
, -1, CVTOP_FLOOR
, CVT_F32_F32
| CVT_RI
);
1229 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1230 struct nv50_reg
*v
, struct nv50_reg
*e
)
1232 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
1234 emit_flop(pc
, 3, temp
, v
);
1235 emit_mul(pc
, temp
, temp
, e
);
1236 emit_preex2(pc
, temp
, temp
);
1237 emit_flop(pc
, 6, dst
, temp
);
1239 free_temp(pc
, temp
);
1243 emit_abs(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1245 emit_cvt(pc
, dst
, src
, -1, CVTOP_ABS
, CVT_F32_F32
);
1249 emit_sat(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1251 emit_cvt(pc
, dst
, src
, -1, CVTOP_SAT
, CVT_F32_F32
);
1255 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1256 struct nv50_reg
**src
)
1258 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1259 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
1260 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
1261 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
1262 struct nv50_reg
*tmp
[4];
1263 boolean allow32
= pc
->allow32
;
1265 pc
->allow32
= FALSE
;
1267 if (mask
& (3 << 1)) {
1268 tmp
[0] = alloc_temp(pc
, NULL
);
1269 emit_minmax(pc
, 4, tmp
[0], src
[0], zero
);
1272 if (mask
& (1 << 2)) {
1273 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
1275 tmp
[1] = temp_temp(pc
);
1276 emit_minmax(pc
, 4, tmp
[1], src
[1], zero
);
1278 tmp
[3] = temp_temp(pc
);
1279 emit_minmax(pc
, 4, tmp
[3], src
[3], neg128
);
1280 emit_minmax(pc
, 5, tmp
[3], tmp
[3], pos128
);
1282 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
1283 emit_mov(pc
, dst
[2], zero
);
1284 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
1287 if (mask
& (1 << 1))
1288 assimilate_temp(pc
, dst
[1], tmp
[0]);
1290 if (mask
& (1 << 2))
1291 free_temp(pc
, tmp
[0]);
1293 pc
->allow32
= allow32
;
1295 /* do this last, in case src[i,j] == dst[0,3] */
1296 if (mask
& (1 << 0))
1297 emit_mov(pc
, dst
[0], one
);
1299 if (mask
& (1 << 3))
1300 emit_mov(pc
, dst
[3], one
);
1309 emit_neg(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1311 emit_cvt(pc
, dst
, src
, -1, CVTOP_RN
, CVT_F32_F32
| CVT_NEG
);
1315 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
1317 struct nv50_program_exec
*e
;
1318 const int r_pred
= 1;
1321 e
->inst
[0] = 0x00000002; /* discard */
1322 set_long(pc
, e
); /* sets cond code to ALWAYS */
1325 unsigned cvn
= CVT_F32_F32
;
1327 set_pred(pc
, 0x1 /* cc = LT */, r_pred
, e
);
1329 if (src
->mod
& NV50_MOD_NEG
)
1331 /* write predicate reg */
1332 emit_cvt(pc
, NULL
, src
, r_pred
, CVTOP_RN
, cvn
);
1338 static struct nv50_program_exec
*
1339 emit_breakaddr(struct nv50_pc
*pc
)
1341 struct nv50_program_exec
*e
= exec(pc
);
1343 e
->inst
[0] = 0x40000002;
1351 emit_break(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1353 struct nv50_program_exec
*e
= exec(pc
);
1355 e
->inst
[0] = 0x50000002;
1358 set_pred(pc
, cc
, pred
, e
);
1363 static struct nv50_program_exec
*
1364 emit_joinat(struct nv50_pc
*pc
)
1366 struct nv50_program_exec
*e
= exec(pc
);
1368 e
->inst
[0] = 0xa0000002;
1375 static struct nv50_program_exec
*
1376 emit_branch(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1378 struct nv50_program_exec
*e
= exec(pc
);
1380 e
->inst
[0] = 0x10000002;
1383 set_pred(pc
, cc
, pred
, e
);
1385 return pc
->p
->exec_tail
;
1389 emit_ret(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1391 struct nv50_program_exec
*e
= exec(pc
);
1393 e
->inst
[0] = 0x30000002;
1396 set_pred(pc
, cc
, pred
, e
);
1404 #define QOP_MOV_SRC1 3
1406 /* For a quad of threads / top left, top right, bottom left, bottom right
1407 * pixels, do a different operation, and take src0 from a specific thread.
1410 emit_quadop(struct nv50_pc
*pc
, struct nv50_reg
*dst
, int wp
, int lane_src0
,
1411 struct nv50_reg
*src0
, struct nv50_reg
*src1
, ubyte qop
)
1413 struct nv50_program_exec
*e
= exec(pc
);
1415 e
->inst
[0] = 0xc0000000;
1416 e
->inst
[1] = 0x80000000;
1418 e
->inst
[0] |= lane_src0
<< 16;
1419 set_src_0(pc
, src0
, e
);
1420 set_src_2(pc
, src1
, e
);
1423 set_pred_wr(pc
, 1, wp
, e
);
1426 set_dst(pc
, dst
, e
);
1428 e
->inst
[0] |= 0x000001fc;
1429 e
->inst
[1] |= 0x00000008;
1432 e
->inst
[0] |= (qop
& 3) << 20;
1433 e
->inst
[1] |= (qop
>> 2) << 22;
1439 load_cube_tex_coords(struct nv50_pc
*pc
, struct nv50_reg
*t
[4],
1440 struct nv50_reg
**src
, unsigned arg
, boolean proj
)
1442 int mod
[3] = { src
[0]->mod
, src
[1]->mod
, src
[2]->mod
};
1444 src
[0]->mod
|= NV50_MOD_ABS
;
1445 src
[1]->mod
|= NV50_MOD_ABS
;
1446 src
[2]->mod
|= NV50_MOD_ABS
;
1448 emit_minmax(pc
, 4, t
[2], src
[0], src
[1]);
1449 emit_minmax(pc
, 4, t
[2], src
[2], t
[2]);
1451 src
[0]->mod
= mod
[0];
1452 src
[1]->mod
= mod
[1];
1453 src
[2]->mod
= mod
[2];
1455 if (proj
&& 0 /* looks more correct without this */)
1456 emit_mul(pc
, t
[2], t
[2], src
[3]);
1458 if (arg
== 4) /* there is no textureProj(samplerCubeShadow) */
1459 emit_mov(pc
, t
[3], src
[3]);
1461 emit_flop(pc
, 0, t
[2], t
[2]);
1463 emit_mul(pc
, t
[0], src
[0], t
[2]);
1464 emit_mul(pc
, t
[1], src
[1], t
[2]);
1465 emit_mul(pc
, t
[2], src
[2], t
[2]);
1469 load_proj_tex_coords(struct nv50_pc
*pc
, struct nv50_reg
*t
[4],
1470 struct nv50_reg
**src
, unsigned dim
, unsigned arg
)
1474 if (src
[0]->type
== P_TEMP
&& src
[0]->rhw
!= -1) {
1475 mode
= pc
->interp_mode
[src
[0]->index
] | INTERP_PERSPECTIVE
;
1477 t
[3]->rhw
= src
[3]->rhw
;
1478 emit_interp(pc
, t
[3], NULL
, (mode
& INTERP_CENTROID
));
1479 emit_flop(pc
, 0, t
[3], t
[3]);
1481 for (c
= 0; c
< dim
; ++c
) {
1482 t
[c
]->rhw
= src
[c
]->rhw
;
1483 emit_interp(pc
, t
[c
], t
[3], mode
);
1485 if (arg
!= dim
) { /* depth reference value */
1486 t
[dim
]->rhw
= src
[2]->rhw
;
1487 emit_interp(pc
, t
[dim
], t
[3], mode
);
1490 /* XXX: for some reason the blob sometimes uses MAD
1491 * (mad f32 $rX $rY $rZ neg $r63)
1493 emit_flop(pc
, 0, t
[3], src
[3]);
1494 for (c
= 0; c
< dim
; ++c
)
1495 emit_mul(pc
, t
[c
], src
[c
], t
[3]);
1496 if (arg
!= dim
) /* depth reference value */
1497 emit_mul(pc
, t
[dim
], src
[2], t
[3]);
1502 get_tex_dim(unsigned type
, unsigned *dim
, unsigned *arg
)
1505 case TGSI_TEXTURE_1D
:
1508 case TGSI_TEXTURE_SHADOW1D
:
1512 case TGSI_TEXTURE_UNKNOWN
:
1513 case TGSI_TEXTURE_2D
:
1514 case TGSI_TEXTURE_RECT
:
1517 case TGSI_TEXTURE_SHADOW2D
:
1518 case TGSI_TEXTURE_SHADOWRECT
:
1522 case TGSI_TEXTURE_3D
:
1523 case TGSI_TEXTURE_CUBE
:
1532 /* We shouldn't execute TEXLOD if any of the pixels in a quad have
1533 * different LOD values, so branch off groups of equal LOD.
1536 emit_texlod_sequence(struct nv50_pc
*pc
, struct nv50_reg
*tlod
,
1537 struct nv50_reg
*src
, struct nv50_program_exec
*tex
)
1539 struct nv50_program_exec
*join_at
;
1540 unsigned i
, target
= pc
->p
->exec_size
+ 7 * 2;
1542 /* Subtract lod of each pixel from lod of top left pixel, jump
1543 * texlod insn if result is 0, then repeat for 2 other pixels.
1545 join_at
= emit_joinat(pc
);
1546 emit_quadop(pc
, NULL
, 0, 0, tlod
, tlod
, 0x55);
1547 emit_branch(pc
, 0, 2)->param
.index
= target
;
1549 for (i
= 1; i
< 4; ++i
) {
1550 emit_quadop(pc
, NULL
, 0, i
, tlod
, tlod
, 0x55);
1551 emit_branch(pc
, 0, 2)->param
.index
= target
;
1554 emit_mov(pc
, tlod
, src
); /* target */
1555 emit(pc
, tex
); /* texlod */
1557 join_at
->param
.index
= target
+ 2 * 2;
1558 JOIN_ON(emit_nop(pc
)); /* join _after_ tex */
1562 emit_texbias_sequence(struct nv50_pc
*pc
, struct nv50_reg
*t
[4], unsigned arg
,
1563 struct nv50_program_exec
*tex
)
1565 struct nv50_program_exec
*e
;
1566 struct nv50_reg imm_1248
, *t123
[4][4], *r_bits
= alloc_temp(pc
, NULL
);
1568 unsigned n
, c
, i
, cc
[4] = { 0x0a, 0x13, 0x11, 0x10 };
1570 pc
->allow32
= FALSE
;
1571 ctor_reg(&imm_1248
, P_IMMD
, -1, ctor_immd_4u32(pc
, 1, 2, 4, 8) * 4);
1573 /* Subtract bias value of thread i from bias values of each thread,
1574 * store result in r_pred, and set bit i in r_bits if result was 0.
1577 for (i
= 0; i
< 4; ++i
, ++imm_1248
.hw
) {
1578 emit_quadop(pc
, NULL
, r_pred
, i
, t
[arg
], t
[arg
], 0x55);
1579 emit_mov(pc
, r_bits
, &imm_1248
);
1580 set_pred(pc
, 2, r_pred
, pc
->p
->exec_tail
);
1582 emit_mov_to_pred(pc
, r_pred
, r_bits
);
1584 /* The lanes of a quad are now grouped by the bit in r_pred they have
1585 * set. Put the input values for TEX into a new register set for each
1586 * group and execute TEX only for a specific group.
1587 * We cannot use the same register set for each group because we need
1588 * the derivatives, which are implicitly calculated, to be correct.
1590 for (i
= 1; i
< 4; ++i
) {
1591 alloc_temp4(pc
, t123
[i
], 0);
1593 for (c
= 0; c
<= arg
; ++c
)
1594 emit_mov(pc
, t123
[i
][c
], t
[c
]);
1596 *(e
= exec(pc
)) = *(tex
);
1597 e
->inst
[0] &= ~0x01fc;
1598 set_dst(pc
, t123
[i
][0], e
);
1599 set_pred(pc
, cc
[i
], r_pred
, e
);
1602 /* finally TEX on the original regs (where we kept the input) */
1603 set_pred(pc
, cc
[0], r_pred
, tex
);
1606 /* put the 3 * n other results into regs for lane 0 */
1607 n
= popcnt4(((e
->inst
[0] >> 25) & 0x3) | ((e
->inst
[1] >> 12) & 0xc));
1608 for (i
= 1; i
< 4; ++i
) {
1609 for (c
= 0; c
< n
; ++c
) {
1610 emit_mov(pc
, t
[c
], t123
[i
][c
]);
1611 set_pred(pc
, cc
[i
], r_pred
, pc
->p
->exec_tail
);
1613 free_temp4(pc
, t123
[i
]);
1617 free_temp(pc
, r_bits
);
1621 emit_tex(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1622 struct nv50_reg
**src
, unsigned unit
, unsigned type
,
1623 boolean proj
, int bias_lod
)
1625 struct nv50_reg
*t
[4];
1626 struct nv50_program_exec
*e
;
1627 unsigned c
, dim
, arg
;
1629 /* t[i] must be within a single 128 bit super-reg */
1630 alloc_temp4(pc
, t
, 0);
1633 e
->inst
[0] = 0xf0000000;
1635 set_dst(pc
, t
[0], e
);
1637 /* TIC and TSC binding indices (TSC is ignored as TSC_LINKED = TRUE): */
1638 e
->inst
[0] |= (unit
<< 9) /* | (unit << 17) */;
1640 /* live flag (don't set if TEX results affect input to another TEX): */
1641 /* e->inst[0] |= 0x00000004; */
1643 get_tex_dim(type
, &dim
, &arg
);
1645 if (type
== TGSI_TEXTURE_CUBE
) {
1646 e
->inst
[0] |= 0x08000000;
1647 load_cube_tex_coords(pc
, t
, src
, arg
, proj
);
1650 load_proj_tex_coords(pc
, t
, src
, dim
, arg
);
1652 for (c
= 0; c
< dim
; c
++)
1653 emit_mov(pc
, t
[c
], src
[c
]);
1654 if (arg
!= dim
) /* depth reference value (always src.z here) */
1655 emit_mov(pc
, t
[dim
], src
[2]);
1658 e
->inst
[0] |= (mask
& 0x3) << 25;
1659 e
->inst
[1] |= (mask
& 0xc) << 12;
1662 e
->inst
[0] |= (arg
- 1) << 22;
1666 e
->inst
[0] |= arg
<< 22;
1667 e
->inst
[1] |= 0x20000000; /* texbias */
1668 emit_mov(pc
, t
[arg
], src
[3]);
1669 emit_texbias_sequence(pc
, t
, arg
, e
);
1671 e
->inst
[0] |= arg
<< 22;
1672 e
->inst
[1] |= 0x40000000; /* texlod */
1673 emit_mov(pc
, t
[arg
], src
[3]);
1674 emit_texlod_sequence(pc
, t
[arg
], src
[3], e
);
1679 if (mask
& 1) emit_mov(pc
, dst
[0], t
[c
++]);
1680 if (mask
& 2) emit_mov(pc
, dst
[1], t
[c
++]);
1681 if (mask
& 4) emit_mov(pc
, dst
[2], t
[c
++]);
1682 if (mask
& 8) emit_mov(pc
, dst
[3], t
[c
]);
1686 /* XXX: if p.e. MUL is used directly after TEX, it would still use
1687 * the texture coordinates, not the fetched values: latency ? */
1689 for (c
= 0; c
< 4; c
++) {
1690 if (mask
& (1 << c
))
1691 assimilate_temp(pc
, dst
[c
], t
[c
]);
1693 free_temp(pc
, t
[c
]);
1699 emit_ddx(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1701 struct nv50_program_exec
*e
= exec(pc
);
1703 assert(src
->type
== P_TEMP
);
1705 e
->inst
[0] = 0xc0140000;
1706 e
->inst
[1] = 0x89800000;
1708 set_dst(pc
, dst
, e
);
1709 set_src_0(pc
, src
, e
);
1710 set_src_2(pc
, src
, e
);
1716 emit_ddy(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1718 struct nv50_reg
*r
= src
;
1719 struct nv50_program_exec
*e
= exec(pc
);
1721 assert(src
->type
== P_TEMP
);
1723 if (!(src
->mod
& NV50_MOD_NEG
)) { /* ! double negation */
1724 r
= alloc_temp(pc
, NULL
);
1725 emit_neg(pc
, r
, src
);
1728 e
->inst
[0] = 0xc0150000;
1729 e
->inst
[1] = 0x8a400000;
1731 set_dst(pc
, dst
, e
);
1732 set_src_0(pc
, r
, e
);
1733 set_src_2(pc
, r
, e
);
1742 convert_to_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
1744 unsigned q
= 0, m
= ~0;
1746 assert(!is_long(e
));
1748 switch (e
->inst
[0] >> 28) {
1755 /* INTERP (move centroid, perspective and flat bits) */
1757 q
= (e
->inst
[0] & (3 << 24)) >> (24 - 16);
1758 q
|= (e
->inst
[0] & (1 << 8)) << (18 - 8);
1766 q
= ((e
->inst
[0] & (~m
)) >> 2);
1771 q
= ((e
->inst
[0] & (~m
)) << 12);
1774 /* MAD (if src2 == dst) */
1775 q
= ((e
->inst
[0] & 0x1fc) << 12);
1789 /* Some operations support an optional negation flag. */
1791 negate_supported(const struct tgsi_full_instruction
*insn
, int i
)
1793 switch (insn
->Instruction
.Opcode
) {
1794 case TGSI_OPCODE_DDY
:
1795 case TGSI_OPCODE_DP3
:
1796 case TGSI_OPCODE_DP4
:
1797 case TGSI_OPCODE_MUL
:
1798 case TGSI_OPCODE_KIL
:
1799 case TGSI_OPCODE_ADD
:
1800 case TGSI_OPCODE_SUB
:
1801 case TGSI_OPCODE_MAD
:
1803 case TGSI_OPCODE_POW
:
1812 /* Return a read mask for source registers deduced from opcode & write mask. */
1814 nv50_tgsi_src_mask(const struct tgsi_full_instruction
*insn
, int c
)
1816 unsigned x
, mask
= insn
->Dst
[0].Register
.WriteMask
;
1818 switch (insn
->Instruction
.Opcode
) {
1819 case TGSI_OPCODE_COS
:
1820 case TGSI_OPCODE_SIN
:
1821 return (mask
& 0x8) | ((mask
& 0x7) ? 0x1 : 0x0);
1822 case TGSI_OPCODE_DP3
:
1824 case TGSI_OPCODE_DP4
:
1825 case TGSI_OPCODE_DPH
:
1826 case TGSI_OPCODE_KIL
: /* WriteMask ignored */
1828 case TGSI_OPCODE_DST
:
1829 return mask
& (c
? 0xa : 0x6);
1830 case TGSI_OPCODE_EX2
:
1831 case TGSI_OPCODE_LG2
:
1832 case TGSI_OPCODE_POW
:
1833 case TGSI_OPCODE_RCP
:
1834 case TGSI_OPCODE_RSQ
:
1835 case TGSI_OPCODE_SCS
:
1837 case TGSI_OPCODE_IF
:
1839 case TGSI_OPCODE_LIT
:
1841 case TGSI_OPCODE_TEX
:
1842 case TGSI_OPCODE_TXB
:
1843 case TGSI_OPCODE_TXL
:
1844 case TGSI_OPCODE_TXP
:
1846 const struct tgsi_instruction_texture
*tex
;
1848 assert(insn
->Instruction
.Texture
);
1849 tex
= &insn
->Texture
;
1852 if (insn
->Instruction
.Opcode
!= TGSI_OPCODE_TEX
&&
1853 insn
->Instruction
.Opcode
!= TGSI_OPCODE_TXD
)
1854 mask
|= 0x8; /* bias, lod or proj */
1856 switch (tex
->Texture
) {
1857 case TGSI_TEXTURE_1D
:
1860 case TGSI_TEXTURE_SHADOW1D
:
1863 case TGSI_TEXTURE_2D
:
1871 case TGSI_OPCODE_XPD
:
1873 if (mask
& 1) x
|= 0x6;
1874 if (mask
& 2) x
|= 0x5;
1875 if (mask
& 4) x
|= 0x3;
1884 static struct nv50_reg
*
1885 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
1887 switch (dst
->Register
.File
) {
1888 case TGSI_FILE_TEMPORARY
:
1889 return &pc
->temp
[dst
->Register
.Index
* 4 + c
];
1890 case TGSI_FILE_OUTPUT
:
1891 return &pc
->result
[dst
->Register
.Index
* 4 + c
];
1892 case TGSI_FILE_ADDRESS
:
1894 struct nv50_reg
*r
= pc
->addr
[dst
->Register
.Index
* 4 + c
];
1896 r
= alloc_addr(pc
, NULL
);
1897 pc
->addr
[dst
->Register
.Index
* 4 + c
] = r
;
1902 case TGSI_FILE_NULL
:
1911 static struct nv50_reg
*
1912 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
,
1915 struct nv50_reg
*r
= NULL
;
1916 struct nv50_reg
*temp
;
1917 unsigned sgn
, c
, swz
;
1919 if (src
->Register
.File
!= TGSI_FILE_CONSTANT
)
1920 assert(!src
->Register
.Indirect
);
1922 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1924 c
= tgsi_util_get_full_src_register_swizzle(src
, chan
);
1926 case TGSI_SWIZZLE_X
:
1927 case TGSI_SWIZZLE_Y
:
1928 case TGSI_SWIZZLE_Z
:
1929 case TGSI_SWIZZLE_W
:
1930 switch (src
->Register
.File
) {
1931 case TGSI_FILE_INPUT
:
1932 r
= &pc
->attr
[src
->Register
.Index
* 4 + c
];
1934 case TGSI_FILE_TEMPORARY
:
1935 r
= &pc
->temp
[src
->Register
.Index
* 4 + c
];
1937 case TGSI_FILE_CONSTANT
:
1938 if (!src
->Register
.Indirect
) {
1939 r
= &pc
->param
[src
->Register
.Index
* 4 + c
];
1942 /* Indicate indirection by setting r->acc < 0 and
1943 * use the index field to select the address reg.
1945 r
= reg_instance(pc
, NULL
);
1946 swz
= tgsi_util_get_src_register_swizzle(
1948 ctor_reg(r
, P_CONST
,
1949 src
->Indirect
.Index
* 4 + swz
,
1950 src
->Register
.Index
* 4 + c
);
1953 case TGSI_FILE_IMMEDIATE
:
1954 r
= &pc
->immd
[src
->Register
.Index
* 4 + c
];
1956 case TGSI_FILE_SAMPLER
:
1958 case TGSI_FILE_ADDRESS
:
1959 r
= pc
->addr
[src
->Register
.Index
* 4 + c
];
1973 case TGSI_UTIL_SIGN_KEEP
:
1975 case TGSI_UTIL_SIGN_CLEAR
:
1976 temp
= temp_temp(pc
);
1977 emit_abs(pc
, temp
, r
);
1980 case TGSI_UTIL_SIGN_TOGGLE
:
1982 r
->mod
= NV50_MOD_NEG
;
1984 temp
= temp_temp(pc
);
1985 emit_neg(pc
, temp
, r
);
1989 case TGSI_UTIL_SIGN_SET
:
1990 temp
= temp_temp(pc
);
1991 emit_cvt(pc
, temp
, r
, -1, CVTOP_ABS
, CVT_F32_F32
| CVT_NEG
);
1999 if (r
&& r
->acc
>= 0 && r
!= temp
)
2000 return reg_instance(pc
, r
);
2004 /* return TRUE for ops that produce only a single result */
2006 is_scalar_op(unsigned op
)
2009 case TGSI_OPCODE_COS
:
2010 case TGSI_OPCODE_DP2
:
2011 case TGSI_OPCODE_DP3
:
2012 case TGSI_OPCODE_DP4
:
2013 case TGSI_OPCODE_DPH
:
2014 case TGSI_OPCODE_EX2
:
2015 case TGSI_OPCODE_LG2
:
2016 case TGSI_OPCODE_POW
:
2017 case TGSI_OPCODE_RCP
:
2018 case TGSI_OPCODE_RSQ
:
2019 case TGSI_OPCODE_SIN
:
2021 case TGSI_OPCODE_KIL:
2022 case TGSI_OPCODE_LIT:
2023 case TGSI_OPCODE_SCS:
2031 /* Returns a bitmask indicating which dst components depend
2032 * on source s, component c (reverse of nv50_tgsi_src_mask).
2035 nv50_tgsi_dst_revdep(unsigned op
, int s
, int c
)
2037 if (is_scalar_op(op
))
2041 case TGSI_OPCODE_DST
:
2042 return (1 << c
) & (s
? 0xa : 0x6);
2043 case TGSI_OPCODE_XPD
:
2053 case TGSI_OPCODE_LIT
:
2054 case TGSI_OPCODE_SCS
:
2055 case TGSI_OPCODE_TEX
:
2056 case TGSI_OPCODE_TXB
:
2057 case TGSI_OPCODE_TXL
:
2058 case TGSI_OPCODE_TXP
:
2059 /* these take care of dangerous swizzles themselves */
2061 case TGSI_OPCODE_IF
:
2062 case TGSI_OPCODE_KIL
:
2063 /* don't call this function for these ops */
2067 /* linear vector instruction */
2072 static INLINE boolean
2073 has_pred(struct nv50_program_exec
*e
, unsigned cc
)
2075 if (!is_long(e
) || is_immd(e
))
2077 return ((e
->inst
[1] & 0x780) == (cc
<< 7));
2080 /* on ENDIF see if we can do "@p0.neu single_op" instead of:
2087 nv50_kill_branch(struct nv50_pc
*pc
)
2089 int lvl
= pc
->if_lvl
;
2091 if (pc
->if_insn
[lvl
]->next
!= pc
->p
->exec_tail
)
2094 /* if ccode == 'true', the BRA is from an ELSE and the predicate
2095 * reg may no longer be valid, since we currently always use $p0
2097 if (has_pred(pc
->if_insn
[lvl
], 0xf))
2099 assert(pc
->if_insn
[lvl
] && pc
->if_join
[lvl
]);
2101 /* We'll use the exec allocated for JOIN_AT (we can't easily
2102 * access nv50_program_exec's prev).
2104 pc
->p
->exec_size
-= 4; /* remove JOIN_AT and BRA */
2106 *pc
->if_join
[lvl
] = *pc
->p
->exec_tail
;
2108 FREE(pc
->if_insn
[lvl
]);
2109 FREE(pc
->p
->exec_tail
);
2111 pc
->p
->exec_tail
= pc
->if_join
[lvl
];
2112 pc
->p
->exec_tail
->next
= NULL
;
2113 set_pred(pc
, 0xd, 0, pc
->p
->exec_tail
);
2119 nv50_fp_move_results(struct nv50_pc
*pc
)
2121 struct nv50_reg reg
;
2124 ctor_reg(®
, P_TEMP
, -1, -1);
2126 for (i
= 0; i
< pc
->result_nr
* 4; ++i
) {
2127 if (pc
->result
[i
].rhw
< 0 || pc
->result
[i
].hw
< 0)
2129 if (pc
->result
[i
].rhw
!= pc
->result
[i
].hw
) {
2130 reg
.hw
= pc
->result
[i
].rhw
;
2131 emit_mov(pc
, ®
, &pc
->result
[i
]);
2137 nv50_program_tx_insn(struct nv50_pc
*pc
,
2138 const struct tgsi_full_instruction
*inst
)
2140 struct nv50_reg
*rdst
[4], *dst
[4], *brdc
, *src
[3][4], *temp
;
2141 unsigned mask
, sat
, unit
;
2144 mask
= inst
->Dst
[0].Register
.WriteMask
;
2145 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
2147 memset(src
, 0, sizeof(src
));
2149 for (c
= 0; c
< 4; c
++) {
2150 if ((mask
& (1 << c
)) && !pc
->r_dst
[c
])
2151 dst
[c
] = tgsi_dst(pc
, c
, &inst
->Dst
[0]);
2153 dst
[c
] = pc
->r_dst
[c
];
2157 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
2158 const struct tgsi_full_src_register
*fs
= &inst
->Src
[i
];
2162 src_mask
= nv50_tgsi_src_mask(inst
, i
);
2163 neg_supp
= negate_supported(inst
, i
);
2165 if (fs
->Register
.File
== TGSI_FILE_SAMPLER
)
2166 unit
= fs
->Register
.Index
;
2168 for (c
= 0; c
< 4; c
++)
2169 if (src_mask
& (1 << c
))
2170 src
[i
][c
] = tgsi_src(pc
, c
, fs
, neg_supp
);
2173 brdc
= temp
= pc
->r_brdc
;
2174 if (brdc
&& brdc
->type
!= P_TEMP
) {
2175 temp
= temp_temp(pc
);
2180 for (c
= 0; c
< 4; c
++) {
2181 if (!(mask
& (1 << c
)) || dst
[c
]->type
== P_TEMP
)
2183 /* rdst[c] = dst[c]; */ /* done above */
2184 dst
[c
] = temp_temp(pc
);
2188 assert(brdc
|| !is_scalar_op(inst
->Instruction
.Opcode
));
2190 switch (inst
->Instruction
.Opcode
) {
2191 case TGSI_OPCODE_ABS
:
2192 for (c
= 0; c
< 4; c
++) {
2193 if (!(mask
& (1 << c
)))
2195 emit_abs(pc
, dst
[c
], src
[0][c
]);
2198 case TGSI_OPCODE_ADD
:
2199 for (c
= 0; c
< 4; c
++) {
2200 if (!(mask
& (1 << c
)))
2202 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2205 case TGSI_OPCODE_AND
:
2206 case TGSI_OPCODE_XOR
:
2207 case TGSI_OPCODE_OR
:
2208 for (c
= 0; c
< 4; c
++) {
2209 if (!(mask
& (1 << c
)))
2211 emit_bitop2(pc
, dst
[c
], src
[0][c
], src
[1][c
],
2212 inst
->Instruction
.Opcode
);
2215 case TGSI_OPCODE_ARL
:
2217 temp
= temp_temp(pc
);
2218 emit_cvt(pc
, temp
, src
[0][0], -1, CVTOP_FLOOR
, CVT_S32_F32
);
2219 emit_arl(pc
, dst
[0], temp
, 4);
2221 case TGSI_OPCODE_BGNLOOP
:
2222 pc
->loop_brka
[pc
->loop_lvl
] = emit_breakaddr(pc
);
2223 pc
->loop_pos
[pc
->loop_lvl
++] = pc
->p
->exec_size
;
2226 case TGSI_OPCODE_BRK
:
2227 assert(pc
->loop_lvl
> 0);
2228 emit_break(pc
, -1, 0);
2230 case TGSI_OPCODE_CEIL
:
2231 for (c
= 0; c
< 4; c
++) {
2232 if (!(mask
& (1 << c
)))
2234 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2235 CVTOP_CEIL
, CVT_F32_F32
| CVT_RI
);
2238 case TGSI_OPCODE_CMP
:
2239 pc
->allow32
= FALSE
;
2240 for (c
= 0; c
< 4; c
++) {
2241 if (!(mask
& (1 << c
)))
2243 emit_cvt(pc
, NULL
, src
[0][c
], 1, CVTOP_RN
, CVT_F32_F32
);
2244 emit_mov(pc
, dst
[c
], src
[1][c
]);
2245 set_pred(pc
, 0x1, 1, pc
->p
->exec_tail
); /* @SF */
2246 emit_mov(pc
, dst
[c
], src
[2][c
]);
2247 set_pred(pc
, 0x6, 1, pc
->p
->exec_tail
); /* @NSF */
2250 case TGSI_OPCODE_COS
:
2252 emit_precossin(pc
, temp
, src
[0][3]);
2253 emit_flop(pc
, 5, dst
[3], temp
);
2257 temp
= brdc
= temp_temp(pc
);
2259 emit_precossin(pc
, temp
, src
[0][0]);
2260 emit_flop(pc
, 5, brdc
, temp
);
2262 case TGSI_OPCODE_DDX
:
2263 for (c
= 0; c
< 4; c
++) {
2264 if (!(mask
& (1 << c
)))
2266 emit_ddx(pc
, dst
[c
], src
[0][c
]);
2269 case TGSI_OPCODE_DDY
:
2270 for (c
= 0; c
< 4; c
++) {
2271 if (!(mask
& (1 << c
)))
2273 emit_ddy(pc
, dst
[c
], src
[0][c
]);
2276 case TGSI_OPCODE_DP3
:
2277 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2278 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2279 emit_mad(pc
, brdc
, src
[0][2], src
[1][2], temp
);
2281 case TGSI_OPCODE_DP4
:
2282 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2283 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2284 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2285 emit_mad(pc
, brdc
, src
[0][3], src
[1][3], temp
);
2287 case TGSI_OPCODE_DPH
:
2288 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2289 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2290 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2291 emit_add(pc
, brdc
, src
[1][3], temp
);
2293 case TGSI_OPCODE_DST
:
2294 if (mask
& (1 << 1))
2295 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
2296 if (mask
& (1 << 2))
2297 emit_mov(pc
, dst
[2], src
[0][2]);
2298 if (mask
& (1 << 3))
2299 emit_mov(pc
, dst
[3], src
[1][3]);
2300 if (mask
& (1 << 0))
2301 emit_mov_immdval(pc
, dst
[0], 1.0f
);
2303 case TGSI_OPCODE_ELSE
:
2304 emit_branch(pc
, -1, 0);
2305 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2306 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
2309 case TGSI_OPCODE_ENDIF
:
2310 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2312 /* try to replace branch over 1 insn with a predicated insn */
2313 if (nv50_kill_branch(pc
) == TRUE
)
2316 if (pc
->if_join
[pc
->if_lvl
]) {
2317 pc
->if_join
[pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2318 pc
->if_join
[pc
->if_lvl
] = NULL
;
2321 /* emit a NOP as join point, we could set it on the next
2322 * one, but would have to make sure it is long and !immd
2324 JOIN_ON(emit_nop(pc
));
2326 case TGSI_OPCODE_ENDLOOP
:
2327 emit_branch(pc
, -1, 0)->param
.index
=
2328 pc
->loop_pos
[--pc
->loop_lvl
];
2329 pc
->loop_brka
[pc
->loop_lvl
]->param
.index
= pc
->p
->exec_size
;
2332 case TGSI_OPCODE_EX2
:
2333 emit_preex2(pc
, temp
, src
[0][0]);
2334 emit_flop(pc
, 6, brdc
, temp
);
2336 case TGSI_OPCODE_FLR
:
2337 for (c
= 0; c
< 4; c
++) {
2338 if (!(mask
& (1 << c
)))
2340 emit_flr(pc
, dst
[c
], src
[0][c
]);
2343 case TGSI_OPCODE_FRC
:
2344 temp
= temp_temp(pc
);
2345 for (c
= 0; c
< 4; c
++) {
2346 if (!(mask
& (1 << c
)))
2348 emit_flr(pc
, temp
, src
[0][c
]);
2349 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
2352 case TGSI_OPCODE_IF
:
2353 assert(pc
->if_lvl
< NV50_MAX_COND_NESTING
);
2354 emit_cvt(pc
, NULL
, src
[0][0], 0, CVTOP_ABS
| CVTOP_RN
,
2356 pc
->if_join
[pc
->if_lvl
] = emit_joinat(pc
);
2357 pc
->if_insn
[pc
->if_lvl
++] = emit_branch(pc
, 0, 2);;
2360 case TGSI_OPCODE_KIL
:
2361 assert(src
[0][0] && src
[0][1] && src
[0][2] && src
[0][3]);
2362 emit_kil(pc
, src
[0][0]);
2363 emit_kil(pc
, src
[0][1]);
2364 emit_kil(pc
, src
[0][2]);
2365 emit_kil(pc
, src
[0][3]);
2367 case TGSI_OPCODE_KILP
:
2370 case TGSI_OPCODE_LIT
:
2371 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
2373 case TGSI_OPCODE_LG2
:
2374 emit_flop(pc
, 3, brdc
, src
[0][0]);
2376 case TGSI_OPCODE_LRP
:
2377 temp
= temp_temp(pc
);
2378 for (c
= 0; c
< 4; c
++) {
2379 if (!(mask
& (1 << c
)))
2381 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
2382 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
2385 case TGSI_OPCODE_MAD
:
2386 for (c
= 0; c
< 4; c
++) {
2387 if (!(mask
& (1 << c
)))
2389 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
2392 case TGSI_OPCODE_MAX
:
2393 for (c
= 0; c
< 4; c
++) {
2394 if (!(mask
& (1 << c
)))
2396 emit_minmax(pc
, 4, dst
[c
], src
[0][c
], src
[1][c
]);
2399 case TGSI_OPCODE_MIN
:
2400 for (c
= 0; c
< 4; c
++) {
2401 if (!(mask
& (1 << c
)))
2403 emit_minmax(pc
, 5, dst
[c
], src
[0][c
], src
[1][c
]);
2406 case TGSI_OPCODE_MOV
:
2407 for (c
= 0; c
< 4; c
++) {
2408 if (!(mask
& (1 << c
)))
2410 emit_mov(pc
, dst
[c
], src
[0][c
]);
2413 case TGSI_OPCODE_MUL
:
2414 for (c
= 0; c
< 4; c
++) {
2415 if (!(mask
& (1 << c
)))
2417 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2420 case TGSI_OPCODE_POW
:
2421 emit_pow(pc
, brdc
, src
[0][0], src
[1][0]);
2423 case TGSI_OPCODE_RCP
:
2424 emit_flop(pc
, 0, brdc
, src
[0][0]);
2426 case TGSI_OPCODE_RET
:
2427 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
)
2428 nv50_fp_move_results(pc
);
2429 emit_ret(pc
, -1, 0);
2431 case TGSI_OPCODE_RSQ
:
2432 emit_flop(pc
, 2, brdc
, src
[0][0]);
2434 case TGSI_OPCODE_SCS
:
2435 temp
= temp_temp(pc
);
2437 emit_precossin(pc
, temp
, src
[0][0]);
2438 if (mask
& (1 << 0))
2439 emit_flop(pc
, 5, dst
[0], temp
);
2440 if (mask
& (1 << 1))
2441 emit_flop(pc
, 4, dst
[1], temp
);
2442 if (mask
& (1 << 2))
2443 emit_mov_immdval(pc
, dst
[2], 0.0);
2444 if (mask
& (1 << 3))
2445 emit_mov_immdval(pc
, dst
[3], 1.0);
2447 case TGSI_OPCODE_SIN
:
2449 emit_precossin(pc
, temp
, src
[0][3]);
2450 emit_flop(pc
, 4, dst
[3], temp
);
2454 temp
= brdc
= temp_temp(pc
);
2456 emit_precossin(pc
, temp
, src
[0][0]);
2457 emit_flop(pc
, 4, brdc
, temp
);
2459 case TGSI_OPCODE_SLT
:
2460 case TGSI_OPCODE_SGE
:
2461 case TGSI_OPCODE_SEQ
:
2462 case TGSI_OPCODE_SGT
:
2463 case TGSI_OPCODE_SLE
:
2464 case TGSI_OPCODE_SNE
:
2465 i
= map_tgsi_setop_cc(inst
->Instruction
.Opcode
);
2466 for (c
= 0; c
< 4; c
++) {
2467 if (!(mask
& (1 << c
)))
2469 emit_set(pc
, i
, dst
[c
], -1, src
[0][c
], src
[1][c
]);
2472 case TGSI_OPCODE_SUB
:
2473 for (c
= 0; c
< 4; c
++) {
2474 if (!(mask
& (1 << c
)))
2476 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2479 case TGSI_OPCODE_TEX
:
2480 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2481 inst
->Texture
.Texture
, FALSE
, 0);
2483 case TGSI_OPCODE_TXB
:
2484 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2485 inst
->Texture
.Texture
, FALSE
, -1);
2487 case TGSI_OPCODE_TXL
:
2488 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2489 inst
->Texture
.Texture
, FALSE
, 1);
2491 case TGSI_OPCODE_TXP
:
2492 emit_tex(pc
, dst
, mask
, src
[0], unit
,
2493 inst
->Texture
.Texture
, TRUE
, 0);
2495 case TGSI_OPCODE_TRUNC
:
2496 for (c
= 0; c
< 4; c
++) {
2497 if (!(mask
& (1 << c
)))
2499 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2500 CVTOP_TRUNC
, CVT_F32_F32
| CVT_RI
);
2503 case TGSI_OPCODE_XPD
:
2504 temp
= temp_temp(pc
);
2505 if (mask
& (1 << 0)) {
2506 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
2507 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
2509 if (mask
& (1 << 1)) {
2510 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
2511 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
2513 if (mask
& (1 << 2)) {
2514 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
2515 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
2517 if (mask
& (1 << 3))
2518 emit_mov_immdval(pc
, dst
[3], 1.0);
2520 case TGSI_OPCODE_END
:
2523 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
2529 emit_sat(pc
, brdc
, brdc
);
2530 for (c
= 0; c
< 4; c
++)
2531 if ((mask
& (1 << c
)) && dst
[c
] != brdc
)
2532 emit_mov(pc
, dst
[c
], brdc
);
2535 for (c
= 0; c
< 4; c
++) {
2536 if (!(mask
& (1 << c
)))
2538 /* In this case we saturate later, and dst[c] won't
2539 * be another temp_temp (and thus lost), since rdst
2540 * already is TEMP (see above). */
2541 if (rdst
[c
]->type
== P_TEMP
&& rdst
[c
]->index
< 0)
2543 emit_sat(pc
, rdst
[c
], dst
[c
]);
2548 pc
->reg_instance_nr
= 0;
2554 prep_inspect_insn(struct nv50_pc
*pc
, const struct tgsi_full_instruction
*insn
)
2556 struct nv50_reg
*reg
= NULL
;
2557 const struct tgsi_full_src_register
*src
;
2558 const struct tgsi_dst_register
*dst
;
2559 unsigned i
, c
, k
, mask
;
2561 dst
= &insn
->Dst
[0].Register
;
2562 mask
= dst
->WriteMask
;
2564 if (dst
->File
== TGSI_FILE_TEMPORARY
)
2567 if (dst
->File
== TGSI_FILE_OUTPUT
)
2571 for (c
= 0; c
< 4; c
++) {
2572 if (!(mask
& (1 << c
)))
2574 reg
[dst
->Index
* 4 + c
].acc
= pc
->insn_nr
;
2578 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2579 src
= &insn
->Src
[i
];
2581 if (src
->Register
.File
== TGSI_FILE_TEMPORARY
)
2584 if (src
->Register
.File
== TGSI_FILE_INPUT
)
2589 mask
= nv50_tgsi_src_mask(insn
, i
);
2591 for (c
= 0; c
< 4; c
++) {
2592 if (!(mask
& (1 << c
)))
2594 k
= tgsi_util_get_full_src_register_swizzle(src
, c
);
2596 reg
[src
->Register
.Index
* 4 + k
].acc
= pc
->insn_nr
;
2601 /* Returns a bitmask indicating which dst components need to be
2602 * written to temporaries first to avoid 'corrupting' sources.
2604 * m[i] (out) indicate component to write in the i-th position
2605 * rdep[c] (in) bitmasks of dst[i] that require dst[c] as source
2608 nv50_revdep_reorder(unsigned m
[4], unsigned rdep
[4])
2610 unsigned i
, c
, x
, unsafe
;
2612 for (c
= 0; c
< 4; c
++)
2615 /* Swap as long as a dst component written earlier is depended on
2616 * by one written later, but the next one isn't depended on by it.
2618 for (c
= 0; c
< 3; c
++) {
2619 if (rdep
[m
[c
+ 1]] & (1 << m
[c
]))
2620 continue; /* if next one is depended on by us */
2621 for (i
= c
+ 1; i
< 4; i
++)
2622 /* if we are depended on by a later one */
2623 if (rdep
[m
[c
]] & (1 << m
[i
]))
2636 /* mark dependencies that could not be resolved by reordering */
2637 for (i
= 0; i
< 3; ++i
)
2638 for (c
= i
+ 1; c
< 4; ++c
)
2639 if (rdep
[m
[i
]] & (1 << m
[c
]))
2642 /* NOTE: $unsafe is with respect to order, not component */
2646 /* Select a suitable dst register for broadcasting scalar results,
2647 * or return NULL if we have to allocate an extra TEMP.
2649 * If e.g. only 1 component is written, we may also emit the final
2650 * result to a write-only register.
2652 static struct nv50_reg
*
2653 tgsi_broadcast_dst(struct nv50_pc
*pc
,
2654 const struct tgsi_full_dst_register
*fd
, unsigned mask
)
2656 if (fd
->Register
.File
== TGSI_FILE_TEMPORARY
) {
2657 int c
= ffs(~mask
& fd
->Register
.WriteMask
);
2659 return tgsi_dst(pc
, c
- 1, fd
);
2661 int c
= ffs(fd
->Register
.WriteMask
) - 1;
2662 if ((1 << c
) == fd
->Register
.WriteMask
)
2663 return tgsi_dst(pc
, c
, fd
);
2669 /* Scan source swizzles and return a bitmask indicating dst regs that
2670 * also occur among the src regs, and fill rdep for nv50_revdep_reoder.
2673 nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction
*insn
,
2676 const struct tgsi_full_dst_register
*fd
= &insn
->Dst
[0];
2677 const struct tgsi_full_src_register
*fs
;
2678 unsigned i
, deqs
= 0;
2680 for (i
= 0; i
< 4; ++i
)
2683 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
2684 unsigned chn
, mask
= nv50_tgsi_src_mask(insn
, i
);
2685 boolean neg_supp
= negate_supported(insn
, i
);
2688 if (fs
->Register
.File
!= fd
->Register
.File
||
2689 fs
->Register
.Index
!= fd
->Register
.Index
)
2692 for (chn
= 0; chn
< 4; ++chn
) {
2695 if (!(mask
& (1 << chn
))) /* src is not read */
2697 c
= tgsi_util_get_full_src_register_swizzle(fs
, chn
);
2698 s
= tgsi_util_get_full_src_register_sign_mode(fs
, chn
);
2700 if (!(fd
->Register
.WriteMask
& (1 << c
)))
2703 /* no danger if src is copied to TEMP first */
2704 if ((s
!= TGSI_UTIL_SIGN_KEEP
) &&
2705 (s
!= TGSI_UTIL_SIGN_TOGGLE
|| !neg_supp
))
2708 rdep
[c
] |= nv50_tgsi_dst_revdep(
2709 insn
->Instruction
.Opcode
, i
, chn
);
2718 nv50_tgsi_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
2720 struct tgsi_full_instruction insn
= tok
->FullInstruction
;
2721 const struct tgsi_full_dst_register
*fd
;
2722 unsigned i
, deqs
, rdep
[4], m
[4];
2724 fd
= &tok
->FullInstruction
.Dst
[0];
2725 deqs
= nv50_tgsi_scan_swizzle(&insn
, rdep
);
2727 if (is_scalar_op(insn
.Instruction
.Opcode
)) {
2728 pc
->r_brdc
= tgsi_broadcast_dst(pc
, fd
, deqs
);
2730 pc
->r_brdc
= temp_temp(pc
);
2731 return nv50_program_tx_insn(pc
, &insn
);
2736 return nv50_program_tx_insn(pc
, &insn
);
2738 deqs
= nv50_revdep_reorder(m
, rdep
);
2740 for (i
= 0; i
< 4; ++i
) {
2741 assert(pc
->r_dst
[m
[i
]] == NULL
);
2743 insn
.Dst
[0].Register
.WriteMask
=
2744 fd
->Register
.WriteMask
& (1 << m
[i
]);
2746 if (!insn
.Dst
[0].Register
.WriteMask
)
2749 if (deqs
& (1 << i
))
2750 pc
->r_dst
[m
[i
]] = alloc_temp(pc
, NULL
);
2752 if (!nv50_program_tx_insn(pc
, &insn
))
2756 for (i
= 0; i
< 4; i
++) {
2757 struct nv50_reg
*reg
= pc
->r_dst
[i
];
2760 pc
->r_dst
[i
] = NULL
;
2762 if (insn
.Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
2763 emit_sat(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2765 emit_mov(pc
, tgsi_dst(pc
, i
, fd
), reg
);
2773 load_interpolant(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
2775 struct nv50_reg
*iv
, **ppiv
;
2776 unsigned mode
= pc
->interp_mode
[reg
->index
];
2778 ppiv
= (mode
& INTERP_CENTROID
) ? &pc
->iv_c
: &pc
->iv_p
;
2781 if ((mode
& INTERP_PERSPECTIVE
) && !iv
) {
2782 iv
= *ppiv
= alloc_temp(pc
, NULL
);
2783 iv
->rhw
= popcnt4(pc
->p
->cfg
.regs
[1] >> 24) - 1;
2785 emit_interp(pc
, iv
, NULL
, mode
& INTERP_CENTROID
);
2786 emit_flop(pc
, 0, iv
, iv
);
2788 /* XXX: when loading interpolants dynamically, move these
2789 * to the program head, or make sure it can't be skipped.
2793 emit_interp(pc
, reg
, iv
, mode
);
2796 /* The face input is always at v[255] (varying space), with a
2797 * value of 0 for back-facing, and 0xffffffff for front-facing.
2800 load_frontfacing(struct nv50_pc
*pc
, struct nv50_reg
*a
)
2802 struct nv50_reg
*one
= alloc_immd(pc
, 1.0f
);
2804 assert(a
->rhw
== -1);
2805 alloc_reg(pc
, a
); /* do this before rhw is set */
2807 load_interpolant(pc
, a
);
2808 emit_bitop2(pc
, a
, a
, one
, TGSI_OPCODE_AND
);
2814 nv50_program_tx_prep(struct nv50_pc
*pc
)
2816 struct tgsi_parse_context tp
;
2817 struct nv50_program
*p
= pc
->p
;
2818 boolean ret
= FALSE
;
2819 unsigned i
, c
, flat_nr
= 0;
2821 tgsi_parse_init(&tp
, pc
->p
->pipe
.tokens
);
2822 while (!tgsi_parse_end_of_tokens(&tp
)) {
2823 const union tgsi_full_token
*tok
= &tp
.FullToken
;
2825 tgsi_parse_token(&tp
);
2826 switch (tok
->Token
.Type
) {
2827 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2829 const struct tgsi_full_immediate
*imm
=
2830 &tp
.FullToken
.FullImmediate
;
2832 ctor_immd_4f32(pc
, imm
->u
[0].Float
,
2838 case TGSI_TOKEN_TYPE_DECLARATION
:
2840 const struct tgsi_full_declaration
*d
;
2841 unsigned si
, last
, first
, mode
;
2843 d
= &tp
.FullToken
.FullDeclaration
;
2844 first
= d
->Range
.First
;
2845 last
= d
->Range
.Last
;
2847 switch (d
->Declaration
.File
) {
2848 case TGSI_FILE_TEMPORARY
:
2850 case TGSI_FILE_OUTPUT
:
2851 if (!d
->Declaration
.Semantic
||
2852 p
->type
== PIPE_SHADER_FRAGMENT
)
2855 si
= d
->Semantic
.Index
;
2856 switch (d
->Semantic
.Name
) {
2857 case TGSI_SEMANTIC_BCOLOR
:
2858 p
->cfg
.two_side
[si
].hw
= first
;
2859 if (p
->cfg
.io_nr
> first
)
2860 p
->cfg
.io_nr
= first
;
2862 case TGSI_SEMANTIC_PSIZE
:
2863 p
->cfg
.psiz
= first
;
2864 if (p
->cfg
.io_nr
> first
)
2865 p
->cfg
.io_nr
= first
;
2868 case TGSI_SEMANTIC_CLIP_DISTANCE:
2869 p->cfg.clpd = MIN2(p->cfg.clpd, first);
2876 case TGSI_FILE_INPUT
:
2878 if (p
->type
!= PIPE_SHADER_FRAGMENT
)
2881 switch (d
->Declaration
.Interpolate
) {
2882 case TGSI_INTERPOLATE_CONSTANT
:
2886 case TGSI_INTERPOLATE_PERSPECTIVE
:
2887 mode
= INTERP_PERSPECTIVE
;
2888 p
->cfg
.regs
[1] |= 0x08 << 24;
2891 mode
= INTERP_LINEAR
;
2894 if (d
->Declaration
.Centroid
)
2895 mode
|= INTERP_CENTROID
;
2898 for (i
= first
; i
<= last
; i
++)
2899 pc
->interp_mode
[i
] = mode
;
2902 case TGSI_FILE_ADDRESS
:
2903 case TGSI_FILE_CONSTANT
:
2904 case TGSI_FILE_SAMPLER
:
2907 NOUVEAU_ERR("bad decl file %d\n",
2908 d
->Declaration
.File
);
2913 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2915 prep_inspect_insn(pc
, &tok
->FullInstruction
);
2922 if (p
->type
== PIPE_SHADER_VERTEX
) {
2925 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
) {
2926 if (pc
->attr
[i
].acc
) {
2927 pc
->attr
[i
].hw
= rid
++;
2928 p
->cfg
.attr
[i
/ 32] |= 1 << (i
% 32);
2932 for (i
= 0, rid
= 0; i
< pc
->result_nr
; ++i
) {
2933 p
->cfg
.io
[i
].hw
= rid
;
2934 p
->cfg
.io
[i
].id
= i
;
2936 for (c
= 0; c
< 4; ++c
) {
2938 if (!pc
->result
[n
].acc
)
2940 pc
->result
[n
].hw
= rid
++;
2941 p
->cfg
.io
[i
].mask
|= 1 << c
;
2945 for (c
= 0; c
< 2; ++c
)
2946 if (p
->cfg
.two_side
[c
].hw
< 0x40)
2947 p
->cfg
.two_side
[c
] = p
->cfg
.io
[
2948 p
->cfg
.two_side
[c
].hw
];
2950 if (p
->cfg
.psiz
< 0x40)
2951 p
->cfg
.psiz
= p
->cfg
.io
[p
->cfg
.psiz
].hw
;
2953 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
2955 unsigned n
= 0, m
= pc
->attr_nr
- flat_nr
;
2959 int base
= (TGSI_SEMANTIC_POSITION
==
2960 p
->info
.input_semantic_name
[0]) ? 0 : 1;
2962 /* non-flat interpolants have to be mapped to
2963 * the lower hardware IDs, so sort them:
2965 for (i
= 0; i
< pc
->attr_nr
; i
++) {
2966 if (pc
->interp_mode
[i
] == INTERP_FLAT
)
2967 p
->cfg
.io
[m
++].id
= i
;
2969 if (!(pc
->interp_mode
[i
] & INTERP_PERSPECTIVE
))
2970 p
->cfg
.io
[n
].linear
= TRUE
;
2971 p
->cfg
.io
[n
++].id
= i
;
2975 if (!base
) /* set w-coordinate mask from perspective interp */
2976 p
->cfg
.io
[0].mask
|= p
->cfg
.regs
[1] >> 24;
2978 aid
= popcnt4( /* if fcrd isn't contained in cfg.io */
2979 base
? (p
->cfg
.regs
[1] >> 24) : p
->cfg
.io
[0].mask
);
2981 for (n
= 0; n
< pc
->attr_nr
; ++n
) {
2982 p
->cfg
.io
[n
].hw
= rid
= aid
;
2983 i
= p
->cfg
.io
[n
].id
;
2985 if (p
->info
.input_semantic_name
[n
] ==
2986 TGSI_SEMANTIC_FACE
) {
2987 load_frontfacing(pc
, &pc
->attr
[i
* 4]);
2991 for (c
= 0; c
< 4; ++c
) {
2992 if (!pc
->attr
[i
* 4 + c
].acc
)
2994 pc
->attr
[i
* 4 + c
].rhw
= rid
++;
2995 p
->cfg
.io
[n
].mask
|= 1 << c
;
2997 load_interpolant(pc
, &pc
->attr
[i
* 4 + c
]);
2999 aid
+= popcnt4(p
->cfg
.io
[n
].mask
);
3003 p
->cfg
.regs
[1] |= p
->cfg
.io
[0].mask
<< 24;
3005 m
= popcnt4(p
->cfg
.regs
[1] >> 24);
3007 /* set count of non-position inputs and of non-flat
3008 * non-position inputs for FP_INTERPOLANT_CTRL
3010 p
->cfg
.regs
[1] |= aid
- m
;
3013 i
= p
->cfg
.io
[pc
->attr_nr
- flat_nr
].hw
;
3014 p
->cfg
.regs
[1] |= (i
- m
) << 16;
3016 p
->cfg
.regs
[1] |= p
->cfg
.regs
[1] << 16;
3018 /* mark color semantic for light-twoside */
3020 for (i
= 0; i
< pc
->attr_nr
; i
++) {
3023 sn
= p
->info
.input_semantic_name
[p
->cfg
.io
[i
].id
];
3024 si
= p
->info
.input_semantic_index
[p
->cfg
.io
[i
].id
];
3026 if (sn
== TGSI_SEMANTIC_COLOR
) {
3027 p
->cfg
.two_side
[si
] = p
->cfg
.io
[i
];
3029 /* increase colour count */
3030 p
->cfg
.regs
[0] += popcnt4(
3031 p
->cfg
.two_side
[si
].mask
) << 16;
3033 n
= MIN2(n
, p
->cfg
.io
[i
].hw
- m
);
3037 p
->cfg
.regs
[0] += n
;
3039 /* Initialize FP results:
3040 * FragDepth is always first TGSI and last hw output
3042 i
= p
->info
.writes_z
? 4 : 0;
3043 for (rid
= 0; i
< pc
->result_nr
* 4; i
++)
3044 pc
->result
[i
].rhw
= rid
++;
3045 if (p
->info
.writes_z
)
3046 pc
->result
[2].rhw
= rid
;
3048 p
->cfg
.high_result
= rid
;
3050 /* separate/different colour results for MRTs ? */
3051 if (pc
->result_nr
- (p
->info
.writes_z
? 1 : 0) > 1)
3052 p
->cfg
.regs
[2] |= 1;
3058 pc
->immd
= MALLOC(pc
->immd_nr
* 4 * sizeof(struct nv50_reg
));
3062 for (i
= 0; i
< pc
->immd_nr
; i
++) {
3063 for (c
= 0; c
< 4; c
++, rid
++)
3064 ctor_reg(&pc
->immd
[rid
], P_IMMD
, i
, rid
);
3071 free_temp(pc
, pc
->iv_p
);
3073 free_temp(pc
, pc
->iv_c
);
3075 tgsi_parse_free(&tp
);
3080 free_nv50_pc(struct nv50_pc
*pc
)
3097 ctor_nv50_pc(struct nv50_pc
*pc
, struct nv50_program
*p
)
3100 unsigned rtype
[2] = { P_ATTR
, P_RESULT
};
3103 pc
->temp_nr
= p
->info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
3104 pc
->attr_nr
= p
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
3105 pc
->result_nr
= p
->info
.file_max
[TGSI_FILE_OUTPUT
] + 1;
3106 pc
->param_nr
= p
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
3107 pc
->addr_nr
= p
->info
.file_max
[TGSI_FILE_ADDRESS
] + 1;
3108 assert(pc
->addr_nr
<= 2);
3110 p
->cfg
.high_temp
= 4;
3112 p
->cfg
.two_side
[0].hw
= 0x40;
3113 p
->cfg
.two_side
[1].hw
= 0x40;
3116 case PIPE_SHADER_VERTEX
:
3119 p
->cfg
.io_nr
= pc
->result_nr
;
3121 case PIPE_SHADER_FRAGMENT
:
3122 rtype
[0] = rtype
[1] = P_TEMP
;
3124 p
->cfg
.regs
[0] = 0x01000004;
3125 p
->cfg
.io_nr
= pc
->attr_nr
;
3127 if (p
->info
.writes_z
) {
3128 p
->cfg
.regs
[2] |= 0x00000100;
3129 p
->cfg
.regs
[3] |= 0x00000011;
3131 if (p
->info
.uses_kill
)
3132 p
->cfg
.regs
[2] |= 0x00100000;
3137 pc
->temp
= MALLOC(pc
->temp_nr
* 4 * sizeof(struct nv50_reg
));
3141 for (i
= 0; i
< pc
->temp_nr
* 4; ++i
)
3142 ctor_reg(&pc
->temp
[i
], P_TEMP
, i
/ 4, -1);
3146 pc
->attr
= MALLOC(pc
->attr_nr
* 4 * sizeof(struct nv50_reg
));
3150 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
)
3151 ctor_reg(&pc
->attr
[i
], rtype
[0], i
/ 4, -1);
3154 if (pc
->result_nr
) {
3155 unsigned nr
= pc
->result_nr
* 4;
3157 pc
->result
= MALLOC(nr
* sizeof(struct nv50_reg
));
3161 for (i
= 0; i
< nr
; ++i
)
3162 ctor_reg(&pc
->result
[i
], rtype
[1], i
/ 4, -1);
3168 pc
->param
= MALLOC(pc
->param_nr
* 4 * sizeof(struct nv50_reg
));
3172 for (i
= 0; i
< pc
->param_nr
; ++i
)
3173 for (c
= 0; c
< 4; ++c
, ++rid
)
3174 ctor_reg(&pc
->param
[rid
], P_CONST
, i
, rid
);
3178 pc
->addr
= CALLOC(pc
->addr_nr
* 4, sizeof(struct nv50_reg
*));
3182 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
3183 ctor_reg(&pc
->r_addr
[i
], P_ADDR
, -256, i
+ 1);
3189 nv50_program_fixup_insns(struct nv50_pc
*pc
)
3191 struct nv50_program_exec
*e
, **bra_list
;
3194 bra_list
= CALLOC(pc
->p
->exec_size
, sizeof(struct nv50_program_exec
*));
3196 /* Collect branch instructions, we need to adjust their offsets
3197 * when converting 32 bit instructions to 64 bit ones
3199 for (n
= 0, e
= pc
->p
->exec_head
; e
; e
= e
->next
)
3200 if (e
->param
.index
>= 0 && !e
->param
.mask
)
3203 /* last instruction must be long so it can have the exit bit set */
3204 if (!is_long(pc
->p
->exec_tail
))
3205 convert_to_long(pc
, pc
->p
->exec_tail
);
3207 pc
->p
->exec_tail
->inst
[1] |= 1;
3209 /* !immd on exit insn simultaneously means !join */
3210 assert(!is_immd(pc
->p
->exec_head
));
3211 assert(!is_immd(pc
->p
->exec_tail
));
3213 /* Make sure we don't have any single 32 bit instructions. */
3214 for (e
= pc
->p
->exec_head
, pos
= 0; e
; e
= e
->next
) {
3215 pos
+= is_long(e
) ? 2 : 1;
3217 if ((pos
& 1) && (!e
->next
|| is_long(e
->next
))) {
3218 for (i
= 0; i
< n
; ++i
)
3219 if (bra_list
[i
]->param
.index
>= pos
)
3220 bra_list
[i
]->param
.index
+= 1;
3221 convert_to_long(pc
, e
);
3230 nv50_program_tx(struct nv50_program
*p
)
3232 struct tgsi_parse_context parse
;
3236 pc
= CALLOC_STRUCT(nv50_pc
);
3240 ret
= ctor_nv50_pc(pc
, p
);
3244 ret
= nv50_program_tx_prep(pc
);
3248 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
3249 while (!tgsi_parse_end_of_tokens(&parse
)) {
3250 const union tgsi_full_token
*tok
= &parse
.FullToken
;
3252 /* don't allow half insn/immd on first and last instruction */
3254 if (pc
->insn_cur
== 0 || pc
->insn_cur
+ 2 == pc
->insn_nr
)
3255 pc
->allow32
= FALSE
;
3257 tgsi_parse_token(&parse
);
3259 switch (tok
->Token
.Type
) {
3260 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3262 ret
= nv50_tgsi_insn(pc
, tok
);
3271 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
)
3272 nv50_fp_move_results(pc
);
3274 nv50_program_fixup_insns(pc
);
3276 p
->param_nr
= pc
->param_nr
* 4;
3277 p
->immd_nr
= pc
->immd_nr
* 4;
3278 p
->immd
= pc
->immd_buf
;
3281 tgsi_parse_free(&parse
);
3289 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
3291 if (nv50_program_tx(p
) == FALSE
)
3293 p
->translated
= TRUE
;
3297 nv50_program_upload_data(struct nv50_context
*nv50
, uint32_t *map
,
3298 unsigned start
, unsigned count
, unsigned cbuf
)
3300 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
3301 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3304 unsigned nr
= count
> 2047 ? 2047 : count
;
3306 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
3307 OUT_RING (chan
, (cbuf
<< 0) | (start
<< 8));
3308 BEGIN_RING(chan
, tesla
, NV50TCL_CB_DATA(0) | 0x40000000, nr
);
3309 OUT_RINGp (chan
, map
, nr
);
3318 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
3320 struct pipe_screen
*pscreen
= nv50
->pipe
.screen
;
3322 if (!p
->data
[0] && p
->immd_nr
) {
3323 struct nouveau_resource
*heap
= nv50
->screen
->immd_heap
[0];
3325 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
, &p
->data
[0])) {
3326 while (heap
->next
&& heap
->size
< p
->immd_nr
) {
3327 struct nv50_program
*evict
= heap
->next
->priv
;
3328 nouveau_resource_free(&evict
->data
[0]);
3331 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
,
3336 /* immediates only need to be uploaded again when freed */
3337 nv50_program_upload_data(nv50
, p
->immd
, p
->data
[0]->start
,
3338 p
->immd_nr
, NV50_CB_PMISC
);
3341 assert(p
->param_nr
<= 512);
3345 uint32_t *map
= pipe_buffer_map(pscreen
, nv50
->constbuf
[p
->type
],
3346 PIPE_BUFFER_USAGE_CPU_READ
);
3348 if (p
->type
== PIPE_SHADER_VERTEX
)
3353 nv50_program_upload_data(nv50
, map
, 0, p
->param_nr
, cb
);
3354 pipe_buffer_unmap(pscreen
, nv50
->constbuf
[p
->type
]);
3359 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
3361 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
3362 struct nv50_program_exec
*e
;
3364 boolean upload
= FALSE
;
3367 nouveau_bo_new(chan
->device
, NOUVEAU_BO_VRAM
, 0x100,
3368 p
->exec_size
* 4, &p
->bo
);
3372 if (p
->data
[0] && p
->data
[0]->start
!= p
->data_start
[0])
3378 up
= MALLOC(p
->exec_size
* 4);
3380 for (i
= 0, e
= p
->exec_head
; e
; e
= e
->next
) {
3381 unsigned ei
, ci
, bs
;
3383 if (e
->param
.index
>= 0 && e
->param
.mask
) {
3384 bs
= (e
->inst
[1] >> 22) & 0x07;
3386 ei
= e
->param
.shift
>> 5;
3387 ci
= e
->param
.index
;
3389 ci
+= p
->data
[bs
]->start
;
3391 e
->inst
[ei
] &= ~e
->param
.mask
;
3392 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
3394 if (e
->param
.index
>= 0) {
3395 /* zero mask means param is a jump/branch offset */
3396 assert(!(e
->param
.index
& 1));
3397 /* seem to be 8 byte steps */
3398 ei
= (e
->param
.index
>> 1) + 0 /* START_ID */;
3400 e
->inst
[0] &= 0xf0000fff;
3401 e
->inst
[0] |= ei
<< 12;
3404 up
[i
++] = e
->inst
[0];
3406 up
[i
++] = e
->inst
[1];
3408 assert(i
== p
->exec_size
);
3411 p
->data_start
[0] = p
->data
[0]->start
;
3413 #ifdef NV50_PROGRAM_DUMP
3414 NOUVEAU_ERR("-------\n");
3415 for (e
= p
->exec_head
; e
; e
= e
->next
) {
3416 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
3418 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
3421 nv50_upload_sifc(nv50
, p
->bo
, 0, NOUVEAU_BO_VRAM
,
3422 NV50_2D_DST_FORMAT_R8_UNORM
, 65536, 1, 262144,
3423 up
, NV50_2D_SIFC_FORMAT_R8_UNORM
, 0,
3424 0, 0, p
->exec_size
* 4, 1, 1);
3430 nv50_vertprog_validate(struct nv50_context
*nv50
)
3432 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3433 struct nv50_program
*p
= nv50
->vertprog
;
3434 struct nouveau_stateobj
*so
;
3436 if (!p
->translated
) {
3437 nv50_program_validate(nv50
, p
);
3442 nv50_program_validate_data(nv50
, p
);
3443 nv50_program_validate_code(nv50
, p
);
3446 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
3447 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3448 NOUVEAU_BO_HIGH
, 0, 0);
3449 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3450 NOUVEAU_BO_LOW
, 0, 0);
3451 so_method(so
, tesla
, NV50TCL_VP_ATTR_EN_0
, 2);
3452 so_data (so
, p
->cfg
.attr
[0]);
3453 so_data (so
, p
->cfg
.attr
[1]);
3454 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_RESULT
, 1);
3455 so_data (so
, p
->cfg
.high_result
);
3456 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 2);
3457 so_data (so
, p
->cfg
.high_result
); //8);
3458 so_data (so
, p
->cfg
.high_temp
);
3459 so_method(so
, tesla
, NV50TCL_VP_START_ID
, 1);
3460 so_data (so
, 0); /* program start offset */
3461 so_ref(so
, &nv50
->state
.vertprog
);
3466 nv50_fragprog_validate(struct nv50_context
*nv50
)
3468 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3469 struct nv50_program
*p
= nv50
->fragprog
;
3470 struct nouveau_stateobj
*so
;
3472 if (!p
->translated
) {
3473 nv50_program_validate(nv50
, p
);
3478 nv50_program_validate_data(nv50
, p
);
3479 nv50_program_validate_code(nv50
, p
);
3482 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
3483 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3484 NOUVEAU_BO_HIGH
, 0, 0);
3485 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
3486 NOUVEAU_BO_LOW
, 0, 0);
3487 so_method(so
, tesla
, NV50TCL_FP_REG_ALLOC_TEMP
, 1);
3488 so_data (so
, p
->cfg
.high_temp
);
3489 so_method(so
, tesla
, NV50TCL_FP_RESULT_COUNT
, 1);
3490 so_data (so
, p
->cfg
.high_result
);
3491 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK19A8
, 1);
3492 so_data (so
, p
->cfg
.regs
[2]);
3493 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK196C
, 1);
3494 so_data (so
, p
->cfg
.regs
[3]);
3495 so_method(so
, tesla
, NV50TCL_FP_START_ID
, 1);
3496 so_data (so
, 0); /* program start offset */
3497 so_ref(so
, &nv50
->state
.fragprog
);
3502 nv50_pntc_replace(struct nv50_context
*nv50
, uint32_t pntc
[8], unsigned base
)
3504 struct nv50_program
*fp
= nv50
->fragprog
;
3505 struct nv50_program
*vp
= nv50
->vertprog
;
3506 unsigned i
, c
, m
= base
;
3508 /* XXX: this might not work correctly in all cases yet - we'll
3509 * just assume that an FP generic input that is not written in
3510 * the VP is PointCoord.
3512 memset(pntc
, 0, 8 * sizeof(uint32_t));
3514 for (i
= 0; i
< fp
->cfg
.io_nr
; i
++) {
3516 uint8_t j
, k
= fp
->cfg
.io
[i
].id
;
3517 unsigned n
= popcnt4(fp
->cfg
.io
[i
].mask
);
3519 if (fp
->info
.input_semantic_name
[k
] != TGSI_SEMANTIC_GENERIC
) {
3524 for (j
= 0; j
< vp
->info
.num_outputs
; ++j
) {
3525 sn
= vp
->info
.output_semantic_name
[j
];
3526 si
= vp
->info
.output_semantic_index
[j
];
3528 if (sn
== fp
->info
.input_semantic_name
[k
] &&
3529 si
== fp
->info
.input_semantic_index
[k
])
3533 if (j
< vp
->info
.num_outputs
) {
3535 nv50
->rasterizer
->pipe
.sprite_coord_mode
[si
];
3537 if (mode
== PIPE_SPRITE_COORD_NONE
) {
3543 /* this is either PointCoord or replaced by sprite coords */
3544 for (c
= 0; c
< 4; c
++) {
3545 if (!(fp
->cfg
.io
[i
].mask
& (1 << c
)))
3547 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
3554 nv50_sreg4_map(uint32_t *p_map
, int mid
, uint32_t lin
[4],
3555 struct nv50_sreg4
*fpi
, struct nv50_sreg4
*vpo
)
3558 uint8_t mv
= vpo
->mask
, mf
= fpi
->mask
, oid
= vpo
->hw
;
3559 uint8_t *map
= (uint8_t *)p_map
;
3561 for (c
= 0; c
< 4; ++c
) {
3563 if (fpi
->linear
== TRUE
)
3564 lin
[mid
/ 32] |= 1 << (mid
% 32);
3565 map
[mid
++] = (mv
& 1) ? oid
: ((c
== 3) ? 0x41 : 0x40);
3577 nv50_linkage_validate(struct nv50_context
*nv50
)
3579 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
3580 struct nv50_program
*vp
= nv50
->vertprog
;
3581 struct nv50_program
*fp
= nv50
->fragprog
;
3582 struct nouveau_stateobj
*so
;
3583 struct nv50_sreg4 dummy
, *vpo
;
3585 uint32_t map
[16], lin
[4], reg
[5], pcrd
[8];
3587 memset(map
, 0, sizeof(map
));
3588 memset(lin
, 0, sizeof(lin
));
3590 reg
[1] = 0x00000004; /* low and high clip distance map ids */
3591 reg
[2] = 0x00000000; /* layer index map id (disabled, GP only) */
3592 reg
[3] = 0x00000000; /* point size map id & enable */
3593 reg
[0] = fp
->cfg
.regs
[0]; /* colour semantic reg */
3594 reg
[4] = fp
->cfg
.regs
[1]; /* interpolant info */
3596 dummy
.linear
= FALSE
;
3597 dummy
.mask
= 0xf; /* map all components of HPOS */
3598 m
= nv50_sreg4_map(map
, m
, lin
, &dummy
, &vp
->cfg
.io
[0]);
3602 if (vp
->cfg
.clpd
< 0x40) {
3603 for (c
= 0; c
< vp
->cfg
.clpd_nr
; ++c
)
3604 map
[m
++] = vp
->cfg
.clpd
+ c
;
3608 reg
[0] |= m
<< 8; /* adjust BFC0 id */
3610 /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
3611 if (nv50
->rasterizer
->pipe
.light_twoside
) {
3612 vpo
= &vp
->cfg
.two_side
[0];
3614 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[0], &vpo
[0]);
3615 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.two_side
[1], &vpo
[1]);
3618 reg
[0] += m
- 4; /* adjust FFC0 id */
3619 reg
[4] |= m
<< 8; /* set mid where 'normal' FP inputs start */
3621 for (i
= 0; i
< fp
->cfg
.io_nr
; i
++) {
3622 ubyte sn
= fp
->info
.input_semantic_name
[fp
->cfg
.io
[i
].id
];
3623 ubyte si
= fp
->info
.input_semantic_index
[fp
->cfg
.io
[i
].id
];
3625 /* position must be mapped first */
3626 assert(i
== 0 || sn
!= TGSI_SEMANTIC_POSITION
);
3628 /* maybe even remove these from cfg.io */
3629 if (sn
== TGSI_SEMANTIC_POSITION
|| sn
== TGSI_SEMANTIC_FACE
)
3632 /* VP outputs and vp->cfg.io are in the same order */
3633 for (n
= 0; n
< vp
->info
.num_outputs
; ++n
) {
3634 if (vp
->info
.output_semantic_name
[n
] == sn
&&
3635 vp
->info
.output_semantic_index
[n
] == si
)
3638 vpo
= (n
< vp
->info
.num_outputs
) ? &vp
->cfg
.io
[n
] : &dummy
;
3640 m
= nv50_sreg4_map(map
, m
, lin
, &fp
->cfg
.io
[i
], vpo
);
3643 if (nv50
->rasterizer
->pipe
.point_size_per_vertex
) {
3644 map
[m
/ 4] |= vp
->cfg
.psiz
<< ((m
% 4) * 8);
3645 reg
[3] = (m
++ << 4) | 1;
3648 /* now fill the stateobj */
3652 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
3654 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), n
);
3655 so_datap (so
, map
, n
);
3657 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_0
, 4);
3658 so_datap (so
, reg
, 4);
3660 so_method(so
, tesla
, NV50TCL_FP_INTERPOLANT_CTRL
, 1);
3661 so_data (so
, reg
[4]);
3663 so_method(so
, tesla
, 0x1540, 4);
3664 so_datap (so
, lin
, 4);
3666 if (nv50
->rasterizer
->pipe
.point_sprite
) {
3667 nv50_pntc_replace(nv50
, pcrd
, (reg
[4] >> 8) & 0xff);
3669 so_method(so
, tesla
, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
3670 so_datap (so
, pcrd
, 8);
3673 so_ref(so
, &nv50
->state
.programs
);
3678 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
3680 while (p
->exec_head
) {
3681 struct nv50_program_exec
*e
= p
->exec_head
;
3683 p
->exec_head
= e
->next
;
3686 p
->exec_tail
= NULL
;
3689 nouveau_bo_ref(NULL
, &p
->bo
);
3691 nouveau_resource_free(&p
->data
[0]);