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 "util/u_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"
33 #include "nv50_transfer.h"
35 #define NV50_SU_MAX_TEMP 127
36 #define NV50_SU_MAX_ADDR 4
37 //#define NV50_PROGRAM_DUMP
39 /* $a5 and $a6 always seem to be 0, and using $a7 gives you noise */
41 /* ARL - gallium craps itself on progs/vp/arl.txt
43 * MSB - Like MAD, but MUL+SUB
44 * - Fuck it off, introduce a way to negate args for ops that
47 * Look into inlining IMMD for ops other than MOV (make it general?)
48 * - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
49 * but can emit to P_TEMP first - then MOV later. NVIDIA does this
51 * In ops such as ADD it's possible to construct a bad opcode in the !is_long()
52 * case, if the emit_src() causes the inst to suddenly become long.
54 * Verify half-insns work where expected - and force disable them where they
55 * don't work - MUL has it forcibly disabled atm as it fixes POW..
57 * FUCK! watch dst==src vectors, can overwrite components that are needed.
58 * ie. SUB R0, R0.yzxw, R0
60 * Things to check with renouveau:
61 * FP attr/result assignment - how?
63 * - 0x16bc maps vp output onto fp hpos
64 * - 0x16c0 maps vp output onto fp col0
68 * 0x16bc->0x16e8 --> some binding between vp/fp regs
69 * 0x16b8 --> VP output count
71 * 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
72 * "MOV rcol.x, fcol.y" = 0x00000004
73 * 0x19a8 --> as above but 0x00000100 and 0x00000000
74 * - 0x00100000 used when KIL used
75 * 0x196c --> as above but 0x00000011 and 0x00000000
77 * 0x1988 --> 0xXXNNNNNN
78 * - XX == FP high something
94 int rhw
; /* result hw for FP outputs, or interpolant index */
95 int acc
; /* instruction where this reg is last read (first insn == 1) */
97 int vtx
; /* vertex index, for GP inputs (TGSI Dimension.Index) */
98 int indirect
[2]; /* index into pc->addr, or -1 */
100 ubyte buf_index
; /* c{0 .. 15}[] or g{0 .. 15}[] */
103 #define NV50_MOD_NEG 1
104 #define NV50_MOD_ABS 2
105 #define NV50_MOD_NEG_ABS (NV50_MOD_NEG | NV50_MOD_ABS)
106 #define NV50_MOD_SAT 4
107 #define NV50_MOD_I32 8
109 /* NV50_MOD_I32 is used to indicate integer mode for neg/abs */
111 /* STACK: Conditionals and loops have to use the (per warp) stack.
112 * Stack entries consist of an entry type (divergent path, join at),
113 * a mask indicating the active threads of the warp, and an address.
114 * MPs can store 12 stack entries internally, if we need more (and
115 * we probably do), we have to create a stack buffer in VRAM.
117 /* impose low limits for now */
118 #define NV50_MAX_COND_NESTING 4
119 #define NV50_MAX_LOOP_NESTING 3
121 #define JOIN_ON(e) e; pc->p->exec_tail->inst[1] |= 2
124 struct nv50_program
*p
;
127 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
128 struct nv50_reg r_addr
[NV50_SU_MAX_ADDR
];
131 struct nv50_reg
*temp
;
133 struct nv50_reg
*attr
;
135 struct nv50_reg
*result
;
137 struct nv50_reg
*param
;
139 struct nv50_reg
*immd
;
142 struct nv50_reg
**addr
;
144 struct nv50_reg
*sysval
;
147 struct nv50_reg
*temp_temp
[16];
148 struct nv50_program_exec
*temp_temp_exec
[16];
149 unsigned temp_temp_nr
;
151 /* broadcast and destination replacement regs */
152 struct nv50_reg
*r_brdc
;
153 struct nv50_reg
*r_dst
[4];
155 struct nv50_reg reg_instances
[16];
156 unsigned reg_instance_nr
;
158 unsigned interp_mode
[32];
159 /* perspective interpolation registers */
160 struct nv50_reg
*iv_p
;
161 struct nv50_reg
*iv_c
;
163 struct nv50_program_exec
*if_insn
[NV50_MAX_COND_NESTING
];
164 struct nv50_program_exec
*if_join
[NV50_MAX_COND_NESTING
];
165 struct nv50_program_exec
*loop_brka
[NV50_MAX_LOOP_NESTING
];
166 int if_lvl
, loop_lvl
;
167 unsigned loop_pos
[NV50_MAX_LOOP_NESTING
];
169 unsigned *insn_pos
; /* actual program offset of each TGSI insn */
170 boolean in_subroutine
;
172 /* current instruction and total number of insns */
178 uint8_t edgeflag_out
;
181 static struct nv50_reg
*get_address_reg(struct nv50_pc
*, struct nv50_reg
*);
184 ctor_reg(struct nv50_reg
*reg
, unsigned type
, int index
, int hw
)
193 reg
->indirect
[0] = reg
->indirect
[1] = -1;
194 reg
->buf_index
= (type
== P_CONST
) ? 1 : 0;
197 static INLINE
unsigned
198 popcnt4(uint32_t val
)
200 static const unsigned cnt
[16]
201 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
202 return cnt
[val
& 0xf];
206 terminate_mbb(struct nv50_pc
*pc
)
210 /* remove records of temporary address register values */
211 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
212 if (pc
->r_addr
[i
].index
< 0)
213 pc
->r_addr
[i
].acc
= 0;
217 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
221 if (reg
->type
== P_RESULT
) {
222 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
223 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
226 if (reg
->type
!= P_TEMP
)
230 /*XXX: do this here too to catch FP temp-as-attr usage..
231 * not clean, but works */
232 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
233 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
237 if (reg
->rhw
!= -1) {
238 /* try to allocate temporary with index rhw first */
239 if (!(pc
->r_temp
[reg
->rhw
])) {
240 pc
->r_temp
[reg
->rhw
] = reg
;
242 if (pc
->p
->cfg
.high_temp
< (reg
->rhw
+ 1))
243 pc
->p
->cfg
.high_temp
= reg
->rhw
+ 1;
246 /* make sure we don't get things like $r0 needs to go
247 * in $r1 and $r1 in $r0
249 i
= pc
->result_nr
* 4;
252 for (; i
< NV50_SU_MAX_TEMP
; i
++) {
253 if (!(pc
->r_temp
[i
])) {
256 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
257 pc
->p
->cfg
.high_temp
= i
+ 1;
262 NOUVEAU_ERR("out of registers\n");
266 static INLINE
struct nv50_reg
*
267 reg_instance(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
271 assert(pc
->reg_instance_nr
< 16);
272 ri
= &pc
->reg_instances
[pc
->reg_instance_nr
++];
276 reg
->indirect
[0] = reg
->indirect
[1] = -1;
282 /* XXX: For shaders that aren't executed linearly (e.g. shaders that
283 * contain loops), we need to assign all hw regs to TGSI TEMPs early,
284 * lest we risk temp_temps overwriting regs alloc'd "later".
286 static struct nv50_reg
*
287 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
292 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
295 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
296 if (!pc
->r_temp
[i
]) {
297 r
= MALLOC_STRUCT(nv50_reg
);
298 ctor_reg(r
, P_TEMP
, -1, i
);
304 NOUVEAU_ERR("out of registers\n");
309 /* release the hardware resource held by r */
311 release_hw(struct nv50_pc
*pc
, struct nv50_reg
*r
)
313 assert(r
->type
== P_TEMP
);
317 assert(pc
->r_temp
[r
->hw
] == r
);
318 pc
->r_temp
[r
->hw
] = NULL
;
326 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
328 if (r
->index
== -1) {
331 FREE(pc
->r_temp
[hw
]);
332 pc
->r_temp
[hw
] = NULL
;
337 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
341 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
344 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
345 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
346 return alloc_temp4(pc
, dst
, idx
+ 4);
348 for (i
= 0; i
< 4; i
++) {
349 dst
[i
] = MALLOC_STRUCT(nv50_reg
);
350 ctor_reg(dst
[i
], P_TEMP
, -1, idx
+ i
);
351 pc
->r_temp
[idx
+ i
] = dst
[i
];
358 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
362 for (i
= 0; i
< 4; i
++)
363 free_temp(pc
, reg
[i
]);
366 static struct nv50_reg
*
367 temp_temp(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
369 if (pc
->temp_temp_nr
>= 16)
372 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
373 pc
->temp_temp_exec
[pc
->temp_temp_nr
] = e
;
374 return pc
->temp_temp
[pc
->temp_temp_nr
++];
377 /* This *must* be called for all nv50_program_exec that have been
378 * given as argument to temp_temp, or the temps will be leaked !
381 kill_temp_temp(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
385 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
386 if (pc
->temp_temp_exec
[i
] == e
)
387 free_temp(pc
, pc
->temp_temp
[i
]);
389 pc
->temp_temp_nr
= 0;
393 ctor_immd_4u32(struct nv50_pc
*pc
,
394 uint32_t x
, uint32_t y
, uint32_t z
, uint32_t w
)
396 unsigned size
= pc
->immd_nr
* 4 * sizeof(uint32_t);
398 pc
->immd_buf
= REALLOC(pc
->immd_buf
, size
, size
+ 4 * sizeof(uint32_t));
400 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
401 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
402 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
403 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
405 return pc
->immd_nr
++;
409 ctor_immd_4f32(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
411 return ctor_immd_4u32(pc
, fui(x
), fui(y
), fui(z
), fui(w
));
414 static struct nv50_reg
*
415 alloc_immd(struct nv50_pc
*pc
, float f
)
417 struct nv50_reg
*r
= MALLOC_STRUCT(nv50_reg
);
420 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
421 if (pc
->immd_buf
[hw
] == fui(f
))
424 if (hw
== pc
->immd_nr
* 4)
425 hw
= ctor_immd_4f32(pc
, f
, -f
, 0.5 * f
, 0) * 4;
427 ctor_reg(r
, P_IMMD
, -1, hw
);
431 static struct nv50_program_exec
*
432 exec(struct nv50_pc
*pc
)
434 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
441 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
443 struct nv50_program
*p
= pc
->p
;
446 p
->exec_tail
->next
= e
;
450 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
452 kill_temp_temp(pc
, e
);
455 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
458 is_long(struct nv50_program_exec
*e
)
466 is_immd(struct nv50_program_exec
*e
)
468 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
474 is_join(struct nv50_program_exec
*e
)
476 if (is_long(e
) && (e
->inst
[1] & 3) == 2)
481 static INLINE boolean
482 is_control_flow(struct nv50_program_exec
*e
)
484 return (e
->inst
[0] & 2);
488 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
489 struct nv50_program_exec
*e
)
493 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
494 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
498 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
499 struct nv50_program_exec
*e
)
502 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
503 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
507 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
513 set_pred(pc
, 0xf, 0, e
);
514 set_pred_wr(pc
, 0, 0, e
);
518 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
520 if (dst
->type
== P_RESULT
) {
522 e
->inst
[1] |= 0x00000008;
528 e
->inst
[0] |= (dst
->hw
<< 2);
532 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
535 /* XXX: can't be predicated - bits overlap; cases where both
536 * are required should be avoided by using pc->allow32 */
537 set_pred(pc
, 0, 0, e
);
538 set_pred_wr(pc
, 0, 0, e
);
540 e
->inst
[1] |= 0x00000002 | 0x00000001;
541 e
->inst
[0] |= (pc
->immd_buf
[imm
->hw
] & 0x3f) << 16;
542 e
->inst
[1] |= (pc
->immd_buf
[imm
->hw
] >> 6) << 2;
546 set_addr(struct nv50_program_exec
*e
, struct nv50_reg
*a
)
548 assert(a
->type
== P_ADDR
);
550 assert(!(e
->inst
[0] & 0x0c000000));
551 assert(!(e
->inst
[1] & 0x00000004));
553 e
->inst
[0] |= (a
->hw
& 3) << 26;
554 e
->inst
[1] |= a
->hw
& 4;
558 emit_arl(struct nv50_pc
*, struct nv50_reg
*, struct nv50_reg
*, uint8_t);
561 emit_shl_imm(struct nv50_pc
*, struct nv50_reg
*, struct nv50_reg
*, int);
564 emit_mov_from_addr(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
565 struct nv50_reg
*src
)
567 struct nv50_program_exec
*e
= exec(pc
);
569 e
->inst
[1] = 0x40000000;
578 emit_add_addr_imm(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
579 struct nv50_reg
*src0
, uint16_t src1_val
)
581 struct nv50_program_exec
*e
= exec(pc
);
583 e
->inst
[0] = 0xd0000000 | (src1_val
<< 9);
584 e
->inst
[1] = 0x20000000;
586 e
->inst
[0] |= dst
->hw
<< 2;
587 if (src0
) /* otherwise will add to $a0, which is always 0 */
593 #define INTERP_LINEAR 0
594 #define INTERP_FLAT 1
595 #define INTERP_PERSPECTIVE 2
596 #define INTERP_CENTROID 4
598 /* interpolant index has been stored in dst->rhw */
600 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*iv
,
603 struct nv50_program_exec
*e
= exec(pc
);
604 assert(dst
->rhw
!= -1);
606 e
->inst
[0] |= 0x80000000;
608 e
->inst
[0] |= (dst
->rhw
<< 16);
610 if (mode
& INTERP_FLAT
) {
611 e
->inst
[0] |= (1 << 8);
613 if (mode
& INTERP_PERSPECTIVE
) {
614 e
->inst
[0] |= (1 << 25);
616 e
->inst
[0] |= (iv
->hw
<< 9);
619 if (mode
& INTERP_CENTROID
)
620 e
->inst
[0] |= (1 << 24);
627 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
628 struct nv50_program_exec
*e
)
632 e
->param
.index
= src
->hw
& 127;
634 e
->param
.mask
= m
<< (s
% 32);
636 if (src
->hw
< 0 || src
->hw
> 127) /* need (additional) address reg */
637 set_addr(e
, get_address_reg(pc
, src
));
640 assert(src
->type
== P_CONST
);
641 set_addr(e
, pc
->addr
[src
->indirect
[0]]);
644 e
->inst
[1] |= (src
->buf_index
<< 22);
647 /* Never apply nv50_reg::mod in emit_mov, or carefully check the code !!! */
649 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
651 struct nv50_program_exec
*e
= exec(pc
);
653 e
->inst
[0] = 0x10000000;
659 if (!is_long(e
) && src
->type
== P_IMMD
) {
660 set_immd(pc
, src
, e
);
661 /*XXX: 32-bit, but steals part of "half" reg space - need to
662 * catch and handle this case if/when we do half-regs
665 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
667 set_data(pc
, src
, 0x7f, 9, e
);
668 e
->inst
[1] |= 0x20000000; /* mov from c[] */
670 if (src
->type
== P_ATTR
) {
672 e
->inst
[1] |= 0x00200000;
675 /* indirect (vertex base + c) load from p[] */
676 e
->inst
[0] |= 0x01800000;
677 set_addr(e
, get_address_reg(pc
, src
));
684 e
->inst
[0] |= (src
->hw
<< 9);
687 if (is_long(e
) && !is_immd(e
)) {
688 e
->inst
[1] |= 0x04000000; /* 32-bit */
689 e
->inst
[1] |= 0x0000c000; /* 32-bit c[] load / lane mask 0:1 */
690 if (!(e
->inst
[1] & 0x20000000))
691 e
->inst
[1] |= 0x00030000; /* lane mask 2:3 */
693 e
->inst
[0] |= 0x00008000;
699 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
701 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
702 emit_mov(pc
, dst
, imm
);
706 /* Assign the hw of the discarded temporary register src
707 * to the tgsi register dst and free src.
710 assimilate_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
712 assert(src
->index
== -1 && src
->hw
!= -1);
714 if (pc
->if_lvl
|| pc
->loop_lvl
||
715 (dst
->type
!= P_TEMP
) ||
716 (src
->hw
< pc
->result_nr
* 4 &&
717 pc
->p
->type
== PIPE_SHADER_FRAGMENT
) ||
718 pc
->p
->info
.opcode_count
[TGSI_OPCODE_CAL
] ||
719 pc
->p
->info
.opcode_count
[TGSI_OPCODE_BRA
]) {
721 emit_mov(pc
, dst
, src
);
727 pc
->r_temp
[dst
->hw
] = NULL
;
728 pc
->r_temp
[src
->hw
] = dst
;
735 emit_nop(struct nv50_pc
*pc
)
737 struct nv50_program_exec
*e
= exec(pc
);
739 e
->inst
[0] = 0xf0000000;
741 e
->inst
[1] = 0xe0000000;
746 check_swap_src_0_1(struct nv50_pc
*pc
,
747 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
749 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
751 if (src0
->type
== P_CONST
) {
752 if (src1
->type
!= P_CONST
) {
758 if (src1
->type
== P_ATTR
) {
759 if (src0
->type
!= P_ATTR
) {
770 set_src_0_restricted(struct nv50_pc
*pc
, struct nv50_reg
*src
,
771 struct nv50_program_exec
*e
)
773 struct nv50_reg
*temp
;
775 if (src
->type
!= P_TEMP
) {
776 temp
= temp_temp(pc
, e
);
777 emit_mov(pc
, temp
, src
);
784 e
->inst
[0] |= (src
->hw
<< 9);
788 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
790 if (src
->type
== P_ATTR
) {
792 e
->inst
[1] |= 0x00200000;
795 e
->inst
[0] |= 0x01800000; /* src from p[] */
796 set_addr(e
, get_address_reg(pc
, src
));
799 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
800 struct nv50_reg
*temp
= temp_temp(pc
, e
);
802 emit_mov(pc
, temp
, src
);
809 e
->inst
[0] |= (src
->hw
<< 9);
813 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
815 if (src
->type
== P_ATTR
) {
816 struct nv50_reg
*temp
= temp_temp(pc
, e
);
818 emit_mov(pc
, temp
, src
);
821 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
822 if (e
->inst
[0] & 0x01800000) {
823 struct nv50_reg
*temp
= temp_temp(pc
, e
);
825 emit_mov(pc
, temp
, src
);
828 assert(!(e
->inst
[0] & 0x00800000));
829 set_data(pc
, src
, 0x7f, 16, e
);
830 e
->inst
[0] |= 0x00800000;
837 e
->inst
[0] |= ((src
->hw
& 127) << 16);
841 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
845 if (src
->type
== P_ATTR
) {
846 struct nv50_reg
*temp
= temp_temp(pc
, e
);
848 emit_mov(pc
, temp
, src
);
851 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
852 if (e
->inst
[0] & 0x01800000) {
853 struct nv50_reg
*temp
= temp_temp(pc
, e
);
855 emit_mov(pc
, temp
, src
);
858 assert(!(e
->inst
[0] & 0x01000000));
859 set_data(pc
, src
, 0x7f, 32+14, e
);
860 e
->inst
[0] |= 0x01000000;
865 e
->inst
[1] |= ((src
->hw
& 127) << 14);
869 set_half_src(struct nv50_pc
*pc
, struct nv50_reg
*src
, int lh
,
870 struct nv50_program_exec
*e
, int pos
)
872 struct nv50_reg
*r
= src
;
875 if (r
->type
!= P_TEMP
) {
876 r
= temp_temp(pc
, e
);
877 emit_mov(pc
, r
, src
);
880 if (r
->hw
> (NV50_SU_MAX_TEMP
/ 2)) {
881 NOUVEAU_ERR("out of low GPRs\n");
885 e
->inst
[pos
/ 32] |= ((src
->hw
* 2) + lh
) << (pos
% 32);
889 emit_mov_from_pred(struct nv50_pc
*pc
, struct nv50_reg
*dst
, int pred
)
891 struct nv50_program_exec
*e
= exec(pc
);
893 assert(dst
->type
== P_TEMP
);
894 e
->inst
[1] = 0x20000000 | (pred
<< 12);
902 emit_mov_to_pred(struct nv50_pc
*pc
, int pred
, struct nv50_reg
*src
)
904 struct nv50_program_exec
*e
= exec(pc
);
906 e
->inst
[0] = 0x000001fc;
907 e
->inst
[1] = 0xa0000008;
909 set_pred_wr(pc
, 1, pred
, e
);
910 set_src_0_restricted(pc
, src
, e
);
916 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
917 struct nv50_reg
*src1
)
919 struct nv50_program_exec
*e
= exec(pc
);
921 e
->inst
[0] |= 0xc0000000;
926 check_swap_src_0_1(pc
, &src0
, &src1
);
928 set_src_0(pc
, src0
, e
);
929 if (src1
->type
== P_IMMD
&& !is_long(e
)) {
930 if (src0
->mod
^ src1
->mod
)
931 e
->inst
[0] |= 0x00008000;
932 set_immd(pc
, src1
, e
);
934 set_src_1(pc
, src1
, e
);
935 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
) {
937 e
->inst
[1] |= 0x08000000;
939 e
->inst
[0] |= 0x00008000;
947 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
948 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
950 struct nv50_program_exec
*e
= exec(pc
);
952 e
->inst
[0] = 0xb0000000;
955 check_swap_src_0_1(pc
, &src0
, &src1
);
957 if (!pc
->allow32
|| (src0
->mod
| src1
->mod
) || src1
->hw
> 63) {
959 e
->inst
[1] |= ((src0
->mod
& NV50_MOD_NEG
) << 26) |
960 ((src1
->mod
& NV50_MOD_NEG
) << 27);
964 set_src_0(pc
, src0
, e
);
965 if (src1
->type
== P_CONST
|| src1
->type
== P_ATTR
|| is_long(e
))
966 set_src_2(pc
, src1
, e
);
968 if (src1
->type
== P_IMMD
)
969 set_immd(pc
, src1
, e
);
971 set_src_1(pc
, src1
, e
);
977 emit_arl(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
980 struct nv50_program_exec
*e
= exec(pc
);
983 e
->inst
[1] |= 0xc0000000;
985 e
->inst
[0] |= dst
->hw
<< 2;
986 e
->inst
[0] |= s
<< 16; /* shift left */
987 set_src_0(pc
, src
, e
);
993 address_reg_suitable(struct nv50_reg
*a
, struct nv50_reg
*r
)
998 if (r
->vtx
!= a
->vtx
)
1001 return (r
->indirect
[1] == a
->indirect
[1]);
1003 if (r
->hw
< a
->rhw
|| (r
->hw
- a
->rhw
) >= 128)
1007 return (a
->index
== r
->indirect
[0]);
1008 return (a
->indirect
[0] == r
->indirect
[0]);
1012 load_vertex_base(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1013 struct nv50_reg
*a
, int shift
)
1015 struct nv50_reg mem
, *temp
;
1017 ctor_reg(&mem
, P_ATTR
, -1, dst
->vtx
);
1019 assert(dst
->type
== P_ADDR
);
1021 emit_arl(pc
, dst
, &mem
, 0);
1024 temp
= alloc_temp(pc
, NULL
);
1027 emit_mov_from_addr(pc
, temp
, a
);
1029 emit_shl_imm(pc
, temp
, temp
, shift
);
1030 emit_arl(pc
, dst
, temp
, MAX2(shift
, 0));
1032 emit_mov(pc
, temp
, &mem
);
1033 set_addr(pc
->p
->exec_tail
, dst
);
1035 emit_arl(pc
, dst
, temp
, 0);
1036 free_temp(pc
, temp
);
1039 /* case (ref == NULL): allocate address register for TGSI_FILE_ADDRESS
1040 * case (vtx >= 0, acc >= 0): load vertex base from a[vtx * 4] to $aX
1041 * case (vtx >= 0, acc < 0): load vertex base from s[$aY + vtx * 4] to $aX
1042 * case (vtx < 0, acc >= 0): memory address too high to encode
1043 * case (vtx < 0, acc < 0): get source register for TGSI_FILE_ADDRESS
1045 static struct nv50_reg
*
1046 get_address_reg(struct nv50_pc
*pc
, struct nv50_reg
*ref
)
1049 struct nv50_reg
*a_ref
, *a
= NULL
;
1051 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
) {
1052 if (pc
->r_addr
[i
].acc
== 0)
1053 a
= &pc
->r_addr
[i
]; /* an unused address reg */
1055 if (address_reg_suitable(&pc
->r_addr
[i
], ref
)) {
1056 pc
->r_addr
[i
].acc
= pc
->insn_cur
;
1057 return &pc
->r_addr
[i
];
1059 if (!a
&& pc
->r_addr
[i
].index
< 0 &&
1060 pc
->r_addr
[i
].acc
< pc
->insn_cur
)
1064 /* We'll be able to spill address regs when this
1065 * mess is replaced with a proper compiler ...
1067 NOUVEAU_ERR("out of address regs\n");
1072 /* initialize and reserve for this TGSI instruction */
1074 a
->index
= a
->indirect
[0] = a
->indirect
[1] = -1;
1075 a
->acc
= pc
->insn_cur
;
1083 /* now put in the correct value ... */
1085 if (ref
->vtx
>= 0) {
1086 a
->indirect
[1] = ref
->indirect
[1];
1088 /* For an indirect vertex index, we need to shift address right
1089 * by 2, the address register will contain vtx * 16, we need to
1090 * load from a[vtx * 4].
1092 load_vertex_base(pc
, a
, (ref
->acc
< 0) ?
1093 pc
->addr
[ref
->indirect
[1]] : NULL
, -2);
1095 assert(ref
->acc
< 0 || ref
->indirect
[0] < 0);
1097 a
->rhw
= ref
->hw
& ~0x7f;
1098 a
->indirect
[0] = ref
->indirect
[0];
1099 a_ref
= (ref
->acc
< 0) ? pc
->addr
[ref
->indirect
[0]] : NULL
;
1101 emit_add_addr_imm(pc
, a
, a_ref
, a
->rhw
* 4);
1106 #define NV50_MAX_F32 0x880
1107 #define NV50_MAX_S32 0x08c
1108 #define NV50_MAX_U32 0x084
1109 #define NV50_MIN_F32 0x8a0
1110 #define NV50_MIN_S32 0x0ac
1111 #define NV50_MIN_U32 0x0a4
1114 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
1115 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
1117 struct nv50_program_exec
*e
= exec(pc
);
1120 e
->inst
[0] |= 0x30000000 | ((sub
& 0x800) << 20);
1121 e
->inst
[1] |= (sub
<< 24);
1123 check_swap_src_0_1(pc
, &src0
, &src1
);
1124 set_dst(pc
, dst
, e
);
1125 set_src_0(pc
, src0
, e
);
1126 set_src_1(pc
, src1
, e
);
1128 if (src0
->mod
& NV50_MOD_ABS
)
1129 e
->inst
[1] |= 0x00100000;
1130 if (src1
->mod
& NV50_MOD_ABS
)
1131 e
->inst
[1] |= 0x00080000;
1137 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1138 struct nv50_reg
*src1
)
1140 src1
->mod
^= NV50_MOD_NEG
;
1141 emit_add(pc
, dst
, src0
, src1
);
1142 src1
->mod
^= NV50_MOD_NEG
;
1146 emit_bitop2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1147 struct nv50_reg
*src1
, unsigned op
)
1149 struct nv50_program_exec
*e
= exec(pc
);
1151 e
->inst
[0] = 0xd0000000;
1154 check_swap_src_0_1(pc
, &src0
, &src1
);
1155 set_dst(pc
, dst
, e
);
1156 set_src_0(pc
, src0
, e
);
1158 if (op
!= TGSI_OPCODE_AND
&& op
!= TGSI_OPCODE_OR
&&
1159 op
!= TGSI_OPCODE_XOR
)
1160 assert(!"invalid bit op");
1162 assert(!(src0
->mod
| src1
->mod
));
1164 if (src1
->type
== P_IMMD
&& src0
->type
== P_TEMP
&& pc
->allow32
) {
1165 set_immd(pc
, src1
, e
);
1166 if (op
== TGSI_OPCODE_OR
)
1167 e
->inst
[0] |= 0x0100;
1169 if (op
== TGSI_OPCODE_XOR
)
1170 e
->inst
[0] |= 0x8000;
1172 set_src_1(pc
, src1
, e
);
1173 e
->inst
[1] |= 0x04000000; /* 32 bit */
1174 if (op
== TGSI_OPCODE_OR
)
1175 e
->inst
[1] |= 0x4000;
1177 if (op
== TGSI_OPCODE_XOR
)
1178 e
->inst
[1] |= 0x8000;
1185 emit_not(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1187 struct nv50_program_exec
*e
= exec(pc
);
1189 e
->inst
[0] = 0xd0000000;
1190 e
->inst
[1] = 0x0402c000;
1192 set_dst(pc
, dst
, e
);
1193 set_src_1(pc
, src
, e
);
1199 emit_shift(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1200 struct nv50_reg
*src0
, struct nv50_reg
*src1
, unsigned dir
)
1202 struct nv50_program_exec
*e
= exec(pc
);
1204 e
->inst
[0] = 0x30000000;
1205 e
->inst
[1] = 0xc4000000;
1208 set_dst(pc
, dst
, e
);
1209 set_src_0(pc
, src0
, e
);
1211 if (src1
->type
== P_IMMD
) {
1212 e
->inst
[1] |= (1 << 20);
1213 e
->inst
[0] |= (pc
->immd_buf
[src1
->hw
] & 0x7f) << 16;
1215 set_src_1(pc
, src1
, e
);
1217 if (dir
!= TGSI_OPCODE_SHL
)
1218 e
->inst
[1] |= (1 << 29);
1220 if (dir
== TGSI_OPCODE_ISHR
)
1221 e
->inst
[1] |= (1 << 27);
1227 emit_shl_imm(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1228 struct nv50_reg
*src
, int s
)
1230 struct nv50_program_exec
*e
= exec(pc
);
1232 e
->inst
[0] = 0x30000000;
1233 e
->inst
[1] = 0xc4100000;
1235 e
->inst
[1] |= 1 << 29;
1238 e
->inst
[1] |= ((s
& 0x7f) << 16);
1241 set_dst(pc
, dst
, e
);
1242 set_src_0(pc
, src
, e
);
1248 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1249 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
1251 struct nv50_program_exec
*e
= exec(pc
);
1253 e
->inst
[0] |= 0xe0000000;
1255 check_swap_src_0_1(pc
, &src0
, &src1
);
1256 set_dst(pc
, dst
, e
);
1257 set_src_0(pc
, src0
, e
);
1258 set_src_1(pc
, src1
, e
);
1259 set_src_2(pc
, src2
, e
);
1261 if ((src0
->mod
^ src1
->mod
) & NV50_MOD_NEG
)
1262 e
->inst
[1] |= 0x04000000;
1263 if (src2
->mod
& NV50_MOD_NEG
)
1264 e
->inst
[1] |= 0x08000000;
1270 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
1271 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
1273 src2
->mod
^= NV50_MOD_NEG
;
1274 emit_mad(pc
, dst
, src0
, src1
, src2
);
1275 src2
->mod
^= NV50_MOD_NEG
;
1278 #define NV50_FLOP_RCP 0
1279 #define NV50_FLOP_RSQ 2
1280 #define NV50_FLOP_LG2 3
1281 #define NV50_FLOP_SIN 4
1282 #define NV50_FLOP_COS 5
1283 #define NV50_FLOP_EX2 6
1285 /* rcp, rsqrt, lg2 support neg and abs */
1287 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
1288 struct nv50_reg
*dst
, struct nv50_reg
*src
)
1290 struct nv50_program_exec
*e
= exec(pc
);
1292 e
->inst
[0] |= 0x90000000;
1293 if (sub
|| src
->mod
) {
1295 e
->inst
[1] |= (sub
<< 29);
1298 set_dst(pc
, dst
, e
);
1299 set_src_0_restricted(pc
, src
, e
);
1301 assert(!src
->mod
|| sub
< 4);
1303 if (src
->mod
& NV50_MOD_NEG
)
1304 e
->inst
[1] |= 0x04000000;
1305 if (src
->mod
& NV50_MOD_ABS
)
1306 e
->inst
[1] |= 0x00100000;
1312 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1314 struct nv50_program_exec
*e
= exec(pc
);
1316 e
->inst
[0] |= 0xb0000000;
1318 set_dst(pc
, dst
, e
);
1319 set_src_0(pc
, src
, e
);
1321 e
->inst
[1] |= (6 << 29) | 0x00004000;
1323 if (src
->mod
& NV50_MOD_NEG
)
1324 e
->inst
[1] |= 0x04000000;
1325 if (src
->mod
& NV50_MOD_ABS
)
1326 e
->inst
[1] |= 0x00100000;
1332 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1334 struct nv50_program_exec
*e
= exec(pc
);
1336 e
->inst
[0] |= 0xb0000000;
1338 set_dst(pc
, dst
, e
);
1339 set_src_0(pc
, src
, e
);
1341 e
->inst
[1] |= (6 << 29);
1343 if (src
->mod
& NV50_MOD_NEG
)
1344 e
->inst
[1] |= 0x04000000;
1345 if (src
->mod
& NV50_MOD_ABS
)
1346 e
->inst
[1] |= 0x00100000;
1351 #define CVT_RN (0x00 << 16)
1352 #define CVT_FLOOR (0x02 << 16)
1353 #define CVT_CEIL (0x04 << 16)
1354 #define CVT_TRUNC (0x06 << 16)
1355 #define CVT_SAT (0x08 << 16)
1356 #define CVT_ABS (0x10 << 16)
1358 #define CVT_X32_X32 0x04004000
1359 #define CVT_X32_S32 0x04014000
1360 #define CVT_F32_F32 ((0xc0 << 24) | CVT_X32_X32)
1361 #define CVT_S32_F32 ((0x88 << 24) | CVT_X32_X32)
1362 #define CVT_U32_F32 ((0x80 << 24) | CVT_X32_X32)
1363 #define CVT_F32_S32 ((0x40 << 24) | CVT_X32_S32)
1364 #define CVT_F32_U32 ((0x40 << 24) | CVT_X32_X32)
1365 #define CVT_S32_S32 ((0x08 << 24) | CVT_X32_S32)
1366 #define CVT_S32_U32 ((0x08 << 24) | CVT_X32_X32)
1367 #define CVT_U32_S32 ((0x00 << 24) | CVT_X32_S32)
1369 #define CVT_NEG 0x20000000
1370 #define CVT_RI 0x08000000
1373 emit_cvt(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
,
1374 int wp
, uint32_t cvn
)
1376 struct nv50_program_exec
*e
;
1380 if (src
->mod
& NV50_MOD_NEG
) cvn
|= CVT_NEG
;
1381 if (src
->mod
& NV50_MOD_ABS
) cvn
|= CVT_ABS
;
1383 e
->inst
[0] = 0xa0000000;
1386 set_src_0(pc
, src
, e
);
1389 set_pred_wr(pc
, 1, wp
, e
);
1392 set_dst(pc
, dst
, e
);
1394 e
->inst
[0] |= 0x000001fc;
1395 e
->inst
[1] |= 0x00000008;
1401 /* nv50 Condition codes:
1408 * 0x7 = set condition code ? (used before bra.lt/le/gt/ge)
1409 * 0x8 = unordered bit (allows NaN)
1411 * mode = 0x04 (u32), 0x0c (s32), 0x80 (f32)
1414 emit_set(struct nv50_pc
*pc
, unsigned ccode
, struct nv50_reg
*dst
, int wp
,
1415 struct nv50_reg
*src0
, struct nv50_reg
*src1
, uint8_t mode
)
1417 static const unsigned cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
1419 struct nv50_program_exec
*e
= exec(pc
);
1420 struct nv50_reg
*rdst
;
1423 if (check_swap_src_0_1(pc
, &src0
, &src1
))
1424 ccode
= cc_swapped
[ccode
& 7] | (ccode
& 8);
1427 if (dst
&& dst
->type
!= P_TEMP
)
1428 dst
= alloc_temp(pc
, NULL
);
1431 e
->inst
[0] |= 0x30000000 | (mode
<< 24);
1432 e
->inst
[1] |= 0x60000000 | (ccode
<< 14);
1435 set_pred_wr(pc
, 1, wp
, e
);
1437 set_dst(pc
, dst
, e
);
1439 e
->inst
[0] |= 0x000001fc;
1440 e
->inst
[1] |= 0x00000008;
1443 set_src_0(pc
, src0
, e
);
1444 set_src_1(pc
, src1
, e
);
1448 if (rdst
&& mode
== 0x80) /* convert to float ? */
1449 emit_cvt(pc
, rdst
, dst
, -1, CVT_ABS
| CVT_F32_S32
);
1450 if (rdst
&& rdst
!= dst
)
1455 map_tgsi_setop_hw(unsigned op
, uint8_t *cc
, uint8_t *ty
)
1458 case TGSI_OPCODE_SLT
: *cc
= 0x1; *ty
= 0x80; break;
1459 case TGSI_OPCODE_SGE
: *cc
= 0x6; *ty
= 0x80; break;
1460 case TGSI_OPCODE_SEQ
: *cc
= 0x2; *ty
= 0x80; break;
1461 case TGSI_OPCODE_SGT
: *cc
= 0x4; *ty
= 0x80; break;
1462 case TGSI_OPCODE_SLE
: *cc
= 0x3; *ty
= 0x80; break;
1463 case TGSI_OPCODE_SNE
: *cc
= 0xd; *ty
= 0x80; break;
1465 case TGSI_OPCODE_ISLT
: *cc
= 0x1; *ty
= 0x0c; break;
1466 case TGSI_OPCODE_ISGE
: *cc
= 0x6; *ty
= 0x0c; break;
1467 case TGSI_OPCODE_USEQ
: *cc
= 0x2; *ty
= 0x04; break;
1468 case TGSI_OPCODE_USGE
: *cc
= 0x6; *ty
= 0x04; break;
1469 case TGSI_OPCODE_USLT
: *cc
= 0x1; *ty
= 0x04; break;
1470 case TGSI_OPCODE_USNE
: *cc
= 0x5; *ty
= 0x04; break;
1478 emit_add_b32(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1479 struct nv50_reg
*src0
, struct nv50_reg
*rsrc1
)
1481 struct nv50_program_exec
*e
= exec(pc
);
1482 struct nv50_reg
*src1
;
1484 e
->inst
[0] = 0x20000000;
1486 alloc_reg(pc
, rsrc1
);
1487 check_swap_src_0_1(pc
, &src0
, &rsrc1
);
1490 if (src0
->mod
& rsrc1
->mod
& NV50_MOD_NEG
) {
1491 src1
= temp_temp(pc
, e
);
1492 emit_cvt(pc
, src1
, rsrc1
, -1, CVT_S32_S32
);
1495 if (!pc
->allow32
|| src1
->hw
> 63 ||
1496 (src1
->type
!= P_TEMP
&& src1
->type
!= P_IMMD
))
1499 set_dst(pc
, dst
, e
);
1500 set_src_0(pc
, src0
, e
);
1503 e
->inst
[1] |= 1 << 26;
1504 set_src_2(pc
, src1
, e
);
1506 e
->inst
[0] |= 0x8000;
1507 if (src1
->type
== P_IMMD
)
1508 set_immd(pc
, src1
, e
);
1510 set_src_1(pc
, src1
, e
);
1513 if (src0
->mod
& NV50_MOD_NEG
)
1514 e
->inst
[0] |= 1 << 28;
1516 if (src1
->mod
& NV50_MOD_NEG
)
1517 e
->inst
[0] |= 1 << 22;
1523 emit_mad_u16(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1524 struct nv50_reg
*src0
, int lh_0
, struct nv50_reg
*src1
, int lh_1
,
1525 struct nv50_reg
*src2
)
1527 struct nv50_program_exec
*e
= exec(pc
);
1529 e
->inst
[0] = 0x60000000;
1532 set_dst(pc
, dst
, e
);
1534 set_half_src(pc
, src0
, lh_0
, e
, 9);
1535 set_half_src(pc
, src1
, lh_1
, e
, 16);
1536 alloc_reg(pc
, src2
);
1537 if (is_long(e
) || (src2
->type
!= P_TEMP
) || (src2
->hw
!= dst
->hw
))
1538 set_src_2(pc
, src2
, e
);
1544 emit_mul_u16(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1545 struct nv50_reg
*src0
, int lh_0
, struct nv50_reg
*src1
, int lh_1
)
1547 struct nv50_program_exec
*e
= exec(pc
);
1549 e
->inst
[0] = 0x40000000;
1551 set_dst(pc
, dst
, e
);
1553 set_half_src(pc
, src0
, lh_0
, e
, 9);
1554 set_half_src(pc
, src1
, lh_1
, e
, 16);
1560 emit_sad(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1561 struct nv50_reg
*src0
, struct nv50_reg
*src1
, struct nv50_reg
*src2
)
1563 struct nv50_program_exec
*e
= exec(pc
);
1565 e
->inst
[0] = 0x50000000;
1568 check_swap_src_0_1(pc
, &src0
, &src1
);
1569 set_dst(pc
, dst
, e
);
1570 set_src_0(pc
, src0
, e
);
1571 set_src_1(pc
, src1
, e
);
1572 alloc_reg(pc
, src2
);
1573 if (is_long(e
) || (src2
->type
!= dst
->type
) || (src2
->hw
!= dst
->hw
))
1574 set_src_2(pc
, src2
, e
);
1577 e
->inst
[1] |= 0x0c << 24;
1579 e
->inst
[0] |= 0x81 << 8;
1585 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1587 emit_cvt(pc
, dst
, src
, -1, CVT_FLOOR
| CVT_F32_F32
| CVT_RI
);
1591 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
1592 struct nv50_reg
*v
, struct nv50_reg
*e
)
1594 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
1596 emit_flop(pc
, NV50_FLOP_LG2
, temp
, v
);
1597 emit_mul(pc
, temp
, temp
, e
);
1598 emit_preex2(pc
, temp
, temp
);
1599 emit_flop(pc
, NV50_FLOP_EX2
, dst
, temp
);
1601 free_temp(pc
, temp
);
1605 emit_sat(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
1607 emit_cvt(pc
, dst
, src
, -1, CVT_SAT
| CVT_F32_F32
);
1611 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1612 struct nv50_reg
**src
)
1614 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1615 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
1616 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
1617 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
1618 struct nv50_reg
*tmp
[4] = { 0 };
1619 boolean allow32
= pc
->allow32
;
1621 pc
->allow32
= FALSE
;
1623 if (mask
& (3 << 1)) {
1624 tmp
[0] = alloc_temp(pc
, NULL
);
1625 emit_minmax(pc
, NV50_MAX_F32
, tmp
[0], src
[0], zero
);
1628 if (mask
& (1 << 2)) {
1629 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
1631 tmp
[1] = temp_temp(pc
, NULL
);
1632 emit_minmax(pc
, NV50_MAX_F32
, tmp
[1], src
[1], zero
);
1634 tmp
[3] = temp_temp(pc
, NULL
);
1635 emit_minmax(pc
, NV50_MAX_F32
, tmp
[3], src
[3], neg128
);
1636 emit_minmax(pc
, NV50_MIN_F32
, tmp
[3], tmp
[3], pos128
);
1638 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
1639 emit_mov(pc
, dst
[2], zero
);
1640 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
1643 if (mask
& (1 << 1))
1644 assimilate_temp(pc
, dst
[1], tmp
[0]);
1646 if (mask
& (1 << 2))
1647 free_temp(pc
, tmp
[0]);
1649 pc
->allow32
= allow32
;
1651 /* do this last, in case src[i,j] == dst[0,3] */
1652 if (mask
& (1 << 0))
1653 emit_mov(pc
, dst
[0], one
);
1655 if (mask
& (1 << 3))
1656 emit_mov(pc
, dst
[3], one
);
1665 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
1667 struct nv50_program_exec
*e
;
1668 const int r_pred
= 1;
1671 e
->inst
[0] = 0x00000002; /* discard */
1672 set_long(pc
, e
); /* sets cond code to ALWAYS */
1675 set_pred(pc
, 0x1 /* cc = LT */, r_pred
, e
);
1676 /* write to predicate reg */
1677 emit_cvt(pc
, NULL
, src
, r_pred
, CVT_F32_F32
);
1683 static struct nv50_program_exec
*
1684 emit_control_flow(struct nv50_pc
*pc
, unsigned op
, int pred
, unsigned cc
)
1686 struct nv50_program_exec
*e
= exec(pc
);
1688 e
->inst
[0] = (op
<< 28) | 2;
1691 set_pred(pc
, cc
, pred
, e
);
1697 static INLINE
struct nv50_program_exec
*
1698 emit_breakaddr(struct nv50_pc
*pc
)
1700 return emit_control_flow(pc
, 0x4, -1, 0);
1704 emit_break(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1706 emit_control_flow(pc
, 0x5, pred
, cc
);
1709 static INLINE
struct nv50_program_exec
*
1710 emit_joinat(struct nv50_pc
*pc
)
1712 return emit_control_flow(pc
, 0xa, -1, 0);
1715 static INLINE
struct nv50_program_exec
*
1716 emit_branch(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1718 return emit_control_flow(pc
, 0x1, pred
, cc
);
1721 static INLINE
struct nv50_program_exec
*
1722 emit_call(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1724 return emit_control_flow(pc
, 0x2, pred
, cc
);
1728 emit_ret(struct nv50_pc
*pc
, int pred
, unsigned cc
)
1730 emit_control_flow(pc
, 0x3, pred
, cc
);
1734 emit_prim_cmd(struct nv50_pc
*pc
, unsigned cmd
)
1736 struct nv50_program_exec
*e
= exec(pc
);
1738 e
->inst
[0] = 0xf0000000 | (cmd
<< 9);
1739 e
->inst
[1] = 0xc0000000;
1748 #define QOP_MOV_SRC1 3
1750 /* For a quad of threads / top left, top right, bottom left, bottom right
1751 * pixels, do a different operation, and take src0 from a specific thread.
1754 emit_quadop(struct nv50_pc
*pc
, struct nv50_reg
*dst
, int wp
, int lane_src0
,
1755 struct nv50_reg
*src0
, struct nv50_reg
*src1
, ubyte qop
)
1757 struct nv50_program_exec
*e
= exec(pc
);
1759 e
->inst
[0] = 0xc0000000;
1760 e
->inst
[1] = 0x80000000;
1762 e
->inst
[0] |= lane_src0
<< 16;
1763 set_src_0(pc
, src0
, e
);
1764 set_src_2(pc
, src1
, e
);
1767 set_pred_wr(pc
, 1, wp
, e
);
1770 set_dst(pc
, dst
, e
);
1772 e
->inst
[0] |= 0x000001fc;
1773 e
->inst
[1] |= 0x00000008;
1776 e
->inst
[0] |= (qop
& 3) << 20;
1777 e
->inst
[1] |= (qop
>> 2) << 22;
1783 load_cube_tex_coords(struct nv50_pc
*pc
, struct nv50_reg
*t
[4],
1784 struct nv50_reg
**src
, unsigned arg
, boolean proj
)
1786 int mod
[3] = { src
[0]->mod
, src
[1]->mod
, src
[2]->mod
};
1788 src
[0]->mod
|= NV50_MOD_ABS
;
1789 src
[1]->mod
|= NV50_MOD_ABS
;
1790 src
[2]->mod
|= NV50_MOD_ABS
;
1792 emit_minmax(pc
, NV50_MAX_F32
, t
[2], src
[0], src
[1]);
1793 emit_minmax(pc
, NV50_MAX_F32
, t
[2], src
[2], t
[2]);
1795 src
[0]->mod
= mod
[0];
1796 src
[1]->mod
= mod
[1];
1797 src
[2]->mod
= mod
[2];
1799 if (proj
&& 0 /* looks more correct without this */)
1800 emit_mul(pc
, t
[2], t
[2], src
[3]);
1802 if (arg
== 4) /* there is no textureProj(samplerCubeShadow) */
1803 emit_mov(pc
, t
[3], src
[3]);
1805 emit_flop(pc
, NV50_FLOP_RCP
, t
[2], t
[2]);
1807 emit_mul(pc
, t
[0], src
[0], t
[2]);
1808 emit_mul(pc
, t
[1], src
[1], t
[2]);
1809 emit_mul(pc
, t
[2], src
[2], t
[2]);
1813 load_proj_tex_coords(struct nv50_pc
*pc
, struct nv50_reg
*t
[4],
1814 struct nv50_reg
**src
, unsigned dim
, unsigned arg
)
1818 if (src
[0]->type
== P_TEMP
&& src
[0]->rhw
!= -1) {
1819 mode
= pc
->interp_mode
[src
[0]->index
] | INTERP_PERSPECTIVE
;
1821 t
[3]->rhw
= src
[3]->rhw
;
1822 emit_interp(pc
, t
[3], NULL
, (mode
& INTERP_CENTROID
));
1823 emit_flop(pc
, NV50_FLOP_RCP
, t
[3], t
[3]);
1825 for (c
= 0; c
< dim
; ++c
) {
1826 t
[c
]->rhw
= src
[c
]->rhw
;
1827 emit_interp(pc
, t
[c
], t
[3], mode
);
1829 if (arg
!= dim
) { /* depth reference value */
1830 t
[dim
]->rhw
= src
[2]->rhw
;
1831 emit_interp(pc
, t
[dim
], t
[3], mode
);
1834 /* XXX: for some reason the blob sometimes uses MAD
1835 * (mad f32 $rX $rY $rZ neg $r63)
1837 emit_flop(pc
, NV50_FLOP_RCP
, t
[3], src
[3]);
1838 for (c
= 0; c
< dim
; ++c
)
1839 emit_mul(pc
, t
[c
], src
[c
], t
[3]);
1840 if (arg
!= dim
) /* depth reference value */
1841 emit_mul(pc
, t
[dim
], src
[2], t
[3]);
1846 get_tex_dim(unsigned type
, unsigned *dim
, unsigned *arg
)
1849 case TGSI_TEXTURE_1D
:
1852 case TGSI_TEXTURE_SHADOW1D
:
1856 case TGSI_TEXTURE_UNKNOWN
:
1857 case TGSI_TEXTURE_2D
:
1858 case TGSI_TEXTURE_RECT
:
1861 case TGSI_TEXTURE_SHADOW2D
:
1862 case TGSI_TEXTURE_SHADOWRECT
:
1866 case TGSI_TEXTURE_3D
:
1867 case TGSI_TEXTURE_CUBE
:
1876 /* We shouldn't execute TEXLOD if any of the pixels in a quad have
1877 * different LOD values, so branch off groups of equal LOD.
1880 emit_texlod_sequence(struct nv50_pc
*pc
, struct nv50_reg
*tlod
,
1881 struct nv50_reg
*src
, struct nv50_program_exec
*tex
)
1883 struct nv50_program_exec
*join_at
;
1884 unsigned i
, target
= pc
->p
->exec_size
+ 9 * 2;
1886 if (pc
->p
->type
!= PIPE_SHADER_FRAGMENT
) {
1890 pc
->allow32
= FALSE
;
1892 /* Subtract lod of each pixel from lod of top left pixel, jump
1893 * texlod insn if result is 0, then repeat for 2 other pixels.
1895 join_at
= emit_joinat(pc
);
1896 emit_quadop(pc
, NULL
, 0, 0, tlod
, tlod
, 0x55);
1897 emit_branch(pc
, 0, 2)->param
.index
= target
;
1899 for (i
= 1; i
< 4; ++i
) {
1900 emit_quadop(pc
, NULL
, 0, i
, tlod
, tlod
, 0x55);
1901 emit_branch(pc
, 0, 2)->param
.index
= target
;
1904 emit_mov(pc
, tlod
, src
); /* target */
1905 emit(pc
, tex
); /* texlod */
1907 join_at
->param
.index
= target
+ 2 * 2;
1908 JOIN_ON(emit_nop(pc
)); /* join _after_ tex */
1912 emit_texbias_sequence(struct nv50_pc
*pc
, struct nv50_reg
*t
[4], unsigned arg
,
1913 struct nv50_program_exec
*tex
)
1915 struct nv50_program_exec
*e
;
1916 struct nv50_reg imm_1248
, *t123
[4][4], *r_bits
= alloc_temp(pc
, NULL
);
1918 unsigned n
, c
, i
, cc
[4] = { 0x0a, 0x13, 0x11, 0x10 };
1920 pc
->allow32
= FALSE
;
1921 ctor_reg(&imm_1248
, P_IMMD
, -1, ctor_immd_4u32(pc
, 1, 2, 4, 8) * 4);
1923 /* Subtract bias value of thread i from bias values of each thread,
1924 * store result in r_pred, and set bit i in r_bits if result was 0.
1927 for (i
= 0; i
< 4; ++i
, ++imm_1248
.hw
) {
1928 emit_quadop(pc
, NULL
, r_pred
, i
, t
[arg
], t
[arg
], 0x55);
1929 emit_mov(pc
, r_bits
, &imm_1248
);
1930 set_pred(pc
, 2, r_pred
, pc
->p
->exec_tail
);
1932 emit_mov_to_pred(pc
, r_pred
, r_bits
);
1934 /* The lanes of a quad are now grouped by the bit in r_pred they have
1935 * set. Put the input values for TEX into a new register set for each
1936 * group and execute TEX only for a specific group.
1937 * We cannot use the same register set for each group because we need
1938 * the derivatives, which are implicitly calculated, to be correct.
1940 for (i
= 1; i
< 4; ++i
) {
1941 alloc_temp4(pc
, t123
[i
], 0);
1943 for (c
= 0; c
<= arg
; ++c
)
1944 emit_mov(pc
, t123
[i
][c
], t
[c
]);
1946 *(e
= exec(pc
)) = *(tex
);
1947 e
->inst
[0] &= ~0x01fc;
1948 set_dst(pc
, t123
[i
][0], e
);
1949 set_pred(pc
, cc
[i
], r_pred
, e
);
1952 /* finally TEX on the original regs (where we kept the input) */
1953 set_pred(pc
, cc
[0], r_pred
, tex
);
1956 /* put the 3 * n other results into regs for lane 0 */
1957 n
= popcnt4(((e
->inst
[0] >> 25) & 0x3) | ((e
->inst
[1] >> 12) & 0xc));
1958 for (i
= 1; i
< 4; ++i
) {
1959 for (c
= 0; c
< n
; ++c
) {
1960 emit_mov(pc
, t
[c
], t123
[i
][c
]);
1961 set_pred(pc
, cc
[i
], r_pred
, pc
->p
->exec_tail
);
1963 free_temp4(pc
, t123
[i
]);
1967 free_temp(pc
, r_bits
);
1971 emit_tex(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
1972 struct nv50_reg
**src
, unsigned unit
, unsigned type
,
1973 boolean proj
, int bias_lod
)
1975 struct nv50_reg
*t
[4];
1976 struct nv50_program_exec
*e
;
1977 unsigned c
, dim
, arg
;
1979 /* t[i] must be within a single 128 bit super-reg */
1980 alloc_temp4(pc
, t
, 0);
1983 e
->inst
[0] = 0xf0000000;
1985 set_dst(pc
, t
[0], e
);
1987 /* TIC and TSC binding indices (TSC is ignored as TSC_LINKED = TRUE): */
1988 e
->inst
[0] |= (unit
<< 9) /* | (unit << 17) */;
1990 /* live flag (don't set if TEX results affect input to another TEX): */
1991 /* e->inst[0] |= 0x00000004; */
1993 get_tex_dim(type
, &dim
, &arg
);
1995 if (type
== TGSI_TEXTURE_CUBE
) {
1996 e
->inst
[0] |= 0x08000000;
1997 load_cube_tex_coords(pc
, t
, src
, arg
, proj
);
2000 load_proj_tex_coords(pc
, t
, src
, dim
, arg
);
2002 for (c
= 0; c
< dim
; c
++)
2003 emit_mov(pc
, t
[c
], src
[c
]);
2004 if (arg
!= dim
) /* depth reference value (always src.z here) */
2005 emit_mov(pc
, t
[dim
], src
[2]);
2008 e
->inst
[0] |= (mask
& 0x3) << 25;
2009 e
->inst
[1] |= (mask
& 0xc) << 12;
2012 e
->inst
[0] |= (arg
- 1) << 22;
2016 assert(pc
->p
->type
== PIPE_SHADER_FRAGMENT
);
2017 e
->inst
[0] |= arg
<< 22;
2018 e
->inst
[1] |= 0x20000000; /* texbias */
2019 emit_mov(pc
, t
[arg
], src
[3]);
2020 emit_texbias_sequence(pc
, t
, arg
, e
);
2022 e
->inst
[0] |= arg
<< 22;
2023 e
->inst
[1] |= 0x40000000; /* texlod */
2024 emit_mov(pc
, t
[arg
], src
[3]);
2025 emit_texlod_sequence(pc
, t
[arg
], src
[3], e
);
2030 if (mask
& 1) emit_mov(pc
, dst
[0], t
[c
++]);
2031 if (mask
& 2) emit_mov(pc
, dst
[1], t
[c
++]);
2032 if (mask
& 4) emit_mov(pc
, dst
[2], t
[c
++]);
2033 if (mask
& 8) emit_mov(pc
, dst
[3], t
[c
]);
2037 /* XXX: if p.e. MUL is used directly after TEX, it would still use
2038 * the texture coordinates, not the fetched values: latency ? */
2040 for (c
= 0; c
< 4; c
++) {
2041 if (mask
& (1 << c
))
2042 assimilate_temp(pc
, dst
[c
], t
[c
]);
2044 free_temp(pc
, t
[c
]);
2050 emit_ddx(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
2052 struct nv50_program_exec
*e
= exec(pc
);
2054 assert(src
->type
== P_TEMP
);
2056 e
->inst
[0] = (src
->mod
& NV50_MOD_NEG
) ? 0xc0240000 : 0xc0140000;
2057 e
->inst
[1] = (src
->mod
& NV50_MOD_NEG
) ? 0x86400000 : 0x89800000;
2059 set_dst(pc
, dst
, e
);
2060 set_src_0(pc
, src
, e
);
2061 set_src_2(pc
, src
, e
);
2067 emit_ddy(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
2069 struct nv50_program_exec
*e
= exec(pc
);
2071 assert(src
->type
== P_TEMP
);
2073 e
->inst
[0] = (src
->mod
& NV50_MOD_NEG
) ? 0xc0250000 : 0xc0150000;
2074 e
->inst
[1] = (src
->mod
& NV50_MOD_NEG
) ? 0x85800000 : 0x8a400000;
2076 set_dst(pc
, dst
, e
);
2077 set_src_0(pc
, src
, e
);
2078 set_src_2(pc
, src
, e
);
2084 convert_to_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
2086 unsigned q
= 0, m
= ~0;
2088 assert(!is_long(e
));
2090 switch (e
->inst
[0] >> 28) {
2098 /* ADD, SUB, SUBR b32 */
2099 m
= ~(0x8000 | (127 << 16));
2100 q
= ((e
->inst
[0] & (~m
)) >> 2) | (1 << 26);
2105 q
= (0x0c << 24) | ((e
->inst
[0] & (0x7f << 2)) << 12);
2109 q
= (e
->inst
[0] & (0x7f << 2)) << 12;
2112 /* INTERP (move centroid, perspective and flat bits) */
2114 q
= (e
->inst
[0] & (3 << 24)) >> (24 - 16);
2115 q
|= (e
->inst
[0] & (1 << 8)) << (18 - 8);
2123 q
= ((e
->inst
[0] & (~m
)) >> 2);
2128 q
= ((e
->inst
[0] & (~m
)) << 12);
2131 /* MAD (if src2 == dst) */
2132 q
= ((e
->inst
[0] & 0x1fc) << 12);
2146 /* Some operations support an optional negation flag. */
2148 get_supported_mods(const struct tgsi_full_instruction
*insn
, int i
)
2150 switch (insn
->Instruction
.Opcode
) {
2151 case TGSI_OPCODE_ADD
:
2152 case TGSI_OPCODE_COS
:
2153 case TGSI_OPCODE_DDX
:
2154 case TGSI_OPCODE_DDY
:
2155 case TGSI_OPCODE_DP3
:
2156 case TGSI_OPCODE_DP4
:
2157 case TGSI_OPCODE_EX2
:
2158 case TGSI_OPCODE_KIL
:
2159 case TGSI_OPCODE_LG2
:
2160 case TGSI_OPCODE_MAD
:
2161 case TGSI_OPCODE_MUL
:
2162 case TGSI_OPCODE_POW
:
2163 case TGSI_OPCODE_RCP
:
2164 case TGSI_OPCODE_RSQ
: /* ignored, RSQ = rsqrt(abs(src.x)) */
2165 case TGSI_OPCODE_SCS
:
2166 case TGSI_OPCODE_SIN
:
2167 case TGSI_OPCODE_SUB
:
2168 return NV50_MOD_NEG
;
2169 case TGSI_OPCODE_MAX
:
2170 case TGSI_OPCODE_MIN
:
2171 case TGSI_OPCODE_INEG
: /* tgsi src sign toggle/set would be stupid */
2172 return NV50_MOD_ABS
;
2173 case TGSI_OPCODE_CEIL
:
2174 case TGSI_OPCODE_FLR
:
2175 case TGSI_OPCODE_TRUNC
:
2176 return NV50_MOD_NEG
| NV50_MOD_ABS
;
2177 case TGSI_OPCODE_F2I
:
2178 case TGSI_OPCODE_F2U
:
2179 case TGSI_OPCODE_I2F
:
2180 case TGSI_OPCODE_U2F
:
2181 return NV50_MOD_NEG
| NV50_MOD_ABS
| NV50_MOD_I32
;
2182 case TGSI_OPCODE_UADD
:
2183 return NV50_MOD_NEG
| NV50_MOD_I32
;
2184 case TGSI_OPCODE_SAD
:
2185 case TGSI_OPCODE_SHL
:
2186 case TGSI_OPCODE_IMAX
:
2187 case TGSI_OPCODE_IMIN
:
2188 case TGSI_OPCODE_ISHR
:
2189 case TGSI_OPCODE_NOT
:
2190 case TGSI_OPCODE_UMAD
:
2191 case TGSI_OPCODE_UMAX
:
2192 case TGSI_OPCODE_UMIN
:
2193 case TGSI_OPCODE_UMUL
:
2194 case TGSI_OPCODE_USHR
:
2195 return NV50_MOD_I32
;
2201 /* Return a read mask for source registers deduced from opcode & write mask. */
2203 nv50_tgsi_src_mask(const struct tgsi_full_instruction
*insn
, int c
)
2205 unsigned x
, mask
= insn
->Dst
[0].Register
.WriteMask
;
2207 switch (insn
->Instruction
.Opcode
) {
2208 case TGSI_OPCODE_COS
:
2209 case TGSI_OPCODE_SIN
:
2210 return (mask
& 0x8) | ((mask
& 0x7) ? 0x1 : 0x0);
2211 case TGSI_OPCODE_DP3
:
2213 case TGSI_OPCODE_DP4
:
2214 case TGSI_OPCODE_DPH
:
2215 case TGSI_OPCODE_KIL
: /* WriteMask ignored */
2217 case TGSI_OPCODE_DST
:
2218 return mask
& (c
? 0xa : 0x6);
2219 case TGSI_OPCODE_EX2
:
2220 case TGSI_OPCODE_EXP
:
2221 case TGSI_OPCODE_LG2
:
2222 case TGSI_OPCODE_LOG
:
2223 case TGSI_OPCODE_POW
:
2224 case TGSI_OPCODE_RCP
:
2225 case TGSI_OPCODE_RSQ
:
2226 case TGSI_OPCODE_SCS
:
2228 case TGSI_OPCODE_IF
:
2230 case TGSI_OPCODE_LIT
:
2232 case TGSI_OPCODE_TEX
:
2233 case TGSI_OPCODE_TXB
:
2234 case TGSI_OPCODE_TXL
:
2235 case TGSI_OPCODE_TXP
:
2237 const struct tgsi_instruction_texture
*tex
;
2239 assert(insn
->Instruction
.Texture
);
2240 tex
= &insn
->Texture
;
2243 if (insn
->Instruction
.Opcode
!= TGSI_OPCODE_TEX
&&
2244 insn
->Instruction
.Opcode
!= TGSI_OPCODE_TXD
)
2245 mask
|= 0x8; /* bias, lod or proj */
2247 switch (tex
->Texture
) {
2248 case TGSI_TEXTURE_1D
:
2251 case TGSI_TEXTURE_SHADOW1D
:
2254 case TGSI_TEXTURE_2D
:
2262 case TGSI_OPCODE_XPD
:
2264 if (mask
& 1) x
|= 0x6;
2265 if (mask
& 2) x
|= 0x5;
2266 if (mask
& 4) x
|= 0x3;
2275 static struct nv50_reg
*
2276 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
2278 switch (dst
->Register
.File
) {
2279 case TGSI_FILE_TEMPORARY
:
2280 return &pc
->temp
[dst
->Register
.Index
* 4 + c
];
2281 case TGSI_FILE_OUTPUT
:
2282 return &pc
->result
[dst
->Register
.Index
* 4 + c
];
2283 case TGSI_FILE_ADDRESS
:
2285 struct nv50_reg
*r
= pc
->addr
[dst
->Register
.Index
* 4 + c
];
2287 r
= get_address_reg(pc
, NULL
);
2288 r
->index
= dst
->Register
.Index
* 4 + c
;
2289 pc
->addr
[r
->index
] = r
;
2294 case TGSI_FILE_NULL
:
2296 case TGSI_FILE_SYSTEM_VALUE
:
2297 assert(pc
->sysval
[dst
->Register
.Index
].type
== P_RESULT
);
2299 return &pc
->sysval
[dst
->Register
.Index
];
2307 static struct nv50_reg
*
2308 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
,
2311 struct nv50_reg
*r
= NULL
;
2312 struct nv50_reg
*temp
= NULL
;
2313 unsigned sgn
, c
, swz
, cvn
;
2315 if (src
->Register
.File
!= TGSI_FILE_CONSTANT
)
2316 assert(!src
->Register
.Indirect
);
2318 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
2320 c
= tgsi_util_get_full_src_register_swizzle(src
, chan
);
2322 case TGSI_SWIZZLE_X
:
2323 case TGSI_SWIZZLE_Y
:
2324 case TGSI_SWIZZLE_Z
:
2325 case TGSI_SWIZZLE_W
:
2326 switch (src
->Register
.File
) {
2327 case TGSI_FILE_INPUT
:
2328 r
= &pc
->attr
[src
->Register
.Index
* 4 + c
];
2330 if (!src
->Dimension
.Dimension
)
2332 r
= reg_instance(pc
, r
);
2333 r
->vtx
= src
->Dimension
.Index
;
2335 if (!src
->Dimension
.Indirect
)
2337 swz
= tgsi_util_get_src_register_swizzle(
2338 &src
->DimIndirect
, 0);
2340 r
->indirect
[1] = src
->DimIndirect
.Index
* 4 + swz
;
2342 case TGSI_FILE_TEMPORARY
:
2343 r
= &pc
->temp
[src
->Register
.Index
* 4 + c
];
2345 case TGSI_FILE_CONSTANT
:
2346 if (!src
->Register
.Indirect
) {
2347 r
= &pc
->param
[src
->Register
.Index
* 4 + c
];
2350 /* Indicate indirection by setting r->acc < 0 and
2351 * use the index field to select the address reg.
2353 r
= reg_instance(pc
, NULL
);
2354 ctor_reg(r
, P_CONST
, -1, src
->Register
.Index
* 4 + c
);
2356 swz
= tgsi_util_get_src_register_swizzle(
2359 r
->indirect
[0] = src
->Indirect
.Index
* 4 + swz
;
2361 case TGSI_FILE_IMMEDIATE
:
2362 r
= &pc
->immd
[src
->Register
.Index
* 4 + c
];
2364 case TGSI_FILE_SAMPLER
:
2366 case TGSI_FILE_ADDRESS
:
2367 r
= pc
->addr
[src
->Register
.Index
* 4 + c
];
2370 case TGSI_FILE_SYSTEM_VALUE
:
2372 r
= &pc
->sysval
[src
->Register
.Index
];
2384 cvn
= (mod
& NV50_MOD_I32
) ? CVT_S32_S32
: CVT_F32_F32
;
2387 case TGSI_UTIL_SIGN_CLEAR
:
2388 r
->mod
= NV50_MOD_ABS
;
2390 case TGSI_UTIL_SIGN_SET
:
2391 r
->mod
= NV50_MOD_NEG_ABS
;
2393 case TGSI_UTIL_SIGN_TOGGLE
:
2394 r
->mod
= NV50_MOD_NEG
;
2397 assert(!r
->mod
&& sgn
== TGSI_UTIL_SIGN_KEEP
);
2401 if ((r
->mod
& mod
) != r
->mod
) {
2402 temp
= temp_temp(pc
, NULL
);
2403 emit_cvt(pc
, temp
, r
, -1, cvn
);
2407 r
->mod
|= mod
& NV50_MOD_I32
;
2410 if (r
->acc
>= 0 && r
->vtx
< 0 && r
!= temp
)
2411 return reg_instance(pc
, r
); /* will clear r->mod */
2415 /* return TRUE for ops that produce only a single result */
2417 is_scalar_op(unsigned op
)
2420 case TGSI_OPCODE_COS
:
2421 case TGSI_OPCODE_DP2
:
2422 case TGSI_OPCODE_DP3
:
2423 case TGSI_OPCODE_DP4
:
2424 case TGSI_OPCODE_DPH
:
2425 case TGSI_OPCODE_EX2
:
2426 case TGSI_OPCODE_LG2
:
2427 case TGSI_OPCODE_POW
:
2428 case TGSI_OPCODE_RCP
:
2429 case TGSI_OPCODE_RSQ
:
2430 case TGSI_OPCODE_SIN
:
2432 case TGSI_OPCODE_KIL:
2433 case TGSI_OPCODE_LIT:
2434 case TGSI_OPCODE_SCS:
2442 /* Returns a bitmask indicating which dst components depend
2443 * on source s, component c (reverse of nv50_tgsi_src_mask).
2446 nv50_tgsi_dst_revdep(unsigned op
, int s
, int c
)
2448 if (is_scalar_op(op
))
2452 case TGSI_OPCODE_DST
:
2453 return (1 << c
) & (s
? 0xa : 0x6);
2454 case TGSI_OPCODE_XPD
:
2464 case TGSI_OPCODE_EXP
:
2465 case TGSI_OPCODE_LOG
:
2466 case TGSI_OPCODE_LIT
:
2467 case TGSI_OPCODE_SCS
:
2468 case TGSI_OPCODE_TEX
:
2469 case TGSI_OPCODE_TXB
:
2470 case TGSI_OPCODE_TXL
:
2471 case TGSI_OPCODE_TXP
:
2472 /* these take care of dangerous swizzles themselves */
2474 case TGSI_OPCODE_IF
:
2475 case TGSI_OPCODE_KIL
:
2476 /* don't call this function for these ops */
2480 /* linear vector instruction */
2485 static INLINE boolean
2486 has_pred(struct nv50_program_exec
*e
, unsigned cc
)
2488 if (!is_long(e
) || is_immd(e
))
2490 return ((e
->inst
[1] & 0x780) == (cc
<< 7));
2493 /* on ENDIF see if we can do "@p0.neu single_op" instead of:
2500 nv50_kill_branch(struct nv50_pc
*pc
)
2502 int lvl
= pc
->if_lvl
;
2504 if (pc
->if_insn
[lvl
]->next
!= pc
->p
->exec_tail
)
2506 if (is_immd(pc
->p
->exec_tail
))
2509 /* if ccode == 'true', the BRA is from an ELSE and the predicate
2510 * reg may no longer be valid, since we currently always use $p0
2512 if (has_pred(pc
->if_insn
[lvl
], 0xf))
2514 assert(pc
->if_insn
[lvl
] && pc
->if_join
[lvl
]);
2516 /* We'll use the exec allocated for JOIN_AT (we can't easily
2517 * access nv50_program_exec's prev).
2519 pc
->p
->exec_size
-= 4; /* remove JOIN_AT and BRA */
2521 *pc
->if_join
[lvl
] = *pc
->p
->exec_tail
;
2523 FREE(pc
->if_insn
[lvl
]);
2524 FREE(pc
->p
->exec_tail
);
2526 pc
->p
->exec_tail
= pc
->if_join
[lvl
];
2527 pc
->p
->exec_tail
->next
= NULL
;
2528 set_pred(pc
, 0xd, 0, pc
->p
->exec_tail
);
2534 nv50_fp_move_results(struct nv50_pc
*pc
)
2536 struct nv50_reg reg
;
2539 ctor_reg(®
, P_TEMP
, -1, -1);
2541 for (i
= 0; i
< pc
->result_nr
* 4; ++i
) {
2542 if (pc
->result
[i
].rhw
< 0 || pc
->result
[i
].hw
< 0)
2544 if (pc
->result
[i
].rhw
!= pc
->result
[i
].hw
) {
2545 reg
.hw
= pc
->result
[i
].rhw
;
2546 emit_mov(pc
, ®
, &pc
->result
[i
]);
2552 nv50_program_tx_insn(struct nv50_pc
*pc
,
2553 const struct tgsi_full_instruction
*inst
)
2555 struct nv50_reg
*rdst
[4], *dst
[4], *brdc
, *src
[3][4], *temp
;
2556 unsigned mask
, sat
, unit
= 0;
2559 mask
= inst
->Dst
[0].Register
.WriteMask
;
2560 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
2562 memset(src
, 0, sizeof(src
));
2564 for (c
= 0; c
< 4; c
++) {
2565 if ((mask
& (1 << c
)) && !pc
->r_dst
[c
])
2566 dst
[c
] = tgsi_dst(pc
, c
, &inst
->Dst
[0]);
2568 dst
[c
] = pc
->r_dst
[c
];
2572 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
2573 const struct tgsi_full_src_register
*fs
= &inst
->Src
[i
];
2577 src_mask
= nv50_tgsi_src_mask(inst
, i
);
2578 mod_supp
= get_supported_mods(inst
, i
);
2580 if (fs
->Register
.File
== TGSI_FILE_SAMPLER
)
2581 unit
= fs
->Register
.Index
;
2583 for (c
= 0; c
< 4; c
++)
2584 if (src_mask
& (1 << c
))
2585 src
[i
][c
] = tgsi_src(pc
, c
, fs
, mod_supp
);
2588 brdc
= temp
= pc
->r_brdc
;
2589 if (brdc
&& brdc
->type
!= P_TEMP
) {
2590 temp
= temp_temp(pc
, NULL
);
2595 for (c
= 0; c
< 4; c
++) {
2596 if (!(mask
& (1 << c
)) || dst
[c
]->type
== P_TEMP
)
2598 /* rdst[c] = dst[c]; */ /* done above */
2599 dst
[c
] = temp_temp(pc
, NULL
);
2603 assert(brdc
|| !is_scalar_op(inst
->Instruction
.Opcode
));
2605 switch (inst
->Instruction
.Opcode
) {
2606 case TGSI_OPCODE_ABS
:
2607 for (c
= 0; c
< 4; c
++) {
2608 if (!(mask
& (1 << c
)))
2610 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2611 CVT_ABS
| CVT_F32_F32
);
2614 case TGSI_OPCODE_ADD
:
2615 for (c
= 0; c
< 4; c
++) {
2616 if (!(mask
& (1 << c
)))
2618 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2621 case TGSI_OPCODE_AND
:
2622 case TGSI_OPCODE_XOR
:
2623 case TGSI_OPCODE_OR
:
2624 for (c
= 0; c
< 4; c
++) {
2625 if (!(mask
& (1 << c
)))
2627 emit_bitop2(pc
, dst
[c
], src
[0][c
], src
[1][c
],
2628 inst
->Instruction
.Opcode
);
2631 case TGSI_OPCODE_ARL
:
2632 temp
= temp_temp(pc
, NULL
);
2633 for (c
= 0; c
< 4; c
++) {
2634 if (!(mask
& (1 << c
)))
2636 emit_cvt(pc
, temp
, src
[0][c
], -1,
2637 CVT_FLOOR
| CVT_S32_F32
);
2638 emit_arl(pc
, dst
[c
], temp
, 4);
2641 case TGSI_OPCODE_BGNLOOP
:
2642 pc
->loop_brka
[pc
->loop_lvl
] = emit_breakaddr(pc
);
2643 pc
->loop_pos
[pc
->loop_lvl
++] = pc
->p
->exec_size
;
2646 case TGSI_OPCODE_BGNSUB
:
2647 assert(!pc
->in_subroutine
);
2648 pc
->in_subroutine
= TRUE
;
2649 /* probably not necessary, but align to 8 byte boundary */
2650 if (!is_long(pc
->p
->exec_tail
))
2651 convert_to_long(pc
, pc
->p
->exec_tail
);
2653 case TGSI_OPCODE_BRK
:
2654 assert(pc
->loop_lvl
> 0);
2655 emit_break(pc
, -1, 0);
2657 case TGSI_OPCODE_CAL
:
2658 assert(inst
->Label
.Label
< pc
->insn_nr
);
2659 emit_call(pc
, -1, 0)->param
.index
= inst
->Label
.Label
;
2660 /* replaced by actual offset in nv50_program_fixup_insns */
2662 case TGSI_OPCODE_CEIL
:
2663 for (c
= 0; c
< 4; c
++) {
2664 if (!(mask
& (1 << c
)))
2666 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2667 CVT_CEIL
| CVT_F32_F32
| CVT_RI
);
2670 case TGSI_OPCODE_CMP
:
2671 pc
->allow32
= FALSE
;
2672 for (c
= 0; c
< 4; c
++) {
2673 if (!(mask
& (1 << c
)))
2675 emit_cvt(pc
, NULL
, src
[0][c
], 1, CVT_F32_F32
);
2676 emit_mov(pc
, dst
[c
], src
[1][c
]);
2677 set_pred(pc
, 0x1, 1, pc
->p
->exec_tail
); /* @SF */
2678 emit_mov(pc
, dst
[c
], src
[2][c
]);
2679 set_pred(pc
, 0x6, 1, pc
->p
->exec_tail
); /* @NSF */
2682 case TGSI_OPCODE_CONT
:
2683 assert(pc
->loop_lvl
> 0);
2684 emit_branch(pc
, -1, 0)->param
.index
=
2685 pc
->loop_pos
[pc
->loop_lvl
- 1];
2687 case TGSI_OPCODE_COS
:
2689 emit_precossin(pc
, temp
, src
[0][3]);
2690 emit_flop(pc
, NV50_FLOP_COS
, dst
[3], temp
);
2694 temp
= brdc
= temp_temp(pc
, NULL
);
2696 emit_precossin(pc
, temp
, src
[0][0]);
2697 emit_flop(pc
, NV50_FLOP_COS
, brdc
, temp
);
2699 case TGSI_OPCODE_DDX
:
2700 for (c
= 0; c
< 4; c
++) {
2701 if (!(mask
& (1 << c
)))
2703 emit_ddx(pc
, dst
[c
], src
[0][c
]);
2706 case TGSI_OPCODE_DDY
:
2707 for (c
= 0; c
< 4; c
++) {
2708 if (!(mask
& (1 << c
)))
2710 emit_ddy(pc
, dst
[c
], src
[0][c
]);
2713 case TGSI_OPCODE_DP3
:
2714 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2715 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2716 emit_mad(pc
, brdc
, src
[0][2], src
[1][2], temp
);
2718 case TGSI_OPCODE_DP4
:
2719 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2720 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2721 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2722 emit_mad(pc
, brdc
, src
[0][3], src
[1][3], temp
);
2724 case TGSI_OPCODE_DPH
:
2725 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
2726 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
2727 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
2728 emit_add(pc
, brdc
, src
[1][3], temp
);
2730 case TGSI_OPCODE_DST
:
2731 if (mask
& (1 << 1))
2732 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
2733 if (mask
& (1 << 2))
2734 emit_mov(pc
, dst
[2], src
[0][2]);
2735 if (mask
& (1 << 3))
2736 emit_mov(pc
, dst
[3], src
[1][3]);
2737 if (mask
& (1 << 0))
2738 emit_mov_immdval(pc
, dst
[0], 1.0f
);
2740 case TGSI_OPCODE_ELSE
:
2741 emit_branch(pc
, -1, 0);
2742 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2743 pc
->if_insn
[pc
->if_lvl
++] = pc
->p
->exec_tail
;
2746 case TGSI_OPCODE_EMIT
:
2747 emit_prim_cmd(pc
, 1);
2749 case TGSI_OPCODE_ENDIF
:
2750 pc
->if_insn
[--pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2752 /* try to replace branch over 1 insn with a predicated insn */
2753 if (nv50_kill_branch(pc
) == TRUE
)
2756 if (pc
->if_join
[pc
->if_lvl
]) {
2757 pc
->if_join
[pc
->if_lvl
]->param
.index
= pc
->p
->exec_size
;
2758 pc
->if_join
[pc
->if_lvl
] = NULL
;
2761 /* emit a NOP as join point, we could set it on the next
2762 * one, but would have to make sure it is long and !immd
2764 JOIN_ON(emit_nop(pc
));
2766 case TGSI_OPCODE_ENDLOOP
:
2767 emit_branch(pc
, -1, 0)->param
.index
=
2768 pc
->loop_pos
[--pc
->loop_lvl
];
2769 pc
->loop_brka
[pc
->loop_lvl
]->param
.index
= pc
->p
->exec_size
;
2772 case TGSI_OPCODE_ENDPRIM
:
2773 emit_prim_cmd(pc
, 2);
2775 case TGSI_OPCODE_ENDSUB
:
2776 assert(pc
->in_subroutine
);
2778 pc
->in_subroutine
= FALSE
;
2780 case TGSI_OPCODE_EX2
:
2781 emit_preex2(pc
, temp
, src
[0][0]);
2782 emit_flop(pc
, NV50_FLOP_EX2
, brdc
, temp
);
2784 case TGSI_OPCODE_EXP
:
2786 struct nv50_reg
*t
[2];
2789 t
[0] = temp_temp(pc
, NULL
);
2790 t
[1] = temp_temp(pc
, NULL
);
2793 emit_mov(pc
, t
[0], src
[0][0]);
2795 emit_flr(pc
, t
[1], src
[0][0]);
2797 if (mask
& (1 << 1))
2798 emit_sub(pc
, dst
[1], t
[0], t
[1]);
2799 if (mask
& (1 << 0)) {
2800 emit_preex2(pc
, t
[1], t
[1]);
2801 emit_flop(pc
, NV50_FLOP_EX2
, dst
[0], t
[1]);
2803 if (mask
& (1 << 2)) {
2804 emit_preex2(pc
, t
[0], t
[0]);
2805 emit_flop(pc
, NV50_FLOP_EX2
, dst
[2], t
[0]);
2807 if (mask
& (1 << 3))
2808 emit_mov_immdval(pc
, dst
[3], 1.0f
);
2811 case TGSI_OPCODE_F2I
:
2812 for (c
= 0; c
< 4; c
++) {
2813 if (!(mask
& (1 << c
)))
2815 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2816 CVT_TRUNC
| CVT_S32_F32
);
2819 case TGSI_OPCODE_F2U
:
2820 for (c
= 0; c
< 4; c
++) {
2821 if (!(mask
& (1 << c
)))
2823 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2824 CVT_TRUNC
| CVT_U32_F32
);
2827 case TGSI_OPCODE_FLR
:
2828 for (c
= 0; c
< 4; c
++) {
2829 if (!(mask
& (1 << c
)))
2831 emit_flr(pc
, dst
[c
], src
[0][c
]);
2834 case TGSI_OPCODE_FRC
:
2835 temp
= temp_temp(pc
, NULL
);
2836 for (c
= 0; c
< 4; c
++) {
2837 if (!(mask
& (1 << c
)))
2839 emit_flr(pc
, temp
, src
[0][c
]);
2840 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
2843 case TGSI_OPCODE_I2F
:
2844 for (c
= 0; c
< 4; c
++) {
2845 if (!(mask
& (1 << c
)))
2847 emit_cvt(pc
, dst
[c
], src
[0][c
], -1, CVT_F32_S32
);
2850 case TGSI_OPCODE_IF
:
2851 assert(pc
->if_lvl
< NV50_MAX_COND_NESTING
);
2852 emit_cvt(pc
, NULL
, src
[0][0], 0, CVT_ABS
| CVT_F32_F32
);
2853 pc
->if_join
[pc
->if_lvl
] = emit_joinat(pc
);
2854 pc
->if_insn
[pc
->if_lvl
++] = emit_branch(pc
, 0, 2);;
2857 case TGSI_OPCODE_IMAX
:
2858 for (c
= 0; c
< 4; c
++) {
2859 if (!(mask
& (1 << c
)))
2861 emit_minmax(pc
, 0x08c, dst
[c
], src
[0][c
], src
[1][c
]);
2864 case TGSI_OPCODE_IMIN
:
2865 for (c
= 0; c
< 4; c
++) {
2866 if (!(mask
& (1 << c
)))
2868 emit_minmax(pc
, 0x0ac, dst
[c
], src
[0][c
], src
[1][c
]);
2871 case TGSI_OPCODE_INEG
:
2872 for (c
= 0; c
< 4; c
++) {
2873 if (!(mask
& (1 << c
)))
2875 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
2876 CVT_S32_S32
| CVT_NEG
);
2879 case TGSI_OPCODE_KIL
:
2880 assert(src
[0][0] && src
[0][1] && src
[0][2] && src
[0][3]);
2881 emit_kil(pc
, src
[0][0]);
2882 emit_kil(pc
, src
[0][1]);
2883 emit_kil(pc
, src
[0][2]);
2884 emit_kil(pc
, src
[0][3]);
2886 case TGSI_OPCODE_KILP
:
2889 case TGSI_OPCODE_LIT
:
2890 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
2892 case TGSI_OPCODE_LG2
:
2893 emit_flop(pc
, NV50_FLOP_LG2
, brdc
, src
[0][0]);
2895 case TGSI_OPCODE_LOG
:
2897 struct nv50_reg
*t
[2];
2899 t
[0] = temp_temp(pc
, NULL
);
2900 if (mask
& (1 << 1))
2901 t
[1] = temp_temp(pc
, NULL
);
2905 emit_cvt(pc
, t
[0], src
[0][0], -1, CVT_ABS
| CVT_F32_F32
);
2906 emit_flop(pc
, NV50_FLOP_LG2
, t
[1], t
[0]);
2907 if (mask
& (1 << 2))
2908 emit_mov(pc
, dst
[2], t
[1]);
2909 emit_flr(pc
, t
[1], t
[1]);
2910 if (mask
& (1 << 0))
2911 emit_mov(pc
, dst
[0], t
[1]);
2912 if (mask
& (1 << 1)) {
2913 t
[1]->mod
= NV50_MOD_NEG
;
2914 emit_preex2(pc
, t
[1], t
[1]);
2916 emit_flop(pc
, NV50_FLOP_EX2
, t
[1], t
[1]);
2917 emit_mul(pc
, dst
[1], t
[0], t
[1]);
2919 if (mask
& (1 << 3))
2920 emit_mov_immdval(pc
, dst
[3], 1.0f
);
2923 case TGSI_OPCODE_LRP
:
2924 temp
= temp_temp(pc
, NULL
);
2925 for (c
= 0; c
< 4; c
++) {
2926 if (!(mask
& (1 << c
)))
2928 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
2929 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
2932 case TGSI_OPCODE_MAD
:
2933 for (c
= 0; c
< 4; c
++) {
2934 if (!(mask
& (1 << c
)))
2936 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
2939 case TGSI_OPCODE_MAX
:
2940 for (c
= 0; c
< 4; c
++) {
2941 if (!(mask
& (1 << c
)))
2943 emit_minmax(pc
, 0x880, dst
[c
], src
[0][c
], src
[1][c
]);
2946 case TGSI_OPCODE_MIN
:
2947 for (c
= 0; c
< 4; c
++) {
2948 if (!(mask
& (1 << c
)))
2950 emit_minmax(pc
, 0x8a0, dst
[c
], src
[0][c
], src
[1][c
]);
2953 case TGSI_OPCODE_MOV
:
2954 for (c
= 0; c
< 4; c
++) {
2955 if (!(mask
& (1 << c
)))
2957 emit_mov(pc
, dst
[c
], src
[0][c
]);
2960 case TGSI_OPCODE_MUL
:
2961 for (c
= 0; c
< 4; c
++) {
2962 if (!(mask
& (1 << c
)))
2964 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
2967 case TGSI_OPCODE_NOT
:
2968 for (c
= 0; c
< 4; c
++) {
2969 if (!(mask
& (1 << c
)))
2971 emit_not(pc
, dst
[c
], src
[0][c
]);
2974 case TGSI_OPCODE_POW
:
2975 emit_pow(pc
, brdc
, src
[0][0], src
[1][0]);
2977 case TGSI_OPCODE_RCP
:
2978 if (!sat
&& popcnt4(mask
) == 1)
2979 brdc
= dst
[ffs(mask
) - 1];
2980 emit_flop(pc
, NV50_FLOP_RCP
, brdc
, src
[0][0]);
2982 case TGSI_OPCODE_RET
:
2983 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
&& !pc
->in_subroutine
)
2984 nv50_fp_move_results(pc
);
2985 emit_ret(pc
, -1, 0);
2987 case TGSI_OPCODE_RSQ
:
2988 if (!sat
&& popcnt4(mask
) == 1)
2989 brdc
= dst
[ffs(mask
) - 1];
2990 src
[0][0]->mod
|= NV50_MOD_ABS
;
2991 emit_flop(pc
, NV50_FLOP_RSQ
, brdc
, src
[0][0]);
2993 case TGSI_OPCODE_SAD
:
2994 for (c
= 0; c
< 4; c
++) {
2995 if (!(mask
& (1 << c
)))
2997 emit_sad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
3000 case TGSI_OPCODE_SCS
:
3001 temp
= temp_temp(pc
, NULL
);
3003 emit_precossin(pc
, temp
, src
[0][0]);
3004 if (mask
& (1 << 0))
3005 emit_flop(pc
, NV50_FLOP_COS
, dst
[0], temp
);
3006 if (mask
& (1 << 1))
3007 emit_flop(pc
, NV50_FLOP_SIN
, dst
[1], temp
);
3008 if (mask
& (1 << 2))
3009 emit_mov_immdval(pc
, dst
[2], 0.0);
3010 if (mask
& (1 << 3))
3011 emit_mov_immdval(pc
, dst
[3], 1.0);
3013 case TGSI_OPCODE_SHL
:
3014 case TGSI_OPCODE_ISHR
:
3015 case TGSI_OPCODE_USHR
:
3016 for (c
= 0; c
< 4; c
++) {
3017 if (!(mask
& (1 << c
)))
3019 emit_shift(pc
, dst
[c
], src
[0][c
], src
[1][c
],
3020 inst
->Instruction
.Opcode
);
3023 case TGSI_OPCODE_SIN
:
3025 emit_precossin(pc
, temp
, src
[0][3]);
3026 emit_flop(pc
, NV50_FLOP_SIN
, dst
[3], temp
);
3030 temp
= brdc
= temp_temp(pc
, NULL
);
3032 emit_precossin(pc
, temp
, src
[0][0]);
3033 emit_flop(pc
, NV50_FLOP_SIN
, brdc
, temp
);
3035 case TGSI_OPCODE_SLT
:
3036 case TGSI_OPCODE_SGE
:
3037 case TGSI_OPCODE_SEQ
:
3038 case TGSI_OPCODE_SGT
:
3039 case TGSI_OPCODE_SLE
:
3040 case TGSI_OPCODE_SNE
:
3041 case TGSI_OPCODE_ISLT
:
3042 case TGSI_OPCODE_ISGE
:
3043 case TGSI_OPCODE_USEQ
:
3044 case TGSI_OPCODE_USGE
:
3045 case TGSI_OPCODE_USLT
:
3046 case TGSI_OPCODE_USNE
:
3050 map_tgsi_setop_hw(inst
->Instruction
.Opcode
, &cc
, &ty
);
3052 for (c
= 0; c
< 4; c
++) {
3053 if (!(mask
& (1 << c
)))
3055 emit_set(pc
, cc
, dst
[c
], -1, src
[0][c
], src
[1][c
], ty
);
3059 case TGSI_OPCODE_SUB
:
3060 for (c
= 0; c
< 4; c
++) {
3061 if (!(mask
& (1 << c
)))
3063 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
3066 case TGSI_OPCODE_TEX
:
3067 emit_tex(pc
, dst
, mask
, src
[0], unit
,
3068 inst
->Texture
.Texture
, FALSE
, 0);
3070 case TGSI_OPCODE_TXB
:
3071 emit_tex(pc
, dst
, mask
, src
[0], unit
,
3072 inst
->Texture
.Texture
, FALSE
, -1);
3074 case TGSI_OPCODE_TXL
:
3075 emit_tex(pc
, dst
, mask
, src
[0], unit
,
3076 inst
->Texture
.Texture
, FALSE
, 1);
3078 case TGSI_OPCODE_TXP
:
3079 emit_tex(pc
, dst
, mask
, src
[0], unit
,
3080 inst
->Texture
.Texture
, TRUE
, 0);
3082 case TGSI_OPCODE_TRUNC
:
3083 for (c
= 0; c
< 4; c
++) {
3084 if (!(mask
& (1 << c
)))
3086 emit_cvt(pc
, dst
[c
], src
[0][c
], -1,
3087 CVT_TRUNC
| CVT_F32_F32
| CVT_RI
);
3090 case TGSI_OPCODE_U2F
:
3091 for (c
= 0; c
< 4; c
++) {
3092 if (!(mask
& (1 << c
)))
3094 emit_cvt(pc
, dst
[c
], src
[0][c
], -1, CVT_F32_U32
);
3097 case TGSI_OPCODE_UADD
:
3098 for (c
= 0; c
< 4; c
++) {
3099 if (!(mask
& (1 << c
)))
3101 emit_add_b32(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
3104 case TGSI_OPCODE_UMAX
:
3105 for (c
= 0; c
< 4; c
++) {
3106 if (!(mask
& (1 << c
)))
3108 emit_minmax(pc
, 0x084, dst
[c
], src
[0][c
], src
[1][c
]);
3111 case TGSI_OPCODE_UMIN
:
3112 for (c
= 0; c
< 4; c
++) {
3113 if (!(mask
& (1 << c
)))
3115 emit_minmax(pc
, 0x0a4, dst
[c
], src
[0][c
], src
[1][c
]);
3118 case TGSI_OPCODE_UMAD
:
3121 temp
= temp_temp(pc
, NULL
);
3122 for (c
= 0; c
< 4; c
++) {
3123 if (!(mask
& (1 << c
)))
3125 emit_mul_u16(pc
, temp
, src
[0][c
], 0, src
[1][c
], 1);
3126 emit_mad_u16(pc
, temp
, src
[0][c
], 1, src
[1][c
], 0,
3128 emit_shl_imm(pc
, temp
, temp
, 16);
3129 emit_mad_u16(pc
, temp
, src
[0][c
], 0, src
[1][c
], 0,
3131 emit_add_b32(pc
, dst
[c
], temp
, src
[2][c
]);
3135 case TGSI_OPCODE_UMUL
:
3138 temp
= temp_temp(pc
, NULL
);
3139 for (c
= 0; c
< 4; c
++) {
3140 if (!(mask
& (1 << c
)))
3142 emit_mul_u16(pc
, temp
, src
[0][c
], 0, src
[1][c
], 1);
3143 emit_mad_u16(pc
, temp
, src
[0][c
], 1, src
[1][c
], 0,
3145 emit_shl_imm(pc
, temp
, temp
, 16);
3146 emit_mad_u16(pc
, dst
[c
], src
[0][c
], 0, src
[1][c
], 0,
3151 case TGSI_OPCODE_XPD
:
3152 temp
= temp_temp(pc
, NULL
);
3153 if (mask
& (1 << 0)) {
3154 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
3155 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
3157 if (mask
& (1 << 1)) {
3158 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
3159 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
3161 if (mask
& (1 << 2)) {
3162 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
3163 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
3165 if (mask
& (1 << 3))
3166 emit_mov_immdval(pc
, dst
[3], 1.0);
3168 case TGSI_OPCODE_END
:
3169 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
)
3170 nv50_fp_move_results(pc
);
3172 if (!pc
->p
->exec_tail
||
3173 is_immd(pc
->p
->exec_tail
) ||
3174 is_join(pc
->p
->exec_tail
) ||
3175 is_control_flow(pc
->p
->exec_tail
))
3178 /* last insn must be long so it can have the exit bit set */
3179 if (!is_long(pc
->p
->exec_tail
))
3180 convert_to_long(pc
, pc
->p
->exec_tail
);
3182 pc
->p
->exec_tail
->inst
[1] |= 1; /* set exit bit */
3187 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
3193 emit_sat(pc
, brdc
, brdc
);
3194 for (c
= 0; c
< 4; c
++)
3195 if ((mask
& (1 << c
)) && dst
[c
] != brdc
)
3196 emit_mov(pc
, dst
[c
], brdc
);
3199 for (c
= 0; c
< 4; c
++) {
3200 if (!(mask
& (1 << c
)))
3202 /* In this case we saturate later, and dst[c] won't
3203 * be another temp_temp (and thus lost), since rdst
3204 * already is TEMP (see above). */
3205 if (rdst
[c
]->type
== P_TEMP
&& rdst
[c
]->index
< 0)
3207 emit_sat(pc
, rdst
[c
], dst
[c
]);
3211 kill_temp_temp(pc
, NULL
);
3212 pc
->reg_instance_nr
= 0;
3218 prep_inspect_insn(struct nv50_pc
*pc
, const struct tgsi_full_instruction
*insn
)
3220 struct nv50_reg
*r
, *reg
= NULL
;
3221 const struct tgsi_full_src_register
*src
;
3222 const struct tgsi_dst_register
*dst
;
3223 unsigned i
, c
, k
, mask
;
3225 dst
= &insn
->Dst
[0].Register
;
3226 mask
= dst
->WriteMask
;
3228 if (dst
->File
== TGSI_FILE_TEMPORARY
)
3231 if (dst
->File
== TGSI_FILE_OUTPUT
) {
3234 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_MOV
&&
3235 dst
->Index
== pc
->edgeflag_out
&&
3236 insn
->Src
[0].Register
.File
== TGSI_FILE_INPUT
)
3237 pc
->p
->cfg
.edgeflag_in
= insn
->Src
[0].Register
.Index
;
3241 for (c
= 0; c
< 4; c
++) {
3242 if (!(mask
& (1 << c
)))
3244 reg
[dst
->Index
* 4 + c
].acc
= pc
->insn_nr
;
3248 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
3249 src
= &insn
->Src
[i
];
3251 if (src
->Register
.File
== TGSI_FILE_TEMPORARY
)
3254 if (src
->Register
.File
== TGSI_FILE_INPUT
)
3259 mask
= nv50_tgsi_src_mask(insn
, i
);
3261 for (c
= 0; c
< 4; c
++) {
3262 if (!(mask
& (1 << c
)))
3264 k
= tgsi_util_get_full_src_register_swizzle(src
, c
);
3266 r
= ®
[src
->Register
.Index
* 4 + k
];
3268 /* If used before written, pre-allocate the reg,
3269 * lest we overwrite results from a subroutine.
3271 if (!r
->acc
&& r
->type
== P_TEMP
)
3274 r
->acc
= pc
->insn_nr
;
3279 /* Returns a bitmask indicating which dst components need to be
3280 * written to temporaries first to avoid 'corrupting' sources.
3282 * m[i] (out) indicate component to write in the i-th position
3283 * rdep[c] (in) bitmasks of dst[i] that require dst[c] as source
3286 nv50_revdep_reorder(unsigned m
[4], unsigned rdep
[4])
3288 unsigned i
, c
, x
, unsafe
= 0;
3290 for (c
= 0; c
< 4; c
++)
3293 /* Swap as long as a dst component written earlier is depended on
3294 * by one written later, but the next one isn't depended on by it.
3296 for (c
= 0; c
< 3; c
++) {
3297 if (rdep
[m
[c
+ 1]] & (1 << m
[c
]))
3298 continue; /* if next one is depended on by us */
3299 for (i
= c
+ 1; i
< 4; i
++)
3300 /* if we are depended on by a later one */
3301 if (rdep
[m
[c
]] & (1 << m
[i
]))
3314 /* mark dependencies that could not be resolved by reordering */
3315 for (i
= 0; i
< 3; ++i
)
3316 for (c
= i
+ 1; c
< 4; ++c
)
3317 if (rdep
[m
[i
]] & (1 << m
[c
]))
3320 /* NOTE: $unsafe is with respect to order, not component */
3324 /* Select a suitable dst register for broadcasting scalar results,
3325 * or return NULL if we have to allocate an extra TEMP.
3327 * If e.g. only 1 component is written, we may also emit the final
3328 * result to a write-only register.
3330 static struct nv50_reg
*
3331 tgsi_broadcast_dst(struct nv50_pc
*pc
,
3332 const struct tgsi_full_dst_register
*fd
, unsigned mask
)
3334 if (fd
->Register
.File
== TGSI_FILE_TEMPORARY
) {
3335 int c
= ffs(~mask
& fd
->Register
.WriteMask
);
3337 return tgsi_dst(pc
, c
- 1, fd
);
3339 int c
= ffs(fd
->Register
.WriteMask
) - 1;
3340 if ((1 << c
) == fd
->Register
.WriteMask
)
3341 return tgsi_dst(pc
, c
, fd
);
3347 /* Scan source swizzles and return a bitmask indicating dst regs that
3348 * also occur among the src regs, and fill rdep for nv50_revdep_reoder.
3351 nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction
*insn
,
3354 const struct tgsi_full_dst_register
*fd
= &insn
->Dst
[0];
3355 const struct tgsi_full_src_register
*fs
;
3356 unsigned i
, deqs
= 0;
3358 for (i
= 0; i
< 4; ++i
)
3361 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
3362 unsigned chn
, mask
= nv50_tgsi_src_mask(insn
, i
);
3363 int ms
= get_supported_mods(insn
, i
);
3366 if (fs
->Register
.File
!= fd
->Register
.File
||
3367 fs
->Register
.Index
!= fd
->Register
.Index
)
3370 for (chn
= 0; chn
< 4; ++chn
) {
3373 if (!(mask
& (1 << chn
))) /* src is not read */
3375 c
= tgsi_util_get_full_src_register_swizzle(fs
, chn
);
3376 s
= tgsi_util_get_full_src_register_sign_mode(fs
, chn
);
3378 if (!(fd
->Register
.WriteMask
& (1 << c
)))
3381 if (s
== TGSI_UTIL_SIGN_TOGGLE
&& !(ms
& NV50_MOD_NEG
))
3383 if (s
== TGSI_UTIL_SIGN_CLEAR
&& !(ms
& NV50_MOD_ABS
))
3385 if ((s
== TGSI_UTIL_SIGN_SET
) && ((ms
& 3) != 3))
3388 rdep
[c
] |= nv50_tgsi_dst_revdep(
3389 insn
->Instruction
.Opcode
, i
, chn
);
3398 nv50_tgsi_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
3400 struct tgsi_full_instruction insn
= tok
->FullInstruction
;
3401 const struct tgsi_full_dst_register
*fd
;
3402 unsigned i
, deqs
, rdep
[4], m
[4];
3404 fd
= &tok
->FullInstruction
.Dst
[0];
3405 deqs
= nv50_tgsi_scan_swizzle(&insn
, rdep
);
3407 if (is_scalar_op(insn
.Instruction
.Opcode
)) {
3408 pc
->r_brdc
= tgsi_broadcast_dst(pc
, fd
, deqs
);
3410 pc
->r_brdc
= temp_temp(pc
, NULL
);
3411 return nv50_program_tx_insn(pc
, &insn
);
3415 if (!deqs
|| (!rdep
[0] && !rdep
[1] && !rdep
[2] && !rdep
[3]))
3416 return nv50_program_tx_insn(pc
, &insn
);
3418 deqs
= nv50_revdep_reorder(m
, rdep
);
3420 for (i
= 0; i
< 4; ++i
) {
3421 assert(pc
->r_dst
[m
[i
]] == NULL
);
3423 insn
.Dst
[0].Register
.WriteMask
=
3424 fd
->Register
.WriteMask
& (1 << m
[i
]);
3426 if (!insn
.Dst
[0].Register
.WriteMask
)
3429 if (deqs
& (1 << i
))
3430 pc
->r_dst
[m
[i
]] = alloc_temp(pc
, NULL
);
3432 if (!nv50_program_tx_insn(pc
, &insn
))
3436 for (i
= 0; i
< 4; i
++) {
3437 struct nv50_reg
*reg
= pc
->r_dst
[i
];
3440 pc
->r_dst
[i
] = NULL
;
3442 if (insn
.Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
3443 emit_sat(pc
, tgsi_dst(pc
, i
, fd
), reg
);
3445 emit_mov(pc
, tgsi_dst(pc
, i
, fd
), reg
);
3453 load_interpolant(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
3455 struct nv50_reg
*iv
, **ppiv
;
3456 unsigned mode
= pc
->interp_mode
[reg
->index
];
3458 ppiv
= (mode
& INTERP_CENTROID
) ? &pc
->iv_c
: &pc
->iv_p
;
3461 if ((mode
& INTERP_PERSPECTIVE
) && !iv
) {
3462 iv
= *ppiv
= alloc_temp(pc
, NULL
);
3463 iv
->rhw
= popcnt4(pc
->p
->cfg
.regs
[1] >> 24) - 1;
3465 emit_interp(pc
, iv
, NULL
, mode
& INTERP_CENTROID
);
3466 emit_flop(pc
, NV50_FLOP_RCP
, iv
, iv
);
3468 /* XXX: when loading interpolants dynamically, move these
3469 * to the program head, or make sure it can't be skipped.
3473 emit_interp(pc
, reg
, iv
, mode
);
3476 /* The face input is always at v[255] (varying space), with a
3477 * value of 0 for back-facing, and 0xffffffff for front-facing.
3480 load_frontfacing(struct nv50_pc
*pc
, struct nv50_reg
*sv
)
3482 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
3486 emit_interp(pc
, temp
, NULL
, INTERP_FLAT
);
3488 emit_cvt(pc
, sv
, temp
, r_pred
, CVT_ABS
| CVT_F32_S32
);
3490 emit_not(pc
, temp
, temp
);
3491 set_pred(pc
, 0x2, r_pred
, pc
->p
->exec_tail
);
3492 emit_cvt(pc
, sv
, temp
, -1, CVT_F32_S32
);
3493 set_pred(pc
, 0x2, r_pred
, pc
->p
->exec_tail
);
3495 free_temp(pc
, temp
);
3499 load_instance_id(struct nv50_pc
*pc
, unsigned index
)
3501 struct nv50_reg reg
, mem
;
3503 ctor_reg(®
, P_TEMP
, -1, -1);
3504 ctor_reg(&mem
, P_CONST
, -1, 24); /* startInstance */
3507 emit_add_b32(pc
, ®
, &pc
->sysval
[index
], &mem
);
3508 pc
->sysval
[index
] = reg
;
3512 copy_semantic_info(struct nv50_program
*p
)
3516 for (i
= 0; i
< p
->cfg
.in_nr
; ++i
) {
3517 id
= p
->cfg
.in
[i
].id
;
3518 p
->cfg
.in
[i
].sn
= p
->info
.input_semantic_name
[id
];
3519 p
->cfg
.in
[i
].si
= p
->info
.input_semantic_index
[id
];
3522 for (i
= 0; i
< p
->cfg
.out_nr
; ++i
) {
3523 id
= p
->cfg
.out
[i
].id
;
3524 p
->cfg
.out
[i
].sn
= p
->info
.output_semantic_name
[id
];
3525 p
->cfg
.out
[i
].si
= p
->info
.output_semantic_index
[id
];
3530 nv50_program_tx_prep(struct nv50_pc
*pc
)
3532 struct tgsi_parse_context tp
;
3533 struct nv50_program
*p
= pc
->p
;
3534 boolean ret
= FALSE
;
3535 unsigned i
, c
, instance_id
= 0, vertex_id
= 0, flat_nr
= 0;
3537 tgsi_parse_init(&tp
, pc
->p
->pipe
.tokens
);
3538 while (!tgsi_parse_end_of_tokens(&tp
)) {
3539 const union tgsi_full_token
*tok
= &tp
.FullToken
;
3541 tgsi_parse_token(&tp
);
3542 switch (tok
->Token
.Type
) {
3543 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3545 const struct tgsi_full_immediate
*imm
=
3546 &tp
.FullToken
.FullImmediate
;
3548 ctor_immd_4f32(pc
, imm
->u
[0].Float
,
3554 case TGSI_TOKEN_TYPE_DECLARATION
:
3556 const struct tgsi_full_declaration
*d
;
3557 unsigned si
, last
, first
, mode
;
3559 d
= &tp
.FullToken
.FullDeclaration
;
3560 first
= d
->Range
.First
;
3561 last
= d
->Range
.Last
;
3563 switch (d
->Declaration
.File
) {
3564 case TGSI_FILE_TEMPORARY
:
3566 case TGSI_FILE_OUTPUT
:
3567 if (!d
->Declaration
.Semantic
||
3568 p
->type
== PIPE_SHADER_FRAGMENT
)
3571 si
= d
->Semantic
.Index
;
3572 switch (d
->Semantic
.Name
) {
3573 case TGSI_SEMANTIC_BCOLOR
:
3574 p
->cfg
.two_side
[si
].hw
= first
;
3575 if (p
->cfg
.out_nr
> first
)
3576 p
->cfg
.out_nr
= first
;
3578 case TGSI_SEMANTIC_PSIZE
:
3579 p
->cfg
.psiz
= first
;
3580 if (p
->cfg
.out_nr
> first
)
3581 p
->cfg
.out_nr
= first
;
3583 case TGSI_SEMANTIC_EDGEFLAG
:
3584 pc
->edgeflag_out
= first
;
3587 case TGSI_SEMANTIC_CLIP_DISTANCE:
3588 p->cfg.clpd = MIN2(p->cfg.clpd, first);
3595 case TGSI_FILE_INPUT
:
3597 if (p
->type
!= PIPE_SHADER_FRAGMENT
)
3600 switch (d
->Declaration
.Interpolate
) {
3601 case TGSI_INTERPOLATE_CONSTANT
:
3605 case TGSI_INTERPOLATE_PERSPECTIVE
:
3606 mode
= INTERP_PERSPECTIVE
;
3607 p
->cfg
.regs
[1] |= 0x08 << 24;
3610 mode
= INTERP_LINEAR
;
3613 if (d
->Declaration
.Centroid
)
3614 mode
|= INTERP_CENTROID
;
3617 for (i
= first
; i
<= last
; i
++)
3618 pc
->interp_mode
[i
] = mode
;
3621 case TGSI_FILE_SYSTEM_VALUE
:
3622 assert(d
->Declaration
.Semantic
);
3623 switch (d
->Semantic
.Name
) {
3624 case TGSI_SEMANTIC_FACE
:
3625 assert(p
->type
== PIPE_SHADER_FRAGMENT
);
3626 load_frontfacing(pc
,
3627 &pc
->sysval
[first
]);
3629 case TGSI_SEMANTIC_INSTANCEID
:
3630 assert(p
->type
== PIPE_SHADER_VERTEX
);
3631 instance_id
= first
;
3632 p
->cfg
.regs
[0] |= (1 << 4);
3634 case TGSI_SEMANTIC_PRIMID
:
3635 assert(p
->type
!= PIPE_SHADER_VERTEX
);
3636 p
->cfg
.prim_id
= first
;
3639 case TGSI_SEMANTIC_PRIMIDIN:
3640 assert(p->type == PIPE_SHADER_GEOMETRY);
3641 pc->sysval[first].hw = 6;
3642 p->cfg.regs[0] |= (1 << 8);
3644 case TGSI_SEMANTIC_VERTEXID:
3645 assert(p->type == PIPE_SHADER_VERTEX);
3647 p->cfg.regs[0] |= (1 << 12) | (1 << 0);
3652 case TGSI_FILE_ADDRESS
:
3653 case TGSI_FILE_CONSTANT
:
3654 case TGSI_FILE_SAMPLER
:
3657 NOUVEAU_ERR("bad decl file %d\n",
3658 d
->Declaration
.File
);
3663 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3665 prep_inspect_insn(pc
, &tok
->FullInstruction
);
3672 if (p
->type
== PIPE_SHADER_VERTEX
|| p
->type
== PIPE_SHADER_GEOMETRY
) {
3675 if (p
->type
== PIPE_SHADER_GEOMETRY
) {
3676 for (i
= 0; i
< pc
->attr_nr
; ++i
) {
3677 p
->cfg
.in
[i
].hw
= rid
;
3678 p
->cfg
.in
[i
].id
= i
;
3680 for (c
= 0; c
< 4; ++c
) {
3682 if (!pc
->attr
[n
].acc
)
3684 pc
->attr
[n
].hw
= rid
++;
3685 p
->cfg
.in
[i
].mask
|= 1 << c
;
3689 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
) {
3690 if (pc
->attr
[i
].acc
) {
3691 pc
->attr
[i
].hw
= rid
++;
3692 p
->cfg
.attr
[i
/ 32] |= 1 << (i
% 32);
3695 if (p
->cfg
.regs
[0] & (1 << 0))
3696 pc
->sysval
[vertex_id
].hw
= rid
++;
3697 if (p
->cfg
.regs
[0] & (1 << 4)) {
3698 pc
->sysval
[instance_id
].hw
= rid
++;
3699 load_instance_id(pc
, instance_id
);
3703 for (i
= 0, rid
= 0; i
< pc
->result_nr
; ++i
) {
3704 p
->cfg
.out
[i
].hw
= rid
;
3705 p
->cfg
.out
[i
].id
= i
;
3707 for (c
= 0; c
< 4; ++c
) {
3709 if (!pc
->result
[n
].acc
)
3711 pc
->result
[n
].hw
= rid
++;
3712 p
->cfg
.out
[i
].mask
|= 1 << c
;
3715 if (p
->cfg
.prim_id
< 0x40) {
3716 /* GP has to write to PrimitiveID */
3717 ctor_reg(&pc
->sysval
[p
->cfg
.prim_id
],
3718 P_RESULT
, p
->cfg
.prim_id
, rid
);
3719 p
->cfg
.prim_id
= rid
++;
3722 for (c
= 0; c
< 2; ++c
)
3723 if (p
->cfg
.two_side
[c
].hw
< 0x40)
3724 p
->cfg
.two_side
[c
] = p
->cfg
.out
[
3725 p
->cfg
.two_side
[c
].hw
];
3727 if (p
->cfg
.psiz
< 0x40)
3728 p
->cfg
.psiz
= p
->cfg
.out
[p
->cfg
.psiz
].hw
;
3730 copy_semantic_info(p
);
3732 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
3734 unsigned n
= 0, m
= pc
->attr_nr
- flat_nr
;
3738 /* do we read FragCoord ? */
3740 p
->info
.input_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
3741 /* select FCRD components we want accessible */
3742 for (c
= 0; c
< 4; ++c
)
3743 if (pc
->attr
[c
].acc
)
3744 p
->cfg
.regs
[1] |= 1 << (24 + c
);
3746 } else /* offset by 1 if FCRD.w is needed for pinterp */
3747 aid
= popcnt4(p
->cfg
.regs
[1] >> 24);
3749 /* non-flat interpolants have to be mapped to
3750 * the lower hardware IDs, so sort them:
3752 for (i
= 0; i
< pc
->attr_nr
; i
++) {
3753 if (pc
->interp_mode
[i
] == INTERP_FLAT
)
3754 p
->cfg
.in
[m
++].id
= i
;
3756 if (!(pc
->interp_mode
[i
] & INTERP_PERSPECTIVE
))
3757 p
->cfg
.in
[n
].linear
= TRUE
;
3758 p
->cfg
.in
[n
++].id
= i
;
3761 copy_semantic_info(p
);
3763 for (n
= 0; n
< pc
->attr_nr
; ++n
) {
3764 p
->cfg
.in
[n
].hw
= rid
= aid
;
3765 i
= p
->cfg
.in
[n
].id
;
3767 if (p
->info
.input_semantic_name
[i
] ==
3768 TGSI_SEMANTIC_FACE
) {
3769 load_frontfacing(pc
, &pc
->attr
[i
* 4]);
3773 for (c
= 0; c
< 4; ++c
) {
3774 if (!pc
->attr
[i
* 4 + c
].acc
)
3776 pc
->attr
[i
* 4 + c
].rhw
= rid
++;
3777 p
->cfg
.in
[n
].mask
|= 1 << c
;
3779 load_interpolant(pc
, &pc
->attr
[i
* 4 + c
]);
3781 aid
+= popcnt4(p
->cfg
.in
[n
].mask
);
3784 m
= popcnt4(p
->cfg
.regs
[1] >> 24);
3786 /* set count of non-position inputs and of non-flat
3787 * non-position inputs for FP_INTERPOLANT_CTRL
3789 p
->cfg
.regs
[1] |= aid
- m
;
3792 i
= p
->cfg
.in
[pc
->attr_nr
- flat_nr
].hw
;
3793 p
->cfg
.regs
[1] |= (i
- m
) << 16;
3795 p
->cfg
.regs
[1] |= p
->cfg
.regs
[1] << 16;
3797 /* mark color semantic for light-twoside */
3799 for (i
= 0; i
< p
->cfg
.in_nr
; i
++) {
3800 if (p
->cfg
.in
[i
].sn
== TGSI_SEMANTIC_COLOR
) {
3801 n
= MIN2(n
, p
->cfg
.in
[i
].hw
- m
);
3802 p
->cfg
.two_side
[p
->cfg
.in
[i
].si
] = p
->cfg
.in
[i
];
3804 p
->cfg
.regs
[0] += /* increase colour count */
3805 popcnt4(p
->cfg
.in
[i
].mask
) << 16;
3809 p
->cfg
.regs
[0] += n
;
3811 if (p
->cfg
.prim_id
< 0x40) {
3812 pc
->sysval
[p
->cfg
.prim_id
].rhw
= rid
++;
3813 emit_interp(pc
, &pc
->sysval
[p
->cfg
.prim_id
], NULL
,
3815 /* increase FP_INTERPOLANT_CTRL_COUNT */
3816 p
->cfg
.regs
[1] += 1;
3819 /* Initialize FP results:
3820 * FragDepth is always first TGSI and last hw output
3822 i
= p
->info
.writes_z
? 4 : 0;
3823 for (rid
= 0; i
< pc
->result_nr
* 4; i
++)
3824 pc
->result
[i
].rhw
= rid
++;
3825 if (p
->info
.writes_z
)
3826 pc
->result
[2].rhw
= rid
++;
3828 p
->cfg
.high_result
= rid
;
3830 /* separate/different colour results for MRTs ? */
3831 if (pc
->result_nr
- (p
->info
.writes_z
? 1 : 0) > 1)
3832 p
->cfg
.regs
[2] |= 1;
3838 pc
->immd
= MALLOC(pc
->immd_nr
* 4 * sizeof(struct nv50_reg
));
3842 for (i
= 0; i
< pc
->immd_nr
; i
++) {
3843 for (c
= 0; c
< 4; c
++, rid
++)
3844 ctor_reg(&pc
->immd
[rid
], P_IMMD
, i
, rid
);
3851 free_temp(pc
, pc
->iv_p
);
3853 free_temp(pc
, pc
->iv_c
);
3855 tgsi_parse_free(&tp
);
3860 free_nv50_pc(struct nv50_pc
*pc
)
3880 static INLINE
uint32_t
3881 nv50_map_gs_output_prim(unsigned pprim
)
3884 case PIPE_PRIM_POINTS
:
3885 return NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE_POINTS
;
3886 case PIPE_PRIM_LINE_STRIP
:
3887 return NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE_LINE_STRIP
;
3888 case PIPE_PRIM_TRIANGLE_STRIP
:
3889 return NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE_TRIANGLE_STRIP
;
3891 NOUVEAU_ERR("invalid GS_OUTPUT_PRIMITIVE: %u\n", pprim
);
3898 ctor_nv50_pc(struct nv50_pc
*pc
, struct nv50_program
*p
)
3901 unsigned rtype
[2] = { P_ATTR
, P_RESULT
};
3904 pc
->temp_nr
= p
->info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
3905 pc
->attr_nr
= p
->info
.file_max
[TGSI_FILE_INPUT
] + 1;
3906 pc
->result_nr
= p
->info
.file_max
[TGSI_FILE_OUTPUT
] + 1;
3907 pc
->param_nr
= p
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
3908 pc
->addr_nr
= p
->info
.file_max
[TGSI_FILE_ADDRESS
] + 1;
3909 assert(pc
->addr_nr
<= 2);
3910 pc
->sysval_nr
= p
->info
.file_max
[TGSI_FILE_SYSTEM_VALUE
] + 1;
3912 p
->cfg
.high_temp
= 4;
3914 p
->cfg
.two_side
[0].hw
= 0x40;
3915 p
->cfg
.two_side
[1].hw
= 0x40;
3916 p
->cfg
.prim_id
= 0x40;
3918 p
->cfg
.edgeflag_in
= pc
->edgeflag_out
= 0xff;
3920 for (i
= 0; i
< p
->info
.num_properties
; ++i
) {
3921 unsigned *data
= &p
->info
.properties
[i
].data
[0];
3923 switch (p
->info
.properties
[i
].name
) {
3924 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
3925 p
->cfg
.prim_type
= nv50_map_gs_output_prim(data
[0]);
3927 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
3928 p
->cfg
.vert_count
= data
[0];
3936 case PIPE_SHADER_VERTEX
:
3939 p
->cfg
.out_nr
= pc
->result_nr
;
3941 case PIPE_SHADER_GEOMETRY
:
3942 assert(p
->cfg
.prim_type
);
3943 assert(p
->cfg
.vert_count
);
3947 p
->cfg
.prim_id
= 0x80;
3948 p
->cfg
.out_nr
= pc
->result_nr
;
3949 p
->cfg
.in_nr
= pc
->attr_nr
;
3951 p
->cfg
.two_side
[0].hw
= 0x80;
3952 p
->cfg
.two_side
[1].hw
= 0x80;
3954 case PIPE_SHADER_FRAGMENT
:
3955 rtype
[0] = rtype
[1] = P_TEMP
;
3957 p
->cfg
.regs
[0] = 0x01000004;
3958 p
->cfg
.in_nr
= pc
->attr_nr
;
3960 if (p
->info
.writes_z
) {
3961 p
->cfg
.regs
[2] |= 0x00000100;
3962 p
->cfg
.regs
[3] |= 0x00000011;
3964 if (p
->info
.uses_kill
)
3965 p
->cfg
.regs
[2] |= 0x00100000;
3970 pc
->temp
= MALLOC(pc
->temp_nr
* 4 * sizeof(struct nv50_reg
));
3974 for (i
= 0; i
< pc
->temp_nr
* 4; ++i
)
3975 ctor_reg(&pc
->temp
[i
], P_TEMP
, i
/ 4, -1);
3979 pc
->attr
= MALLOC(pc
->attr_nr
* 4 * sizeof(struct nv50_reg
));
3983 for (i
= 0; i
< pc
->attr_nr
* 4; ++i
)
3984 ctor_reg(&pc
->attr
[i
], rtype
[0], i
/ 4, -1);
3987 if (pc
->result_nr
) {
3988 unsigned nr
= pc
->result_nr
* 4;
3990 pc
->result
= MALLOC(nr
* sizeof(struct nv50_reg
));
3994 for (i
= 0; i
< nr
; ++i
)
3995 ctor_reg(&pc
->result
[i
], rtype
[1], i
/ 4, -1);
4001 pc
->param
= MALLOC(pc
->param_nr
* 4 * sizeof(struct nv50_reg
));
4005 for (i
= 0; i
< pc
->param_nr
; ++i
)
4006 for (c
= 0; c
< 4; ++c
, ++rid
)
4007 ctor_reg(&pc
->param
[rid
], P_CONST
, i
, rid
);
4011 pc
->addr
= CALLOC(pc
->addr_nr
* 4, sizeof(struct nv50_reg
*));
4015 for (i
= 0; i
< NV50_SU_MAX_ADDR
; ++i
)
4016 ctor_reg(&pc
->r_addr
[i
], P_ADDR
, -1, i
+ 1);
4018 if (pc
->sysval_nr
) {
4019 pc
->sysval
= CALLOC(pc
->sysval_nr
, sizeof(struct nv50_reg
*));
4022 /* will only ever use SYSTEM_VALUE[i].x (hopefully) */
4023 for (i
= 0; i
< pc
->sysval_nr
; ++i
)
4024 ctor_reg(&pc
->sysval
[i
], rtype
[0], i
, -1);
4031 nv50_program_fixup_insns(struct nv50_pc
*pc
)
4033 struct nv50_program_exec
*e
, **bra_list
;
4036 bra_list
= CALLOC(pc
->p
->exec_size
, sizeof(struct nv50_program_exec
*));
4038 /* Collect branch instructions, we need to adjust their offsets
4039 * when converting 32 bit instructions to 64 bit ones
4041 for (n
= 0, e
= pc
->p
->exec_head
; e
; e
= e
->next
)
4042 if (e
->param
.index
>= 0 && !e
->param
.mask
)
4045 /* Make sure we don't have any single 32 bit instructions. */
4046 for (e
= pc
->p
->exec_head
, pos
= 0; e
; e
= e
->next
) {
4047 pos
+= is_long(e
) ? 2 : 1;
4049 if ((pos
& 1) && (!e
->next
|| is_long(e
->next
))) {
4050 for (i
= 0; i
< n
; ++i
)
4051 if (bra_list
[i
]->param
.index
>= pos
)
4052 bra_list
[i
]->param
.index
+= 1;
4053 for (i
= 0; i
< pc
->insn_nr
; ++i
)
4054 if (pc
->insn_pos
[i
] >= pos
)
4055 pc
->insn_pos
[i
] += 1;
4056 convert_to_long(pc
, e
);
4063 if (!pc
->p
->info
.opcode_count
[TGSI_OPCODE_CAL
])
4066 /* fill in CALL offsets */
4067 for (e
= pc
->p
->exec_head
; e
; e
= e
->next
) {
4068 if ((e
->inst
[0] & 2) && (e
->inst
[0] >> 28) == 0x2)
4069 e
->param
.index
= pc
->insn_pos
[e
->param
.index
];
4074 nv50_program_tx(struct nv50_program
*p
)
4076 struct tgsi_parse_context parse
;
4080 pc
= CALLOC_STRUCT(nv50_pc
);
4084 ret
= ctor_nv50_pc(pc
, p
);
4088 ret
= nv50_program_tx_prep(pc
);
4092 pc
->insn_pos
= MALLOC(pc
->insn_nr
* sizeof(unsigned));
4094 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
4095 while (!tgsi_parse_end_of_tokens(&parse
)) {
4096 const union tgsi_full_token
*tok
= &parse
.FullToken
;
4098 /* previously allow32 was FALSE for first & last instruction */
4101 tgsi_parse_token(&parse
);
4103 switch (tok
->Token
.Type
) {
4104 case TGSI_TOKEN_TYPE_INSTRUCTION
:
4105 pc
->insn_pos
[pc
->insn_cur
] = pc
->p
->exec_size
;
4107 ret
= nv50_tgsi_insn(pc
, tok
);
4116 nv50_program_fixup_insns(pc
);
4118 p
->param_nr
= pc
->param_nr
* 4;
4119 p
->immd_nr
= pc
->immd_nr
* 4;
4120 p
->immd
= pc
->immd_buf
;
4123 tgsi_parse_free(&parse
);
4131 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
4133 if (nv50_program_tx(p
) == FALSE
)
4135 p
->translated
= TRUE
;
4139 nv50_program_upload_data(struct nv50_context
*nv50
, uint32_t *map
,
4140 unsigned start
, unsigned count
, unsigned cbuf
)
4142 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
4143 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4146 unsigned nr
= count
> 2047 ? 2047 : count
;
4148 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
4149 OUT_RING (chan
, (cbuf
<< 0) | (start
<< 8));
4150 BEGIN_RING(chan
, tesla
, NV50TCL_CB_DATA(0) | 0x40000000, nr
);
4151 OUT_RINGp (chan
, map
, nr
);
4160 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
4162 struct pipe_context
*pipe
= &nv50
->pipe
;
4163 struct pipe_transfer
*transfer
;
4165 if (!p
->data
[0] && p
->immd_nr
) {
4166 struct nouveau_resource
*heap
= nv50
->screen
->immd_heap
;
4168 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
, &p
->data
[0])) {
4169 while (heap
->next
&& heap
->size
< p
->immd_nr
) {
4170 struct nv50_program
*evict
= heap
->next
->priv
;
4171 nouveau_resource_free(&evict
->data
[0]);
4174 if (nouveau_resource_alloc(heap
, p
->immd_nr
, p
,
4179 /* immediates only need to be uploaded again when freed */
4180 nv50_program_upload_data(nv50
, p
->immd
, p
->data
[0]->start
,
4181 p
->immd_nr
, NV50_CB_PMISC
);
4184 assert(p
->param_nr
<= 16384);
4188 uint32_t *map
= pipe_buffer_map(pipe
,
4189 nv50
->constbuf
[p
->type
],
4193 case PIPE_SHADER_GEOMETRY
: cb
= NV50_CB_PGP
; break;
4194 case PIPE_SHADER_FRAGMENT
: cb
= NV50_CB_PFP
; break;
4197 assert(p
->type
== PIPE_SHADER_VERTEX
);
4201 nv50_program_upload_data(nv50
, map
, 0, p
->param_nr
, cb
);
4202 pipe_buffer_unmap(pipe
, nv50
->constbuf
[p
->type
],
4208 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
4210 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
4211 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4212 struct nv50_program_exec
*e
;
4214 boolean upload
= FALSE
;
4219 nouveau_bo_new(chan
->device
, NOUVEAU_BO_VRAM
, 0x100,
4220 p
->exec_size
* 4, &p
->bo
);
4224 if (p
->data
[0] && p
->data
[0]->start
!= p
->data_start
[0])
4230 up
= MALLOC(p
->exec_size
* 4);
4232 for (i
= 0, e
= p
->exec_head
; e
; e
= e
->next
) {
4233 unsigned ei
, ci
, bs
;
4235 if (e
->param
.index
>= 0 && e
->param
.mask
) {
4236 bs
= (e
->inst
[1] >> 22) & 0x07;
4238 ei
= e
->param
.shift
>> 5;
4239 ci
= e
->param
.index
;
4241 ci
+= p
->data
[bs
]->start
;
4243 e
->inst
[ei
] &= ~e
->param
.mask
;
4244 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
4246 if (e
->param
.index
>= 0) {
4247 /* zero mask means param is a jump/branch offset */
4248 assert(!(e
->param
.index
& 1));
4249 /* seem to be 8 byte steps */
4250 ei
= (e
->param
.index
>> 1) + 0 /* START_ID */;
4252 e
->inst
[0] &= 0xf0000fff;
4253 e
->inst
[0] |= ei
<< 12;
4256 up
[i
++] = e
->inst
[0];
4258 up
[i
++] = e
->inst
[1];
4260 assert(i
== p
->exec_size
);
4263 p
->data_start
[0] = p
->data
[0]->start
;
4265 #ifdef NV50_PROGRAM_DUMP
4266 NOUVEAU_ERR("-------\n");
4267 for (e
= p
->exec_head
; e
; e
= e
->next
) {
4268 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
4270 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
4274 /* SIFC_HEIGHT/SIFC_WIDTH of 65536 do not work, and are not reported
4275 * as data error either. hw bug ? */
4276 #define SIFC_MAX_WIDTH (65536 - 256)
4278 width
= p
->exec_size
* 4;
4280 nv50_upload_sifc(nv50
, p
->bo
, offset
, NOUVEAU_BO_VRAM
,
4281 NV50_2D_DST_FORMAT_R8_UNORM
, 65536, 1, 262144,
4282 &up
[offset
/ 4], NV50_2D_SIFC_FORMAT_R8_UNORM
,
4283 0, 0, 0, MIN2(SIFC_MAX_WIDTH
, width
), 1, 1);
4284 width
-= SIFC_MAX_WIDTH
;
4285 offset
+= SIFC_MAX_WIDTH
;
4287 BEGIN_RING(chan
, tesla
, NV50TCL_CODE_CB_FLUSH
, 1);
4293 struct nouveau_stateobj
*
4294 nv50_vertprog_validate(struct nv50_context
*nv50
)
4296 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4297 struct nv50_program
*p
= nv50
->vertprog
;
4298 struct nouveau_stateobj
*so
;
4300 if (!p
->translated
) {
4301 nv50_program_validate(nv50
, p
);
4306 nv50_program_validate_data(nv50
, p
);
4307 nv50_program_validate_code(nv50
, p
);
4309 if (!(nv50
->dirty
& NV50_NEW_VERTPROG
))
4312 so
= so_new(5, 7, 2);
4313 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
4314 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4315 NOUVEAU_BO_HIGH
, 0, 0);
4316 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4317 NOUVEAU_BO_LOW
, 0, 0);
4318 so_method(so
, tesla
, NV50TCL_VP_ATTR_EN_0
, 2);
4319 so_data (so
, p
->cfg
.attr
[0]);
4320 so_data (so
, p
->cfg
.attr
[1]);
4321 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_RESULT
, 1);
4322 so_data (so
, p
->cfg
.high_result
);
4323 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_TEMP
, 1);
4324 so_data (so
, p
->cfg
.high_temp
);
4325 so_method(so
, tesla
, NV50TCL_VP_START_ID
, 1);
4326 so_data (so
, 0); /* program start offset */
4330 struct nouveau_stateobj
*
4331 nv50_fragprog_validate(struct nv50_context
*nv50
)
4333 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4334 struct nv50_program
*p
= nv50
->fragprog
;
4335 struct nouveau_stateobj
*so
;
4337 if (!p
->translated
) {
4338 nv50_program_validate(nv50
, p
);
4343 nv50_program_validate_data(nv50
, p
);
4344 nv50_program_validate_code(nv50
, p
);
4346 if (!(nv50
->dirty
& NV50_NEW_FRAGPROG
))
4349 so
= so_new(6, 7, 2);
4350 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
4351 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4352 NOUVEAU_BO_HIGH
, 0, 0);
4353 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4354 NOUVEAU_BO_LOW
, 0, 0);
4355 so_method(so
, tesla
, NV50TCL_FP_REG_ALLOC_TEMP
, 1);
4356 so_data (so
, p
->cfg
.high_temp
);
4357 so_method(so
, tesla
, NV50TCL_FP_RESULT_COUNT
, 1);
4358 so_data (so
, p
->cfg
.high_result
);
4359 so_method(so
, tesla
, NV50TCL_FP_CONTROL
, 1);
4360 so_data (so
, p
->cfg
.regs
[2]);
4361 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK196C
, 1);
4362 so_data (so
, p
->cfg
.regs
[3]);
4363 so_method(so
, tesla
, NV50TCL_FP_START_ID
, 1);
4364 so_data (so
, 0); /* program start offset */
4368 struct nouveau_stateobj
*
4369 nv50_geomprog_validate(struct nv50_context
*nv50
)
4371 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4372 struct nv50_program
*p
= nv50
->geomprog
;
4373 struct nouveau_stateobj
*so
;
4375 if (!p
->translated
) {
4376 nv50_program_validate(nv50
, p
);
4381 nv50_program_validate_data(nv50
, p
);
4382 nv50_program_validate_code(nv50
, p
);
4384 if (!(nv50
->dirty
& NV50_NEW_GEOMPROG
))
4387 so
= so_new(6, 7, 2);
4388 so_method(so
, tesla
, NV50TCL_GP_ADDRESS_HIGH
, 2);
4389 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4390 NOUVEAU_BO_HIGH
, 0, 0);
4391 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
4392 NOUVEAU_BO_LOW
, 0, 0);
4393 so_method(so
, tesla
, NV50TCL_GP_REG_ALLOC_TEMP
, 1);
4394 so_data (so
, p
->cfg
.high_temp
);
4395 so_method(so
, tesla
, NV50TCL_GP_REG_ALLOC_RESULT
, 1);
4396 so_data (so
, p
->cfg
.high_result
);
4397 so_method(so
, tesla
, NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE
, 1);
4398 so_data (so
, p
->cfg
.prim_type
);
4399 so_method(so
, tesla
, NV50TCL_GP_VERTEX_OUTPUT_COUNT
, 1);
4400 so_data (so
, p
->cfg
.vert_count
);
4401 so_method(so
, tesla
, NV50TCL_GP_START_ID
, 1);
4407 nv50_pntc_replace(struct nv50_context
*nv50
, uint32_t pntc
[8], unsigned base
)
4409 struct nv50_program
*vp
;
4410 struct nv50_program
*fp
= nv50
->fragprog
;
4411 unsigned i
, c
, m
= base
;
4412 uint32_t origin
= 0x00000010;
4414 vp
= nv50
->geomprog
? nv50
->geomprog
: nv50
->vertprog
;
4416 /* XXX: this might not work correctly in all cases yet - we'll
4417 * just assume that an FP generic input that is not written in
4418 * the VP is PointCoord.
4420 memset(pntc
, 0, 8 * sizeof(uint32_t));
4422 for (i
= 0; i
< fp
->cfg
.in_nr
; i
++) {
4423 unsigned j
, n
= popcnt4(fp
->cfg
.in
[i
].mask
);
4425 if (fp
->cfg
.in
[i
].sn
!= TGSI_SEMANTIC_GENERIC
) {
4430 for (j
= 0; j
< vp
->cfg
.out_nr
; ++j
)
4431 if (vp
->cfg
.out
[j
].sn
== fp
->cfg
.in
[i
].sn
&&
4432 vp
->cfg
.out
[j
].si
== fp
->cfg
.in
[i
].si
)
4435 if (j
< vp
->info
.num_outputs
) {
4437 (nv50
->rasterizer
->pipe
.sprite_coord_enable
>> vp
->cfg
.out
[j
].si
) & 1;
4445 /* this is either PointCoord or replaced by sprite coords */
4446 for (c
= 0; c
< 4; c
++) {
4447 if (!(fp
->cfg
.in
[i
].mask
& (1 << c
)))
4449 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
4453 return (nv50
->rasterizer
->pipe
.sprite_coord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
? 0 : origin
);
4457 nv50_vec4_map(uint32_t *map32
, int mid
, uint8_t zval
, uint32_t lin
[4],
4458 struct nv50_sreg4
*fpi
, struct nv50_sreg4
*vpo
)
4461 uint8_t mv
= vpo
->mask
, mf
= fpi
->mask
, oid
= vpo
->hw
;
4462 uint8_t *map
= (uint8_t *)map32
;
4464 for (c
= 0; c
< 4; ++c
) {
4466 if (fpi
->linear
== TRUE
)
4467 lin
[mid
/ 32] |= 1 << (mid
% 32);
4471 map
[mid
] = (c
== 3) ? (zval
+ 1) : zval
;
4483 struct nouveau_stateobj
*
4484 nv50_fp_linkage_validate(struct nv50_context
*nv50
)
4486 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4487 struct nv50_program
*vp
= nv50
->vertprog
;
4488 struct nv50_program
*fp
= nv50
->fragprog
;
4489 struct nouveau_stateobj
*so
;
4490 struct nv50_sreg4 dummy
;
4492 uint32_t map
[16], lin
[4], reg
[6], pcrd
[8];
4493 uint8_t zval
= 0x40;
4495 if (nv50
->geomprog
) {
4496 vp
= nv50
->geomprog
;
4499 memset(map
, 0, sizeof(map
));
4500 memset(lin
, 0, sizeof(lin
));
4502 reg
[1] = 0x00000004; /* low and high clip distance map ids */
4503 reg
[2] = 0x00000000; /* layer index map id (disabled, GP only) */
4504 reg
[3] = 0x00000000; /* point size map id & enable */
4505 reg
[5] = 0x00000000; /* primitive ID map slot */
4506 reg
[0] = fp
->cfg
.regs
[0]; /* colour semantic reg */
4507 reg
[4] = fp
->cfg
.regs
[1]; /* interpolant info */
4509 dummy
.linear
= FALSE
;
4510 dummy
.mask
= 0xf; /* map all components of HPOS */
4511 m
= nv50_vec4_map(map
, m
, zval
, lin
, &dummy
, &vp
->cfg
.out
[0]);
4515 if (vp
->cfg
.clpd
< 0x40) {
4516 for (c
= 0; c
< vp
->cfg
.clpd_nr
; ++c
) {
4517 map
[m
/ 4] |= (vp
->cfg
.clpd
+ c
) << ((m
% 4) * 8);
4523 reg
[0] |= m
<< 8; /* adjust BFC0 id */
4525 /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
4526 if (nv50
->rasterizer
->pipe
.light_twoside
) {
4527 struct nv50_sreg4
*vpo
= &vp
->cfg
.two_side
[0];
4528 struct nv50_sreg4
*fpi
= &fp
->cfg
.two_side
[0];
4530 m
= nv50_vec4_map(map
, m
, zval
, lin
, &fpi
[0], &vpo
[0]);
4531 m
= nv50_vec4_map(map
, m
, zval
, lin
, &fpi
[1], &vpo
[1]);
4534 reg
[0] += m
- 4; /* adjust FFC0 id */
4535 reg
[4] |= m
<< 8; /* set mid where 'normal' FP inputs start */
4537 for (i
= 0; i
< fp
->cfg
.in_nr
; i
++) {
4538 /* maybe even remove these from cfg.io */
4539 if (fp
->cfg
.in
[i
].sn
== TGSI_SEMANTIC_POSITION
||
4540 fp
->cfg
.in
[i
].sn
== TGSI_SEMANTIC_FACE
)
4543 for (n
= 0; n
< vp
->cfg
.out_nr
; ++n
)
4544 if (vp
->cfg
.out
[n
].sn
== fp
->cfg
.in
[i
].sn
&&
4545 vp
->cfg
.out
[n
].si
== fp
->cfg
.in
[i
].si
)
4548 m
= nv50_vec4_map(map
, m
, zval
, lin
, &fp
->cfg
.in
[i
],
4549 (n
< vp
->cfg
.out_nr
) ?
4550 &vp
->cfg
.out
[n
] : &dummy
);
4552 /* PrimitiveID either is replaced by the system value, or
4553 * written by the geometry shader into an output register
4555 if (fp
->cfg
.prim_id
< 0x40) {
4556 map
[m
/ 4] |= vp
->cfg
.prim_id
<< ((m
% 4) * 8);
4560 if (nv50
->rasterizer
->pipe
.point_size_per_vertex
) {
4561 map
[m
/ 4] |= vp
->cfg
.psiz
<< ((m
% 4) * 8);
4562 reg
[3] = (m
++ << 4) | 1;
4565 /* now fill the stateobj (at most 28 so_data) */
4566 so
= so_new(10, 54, 0);
4570 if (vp
->type
== PIPE_SHADER_GEOMETRY
) {
4571 so_method(so
, tesla
, NV50TCL_GP_RESULT_MAP_SIZE
, 1);
4573 so_method(so
, tesla
, NV50TCL_GP_RESULT_MAP(0), n
);
4574 so_datap (so
, map
, n
);
4576 so_method(so
, tesla
, NV50TCL_VP_GP_BUILTIN_ATTR_EN
, 1);
4577 so_data (so
, vp
->cfg
.regs
[0]);
4579 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_4
, 1);
4580 so_data (so
, reg
[5]);
4582 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
4584 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), n
);
4585 so_datap (so
, map
, n
);
4588 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_0
, 4);
4589 so_datap (so
, reg
, 4);
4591 so_method(so
, tesla
, NV50TCL_FP_INTERPOLANT_CTRL
, 1);
4592 so_data (so
, reg
[4]);
4594 so_method(so
, tesla
, NV50TCL_NOPERSPECTIVE_BITMAP(0), 4);
4595 so_datap (so
, lin
, 4);
4597 if (nv50
->rasterizer
->pipe
.sprite_coord_enable
) {
4598 so_method(so
, tesla
, NV50TCL_POINT_SPRITE_CTRL
, 1);
4600 nv50_pntc_replace(nv50
, pcrd
, (reg
[4] >> 8) & 0xff));
4602 so_method(so
, tesla
, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
4603 so_datap (so
, pcrd
, 8);
4606 so_method(so
, tesla
, NV50TCL_GP_ENABLE
, 1);
4607 so_data (so
, (vp
->type
== PIPE_SHADER_GEOMETRY
) ? 1 : 0);
4613 construct_vp_gp_mapping(uint32_t *map32
, int m
,
4614 struct nv50_program
*vp
, struct nv50_program
*gp
)
4616 uint8_t *map
= (uint8_t *)map32
;
4619 for (i
= 0; i
< gp
->cfg
.in_nr
; ++i
) {
4620 uint8_t oid
= 0, mv
= 0, mg
= gp
->cfg
.in
[i
].mask
;
4622 for (j
= 0; j
< vp
->cfg
.out_nr
; ++j
) {
4623 if (vp
->cfg
.out
[j
].sn
== gp
->cfg
.in
[i
].sn
&&
4624 vp
->cfg
.out
[j
].si
== gp
->cfg
.in
[i
].si
) {
4625 mv
= vp
->cfg
.out
[j
].mask
;
4626 oid
= vp
->cfg
.out
[j
].hw
;
4631 for (c
= 0; c
< 4; ++c
, mv
>>= 1, mg
>>= 1) {
4636 map
[m
++] = (c
== 3) ? 0x41 : 0x40;
4643 struct nouveau_stateobj
*
4644 nv50_gp_linkage_validate(struct nv50_context
*nv50
)
4646 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
4647 struct nouveau_stateobj
*so
;
4648 struct nv50_program
*vp
= nv50
->vertprog
;
4649 struct nv50_program
*gp
= nv50
->geomprog
;
4655 memset(map
, 0, sizeof(map
));
4657 m
= construct_vp_gp_mapping(map
, m
, vp
, gp
);
4659 so
= so_new(3, 24 - 3, 0);
4661 so_method(so
, tesla
, NV50TCL_VP_GP_BUILTIN_ATTR_EN
, 1);
4662 so_data (so
, vp
->cfg
.regs
[0] | gp
->cfg
.regs
[0]);
4665 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
4669 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), m
);
4670 so_datap (so
, map
, m
);
4676 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
4678 while (p
->exec_head
) {
4679 struct nv50_program_exec
*e
= p
->exec_head
;
4681 p
->exec_head
= e
->next
;
4684 p
->exec_tail
= NULL
;
4687 nouveau_bo_ref(NULL
, &p
->bo
);
4690 nouveau_resource_free(&p
->data
[0]);