2 * Copyright 2008 Ben Skeggs
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_context.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_state.h"
26 #include "pipe/p_inlines.h"
28 #include "pipe/p_shader_tokens.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_util.h"
32 #include "nv50_context.h"
34 #define NV50_SU_MAX_TEMP 64
35 //#define NV50_PROGRAM_DUMP
37 /* ARL - gallium craps itself on progs/vp/arl.txt
39 * MSB - Like MAD, but MUL+SUB
40 * - Fuck it off, introduce a way to negate args for ops that
43 * Look into inlining IMMD for ops other than MOV (make it general?)
44 * - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
45 * but can emit to P_TEMP first - then MOV later. NVIDIA does this
47 * In ops such as ADD it's possible to construct a bad opcode in the !is_long()
48 * case, if the emit_src() causes the inst to suddenly become long.
50 * Verify half-insns work where expected - and force disable them where they
51 * don't work - MUL has it forcibly disabled atm as it fixes POW..
53 * FUCK! watch dst==src vectors, can overwrite components that are needed.
54 * ie. SUB R0, R0.yzxw, R0
56 * Things to check with renouveau:
57 * FP attr/result assignment - how?
59 * - 0x16bc maps vp output onto fp hpos
60 * - 0x16c0 maps vp output onto fp col0
64 * 0x16bc->0x16e8 --> some binding between vp/fp regs
65 * 0x16b8 --> VP output count
67 * 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
68 * "MOV rcol.x, fcol.y" = 0x00000004
69 * 0x19a8 --> as above but 0x00000100 and 0x00000000
70 * - 0x00100000 used when KIL used
71 * 0x196c --> as above but 0x00000011 and 0x00000000
73 * 0x1988 --> 0xXXNNNNNN
74 * - XX == FP high something
89 int acc
; /* instruction where this reg is last read (first insn == 1) */
93 struct nv50_program
*p
;
96 struct nv50_reg
*r_temp
[NV50_SU_MAX_TEMP
];
99 struct nv50_reg
*temp
;
101 struct nv50_reg
*attr
;
103 struct nv50_reg
*result
;
105 struct nv50_reg
*param
;
107 struct nv50_reg
*immd
;
111 struct nv50_reg
*temp_temp
[16];
112 unsigned temp_temp_nr
;
114 unsigned interp_mode
[32];
116 /* current instruction and total number of insns */
122 alloc_reg(struct nv50_pc
*pc
, struct nv50_reg
*reg
)
126 if (reg
->type
== P_RESULT
) {
127 if (pc
->p
->cfg
.high_result
< (reg
->hw
+ 1))
128 pc
->p
->cfg
.high_result
= reg
->hw
+ 1;
131 if (reg
->type
!= P_TEMP
)
135 /*XXX: do this here too to catch FP temp-as-attr usage..
136 * not clean, but works */
137 if (pc
->p
->cfg
.high_temp
< (reg
->hw
+ 1))
138 pc
->p
->cfg
.high_temp
= reg
->hw
+ 1;
142 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
143 if (!(pc
->r_temp
[i
])) {
146 if (pc
->p
->cfg
.high_temp
< (i
+ 1))
147 pc
->p
->cfg
.high_temp
= i
+ 1;
155 static struct nv50_reg
*
156 alloc_temp(struct nv50_pc
*pc
, struct nv50_reg
*dst
)
161 if (dst
&& dst
->type
== P_TEMP
&& dst
->hw
== -1)
164 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
165 if (!pc
->r_temp
[i
]) {
166 r
= CALLOC_STRUCT(nv50_reg
);
180 free_temp(struct nv50_pc
*pc
, struct nv50_reg
*r
)
182 if (r
->index
== -1) {
185 FREE(pc
->r_temp
[hw
]);
186 pc
->r_temp
[hw
] = NULL
;
191 alloc_temp4(struct nv50_pc
*pc
, struct nv50_reg
*dst
[4], int idx
)
195 if ((idx
+ 4) >= NV50_SU_MAX_TEMP
)
198 if (pc
->r_temp
[idx
] || pc
->r_temp
[idx
+ 1] ||
199 pc
->r_temp
[idx
+ 2] || pc
->r_temp
[idx
+ 3])
200 return alloc_temp4(pc
, dst
, idx
+ 1);
202 for (i
= 0; i
< 4; i
++) {
203 dst
[i
] = CALLOC_STRUCT(nv50_reg
);
204 dst
[i
]->type
= P_TEMP
;
206 dst
[i
]->hw
= idx
+ i
;
207 pc
->r_temp
[idx
+ i
] = dst
[i
];
214 free_temp4(struct nv50_pc
*pc
, struct nv50_reg
*reg
[4])
218 for (i
= 0; i
< 4; i
++)
219 free_temp(pc
, reg
[i
]);
222 static struct nv50_reg
*
223 temp_temp(struct nv50_pc
*pc
)
225 if (pc
->temp_temp_nr
>= 16)
228 pc
->temp_temp
[pc
->temp_temp_nr
] = alloc_temp(pc
, NULL
);
229 return pc
->temp_temp
[pc
->temp_temp_nr
++];
233 kill_temp_temp(struct nv50_pc
*pc
)
237 for (i
= 0; i
< pc
->temp_temp_nr
; i
++)
238 free_temp(pc
, pc
->temp_temp
[i
]);
239 pc
->temp_temp_nr
= 0;
243 ctor_immd(struct nv50_pc
*pc
, float x
, float y
, float z
, float w
)
245 pc
->immd_buf
= REALLOC(pc
->immd_buf
, (pc
->immd_nr
* r
* sizeof(float)),
246 (pc
->immd_nr
+ 1) * 4 * sizeof(float));
247 pc
->immd_buf
[(pc
->immd_nr
* 4) + 0] = x
;
248 pc
->immd_buf
[(pc
->immd_nr
* 4) + 1] = y
;
249 pc
->immd_buf
[(pc
->immd_nr
* 4) + 2] = z
;
250 pc
->immd_buf
[(pc
->immd_nr
* 4) + 3] = w
;
252 return pc
->immd_nr
++;
255 static struct nv50_reg
*
256 alloc_immd(struct nv50_pc
*pc
, float f
)
258 struct nv50_reg
*r
= CALLOC_STRUCT(nv50_reg
);
261 for (hw
= 0; hw
< pc
->immd_nr
* 4; hw
++)
262 if (pc
->immd_buf
[hw
] == f
)
265 if (hw
== pc
->immd_nr
* 4)
266 hw
= ctor_immd(pc
, f
, -f
, 0.5 * f
, 0) * 4;
274 static struct nv50_program_exec
*
275 exec(struct nv50_pc
*pc
)
277 struct nv50_program_exec
*e
= CALLOC_STRUCT(nv50_program_exec
);
284 emit(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
286 struct nv50_program
*p
= pc
->p
;
289 p
->exec_tail
->next
= e
;
293 p
->exec_size
+= (e
->inst
[0] & 1) ? 2 : 1;
296 static INLINE
void set_long(struct nv50_pc
*, struct nv50_program_exec
*);
299 is_long(struct nv50_program_exec
*e
)
307 is_immd(struct nv50_program_exec
*e
)
309 if (is_long(e
) && (e
->inst
[1] & 3) == 3)
315 set_pred(struct nv50_pc
*pc
, unsigned pred
, unsigned idx
,
316 struct nv50_program_exec
*e
)
319 e
->inst
[1] &= ~((0x1f << 7) | (0x3 << 12));
320 e
->inst
[1] |= (pred
<< 7) | (idx
<< 12);
324 set_pred_wr(struct nv50_pc
*pc
, unsigned on
, unsigned idx
,
325 struct nv50_program_exec
*e
)
328 e
->inst
[1] &= ~((0x3 << 4) | (1 << 6));
329 e
->inst
[1] |= (idx
<< 4) | (on
<< 6);
333 set_long(struct nv50_pc
*pc
, struct nv50_program_exec
*e
)
339 set_pred(pc
, 0xf, 0, e
);
340 set_pred_wr(pc
, 0, 0, e
);
344 set_dst(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_program_exec
*e
)
346 if (dst
->type
== P_RESULT
) {
348 e
->inst
[1] |= 0x00000008;
352 e
->inst
[0] |= (dst
->hw
<< 2);
356 set_immd(struct nv50_pc
*pc
, struct nv50_reg
*imm
, struct nv50_program_exec
*e
)
358 unsigned val
= fui(pc
->immd_buf
[imm
->hw
]); /* XXX */
361 /*XXX: can't be predicated - bits overlap.. catch cases where both
362 * are required and avoid them. */
363 set_pred(pc
, 0, 0, e
);
364 set_pred_wr(pc
, 0, 0, e
);
366 e
->inst
[1] |= 0x00000002 | 0x00000001;
367 e
->inst
[0] |= (val
& 0x3f) << 16;
368 e
->inst
[1] |= (val
>> 6) << 2;
372 #define INTERP_LINEAR 0
373 #define INTERP_FLAT 1
374 #define INTERP_PERSPECTIVE 2
375 #define INTERP_CENTROID 4
378 emit_interp(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
379 struct nv50_reg
*src
, struct nv50_reg
*iv
)
381 struct nv50_program_exec
*e
= exec(pc
);
383 e
->inst
[0] |= 0x80000000;
386 e
->inst
[0] |= (src
->hw
<< 16);
388 e
->inst
[0] |= (1 << 25);
390 e
->inst
[0] |= (iv
->hw
<< 9);
397 set_data(struct nv50_pc
*pc
, struct nv50_reg
*src
, unsigned m
, unsigned s
,
398 struct nv50_program_exec
*e
)
402 e
->inst
[1] |= (1 << 22);
404 if (src
->type
== P_IMMD
) {
405 e
->inst
[1] |= (NV50_CB_PMISC
<< 22);
407 if (pc
->p
->type
== PIPE_SHADER_VERTEX
)
408 e
->inst
[1] |= (NV50_CB_PVP
<< 22);
410 e
->inst
[1] |= (NV50_CB_PFP
<< 22);
414 e
->param
.index
= src
->hw
;
416 e
->param
.mask
= m
<< (s
% 32);
420 emit_mov(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
422 struct nv50_program_exec
*e
= exec(pc
);
424 e
->inst
[0] |= 0x10000000;
428 if (0 && dst
->type
!= P_RESULT
&& src
->type
== P_IMMD
) {
429 set_immd(pc
, src
, e
);
430 /*XXX: 32-bit, but steals part of "half" reg space - need to
431 * catch and handle this case if/when we do half-regs
433 e
->inst
[0] |= 0x00008000;
435 if (src
->type
== P_IMMD
|| src
->type
== P_CONST
) {
437 set_data(pc
, src
, 0x7f, 9, e
);
438 e
->inst
[1] |= 0x20000000; /* src0 const? */
440 if (src
->type
== P_ATTR
) {
442 e
->inst
[1] |= 0x00200000;
446 e
->inst
[0] |= (src
->hw
<< 9);
449 /* We really should support "half" instructions here at some point,
450 * but I don't feel confident enough about them yet.
453 if (is_long(e
) && !is_immd(e
)) {
454 e
->inst
[1] |= 0x04000000; /* 32-bit */
455 e
->inst
[1] |= 0x0003c000; /* "subsubop" 0xf == mov */
462 emit_mov_immdval(struct nv50_pc
*pc
, struct nv50_reg
*dst
, float f
)
464 struct nv50_reg
*imm
= alloc_immd(pc
, f
);
465 emit_mov(pc
, dst
, imm
);
470 check_swap_src_0_1(struct nv50_pc
*pc
,
471 struct nv50_reg
**s0
, struct nv50_reg
**s1
)
473 struct nv50_reg
*src0
= *s0
, *src1
= *s1
;
475 if (src0
->type
== P_CONST
) {
476 if (src1
->type
!= P_CONST
) {
482 if (src1
->type
== P_ATTR
) {
483 if (src0
->type
!= P_ATTR
) {
494 set_src_0(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
496 if (src
->type
== P_ATTR
) {
498 e
->inst
[1] |= 0x00200000;
500 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
501 struct nv50_reg
*temp
= temp_temp(pc
);
503 emit_mov(pc
, temp
, src
);
508 e
->inst
[0] |= (src
->hw
<< 9);
512 set_src_1(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
514 if (src
->type
== P_ATTR
) {
515 struct nv50_reg
*temp
= temp_temp(pc
);
517 emit_mov(pc
, temp
, src
);
520 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
521 assert(!(e
->inst
[0] & 0x00800000));
522 if (e
->inst
[0] & 0x01000000) {
523 struct nv50_reg
*temp
= temp_temp(pc
);
525 emit_mov(pc
, temp
, src
);
528 set_data(pc
, src
, 0x7f, 16, e
);
529 e
->inst
[0] |= 0x00800000;
534 e
->inst
[0] |= (src
->hw
<< 16);
538 set_src_2(struct nv50_pc
*pc
, struct nv50_reg
*src
, struct nv50_program_exec
*e
)
542 if (src
->type
== P_ATTR
) {
543 struct nv50_reg
*temp
= temp_temp(pc
);
545 emit_mov(pc
, temp
, src
);
548 if (src
->type
== P_CONST
|| src
->type
== P_IMMD
) {
549 assert(!(e
->inst
[0] & 0x01000000));
550 if (e
->inst
[0] & 0x00800000) {
551 struct nv50_reg
*temp
= temp_temp(pc
);
553 emit_mov(pc
, temp
, src
);
556 set_data(pc
, src
, 0x7f, 32+14, e
);
557 e
->inst
[0] |= 0x01000000;
562 e
->inst
[1] |= (src
->hw
<< 14);
566 emit_mul(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
567 struct nv50_reg
*src1
)
569 struct nv50_program_exec
*e
= exec(pc
);
571 e
->inst
[0] |= 0xc0000000;
574 check_swap_src_0_1(pc
, &src0
, &src1
);
576 set_src_0(pc
, src0
, e
);
577 set_src_1(pc
, src1
, e
);
583 emit_add(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
584 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
586 struct nv50_program_exec
*e
= exec(pc
);
588 e
->inst
[0] |= 0xb0000000;
590 check_swap_src_0_1(pc
, &src0
, &src1
);
592 set_src_0(pc
, src0
, e
);
594 set_src_2(pc
, src1
, e
);
596 set_src_1(pc
, src1
, e
);
602 emit_minmax(struct nv50_pc
*pc
, unsigned sub
, struct nv50_reg
*dst
,
603 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
605 struct nv50_program_exec
*e
= exec(pc
);
608 e
->inst
[0] |= 0xb0000000;
609 e
->inst
[1] |= (sub
<< 29);
611 check_swap_src_0_1(pc
, &src0
, &src1
);
613 set_src_0(pc
, src0
, e
);
614 set_src_1(pc
, src1
, e
);
620 emit_sub(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
621 struct nv50_reg
*src1
)
623 struct nv50_program_exec
*e
= exec(pc
);
625 e
->inst
[0] |= 0xb0000000;
628 if (check_swap_src_0_1(pc
, &src0
, &src1
))
629 e
->inst
[1] |= 0x04000000;
631 e
->inst
[1] |= 0x08000000;
634 set_src_0(pc
, src0
, e
);
635 set_src_2(pc
, src1
, e
);
641 emit_mad(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
642 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
644 struct nv50_program_exec
*e
= exec(pc
);
646 e
->inst
[0] |= 0xe0000000;
648 check_swap_src_0_1(pc
, &src0
, &src1
);
650 set_src_0(pc
, src0
, e
);
651 set_src_1(pc
, src1
, e
);
652 set_src_2(pc
, src2
, e
);
658 emit_msb(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src0
,
659 struct nv50_reg
*src1
, struct nv50_reg
*src2
)
661 struct nv50_program_exec
*e
= exec(pc
);
663 e
->inst
[0] |= 0xe0000000;
665 e
->inst
[1] |= 0x08000000; /* src0 * src1 - src2 */
667 check_swap_src_0_1(pc
, &src0
, &src1
);
669 set_src_0(pc
, src0
, e
);
670 set_src_1(pc
, src1
, e
);
671 set_src_2(pc
, src2
, e
);
677 emit_flop(struct nv50_pc
*pc
, unsigned sub
,
678 struct nv50_reg
*dst
, struct nv50_reg
*src
)
680 struct nv50_program_exec
*e
= exec(pc
);
682 e
->inst
[0] |= 0x90000000;
685 e
->inst
[1] |= (sub
<< 29);
689 set_src_0(pc
, src
, e
);
695 emit_preex2(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
697 struct nv50_program_exec
*e
= exec(pc
);
699 e
->inst
[0] |= 0xb0000000;
702 set_src_0(pc
, src
, e
);
704 e
->inst
[1] |= (6 << 29) | 0x00004000;
710 emit_precossin(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
712 struct nv50_program_exec
*e
= exec(pc
);
714 e
->inst
[0] |= 0xb0000000;
717 set_src_0(pc
, src
, e
);
719 e
->inst
[1] |= (6 << 29);
725 emit_set(struct nv50_pc
*pc
, unsigned c_op
, struct nv50_reg
*dst
,
726 struct nv50_reg
*src0
, struct nv50_reg
*src1
)
728 struct nv50_program_exec
*e
= exec(pc
);
729 unsigned inv_cop
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
730 struct nv50_reg
*rdst
;
733 if (check_swap_src_0_1(pc
, &src0
, &src1
))
734 c_op
= inv_cop
[c_op
];
737 if (dst
->type
!= P_TEMP
)
738 dst
= alloc_temp(pc
, NULL
);
742 e
->inst
[0] |= 0xb0000000;
743 e
->inst
[1] |= (3 << 29);
744 e
->inst
[1] |= (c_op
<< 14);
745 /*XXX: breaks things, .u32 by default?
746 * decuda will disasm as .u16 and use .lo/.hi regs, but this
747 * doesn't seem to match what the hw actually does.
748 inst[1] |= 0x04000000; << breaks things.. .u32 by default?
751 set_src_0(pc
, src0
, e
);
752 set_src_1(pc
, src1
, e
);
757 e
->inst
[0] = 0xa0000001;
758 e
->inst
[1] = 0x64014780;
759 set_dst(pc
, rdst
, e
);
760 set_src_0(pc
, dst
, e
);
768 emit_flr(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
770 struct nv50_program_exec
*e
= exec(pc
);
772 e
->inst
[0] = 0xa0000000; /* cvt */
774 e
->inst
[1] |= (6 << 29); /* cvt */
775 e
->inst
[1] |= 0x08000000; /* integer mode */
776 e
->inst
[1] |= 0x04000000; /* 32 bit */
777 e
->inst
[1] |= ((0x1 << 3)) << 14; /* .rn */
778 e
->inst
[1] |= (1 << 14); /* src .f32 */
780 set_src_0(pc
, src
, e
);
786 emit_pow(struct nv50_pc
*pc
, struct nv50_reg
*dst
,
787 struct nv50_reg
*v
, struct nv50_reg
*e
)
789 struct nv50_reg
*temp
= alloc_temp(pc
, NULL
);
791 emit_flop(pc
, 3, temp
, v
);
792 emit_mul(pc
, temp
, temp
, e
);
793 emit_preex2(pc
, temp
, temp
);
794 emit_flop(pc
, 6, dst
, temp
);
800 emit_abs(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
802 struct nv50_program_exec
*e
= exec(pc
);
804 e
->inst
[0] = 0xa0000000; /* cvt */
806 e
->inst
[1] |= (6 << 29); /* cvt */
807 e
->inst
[1] |= 0x04000000; /* 32 bit */
808 e
->inst
[1] |= (1 << 14); /* src .f32 */
809 e
->inst
[1] |= ((1 << 6) << 14); /* .abs */
811 set_src_0(pc
, src
, e
);
817 emit_lit(struct nv50_pc
*pc
, struct nv50_reg
**dst
, unsigned mask
,
818 struct nv50_reg
**src
)
820 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
821 struct nv50_reg
*zero
= alloc_immd(pc
, 0.0);
822 struct nv50_reg
*neg128
= alloc_immd(pc
, -127.999999);
823 struct nv50_reg
*pos128
= alloc_immd(pc
, 127.999999);
824 struct nv50_reg
*tmp
[4];
827 emit_mov(pc
, dst
[0], one
);
830 emit_mov(pc
, dst
[3], one
);
832 if (mask
& (3 << 1)) {
836 tmp
[0] = temp_temp(pc
);
837 emit_minmax(pc
, 4, tmp
[0], src
[0], zero
);
840 if (mask
& (1 << 2)) {
841 set_pred_wr(pc
, 1, 0, pc
->p
->exec_tail
);
843 tmp
[1] = temp_temp(pc
);
844 emit_minmax(pc
, 4, tmp
[1], src
[1], zero
);
846 tmp
[3] = temp_temp(pc
);
847 emit_minmax(pc
, 4, tmp
[3], src
[3], neg128
);
848 emit_minmax(pc
, 5, tmp
[3], tmp
[3], pos128
);
850 emit_pow(pc
, dst
[2], tmp
[1], tmp
[3]);
851 emit_mov(pc
, dst
[2], zero
);
852 set_pred(pc
, 3, 0, pc
->p
->exec_tail
);
862 emit_neg(struct nv50_pc
*pc
, struct nv50_reg
*dst
, struct nv50_reg
*src
)
864 struct nv50_program_exec
*e
= exec(pc
);
867 e
->inst
[0] |= 0xa0000000; /* delta */
868 e
->inst
[1] |= (7 << 29); /* delta */
869 e
->inst
[1] |= 0x04000000; /* negate arg0? probably not */
870 e
->inst
[1] |= (1 << 14); /* src .f32 */
872 set_src_0(pc
, src
, e
);
878 emit_kil(struct nv50_pc
*pc
, struct nv50_reg
*src
)
880 struct nv50_program_exec
*e
;
881 const int r_pred
= 1;
883 /* Sets predicate reg ? */
885 e
->inst
[0] = 0xa00001fd;
886 e
->inst
[1] = 0xc4014788;
887 set_src_0(pc
, src
, e
);
888 set_pred_wr(pc
, 1, r_pred
, e
);
891 /* This is probably KILP */
893 e
->inst
[0] = 0x000001fe;
895 set_pred(pc
, 1 /* LT? */, r_pred
, e
);
899 static struct nv50_reg
*
900 tgsi_dst(struct nv50_pc
*pc
, int c
, const struct tgsi_full_dst_register
*dst
)
902 switch (dst
->DstRegister
.File
) {
903 case TGSI_FILE_TEMPORARY
:
904 return &pc
->temp
[dst
->DstRegister
.Index
* 4 + c
];
905 case TGSI_FILE_OUTPUT
:
906 return &pc
->result
[dst
->DstRegister
.Index
* 4 + c
];
916 static struct nv50_reg
*
917 tgsi_src(struct nv50_pc
*pc
, int chan
, const struct tgsi_full_src_register
*src
)
919 struct nv50_reg
*r
= NULL
;
920 struct nv50_reg
*temp
;
923 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
925 c
= tgsi_util_get_full_src_register_extswizzle(src
, chan
);
927 case TGSI_EXTSWIZZLE_X
:
928 case TGSI_EXTSWIZZLE_Y
:
929 case TGSI_EXTSWIZZLE_Z
:
930 case TGSI_EXTSWIZZLE_W
:
931 switch (src
->SrcRegister
.File
) {
932 case TGSI_FILE_INPUT
:
933 r
= &pc
->attr
[src
->SrcRegister
.Index
* 4 + c
];
935 case TGSI_FILE_TEMPORARY
:
936 r
= &pc
->temp
[src
->SrcRegister
.Index
* 4 + c
];
938 case TGSI_FILE_CONSTANT
:
939 r
= &pc
->param
[src
->SrcRegister
.Index
* 4 + c
];
941 case TGSI_FILE_IMMEDIATE
:
942 r
= &pc
->immd
[src
->SrcRegister
.Index
* 4 + c
];
944 case TGSI_FILE_SAMPLER
:
951 case TGSI_EXTSWIZZLE_ZERO
:
952 r
= alloc_immd(pc
, 0.0);
954 case TGSI_EXTSWIZZLE_ONE
:
955 if (sgn
== TGSI_UTIL_SIGN_TOGGLE
|| sgn
== TGSI_UTIL_SIGN_SET
)
956 return alloc_immd(pc
, -1.0);
957 return alloc_immd(pc
, 1.0);
964 case TGSI_UTIL_SIGN_KEEP
:
966 case TGSI_UTIL_SIGN_CLEAR
:
967 temp
= temp_temp(pc
);
968 emit_abs(pc
, temp
, r
);
971 case TGSI_UTIL_SIGN_TOGGLE
:
972 temp
= temp_temp(pc
);
973 emit_neg(pc
, temp
, r
);
976 case TGSI_UTIL_SIGN_SET
:
977 temp
= temp_temp(pc
);
978 emit_abs(pc
, temp
, r
);
979 emit_neg(pc
, temp
, temp
);
991 nv50_program_tx_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
)
993 const struct tgsi_full_instruction
*inst
= &tok
->FullInstruction
;
994 struct nv50_reg
*rdst
[4], *dst
[4], *src
[3][4], *temp
;
995 unsigned mask
, sat
, unit
;
998 mask
= inst
->FullDstRegisters
[0].DstRegister
.WriteMask
;
999 sat
= inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
;
1001 for (c
= 0; c
< 4; c
++) {
1002 if (mask
& (1 << c
))
1003 dst
[c
] = tgsi_dst(pc
, c
, &inst
->FullDstRegisters
[0]);
1012 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1013 const struct tgsi_full_src_register
*fs
= &inst
->FullSrcRegisters
[i
];
1015 if (fs
->SrcRegister
.File
== TGSI_FILE_SAMPLER
)
1016 unit
= fs
->SrcRegister
.Index
;
1018 for (c
= 0; c
< 4; c
++)
1019 src
[i
][c
] = tgsi_src(pc
, c
, fs
);
1023 for (c
= 0; c
< 4; c
++) {
1025 dst
[c
] = temp_temp(pc
);
1029 switch (inst
->Instruction
.Opcode
) {
1030 case TGSI_OPCODE_ABS
:
1031 for (c
= 0; c
< 4; c
++) {
1032 if (!(mask
& (1 << c
)))
1034 emit_abs(pc
, dst
[c
], src
[0][c
]);
1037 case TGSI_OPCODE_ADD
:
1038 for (c
= 0; c
< 4; c
++) {
1039 if (!(mask
& (1 << c
)))
1041 emit_add(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1044 case TGSI_OPCODE_COS
:
1045 temp
= temp_temp(pc
);
1046 emit_precossin(pc
, temp
, src
[0][0]);
1047 emit_flop(pc
, 5, temp
, temp
);
1048 for (c
= 0; c
< 4; c
++) {
1049 if (!(mask
& (1 << c
)))
1051 emit_mov(pc
, dst
[c
], temp
);
1054 case TGSI_OPCODE_DP3
:
1055 temp
= temp_temp(pc
);
1056 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1057 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1058 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1059 for (c
= 0; c
< 4; c
++) {
1060 if (!(mask
& (1 << c
)))
1062 emit_mov(pc
, dst
[c
], temp
);
1065 case TGSI_OPCODE_DP4
:
1066 temp
= temp_temp(pc
);
1067 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1068 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1069 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1070 emit_mad(pc
, temp
, src
[0][3], src
[1][3], temp
);
1071 for (c
= 0; c
< 4; c
++) {
1072 if (!(mask
& (1 << c
)))
1074 emit_mov(pc
, dst
[c
], temp
);
1077 case TGSI_OPCODE_DPH
:
1078 temp
= temp_temp(pc
);
1079 emit_mul(pc
, temp
, src
[0][0], src
[1][0]);
1080 emit_mad(pc
, temp
, src
[0][1], src
[1][1], temp
);
1081 emit_mad(pc
, temp
, src
[0][2], src
[1][2], temp
);
1082 emit_add(pc
, temp
, src
[1][3], temp
);
1083 for (c
= 0; c
< 4; c
++) {
1084 if (!(mask
& (1 << c
)))
1086 emit_mov(pc
, dst
[c
], temp
);
1089 case TGSI_OPCODE_DST
:
1091 struct nv50_reg
*one
= alloc_immd(pc
, 1.0);
1092 if (mask
& (1 << 0))
1093 emit_mov(pc
, dst
[0], one
);
1094 if (mask
& (1 << 1))
1095 emit_mul(pc
, dst
[1], src
[0][1], src
[1][1]);
1096 if (mask
& (1 << 2))
1097 emit_mov(pc
, dst
[2], src
[0][2]);
1098 if (mask
& (1 << 3))
1099 emit_mov(pc
, dst
[3], src
[1][3]);
1103 case TGSI_OPCODE_EX2
:
1104 temp
= temp_temp(pc
);
1105 emit_preex2(pc
, temp
, src
[0][0]);
1106 emit_flop(pc
, 6, temp
, temp
);
1107 for (c
= 0; c
< 4; c
++) {
1108 if (!(mask
& (1 << c
)))
1110 emit_mov(pc
, dst
[c
], temp
);
1113 case TGSI_OPCODE_FLR
:
1114 for (c
= 0; c
< 4; c
++) {
1115 if (!(mask
& (1 << c
)))
1117 emit_flr(pc
, dst
[c
], src
[0][c
]);
1120 case TGSI_OPCODE_FRC
:
1121 temp
= temp_temp(pc
);
1122 for (c
= 0; c
< 4; c
++) {
1123 if (!(mask
& (1 << c
)))
1125 emit_flr(pc
, temp
, src
[0][c
]);
1126 emit_sub(pc
, dst
[c
], src
[0][c
], temp
);
1129 case TGSI_OPCODE_KIL
:
1130 emit_kil(pc
, src
[0][0]);
1131 emit_kil(pc
, src
[0][1]);
1132 emit_kil(pc
, src
[0][2]);
1133 emit_kil(pc
, src
[0][3]);
1135 case TGSI_OPCODE_LIT
:
1136 emit_lit(pc
, &dst
[0], mask
, &src
[0][0]);
1138 case TGSI_OPCODE_LG2
:
1139 temp
= temp_temp(pc
);
1140 emit_flop(pc
, 3, temp
, src
[0][0]);
1141 for (c
= 0; c
< 4; c
++) {
1142 if (!(mask
& (1 << c
)))
1144 emit_mov(pc
, dst
[c
], temp
);
1147 case TGSI_OPCODE_LRP
:
1148 temp
= temp_temp(pc
);
1149 for (c
= 0; c
< 4; c
++) {
1150 if (!(mask
& (1 << c
)))
1152 emit_sub(pc
, temp
, src
[1][c
], src
[2][c
]);
1153 emit_mad(pc
, dst
[c
], temp
, src
[0][c
], src
[2][c
]);
1156 case TGSI_OPCODE_MAD
:
1157 for (c
= 0; c
< 4; c
++) {
1158 if (!(mask
& (1 << c
)))
1160 emit_mad(pc
, dst
[c
], src
[0][c
], src
[1][c
], src
[2][c
]);
1163 case TGSI_OPCODE_MAX
:
1164 for (c
= 0; c
< 4; c
++) {
1165 if (!(mask
& (1 << c
)))
1167 emit_minmax(pc
, 4, dst
[c
], src
[0][c
], src
[1][c
]);
1170 case TGSI_OPCODE_MIN
:
1171 for (c
= 0; c
< 4; c
++) {
1172 if (!(mask
& (1 << c
)))
1174 emit_minmax(pc
, 5, dst
[c
], src
[0][c
], src
[1][c
]);
1177 case TGSI_OPCODE_MOV
:
1178 for (c
= 0; c
< 4; c
++) {
1179 if (!(mask
& (1 << c
)))
1181 emit_mov(pc
, dst
[c
], src
[0][c
]);
1184 case TGSI_OPCODE_MUL
:
1185 for (c
= 0; c
< 4; c
++) {
1186 if (!(mask
& (1 << c
)))
1188 emit_mul(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1191 case TGSI_OPCODE_POW
:
1192 temp
= temp_temp(pc
);
1193 emit_pow(pc
, temp
, src
[0][0], src
[1][0]);
1194 for (c
= 0; c
< 4; c
++) {
1195 if (!(mask
& (1 << c
)))
1197 emit_mov(pc
, dst
[c
], temp
);
1200 case TGSI_OPCODE_RCP
:
1201 for (c
= 0; c
< 4; c
++) {
1202 if (!(mask
& (1 << c
)))
1204 emit_flop(pc
, 0, dst
[c
], src
[0][0]);
1207 case TGSI_OPCODE_RSQ
:
1208 for (c
= 0; c
< 4; c
++) {
1209 if (!(mask
& (1 << c
)))
1211 emit_flop(pc
, 2, dst
[c
], src
[0][0]);
1214 case TGSI_OPCODE_SCS
:
1215 temp
= temp_temp(pc
);
1216 emit_precossin(pc
, temp
, src
[0][0]);
1217 if (mask
& (1 << 0))
1218 emit_flop(pc
, 5, dst
[0], temp
);
1219 if (mask
& (1 << 1))
1220 emit_flop(pc
, 4, dst
[1], temp
);
1221 if (mask
& (1 << 2))
1222 emit_mov_immdval(pc
, dst
[2], 0.0);
1223 if (mask
& (1 << 3))
1224 emit_mov_immdval(pc
, dst
[3], 1.0);
1226 case TGSI_OPCODE_SGE
:
1227 for (c
= 0; c
< 4; c
++) {
1228 if (!(mask
& (1 << c
)))
1230 emit_set(pc
, 6, dst
[c
], src
[0][c
], src
[1][c
]);
1233 case TGSI_OPCODE_SIN
:
1234 temp
= temp_temp(pc
);
1235 emit_precossin(pc
, temp
, src
[0][0]);
1236 emit_flop(pc
, 4, temp
, temp
);
1237 for (c
= 0; c
< 4; c
++) {
1238 if (!(mask
& (1 << c
)))
1240 emit_mov(pc
, dst
[c
], temp
);
1243 case TGSI_OPCODE_SLT
:
1244 for (c
= 0; c
< 4; c
++) {
1245 if (!(mask
& (1 << c
)))
1247 emit_set(pc
, 1, dst
[c
], src
[0][c
], src
[1][c
]);
1250 case TGSI_OPCODE_SUB
:
1251 for (c
= 0; c
< 4; c
++) {
1252 if (!(mask
& (1 << c
)))
1254 emit_sub(pc
, dst
[c
], src
[0][c
], src
[1][c
]);
1257 case TGSI_OPCODE_TEX
:
1258 case TGSI_OPCODE_TXP
:
1260 struct nv50_reg
*t
[4];
1261 struct nv50_program_exec
*e
;
1263 alloc_temp4(pc
, t
, 0);
1264 emit_mov(pc
, t
[0], src
[0][0]);
1265 emit_mov(pc
, t
[1], src
[0][1]);
1268 e
->inst
[0] = 0xf6400000;
1269 e
->inst
[0] |= (unit
<< 9);
1271 e
->inst
[1] |= 0x0000c004;
1272 set_dst(pc
, t
[0], e
);
1275 if (mask
& (1 << 0)) emit_mov(pc
, dst
[0], t
[0]);
1276 if (mask
& (1 << 1)) emit_mov(pc
, dst
[1], t
[1]);
1277 if (mask
& (1 << 2)) emit_mov(pc
, dst
[2], t
[2]);
1278 if (mask
& (1 << 3)) emit_mov(pc
, dst
[3], t
[3]);
1283 case TGSI_OPCODE_XPD
:
1284 temp
= temp_temp(pc
);
1285 if (mask
& (1 << 0)) {
1286 emit_mul(pc
, temp
, src
[0][2], src
[1][1]);
1287 emit_msb(pc
, dst
[0], src
[0][1], src
[1][2], temp
);
1289 if (mask
& (1 << 1)) {
1290 emit_mul(pc
, temp
, src
[0][0], src
[1][2]);
1291 emit_msb(pc
, dst
[1], src
[0][2], src
[1][0], temp
);
1293 if (mask
& (1 << 2)) {
1294 emit_mul(pc
, temp
, src
[0][1], src
[1][0]);
1295 emit_msb(pc
, dst
[2], src
[0][0], src
[1][1], temp
);
1297 if (mask
& (1 << 3))
1298 emit_mov_immdval(pc
, dst
[3], 1.0);
1300 case TGSI_OPCODE_END
:
1303 NOUVEAU_ERR("invalid opcode %d\n", inst
->Instruction
.Opcode
);
1308 for (c
= 0; c
< 4; c
++) {
1309 struct nv50_program_exec
*e
;
1311 if (!(mask
& (1 << c
)))
1315 e
->inst
[0] = 0xa0000000; /* cvt */
1317 e
->inst
[1] |= (6 << 29); /* cvt */
1318 e
->inst
[1] |= 0x04000000; /* 32 bit */
1319 e
->inst
[1] |= (1 << 14); /* src .f32 */
1320 e
->inst
[1] |= ((1 << 5) << 14); /* .sat */
1321 set_dst(pc
, rdst
[c
], e
);
1322 set_src_0(pc
, dst
[c
], e
);
1327 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1328 for (c
= 0; c
< 4; c
++) {
1331 if (src
[i
][c
]->index
== -1 && src
[i
][c
]->type
== P_IMMD
)
1340 /* Adjust a bitmask that indicates what components of a source are used,
1341 * we use this in tx_prep so we only load interpolants that are needed.
1344 insn_adjust_mask(const struct tgsi_full_instruction
*insn
, unsigned *mask
)
1346 const struct tgsi_instruction_ext_texture
*tex
;
1348 switch (insn
->Instruction
.Opcode
) {
1349 case TGSI_OPCODE_DP3
:
1352 case TGSI_OPCODE_DP4
:
1353 case TGSI_OPCODE_DPH
:
1356 case TGSI_OPCODE_LIT
:
1359 case TGSI_OPCODE_RCP
:
1360 case TGSI_OPCODE_RSQ
:
1363 case TGSI_OPCODE_TEX
:
1364 case TGSI_OPCODE_TXP
:
1365 assert(insn
->Instruction
.Extended
);
1366 tex
= &insn
->InstructionExtTexture
;
1369 if (tex
->Texture
== TGSI_TEXTURE_1D
)
1372 if (tex
->Texture
== TGSI_TEXTURE_2D
)
1375 if (insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1384 prep_inspect_insn(struct nv50_pc
*pc
, const union tgsi_full_token
*tok
,
1385 unsigned *r_usage
[2])
1387 const struct tgsi_full_instruction
*insn
;
1388 const struct tgsi_full_src_register
*src
;
1389 const struct tgsi_dst_register
*dst
;
1391 unsigned i
, c
, k
, n
, mask
, *acc_p
;
1393 insn
= &tok
->FullInstruction
;
1394 dst
= &insn
->FullDstRegisters
[0].DstRegister
;
1395 mask
= dst
->WriteMask
;
1398 r_usage
[0] = CALLOC(pc
->temp_nr
* 4, sizeof(unsigned));
1400 r_usage
[1] = CALLOC(pc
->attr_nr
* 4, sizeof(unsigned));
1402 if (dst
->File
== TGSI_FILE_TEMPORARY
) {
1403 for (c
= 0; c
< 4; c
++) {
1404 if (!(mask
& (1 << c
)))
1406 r_usage
[0][dst
->Index
* 4 + c
] = pc
->insn_nr
;
1410 for (i
= 0; i
< insn
->Instruction
.NumSrcRegs
; i
++) {
1411 src
= &insn
->FullSrcRegisters
[i
];
1413 switch (src
->SrcRegister
.File
) {
1414 case TGSI_FILE_TEMPORARY
:
1417 case TGSI_FILE_INPUT
:
1424 insn_adjust_mask(insn
, &mask
);
1426 for (c
= 0; c
< 4; c
++) {
1427 if (!(mask
& (1 << c
)))
1430 k
= tgsi_util_get_full_src_register_extswizzle(src
, c
);
1432 case TGSI_EXTSWIZZLE_X
:
1433 case TGSI_EXTSWIZZLE_Y
:
1434 case TGSI_EXTSWIZZLE_Z
:
1435 case TGSI_EXTSWIZZLE_W
:
1436 n
= src
->SrcRegister
.Index
* 4 + k
;
1437 acc_p
[n
] = pc
->insn_nr
;
1447 nv50_program_tx_prep(struct nv50_pc
*pc
)
1449 struct tgsi_parse_context p
;
1450 boolean ret
= FALSE
;
1452 unsigned fcol
, bcol
, fcrd
, depr
;
1454 /* count (centroid) perspective interpolations */
1455 unsigned centroid_loads
= 0;
1456 unsigned perspect_loads
= 0;
1458 /* track register access for temps and attrs */
1459 unsigned *r_usage
[2];
1463 depr
= fcol
= bcol
= fcrd
= 0xffff;
1465 tgsi_parse_init(&p
, pc
->p
->pipe
.tokens
);
1466 while (!tgsi_parse_end_of_tokens(&p
)) {
1467 const union tgsi_full_token
*tok
= &p
.FullToken
;
1469 tgsi_parse_token(&p
);
1470 switch (tok
->Token
.Type
) {
1471 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1473 const struct tgsi_full_immediate
*imm
=
1474 &p
.FullToken
.FullImmediate
;
1476 ctor_immd(pc
, imm
->u
.ImmediateFloat32
[0].Float
,
1477 imm
->u
.ImmediateFloat32
[1].Float
,
1478 imm
->u
.ImmediateFloat32
[2].Float
,
1479 imm
->u
.ImmediateFloat32
[3].Float
);
1482 case TGSI_TOKEN_TYPE_DECLARATION
:
1484 const struct tgsi_full_declaration
*d
;
1485 unsigned last
, first
, mode
;
1487 d
= &p
.FullToken
.FullDeclaration
;
1488 first
= d
->DeclarationRange
.First
;
1489 last
= d
->DeclarationRange
.Last
;
1491 switch (d
->Declaration
.File
) {
1492 case TGSI_FILE_TEMPORARY
:
1493 if (pc
->temp_nr
< (last
+ 1))
1494 pc
->temp_nr
= last
+ 1;
1496 case TGSI_FILE_OUTPUT
:
1497 if (pc
->result_nr
< (last
+ 1))
1498 pc
->result_nr
= last
+ 1;
1500 if (!d
->Declaration
.Semantic
)
1503 switch (d
->Semantic
.SemanticName
) {
1504 case TGSI_SEMANTIC_POSITION
:
1512 case TGSI_FILE_INPUT
:
1514 if (pc
->attr_nr
< (last
+ 1))
1515 pc
->attr_nr
= last
+ 1;
1517 if (pc
->p
->type
!= PIPE_SHADER_FRAGMENT
)
1520 switch (d
->Declaration
.Interpolate
) {
1521 case TGSI_INTERPOLATE_CONSTANT
:
1524 case TGSI_INTERPOLATE_PERSPECTIVE
:
1525 mode
= INTERP_PERSPECTIVE
;
1528 mode
= INTERP_LINEAR
;
1532 if (d
->Declaration
.Semantic
) {
1533 switch (d
->Semantic
.SemanticName
) {
1534 case TGSI_SEMANTIC_POSITION
:
1537 case TGSI_SEMANTIC_COLOR
:
1539 mode
= INTERP_PERSPECTIVE
;
1541 case TGSI_SEMANTIC_BCOLOR
:
1543 mode
= INTERP_PERSPECTIVE
;
1548 if (d
->Declaration
.Centroid
) {
1549 mode
|= INTERP_CENTROID
;
1550 if (mode
& INTERP_PERSPECTIVE
)
1553 if (mode
& INTERP_PERSPECTIVE
)
1557 for (i
= first
; i
<= last
; i
++)
1558 pc
->interp_mode
[i
] = mode
;
1561 case TGSI_FILE_CONSTANT
:
1562 if (pc
->param_nr
< (last
+ 1))
1563 pc
->param_nr
= last
+ 1;
1565 case TGSI_FILE_SAMPLER
:
1568 NOUVEAU_ERR("bad decl file %d\n",
1569 d
->Declaration
.File
);
1574 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1576 prep_inspect_insn(pc
, tok
, r_usage
);
1584 pc
->temp
= CALLOC(pc
->temp_nr
* 4, sizeof(struct nv50_reg
));
1588 for (i
= 0; i
< pc
->temp_nr
; i
++) {
1589 for (c
= 0; c
< 4; c
++) {
1590 pc
->temp
[i
*4+c
].type
= P_TEMP
;
1591 pc
->temp
[i
*4+c
].hw
= -1;
1592 pc
->temp
[i
*4+c
].index
= i
;
1593 pc
->temp
[i
*4+c
].acc
= r_usage
[0][i
*4+c
];
1599 struct nv50_reg
*iv
= NULL
;
1602 pc
->attr
= CALLOC(pc
->attr_nr
* 4, sizeof(struct nv50_reg
));
1606 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1607 iv
= alloc_temp(pc
, NULL
);
1608 emit_interp(pc
, iv
, iv
, NULL
);
1609 emit_flop(pc
, 0, iv
, iv
);
1613 for (i
= 0; i
< pc
->attr_nr
; i
++) {
1614 struct nv50_reg
*a
= &pc
->attr
[i
*4];
1616 for (c
= 0; c
< 4; c
++) {
1617 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1618 struct nv50_reg
*at
=
1619 alloc_temp(pc
, NULL
);
1620 pc
->attr
[i
*4+c
].type
= at
->type
;
1621 pc
->attr
[i
*4+c
].hw
= at
->hw
;
1622 pc
->attr
[i
*4+c
].index
= at
->index
;
1623 pc
->attr
[i
*4+c
].acc
= r_usage
[1][i
*4+c
];
1625 pc
->p
->cfg
.vp
.attr
[aid
/32] |=
1627 pc
->attr
[i
*4+c
].type
= P_ATTR
;
1628 pc
->attr
[i
*4+c
].hw
= aid
++;
1629 pc
->attr
[i
*4+c
].index
= i
;
1633 if (pc
->p
->type
!= PIPE_SHADER_FRAGMENT
)
1636 emit_interp(pc
, &a
[0], &a
[0], iv
);
1637 emit_interp(pc
, &a
[1], &a
[1], iv
);
1638 emit_interp(pc
, &a
[2], &a
[2], iv
);
1639 emit_interp(pc
, &a
[3], &a
[3], iv
);
1646 if (pc
->result_nr
) {
1649 pc
->result
= CALLOC(pc
->result_nr
* 4, sizeof(struct nv50_reg
));
1653 for (i
= 0; i
< pc
->result_nr
; i
++) {
1654 for (c
= 0; c
< 4; c
++) {
1655 if (pc
->p
->type
== PIPE_SHADER_FRAGMENT
) {
1656 pc
->result
[i
*4+c
].type
= P_TEMP
;
1657 pc
->result
[i
*4+c
].hw
= -1;
1659 pc
->result
[i
*4+c
].type
= P_RESULT
;
1660 pc
->result
[i
*4+c
].hw
= rid
++;
1662 pc
->result
[i
*4+c
].index
= i
;
1670 pc
->param
= CALLOC(pc
->param_nr
* 4, sizeof(struct nv50_reg
));
1674 for (i
= 0; i
< pc
->param_nr
; i
++) {
1675 for (c
= 0; c
< 4; c
++) {
1676 pc
->param
[i
*4+c
].type
= P_CONST
;
1677 pc
->param
[i
*4+c
].hw
= rid
++;
1678 pc
->param
[i
*4+c
].index
= i
;
1684 int rid
= pc
->param_nr
* 4;
1686 pc
->immd
= CALLOC(pc
->immd_nr
* 4, sizeof(struct nv50_reg
));
1690 for (i
= 0; i
< pc
->immd_nr
; i
++) {
1691 for (c
= 0; c
< 4; c
++) {
1692 pc
->immd
[i
*4+c
].type
= P_IMMD
;
1693 pc
->immd
[i
*4+c
].hw
= rid
++;
1694 pc
->immd
[i
*4+c
].index
= i
;
1706 tgsi_parse_free(&p
);
1711 free_nv50_pc(struct nv50_pc
*pc
)
1726 for (i
= 0; i
< NV50_SU_MAX_TEMP
; i
++) {
1727 /* deallocate fragment program attributes */
1728 if (pc
->r_temp
[i
] && pc
->r_temp
[i
]->index
== -1)
1729 FREE(pc
->r_temp
[i
]);
1736 nv50_program_tx(struct nv50_program
*p
)
1738 struct tgsi_parse_context parse
;
1742 pc
= CALLOC_STRUCT(nv50_pc
);
1746 pc
->p
->cfg
.high_temp
= 4;
1748 ret
= nv50_program_tx_prep(pc
);
1752 tgsi_parse_init(&parse
, pc
->p
->pipe
.tokens
);
1753 while (!tgsi_parse_end_of_tokens(&parse
)) {
1754 const union tgsi_full_token
*tok
= &parse
.FullToken
;
1756 tgsi_parse_token(&parse
);
1758 switch (tok
->Token
.Type
) {
1759 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1761 ret
= nv50_program_tx_insn(pc
, tok
);
1770 if (p
->type
== PIPE_SHADER_FRAGMENT
) {
1771 struct nv50_reg out
;
1774 for (out
.hw
= 0; out
.hw
< pc
->result_nr
* 4; out
.hw
++)
1775 emit_mov(pc
, &out
, &pc
->result
[out
.hw
]);
1778 assert(is_long(pc
->p
->exec_tail
) && !is_immd(pc
->p
->exec_head
));
1779 pc
->p
->exec_tail
->inst
[1] |= 0x00000001;
1781 p
->param_nr
= pc
->param_nr
* 4;
1782 p
->immd_nr
= pc
->immd_nr
* 4;
1783 p
->immd
= pc
->immd_buf
;
1786 tgsi_parse_free(&parse
);
1794 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*p
)
1796 if (nv50_program_tx(p
) == FALSE
)
1798 p
->translated
= TRUE
;
1802 nv50_program_upload_data(struct nv50_context
*nv50
, float *map
,
1803 unsigned start
, unsigned count
)
1805 struct nouveau_channel
*chan
= nv50
->screen
->nvws
->channel
;
1806 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1809 unsigned nr
= count
> 2047 ? 2047 : count
;
1811 BEGIN_RING(chan
, tesla
, 0x00000f00, 1);
1812 OUT_RING (chan
, (NV50_CB_PMISC
<< 0) | (start
<< 8));
1813 BEGIN_RING(chan
, tesla
, 0x40000f04, nr
);
1814 OUT_RINGp (chan
, map
, nr
);
1823 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
1825 struct nouveau_winsys
*nvws
= nv50
->screen
->nvws
;
1826 struct pipe_winsys
*ws
= nv50
->pipe
.winsys
;
1827 unsigned nr
= p
->param_nr
+ p
->immd_nr
;
1829 if (!p
->data
&& nr
) {
1830 struct nouveau_resource
*heap
= nv50
->screen
->vp_data_heap
;
1832 if (nvws
->res_alloc(heap
, nr
, p
, &p
->data
)) {
1833 while (heap
->next
&& heap
->size
< nr
) {
1834 struct nv50_program
*evict
= heap
->next
->priv
;
1835 nvws
->res_free(&evict
->data
);
1838 if (nvws
->res_alloc(heap
, nr
, p
, &p
->data
))
1844 float *map
= ws
->buffer_map(ws
, nv50
->constbuf
[p
->type
],
1845 PIPE_BUFFER_USAGE_CPU_READ
);
1846 nv50_program_upload_data(nv50
, map
, p
->data
->start
,
1848 ws
->buffer_unmap(ws
, nv50
->constbuf
[p
->type
]);
1852 nv50_program_upload_data(nv50
, p
->immd
,
1853 p
->data
->start
+ p
->param_nr
,
1859 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
1861 struct nouveau_channel
*chan
= nv50
->screen
->nvws
->channel
;
1862 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1863 struct pipe_screen
*screen
= nv50
->pipe
.screen
;
1864 struct nv50_program_exec
*e
;
1865 struct nouveau_stateobj
*so
;
1866 const unsigned flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
;
1867 unsigned start
, count
, *up
, *ptr
;
1868 boolean upload
= FALSE
;
1871 p
->buffer
= screen
->buffer_create(screen
, 0x100, 0, p
->exec_size
* 4);
1875 if (p
->data
&& p
->data
->start
!= p
->data_start
) {
1876 for (e
= p
->exec_head
; e
; e
= e
->next
) {
1879 if (e
->param
.index
< 0)
1881 ei
= e
->param
.shift
>> 5;
1882 ci
= e
->param
.index
+ p
->data
->start
;
1884 e
->inst
[ei
] &= ~e
->param
.mask
;
1885 e
->inst
[ei
] |= (ci
<< e
->param
.shift
);
1888 p
->data_start
= p
->data
->start
;
1895 #ifdef NV50_PROGRAM_DUMP
1896 NOUVEAU_ERR("-------\n");
1897 for (e
= p
->exec_head
; e
; e
= e
->next
) {
1898 NOUVEAU_ERR("0x%08x\n", e
->inst
[0]);
1900 NOUVEAU_ERR("0x%08x\n", e
->inst
[1]);
1904 up
= ptr
= MALLOC(p
->exec_size
* 4);
1905 for (e
= p
->exec_head
; e
; e
= e
->next
) {
1906 *(ptr
++) = e
->inst
[0];
1908 *(ptr
++) = e
->inst
[1];
1912 so_method(so
, nv50
->screen
->tesla
, 0x1280, 3);
1913 so_reloc (so
, p
->buffer
, 0, flags
| NOUVEAU_BO_HIGH
, 0, 0);
1914 so_reloc (so
, p
->buffer
, 0, flags
| NOUVEAU_BO_LOW
, 0, 0);
1915 so_data (so
, (NV50_CB_PUPLOAD
<< 16) | 0x0800); //(p->exec_size * 4));
1917 start
= 0; count
= p
->exec_size
;
1919 struct nouveau_winsys
*nvws
= nv50
->screen
->nvws
;
1924 nr
= MIN2(count
, 2047);
1925 nr
= MIN2(nvws
->channel
->pushbuf
->remaining
, nr
);
1926 if (nvws
->channel
->pushbuf
->remaining
< (nr
+ 3)) {
1931 BEGIN_RING(chan
, tesla
, 0x0f00, 1);
1932 OUT_RING (chan
, (start
<< 8) | NV50_CB_PUPLOAD
);
1933 BEGIN_RING(chan
, tesla
, 0x40000f04, nr
);
1934 OUT_RINGp (chan
, up
+ start
, nr
);
1945 nv50_vertprog_validate(struct nv50_context
*nv50
)
1947 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1948 struct nv50_program
*p
= nv50
->vertprog
;
1949 struct nouveau_stateobj
*so
;
1951 if (!p
->translated
) {
1952 nv50_program_validate(nv50
, p
);
1957 nv50_program_validate_data(nv50
, p
);
1958 nv50_program_validate_code(nv50
, p
);
1961 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
1962 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
1963 NOUVEAU_BO_HIGH
, 0, 0);
1964 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
1965 NOUVEAU_BO_LOW
, 0, 0);
1966 so_method(so
, tesla
, 0x1650, 2);
1967 so_data (so
, p
->cfg
.vp
.attr
[0]);
1968 so_data (so
, p
->cfg
.vp
.attr
[1]);
1969 so_method(so
, tesla
, 0x16b8, 1);
1970 so_data (so
, p
->cfg
.high_result
);
1971 so_method(so
, tesla
, 0x16ac, 2);
1972 so_data (so
, p
->cfg
.high_result
); //8);
1973 so_data (so
, p
->cfg
.high_temp
);
1974 so_method(so
, tesla
, 0x140c, 1);
1975 so_data (so
, 0); /* program start offset */
1976 so_ref(so
, &nv50
->state
.vertprog
);
1981 nv50_fragprog_validate(struct nv50_context
*nv50
)
1983 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1984 struct nv50_program
*p
= nv50
->fragprog
;
1985 struct nouveau_stateobj
*so
;
1987 if (!p
->translated
) {
1988 nv50_program_validate(nv50
, p
);
1993 nv50_program_validate_data(nv50
, p
);
1994 nv50_program_validate_code(nv50
, p
);
1997 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
1998 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
1999 NOUVEAU_BO_HIGH
, 0, 0);
2000 so_reloc (so
, p
->buffer
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
2001 NOUVEAU_BO_LOW
, 0, 0);
2002 so_method(so
, tesla
, 0x1904, 4);
2003 so_data (so
, 0x00040404); /* p: 0x01000404 */
2004 so_data (so
, 0x00000004);
2005 so_data (so
, 0x00000000);
2006 so_data (so
, 0x00000000);
2007 so_method(so
, tesla
, 0x16bc, 3); /*XXX: fixme */
2008 so_data (so
, 0x03020100);
2009 so_data (so
, 0x07060504);
2010 so_data (so
, 0x0b0a0908);
2011 so_method(so
, tesla
, 0x1988, 2);
2012 so_data (so
, 0x08080408); //0x08040404); /* p: 0x0f000401 */
2013 so_data (so
, p
->cfg
.high_temp
);
2014 so_method(so
, tesla
, 0x1414, 1);
2015 so_data (so
, 0); /* program start offset */
2016 so_ref(so
, &nv50
->state
.fragprog
);
2021 nv50_program_destroy(struct nv50_context
*nv50
, struct nv50_program
*p
)
2023 struct pipe_screen
*pscreen
= nv50
->pipe
.screen
;
2025 while (p
->exec_head
) {
2026 struct nv50_program_exec
*e
= p
->exec_head
;
2028 p
->exec_head
= e
->next
;
2031 p
->exec_tail
= NULL
;
2035 pipe_buffer_reference(&p
->buffer
, NULL
);
2037 nv50
->screen
->nvws
->res_free(&p
->data
);