2 * Copyright 2010 Christoph Bumiller
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
25 #include "pipe/p_shader_tokens.h"
26 #include "tgsi/tgsi_parse.h"
27 #include "tgsi/tgsi_util.h"
28 #include "tgsi/tgsi_dump.h"
29 #include "util/u_dynarray.h"
32 #include "nvc0_program.h"
34 /* Arbitrary internal limits. */
35 #define BLD_MAX_TEMPS 64
36 #define BLD_MAX_ADDRS 4
37 #define BLD_MAX_PREDS 4
38 #define BLD_MAX_IMMDS 128
39 #define BLD_MAX_OUTPS PIPE_MAX_SHADER_OUTPUTS
41 #define BLD_MAX_COND_NESTING 8
42 #define BLD_MAX_LOOP_NESTING 4
43 #define BLD_MAX_CALL_NESTING 2
45 /* This structure represents a TGSI register. */
47 struct nv_value
*current
;
48 /* collect all SSA values assigned to it */
49 struct util_dynarray vals
;
50 /* 1 bit per loop level, indicates if used/defd, reset when loop ends */
55 static INLINE
struct nv_value
**
56 bld_register_access(struct bld_register
*reg
, unsigned i
)
58 return util_dynarray_element(®
->vals
, struct nv_value
*, i
);
62 bld_register_add_val(struct bld_register
*reg
, struct nv_value
*val
)
64 util_dynarray_append(®
->vals
, struct nv_value
*, val
);
68 bld_register_del_val(struct bld_register
*reg
, struct nv_value
*val
)
72 for (i
= reg
->vals
.size
/ sizeof(struct nv_value
*); i
> 0; --i
)
73 if (*bld_register_access(reg
, i
- 1) == val
)
78 if (i
!= reg
->vals
.size
/ sizeof(struct nv_value
*))
79 *bld_register_access(reg
, i
- 1) = util_dynarray_pop(®
->vals
,
82 reg
->vals
.size
-= sizeof(struct nv_value
*);
88 struct nvc0_translation_info
*ti
;
91 struct nv_basic_block
*b
;
93 struct tgsi_parse_context parse
[BLD_MAX_CALL_NESTING
];
96 struct nv_basic_block
*cond_bb
[BLD_MAX_COND_NESTING
];
97 struct nv_basic_block
*join_bb
[BLD_MAX_COND_NESTING
];
98 struct nv_basic_block
*else_bb
[BLD_MAX_COND_NESTING
];
100 struct nv_basic_block
*loop_bb
[BLD_MAX_LOOP_NESTING
];
101 struct nv_basic_block
*brkt_bb
[BLD_MAX_LOOP_NESTING
];
104 ubyte out_kind
; /* CFG_EDGE_FORWARD, or FAKE in case of BREAK/CONT */
106 struct bld_register tvs
[BLD_MAX_TEMPS
][4]; /* TGSI_FILE_TEMPORARY */
107 struct bld_register avs
[BLD_MAX_ADDRS
][4]; /* TGSI_FILE_ADDRESS */
108 struct bld_register pvs
[BLD_MAX_PREDS
][4]; /* TGSI_FILE_PREDICATE */
109 struct bld_register ovs
[BLD_MAX_OUTPS
][4]; /* TGSI_FILE_OUTPUT, FP only */
111 uint32_t outputs_written
[(PIPE_MAX_SHADER_OUTPUTS
+ 7) / 8];
113 struct nv_value
*zero
;
114 struct nv_value
*frag_coord
[4];
117 struct nv_value
*saved_sysvals
[4];
118 struct nv_value
*saved_addr
[4][2];
119 struct nv_value
*saved_inputs
[PIPE_MAX_SHADER_INPUTS
][4];
120 struct nv_value
*saved_immd
[BLD_MAX_IMMDS
];
125 bld_register_file(struct bld_context
*bld
, struct bld_register
*reg
)
127 if (reg
< &bld
->avs
[0][0]) return NV_FILE_GPR
;
129 if (reg
< &bld
->pvs
[0][0]) return NV_FILE_GPR
;
131 if (reg
< &bld
->ovs
[0][0]) return NV_FILE_PRED
;
133 return NV_FILE_MEM_V
;
136 static INLINE
struct nv_value
*
137 bld_fetch(struct bld_context
*bld
, struct bld_register
*regs
, int i
, int c
)
139 regs
[i
* 4 + c
].loop_use
|= 1 << bld
->loop_lvl
;
140 return regs
[i
* 4 + c
].current
;
143 static struct nv_value
*
144 bld_loop_phi(struct bld_context
*, struct bld_register
*, struct nv_value
*);
146 /* If a variable is defined in a loop without prior use, we don't need
147 * a phi in the loop header to account for backwards flow.
149 * However, if this variable is then also used outside the loop, we do
150 * need a phi after all. But we must not use this phi's def inside the
151 * loop, so we can eliminate the phi if it is unused later.
154 bld_store(struct bld_context
*bld
,
155 struct bld_register
*regs
, int i
, int c
, struct nv_value
*val
)
157 const uint16_t m
= 1 << bld
->loop_lvl
;
158 struct bld_register
*reg
= ®s
[i
* 4 + c
];
160 if (bld
->loop_lvl
&& !(m
& (reg
->loop_def
| reg
->loop_use
)))
161 bld_loop_phi(bld
, reg
, val
);
164 bld_register_add_val(reg
, reg
->current
);
166 reg
->loop_def
|= 1 << bld
->loop_lvl
;
169 #define FETCH_TEMP(i, c) bld_fetch(bld, &bld->tvs[0][0], i, c)
170 #define STORE_TEMP(i, c, v) bld_store(bld, &bld->tvs[0][0], i, c, (v))
171 #define FETCH_ADDR(i, c) bld_fetch(bld, &bld->avs[0][0], i, c)
172 #define STORE_ADDR(i, c, v) bld_store(bld, &bld->avs[0][0], i, c, (v))
173 #define FETCH_PRED(i, c) bld_fetch(bld, &bld->pvs[0][0], i, c)
174 #define STORE_PRED(i, c, v) bld_store(bld, &bld->pvs[0][0], i, c, (v))
175 #define STORE_OUTP(i, c, v) \
177 bld_store(bld, &bld->ovs[0][0], i, c, (v)); \
178 bld->outputs_written[(i) / 8] |= 1 << (((i) * 4 + (c)) % 32); \
182 bld_clear_def_use(struct bld_register
*regs
, int n
, int lvl
)
185 const uint16_t mask
= ~(1 << lvl
);
187 for (i
= 0; i
< n
* 4; ++i
) {
188 regs
[i
].loop_def
&= mask
;
189 regs
[i
].loop_use
&= mask
;
194 bld_warn_uninitialized(struct bld_context
*bld
, int kind
,
195 struct bld_register
*reg
, struct nv_basic_block
*b
)
197 #ifdef NOUVEAU_DEBUG_BITS
198 long i
= (reg
- &bld
->tvs
[0][0]) / 4;
199 long c
= (reg
- &bld
->tvs
[0][0]) & 3;
203 debug_printf("WARNING: TEMP[%li].%c %s used uninitialized in BB:%i\n",
204 i
, (int)('x' + c
), kind
? "may be" : "is", b
->id
);
208 static INLINE
struct nv_value
*
209 bld_def(struct nv_instruction
*i
, int c
, struct nv_value
*value
)
216 static INLINE
struct nv_value
*
217 find_by_bb(struct bld_register
*reg
, struct nv_basic_block
*b
)
221 if (reg
->current
&& reg
->current
->insn
->bb
== b
)
224 for (i
= 0; i
< reg
->vals
.size
/ sizeof(struct nv_value
*); ++i
)
225 if ((*bld_register_access(reg
, i
))->insn
->bb
== b
)
226 return *bld_register_access(reg
, i
);
230 /* Fetch value from register that was defined in the specified BB,
231 * or search for first definitions in all of its predecessors.
234 fetch_by_bb(struct bld_register
*reg
,
235 struct nv_value
**vals
, int *n
,
236 struct nv_basic_block
*b
)
239 struct nv_value
*val
;
241 assert(*n
< 16); /* MAX_COND_NESTING */
243 val
= find_by_bb(reg
, b
);
245 for (i
= 0; i
< *n
; ++i
)
251 for (i
= 0; i
< b
->num_in
; ++i
)
252 if (!IS_WALL_EDGE(b
->in_kind
[i
]))
253 fetch_by_bb(reg
, vals
, n
, b
->in
[i
]);
256 static INLINE
struct nv_value
*
257 bld_load_imm_u32(struct bld_context
*bld
, uint32_t u
);
259 static INLINE
struct nv_value
*
260 bld_undef(struct bld_context
*bld
, ubyte file
)
262 struct nv_instruction
*nvi
= new_instruction(bld
->pc
, NV_OP_UNDEF
);
264 return bld_def(nvi
, 0, new_value(bld
->pc
, file
, 4));
267 static struct nv_value
*
268 bld_phi(struct bld_context
*bld
, struct nv_basic_block
*b
,
269 struct bld_register
*reg
)
271 struct nv_basic_block
*in
;
272 struct nv_value
*vals
[16] = { NULL
};
273 struct nv_value
*val
;
274 struct nv_instruction
*phi
;
279 fetch_by_bb(reg
, vals
, &n
, b
);
282 bld_warn_uninitialized(bld
, 0, reg
, b
);
287 if (nvc0_bblock_dominated_by(b
, vals
[0]->insn
->bb
))
290 bld_warn_uninitialized(bld
, 1, reg
, b
);
292 /* back-tracking to insert missing value of other path */
295 if (in
->num_in
== 1) {
298 if (!nvc0_bblock_reachable_by(in
->in
[0], vals
[0]->insn
->bb
, b
))
301 if (!nvc0_bblock_reachable_by(in
->in
[1], vals
[0]->insn
->bb
, b
))
307 bld
->pc
->current_block
= in
;
309 /* should make this a no-op */
310 bld_register_add_val(reg
, bld_undef(bld
, vals
[0]->reg
.file
));
314 for (i
= 0; i
< n
; ++i
) {
315 /* if value dominates b, continue to the redefinitions */
316 if (nvc0_bblock_dominated_by(b
, vals
[i
]->insn
->bb
))
319 /* if value dominates any in-block, b should be the dom frontier */
320 for (j
= 0; j
< b
->num_in
; ++j
)
321 if (nvc0_bblock_dominated_by(b
->in
[j
], vals
[i
]->insn
->bb
))
323 /* otherwise, find the dominance frontier and put the phi there */
324 if (j
== b
->num_in
) {
325 in
= nvc0_bblock_dom_frontier(vals
[i
]->insn
->bb
);
326 val
= bld_phi(bld
, in
, reg
);
327 bld_register_add_val(reg
, val
);
333 bld
->pc
->current_block
= b
;
338 phi
= new_instruction(bld
->pc
, NV_OP_PHI
);
340 bld_def(phi
, 0, new_value(bld
->pc
, vals
[0]->reg
.file
, vals
[0]->reg
.size
));
341 for (i
= 0; i
< n
; ++i
)
342 nv_reference(bld
->pc
, phi
, i
, vals
[i
]);
347 /* Insert a phi function in the loop header.
348 * For nested loops, we need to insert phi functions in all the outer
349 * loop headers if they don't have one yet.
351 * @def: redefinition from inside loop, or NULL if to be replaced later
353 static struct nv_value
*
354 bld_loop_phi(struct bld_context
*bld
, struct bld_register
*reg
,
355 struct nv_value
*def
)
357 struct nv_instruction
*phi
;
358 struct nv_basic_block
*bb
= bld
->pc
->current_block
;
359 struct nv_value
*val
= NULL
;
361 if (bld
->loop_lvl
> 1) {
363 if (!((reg
->loop_def
| reg
->loop_use
) & (1 << bld
->loop_lvl
)))
364 val
= bld_loop_phi(bld
, reg
, NULL
);
369 val
= bld_phi(bld
, bld
->pc
->current_block
, reg
); /* old definition */
371 bld
->pc
->current_block
= bld
->loop_bb
[bld
->loop_lvl
- 1]->in
[0];
372 val
= bld_undef(bld
, bld_register_file(bld
, reg
));
375 bld
->pc
->current_block
= bld
->loop_bb
[bld
->loop_lvl
- 1];
377 phi
= new_instruction(bld
->pc
, NV_OP_PHI
);
379 bld_def(phi
, 0, new_value_like(bld
->pc
, val
));
383 bld_register_add_val(reg
, phi
->def
[0]);
385 phi
->target
= (struct nv_basic_block
*)reg
; /* cheat */
387 nv_reference(bld
->pc
, phi
, 0, val
);
388 nv_reference(bld
->pc
, phi
, 1, def
);
390 bld
->pc
->current_block
= bb
;
395 static INLINE
struct nv_value
*
396 bld_fetch_global(struct bld_context
*bld
, struct bld_register
*reg
)
398 const uint16_t m
= 1 << bld
->loop_lvl
;
399 const uint16_t use
= reg
->loop_use
;
403 /* If neither used nor def'd inside the loop, build a phi in foresight,
404 * so we don't have to replace stuff later on, which requires tracking.
406 if (bld
->loop_lvl
&& !((use
| reg
->loop_def
) & m
))
407 return bld_loop_phi(bld
, reg
, NULL
);
409 return bld_phi(bld
, bld
->pc
->current_block
, reg
);
412 static INLINE
struct nv_value
*
413 bld_imm_u32(struct bld_context
*bld
, uint32_t u
)
416 unsigned n
= bld
->num_immds
;
418 for (i
= 0; i
< n
; ++i
)
419 if (bld
->saved_immd
[i
]->reg
.imm
.u32
== u
)
420 return bld
->saved_immd
[i
];
422 assert(n
< BLD_MAX_IMMDS
);
425 bld
->saved_immd
[n
] = new_value(bld
->pc
, NV_FILE_IMM
, 4);
426 bld
->saved_immd
[n
]->reg
.imm
.u32
= u
;
427 return bld
->saved_immd
[n
];
431 bld_replace_value(struct nv_pc
*, struct nv_basic_block
*, struct nv_value
*,
434 /* Replace the source of the phi in the loop header by the last assignment,
435 * or eliminate the phi function if there is no assignment inside the loop.
437 * Redundancy situation 1 - (used) but (not redefined) value:
438 * %3 = phi %0, %3 = %3 is used
439 * %3 = phi %0, %4 = is new definition
441 * Redundancy situation 2 - (not used) but (redefined) value:
442 * %3 = phi %0, %2 = %2 is used, %3 could be used outside, deleted by DCE
445 bld_loop_end(struct bld_context
*bld
, struct nv_basic_block
*bb
)
447 struct nv_basic_block
*save
= bld
->pc
->current_block
;
448 struct nv_instruction
*phi
, *next
;
449 struct nv_value
*val
;
450 struct bld_register
*reg
;
453 for (phi
= bb
->phi
; phi
&& phi
->opcode
== NV_OP_PHI
; phi
= next
) {
456 reg
= (struct bld_register
*)phi
->target
;
459 for (s
= 1, n
= 0; n
< bb
->num_in
; ++n
) {
460 if (bb
->in_kind
[n
] != CFG_EDGE_BACK
)
464 bld
->pc
->current_block
= bb
->in
[n
];
465 val
= bld_fetch_global(bld
, reg
);
467 for (i
= 0; i
< 4; ++i
)
468 if (phi
->src
[i
] && phi
->src
[i
]->value
== val
)
471 nv_reference(bld
->pc
, phi
, s
++, val
);
473 bld
->pc
->current_block
= save
;
475 if (phi
->src
[0]->value
== phi
->def
[0] ||
476 phi
->src
[0]->value
== phi
->src
[1]->value
)
479 if (phi
->src
[1]->value
== phi
->def
[0])
485 /* eliminate the phi */
486 bld_register_del_val(reg
, phi
->def
[0]);
489 bld_replace_value(bld
->pc
, bb
, phi
->def
[0], phi
->src
[s
]->value
);
491 nvc0_insn_delete(phi
);
496 static INLINE
struct nv_value
*
497 bld_imm_f32(struct bld_context
*bld
, float f
)
499 return bld_imm_u32(bld
, fui(f
));
502 static struct nv_value
*
503 bld_insn_1(struct bld_context
*bld
, uint opcode
, struct nv_value
*src0
)
505 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
507 nv_reference(bld
->pc
, insn
, 0, src0
);
509 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
512 static struct nv_value
*
513 bld_insn_2(struct bld_context
*bld
, uint opcode
,
514 struct nv_value
*src0
, struct nv_value
*src1
)
516 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
518 nv_reference(bld
->pc
, insn
, 0, src0
);
519 nv_reference(bld
->pc
, insn
, 1, src1
);
521 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
524 static struct nv_value
*
525 bld_insn_3(struct bld_context
*bld
, uint opcode
,
526 struct nv_value
*src0
, struct nv_value
*src1
,
527 struct nv_value
*src2
)
529 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
531 nv_reference(bld
->pc
, insn
, 0, src0
);
532 nv_reference(bld
->pc
, insn
, 1, src1
);
533 nv_reference(bld
->pc
, insn
, 2, src2
);
535 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
539 bld_src_predicate(struct bld_context
*bld
,
540 struct nv_instruction
*nvi
, int s
, struct nv_value
*val
)
543 nv_reference(bld
->pc
, nvi
, s
, val
);
547 bld_src_pointer(struct bld_context
*bld
,
548 struct nv_instruction
*nvi
, int s
, struct nv_value
*val
)
551 nv_reference(bld
->pc
, nvi
, s
, val
);
555 bld_lmem_store(struct bld_context
*bld
, struct nv_value
*ptr
, int ofst
,
556 struct nv_value
*val
)
558 struct nv_instruction
*insn
= new_instruction(bld
->pc
, NV_OP_ST
);
559 struct nv_value
*loc
;
561 loc
= new_value(bld
->pc
, NV_FILE_MEM_L
, nv_type_sizeof(NV_TYPE_U32
));
563 loc
->reg
.id
= ofst
* 4;
565 nv_reference(bld
->pc
, insn
, 0, loc
);
566 nv_reference(bld
->pc
, insn
, 1, ptr
);
567 nv_reference(bld
->pc
, insn
, 2, val
);
570 static struct nv_value
*
571 bld_lmem_load(struct bld_context
*bld
, struct nv_value
*ptr
, int ofst
)
573 struct nv_value
*loc
, *val
;
575 loc
= new_value(bld
->pc
, NV_FILE_MEM_L
, nv_type_sizeof(NV_TYPE_U32
));
577 loc
->reg
.address
= ofst
* 4;
579 val
= bld_insn_2(bld
, NV_OP_LD
, loc
, ptr
);
584 static struct nv_value
*
585 bld_pow(struct bld_context
*bld
, struct nv_value
*x
, struct nv_value
*e
)
587 struct nv_value
*val
;
589 val
= bld_insn_1(bld
, NV_OP_LG2
, x
);
590 val
= bld_insn_2(bld
, NV_OP_MUL_F32
, e
, val
);
592 val
= bld_insn_1(bld
, NV_OP_PREEX2
, val
);
593 val
= bld_insn_1(bld
, NV_OP_EX2
, val
);
598 static INLINE
struct nv_value
*
599 bld_load_imm_f32(struct bld_context
*bld
, float f
)
603 return bld_insn_1(bld
, NV_OP_MOV
, bld_imm_f32(bld
, f
));
606 static INLINE
struct nv_value
*
607 bld_load_imm_u32(struct bld_context
*bld
, uint32_t u
)
611 return bld_insn_1(bld
, NV_OP_MOV
, bld_imm_u32(bld
, u
));
614 static INLINE
struct nv_value
*
615 bld_setp(struct bld_context
*bld
, uint op
, uint8_t cc
,
616 struct nv_value
*src0
, struct nv_value
*src1
)
618 struct nv_value
*val
= bld_insn_2(bld
, op
, src0
, src1
);
620 val
->reg
.file
= NV_FILE_PRED
;
622 val
->insn
->set_cond
= cc
& 0xf;
626 static INLINE
struct nv_value
*
627 bld_cvt(struct bld_context
*bld
, uint8_t dt
, uint8_t st
, struct nv_value
*src
)
629 struct nv_value
*val
= bld_insn_1(bld
, NV_OP_CVT
, src
);
630 val
->insn
->ext
.cvt
.d
= dt
;
631 val
->insn
->ext
.cvt
.s
= st
;
636 bld_kil(struct bld_context
*bld
, struct nv_value
*src
)
638 struct nv_instruction
*nvi
;
640 src
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LT
, src
, bld
->zero
);
642 nvi
= new_instruction(bld
->pc
, NV_OP_KIL
);
645 bld_src_predicate(bld
, nvi
, 0, src
);
649 bld_flow(struct bld_context
*bld
, uint opcode
,
650 struct nv_value
*src
, struct nv_basic_block
*target
,
653 struct nv_instruction
*nvi
;
656 new_instruction(bld
->pc
, NV_OP_JOINAT
)->fixed
= 1;
658 nvi
= new_instruction(bld
->pc
, opcode
);
659 nvi
->target
= target
;
662 bld_src_predicate(bld
, nvi
, 0, src
);
666 translate_setcc(unsigned opcode
)
669 case TGSI_OPCODE_SLT
: return NV_CC_LT
;
670 case TGSI_OPCODE_SGE
: return NV_CC_GE
;
671 case TGSI_OPCODE_SEQ
: return NV_CC_EQ
;
672 case TGSI_OPCODE_SGT
: return NV_CC_GT
;
673 case TGSI_OPCODE_SLE
: return NV_CC_LE
;
674 case TGSI_OPCODE_SNE
: return NV_CC_NE
| NV_CC_U
;
675 case TGSI_OPCODE_STR
: return NV_CC_TR
;
676 case TGSI_OPCODE_SFL
: return NV_CC_FL
;
678 case TGSI_OPCODE_ISLT
: return NV_CC_LT
;
679 case TGSI_OPCODE_ISGE
: return NV_CC_GE
;
680 case TGSI_OPCODE_USEQ
: return NV_CC_EQ
;
681 case TGSI_OPCODE_USGE
: return NV_CC_GE
;
682 case TGSI_OPCODE_USLT
: return NV_CC_LT
;
683 case TGSI_OPCODE_USNE
: return NV_CC_NE
;
691 translate_opcode(uint opcode
)
694 case TGSI_OPCODE_ABS
: return NV_OP_ABS_F32
;
695 case TGSI_OPCODE_ADD
: return NV_OP_ADD_F32
;
696 case TGSI_OPCODE_SUB
: return NV_OP_SUB_F32
;
697 case TGSI_OPCODE_UADD
: return NV_OP_ADD_B32
;
698 case TGSI_OPCODE_AND
: return NV_OP_AND
;
699 case TGSI_OPCODE_EX2
: return NV_OP_EX2
;
700 case TGSI_OPCODE_CEIL
: return NV_OP_CEIL
;
701 case TGSI_OPCODE_FLR
: return NV_OP_FLOOR
;
702 case TGSI_OPCODE_TRUNC
: return NV_OP_TRUNC
;
703 case TGSI_OPCODE_COS
: return NV_OP_COS
;
704 case TGSI_OPCODE_SIN
: return NV_OP_SIN
;
705 case TGSI_OPCODE_DDX
: return NV_OP_DFDX
;
706 case TGSI_OPCODE_DDY
: return NV_OP_DFDY
;
707 case TGSI_OPCODE_F2I
:
708 case TGSI_OPCODE_F2U
:
709 case TGSI_OPCODE_I2F
:
710 case TGSI_OPCODE_U2F
: return NV_OP_CVT
;
711 case TGSI_OPCODE_INEG
: return NV_OP_NEG_S32
;
712 case TGSI_OPCODE_LG2
: return NV_OP_LG2
;
713 case TGSI_OPCODE_ISHR
: return NV_OP_SAR
;
714 case TGSI_OPCODE_USHR
: return NV_OP_SHR
;
715 case TGSI_OPCODE_MAD
: return NV_OP_MAD_F32
;
716 case TGSI_OPCODE_MAX
: return NV_OP_MAX_F32
;
717 case TGSI_OPCODE_IMAX
: return NV_OP_MAX_S32
;
718 case TGSI_OPCODE_UMAX
: return NV_OP_MAX_U32
;
719 case TGSI_OPCODE_MIN
: return NV_OP_MIN_F32
;
720 case TGSI_OPCODE_IMIN
: return NV_OP_MIN_S32
;
721 case TGSI_OPCODE_UMIN
: return NV_OP_MIN_U32
;
722 case TGSI_OPCODE_MUL
: return NV_OP_MUL_F32
;
723 case TGSI_OPCODE_UMUL
: return NV_OP_MUL_B32
;
724 case TGSI_OPCODE_OR
: return NV_OP_OR
;
725 case TGSI_OPCODE_RCP
: return NV_OP_RCP
;
726 case TGSI_OPCODE_RSQ
: return NV_OP_RSQ
;
727 case TGSI_OPCODE_SAD
: return NV_OP_SAD
;
728 case TGSI_OPCODE_SHL
: return NV_OP_SHL
;
729 case TGSI_OPCODE_SLT
:
730 case TGSI_OPCODE_SGE
:
731 case TGSI_OPCODE_SEQ
:
732 case TGSI_OPCODE_SGT
:
733 case TGSI_OPCODE_SLE
:
734 case TGSI_OPCODE_SNE
: return NV_OP_FSET_F32
;
735 case TGSI_OPCODE_ISLT
:
736 case TGSI_OPCODE_ISGE
: return NV_OP_SET_S32
;
737 case TGSI_OPCODE_USEQ
:
738 case TGSI_OPCODE_USGE
:
739 case TGSI_OPCODE_USLT
:
740 case TGSI_OPCODE_USNE
: return NV_OP_SET_U32
;
741 case TGSI_OPCODE_TEX
: return NV_OP_TEX
;
742 case TGSI_OPCODE_TXP
: return NV_OP_TEX
;
743 case TGSI_OPCODE_TXB
: return NV_OP_TXB
;
744 case TGSI_OPCODE_TXL
: return NV_OP_TXL
;
745 case TGSI_OPCODE_XOR
: return NV_OP_XOR
;
753 infer_src_type(unsigned opcode
)
756 case TGSI_OPCODE_MOV
:
757 case TGSI_OPCODE_AND
:
759 case TGSI_OPCODE_XOR
:
760 case TGSI_OPCODE_SAD
:
761 case TGSI_OPCODE_U2F
:
762 case TGSI_OPCODE_UADD
:
763 case TGSI_OPCODE_UDIV
:
764 case TGSI_OPCODE_UMOD
:
765 case TGSI_OPCODE_UMAD
:
766 case TGSI_OPCODE_UMUL
:
767 case TGSI_OPCODE_UMAX
:
768 case TGSI_OPCODE_UMIN
:
769 case TGSI_OPCODE_USEQ
:
770 case TGSI_OPCODE_USGE
:
771 case TGSI_OPCODE_USLT
:
772 case TGSI_OPCODE_USNE
:
773 case TGSI_OPCODE_USHR
:
775 case TGSI_OPCODE_I2F
:
776 case TGSI_OPCODE_IDIV
:
777 case TGSI_OPCODE_IMAX
:
778 case TGSI_OPCODE_IMIN
:
779 case TGSI_OPCODE_INEG
:
780 case TGSI_OPCODE_ISGE
:
781 case TGSI_OPCODE_ISHR
:
782 case TGSI_OPCODE_ISLT
:
790 infer_dst_type(unsigned opcode
)
793 case TGSI_OPCODE_MOV
:
794 case TGSI_OPCODE_F2U
:
795 case TGSI_OPCODE_AND
:
797 case TGSI_OPCODE_XOR
:
798 case TGSI_OPCODE_SAD
:
799 case TGSI_OPCODE_UADD
:
800 case TGSI_OPCODE_UDIV
:
801 case TGSI_OPCODE_UMOD
:
802 case TGSI_OPCODE_UMAD
:
803 case TGSI_OPCODE_UMUL
:
804 case TGSI_OPCODE_UMAX
:
805 case TGSI_OPCODE_UMIN
:
806 case TGSI_OPCODE_USEQ
:
807 case TGSI_OPCODE_USGE
:
808 case TGSI_OPCODE_USLT
:
809 case TGSI_OPCODE_USNE
:
810 case TGSI_OPCODE_USHR
:
812 case TGSI_OPCODE_F2I
:
813 case TGSI_OPCODE_IDIV
:
814 case TGSI_OPCODE_IMAX
:
815 case TGSI_OPCODE_IMIN
:
816 case TGSI_OPCODE_INEG
:
817 case TGSI_OPCODE_ISGE
:
818 case TGSI_OPCODE_ISHR
:
819 case TGSI_OPCODE_ISLT
:
828 emit_store(struct bld_context
*bld
, const struct tgsi_full_instruction
*inst
,
829 unsigned chan
, struct nv_value
*res
)
831 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
832 struct nv_instruction
*nvi
;
833 struct nv_value
*mem
;
834 struct nv_value
*ptr
= NULL
;
837 idx
= reg
->Register
.Index
;
840 if (reg
->Register
.Indirect
)
841 ptr
= FETCH_ADDR(reg
->Indirect
.Index
,
842 tgsi_util_get_src_register_swizzle(®
->Indirect
, 0));
844 switch (inst
->Instruction
.Saturate
) {
847 case TGSI_SAT_ZERO_ONE
:
848 res
= bld_insn_1(bld
, NV_OP_SAT
, res
);
850 case TGSI_SAT_MINUS_PLUS_ONE
:
851 res
= bld_insn_2(bld
, NV_OP_MAX_F32
, res
, bld_load_imm_f32(bld
, -1.0f
));
852 res
= bld_insn_2(bld
, NV_OP_MIN_F32
, res
, bld_load_imm_f32(bld
, 1.0f
));
856 switch (reg
->Register
.File
) {
857 case TGSI_FILE_OUTPUT
:
859 res
= bld_insn_1(bld
, NV_OP_MOV
, res
);
861 if (bld
->pc
->is_fragprog
) {
863 STORE_OUTP(idx
, chan
, res
);
865 nvi
= new_instruction(bld
->pc
, NV_OP_EXPORT
);
866 mem
= new_value(bld
->pc
, bld
->ti
->output_file
, res
->reg
.size
);
867 nv_reference(bld
->pc
, nvi
, 0, mem
);
868 nv_reference(bld
->pc
, nvi
, 1, res
);
870 mem
->reg
.address
= bld
->ti
->output_loc
[idx
][chan
];
872 mem
->reg
.address
= 0x80 + idx
* 16 + chan
* 4;
876 case TGSI_FILE_TEMPORARY
:
877 assert(idx
< BLD_MAX_TEMPS
);
879 res
= bld_insn_1(bld
, NV_OP_MOV
, res
);
881 assert(res
->reg
.file
== NV_FILE_GPR
);
882 assert(res
->insn
->bb
= bld
->pc
->current_block
);
884 if (bld
->ti
->require_stores
)
885 bld_lmem_store(bld
, ptr
, idx
* 4 + chan
, res
);
887 STORE_TEMP(idx
, chan
, res
);
889 case TGSI_FILE_ADDRESS
:
890 assert(idx
< BLD_MAX_ADDRS
);
891 STORE_ADDR(idx
, chan
, res
);
896 static INLINE
uint32_t
897 bld_is_output_written(struct bld_context
*bld
, int i
, int c
)
900 return bld
->outputs_written
[i
/ 8] & (0xf << ((i
* 4) % 32));
901 return bld
->outputs_written
[i
/ 8] & (1 << ((i
* 4 + c
) % 32));
905 bld_export_fp_outputs(struct bld_context
*bld
)
907 struct nv_value
*vals
[4];
908 struct nv_instruction
*nvi
;
911 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; ++i
) {
912 if (!bld_is_output_written(bld
, i
, -1))
914 for (n
= 0, c
= 0; c
< 4; ++c
) {
915 if (!bld_is_output_written(bld
, i
, c
))
917 vals
[n
] = bld_fetch_global(bld
, &bld
->ovs
[i
][c
]);
919 vals
[n
] = bld_insn_1(bld
, NV_OP_MOV
, vals
[n
]);
920 vals
[n
++]->reg
.id
= bld
->ti
->output_loc
[i
][c
];
924 (nvi
= new_instruction(bld
->pc
, NV_OP_EXPORT
))->fixed
= 1;
925 for (c
= 0; c
< n
; ++c
)
926 nv_reference(bld
->pc
, nvi
, c
, vals
[c
]);
931 bld_new_block(struct bld_context
*bld
, struct nv_basic_block
*b
)
935 bld
->pc
->current_block
= b
;
937 for (i
= 0; i
< 4; ++i
)
938 bld
->saved_addr
[i
][0] = NULL
;
939 for (i
= 0; i
< PIPE_MAX_SHADER_INPUTS
; ++i
)
940 for (c
= 0; c
< 4; ++c
)
941 bld
->saved_inputs
[i
][c
] = NULL
;
943 bld
->out_kind
= CFG_EDGE_FORWARD
;
946 static struct nv_value
*
947 bld_get_saved_input(struct bld_context
*bld
, unsigned i
, unsigned c
)
949 if (bld
->saved_inputs
[i
][c
])
950 return bld
->saved_inputs
[i
][c
];
954 static struct nv_value
*
955 bld_interp(struct bld_context
*bld
, unsigned mode
, struct nv_value
*val
)
957 unsigned cent
= mode
& NVC0_INTERP_CENTROID
;
959 mode
&= ~NVC0_INTERP_CENTROID
;
961 if (val
->reg
.address
== 0x3fc) {
962 /* gl_FrontFacing: 0/~0 to -1.0/+1.0 */
963 val
= bld_insn_1(bld
, NV_OP_LINTERP
, val
);
964 val
= bld_insn_2(bld
, NV_OP_SHL
, val
, bld_imm_u32(bld
, 31));
965 val
= bld_insn_2(bld
, NV_OP_XOR
, val
, bld_imm_f32(bld
, -1.0f
));
967 if (mode
== NVC0_INTERP_PERSPECTIVE
) {
968 val
= bld_insn_2(bld
, NV_OP_PINTERP
, val
, bld
->frag_coord
[3]);
970 val
= bld_insn_1(bld
, NV_OP_LINTERP
, val
);
973 val
->insn
->flat
= mode
== NVC0_INTERP_FLAT
? 1 : 0;
974 val
->insn
->centroid
= cent
? 1 : 0;
978 static struct nv_value
*
979 emit_fetch(struct bld_context
*bld
, const struct tgsi_full_instruction
*insn
,
980 const unsigned s
, const unsigned chan
)
982 const struct tgsi_full_src_register
*src
= &insn
->Src
[s
];
983 struct nv_value
*res
= NULL
;
984 struct nv_value
*ptr
= NULL
;
985 int idx
, ind_idx
, dim_idx
;
986 unsigned swz
, ind_swz
, sgn
;
988 idx
= src
->Register
.Index
;
989 swz
= tgsi_util_get_full_src_register_swizzle(src
, chan
);
991 if (src
->Register
.Indirect
) {
992 ind_idx
= src
->Indirect
.Index
;
993 ind_swz
= tgsi_util_get_src_register_swizzle(&src
->Indirect
, 0);
995 ptr
= FETCH_ADDR(ind_idx
, ind_swz
);
998 if (src
->Register
.Dimension
)
999 dim_idx
= src
->Dimension
.Index
;
1003 switch (src
->Register
.File
) {
1004 case TGSI_FILE_CONSTANT
:
1005 assert(dim_idx
< 14);
1006 res
= new_value(bld
->pc
, NV_FILE_MEM_C(dim_idx
), 4);
1007 res
->reg
.address
= idx
* 16 + swz
* 4;
1008 res
= bld_insn_1(bld
, NV_OP_LD
, res
);
1010 bld_src_pointer(bld
, res
->insn
, 1, ptr
);
1012 case TGSI_FILE_IMMEDIATE
: /* XXX: type for MOV TEMP[0], -IMM[0] */
1013 assert(idx
< bld
->ti
->immd32_nr
);
1014 res
= bld_load_imm_u32(bld
, bld
->ti
->immd32
[idx
* 4 + swz
]);
1016 case TGSI_FILE_INPUT
:
1017 assert(!src
->Register
.Dimension
);
1019 res
= bld_get_saved_input(bld
, idx
, swz
);
1023 res
= new_value(bld
->pc
, bld
->ti
->input_file
, 4);
1025 res
->reg
.address
= 0x80 + idx
* 16 + swz
* 4;
1027 res
->reg
.address
= bld
->ti
->input_loc
[idx
][swz
];
1029 if (bld
->pc
->is_fragprog
)
1030 res
= bld_interp(bld
, bld
->ti
->interp_mode
[idx
], res
);
1032 res
= bld_insn_1(bld
, NV_OP_VFETCH
, res
);
1035 bld_src_pointer(bld
, res
->insn
, res
->insn
->src
[1] ? 2 : 1, ptr
);
1037 bld
->saved_inputs
[idx
][swz
] = res
;
1039 case TGSI_FILE_TEMPORARY
:
1040 if (bld
->ti
->require_stores
)
1041 res
= bld_lmem_load(bld
, ptr
, idx
* 4 + swz
);
1043 res
= bld_fetch_global(bld
, &bld
->tvs
[idx
][swz
]);
1045 case TGSI_FILE_ADDRESS
:
1046 res
= bld_fetch_global(bld
, &bld
->avs
[idx
][swz
]);
1048 case TGSI_FILE_PREDICATE
:
1049 res
= bld_fetch_global(bld
, &bld
->pvs
[idx
][swz
]);
1052 NOUVEAU_ERR("illegal/unhandled src reg file: %d\n", src
->Register
.File
);
1057 return bld_undef(bld
, NV_FILE_GPR
);
1059 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1062 case TGSI_UTIL_SIGN_KEEP
:
1064 case TGSI_UTIL_SIGN_CLEAR
:
1065 res
= bld_insn_1(bld
, NV_OP_ABS_F32
, res
);
1067 case TGSI_UTIL_SIGN_TOGGLE
:
1068 res
= bld_insn_1(bld
, NV_OP_NEG_F32
, res
);
1070 case TGSI_UTIL_SIGN_SET
:
1071 res
= bld_insn_1(bld
, NV_OP_ABS_F32
, res
);
1072 res
= bld_insn_1(bld
, NV_OP_NEG_F32
, res
);
1075 NOUVEAU_ERR("illegal/unhandled src reg sign mode\n");
1084 bld_lit(struct bld_context
*bld
, struct nv_value
*dst0
[4],
1085 const struct tgsi_full_instruction
*insn
)
1087 struct nv_value
*val0
= NULL
;
1088 unsigned mask
= insn
->Dst
[0].Register
.WriteMask
;
1090 if (mask
& ((1 << 0) | (1 << 3)))
1091 dst0
[3] = dst0
[0] = bld_load_imm_f32(bld
, 1.0f
);
1093 if (mask
& (3 << 1)) {
1094 val0
= bld_insn_2(bld
, NV_OP_MAX
, emit_fetch(bld
, insn
, 0, 0), bld
->zero
);
1095 if (mask
& (1 << 1))
1099 if (mask
& (1 << 2)) {
1100 struct nv_value
*val1
, *val3
, *src1
, *src3
, *pred
;
1101 struct nv_value
*pos128
= bld_load_imm_f32(bld
, 127.999999f
);
1102 struct nv_value
*neg128
= bld_load_imm_f32(bld
, -127.999999f
);
1104 src1
= emit_fetch(bld
, insn
, 0, 1);
1105 src3
= emit_fetch(bld
, insn
, 0, 3);
1107 pred
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LE
, val0
, bld
->zero
);
1109 val1
= bld_insn_2(bld
, NV_OP_MAX_F32
, src1
, bld
->zero
);
1110 val3
= bld_insn_2(bld
, NV_OP_MAX_F32
, src3
, neg128
);
1111 val3
= bld_insn_2(bld
, NV_OP_MIN_F32
, val3
, pos128
);
1112 val3
= bld_pow(bld
, val1
, val3
);
1114 dst0
[2] = bld_insn_1(bld
, NV_OP_MOV
, bld
->zero
);
1115 bld_src_predicate(bld
, dst0
[2]->insn
, 1, pred
);
1117 dst0
[2] = bld_insn_2(bld
, NV_OP_SELECT
, val3
, dst0
[2]);
1122 get_tex_dim(const struct tgsi_full_instruction
*insn
, int *dim
, int *arg
)
1124 switch (insn
->Texture
.Texture
) {
1125 case TGSI_TEXTURE_1D
:
1128 case TGSI_TEXTURE_SHADOW1D
:
1132 case TGSI_TEXTURE_UNKNOWN
:
1133 case TGSI_TEXTURE_2D
:
1134 case TGSI_TEXTURE_RECT
:
1137 case TGSI_TEXTURE_SHADOW2D
:
1138 case TGSI_TEXTURE_SHADOWRECT
:
1142 case TGSI_TEXTURE_3D
:
1143 case TGSI_TEXTURE_CUBE
:
1152 static struct nv_value
*
1153 bld_clone(struct bld_context
*bld
, struct nv_instruction
*nvi
)
1155 struct nv_instruction
*dupi
= new_instruction(bld
->pc
, nvi
->opcode
);
1156 struct nv_instruction
*next
, *prev
;
1167 for (c
= 0; c
< 5 && nvi
->def
[c
]; ++c
)
1168 bld_def(dupi
, c
, new_value_like(bld
->pc
, nvi
->def
[c
]));
1170 for (c
= 0; c
< 6 && nvi
->src
[c
]; ++c
) {
1171 dupi
->src
[c
] = NULL
;
1172 nv_reference(bld
->pc
, dupi
, c
, nvi
->src
[c
]->value
);
1175 return dupi
->def
[0];
1178 /* NOTE: proj(t0) = (t0 / w) / (tc3 / w) = tc0 / tc2 handled by optimizer */
1180 load_proj_tex_coords(struct bld_context
*bld
,
1181 struct nv_value
*t
[4], int dim
, int arg
,
1182 const struct tgsi_full_instruction
*insn
)
1185 unsigned mask
= (1 << dim
) - 1;
1188 mask
|= 4; /* depth comparison value */
1190 t
[3] = emit_fetch(bld
, insn
, 0, 3);
1191 if (t
[3]->insn
->opcode
== NV_OP_PINTERP
) {
1192 t
[3] = bld_clone(bld
, t
[3]->insn
);
1193 t
[3]->insn
->opcode
= NV_OP_LINTERP
;
1194 nv_reference(bld
->pc
, t
[3]->insn
, 1, NULL
);
1196 t
[3] = bld_insn_1(bld
, NV_OP_RCP
, t
[3]);
1198 for (c
= 0; c
< 4; ++c
) {
1199 if (!(mask
& (1 << c
)))
1201 t
[c
] = emit_fetch(bld
, insn
, 0, c
);
1203 if (t
[c
]->insn
->opcode
!= NV_OP_PINTERP
)
1207 t
[c
] = bld_clone(bld
, t
[c
]->insn
);
1208 nv_reference(bld
->pc
, t
[c
]->insn
, 1, t
[3]);
1213 t
[3] = emit_fetch(bld
, insn
, 0, 3);
1214 t
[3] = bld_insn_1(bld
, NV_OP_RCP
, t
[3]);
1216 for (c
= 0; c
< 4; ++c
)
1217 if (mask
& (1 << c
))
1218 t
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, t
[c
], t
[3]);
1221 /* For a quad of threads / top left, top right, bottom left, bottom right
1222 * pixels, do a different operation, and take src0 from a specific thread.
1229 #define QOP(a, b, c, d) \
1230 ((QOP_##a << 0) | (QOP_##b << 2) | (QOP_##c << 4) | (QOP_##d << 6))
1232 static INLINE
struct nv_value
*
1233 bld_quadop(struct bld_context
*bld
, ubyte qop
, struct nv_value
*src0
, int lane
,
1234 struct nv_value
*src1
, boolean wp
)
1236 struct nv_value
*val
= bld_insn_2(bld
, NV_OP_QUADOP
, src0
, src1
);
1237 val
->insn
->lanes
= lane
;
1238 val
->insn
->quadop
= qop
;
1240 assert(!"quadop predicate write");
1245 static struct nv_instruction
*
1246 emit_tex(struct bld_context
*bld
, uint opcode
,
1247 struct nv_value
*dst
[4], struct nv_value
*t_in
[4],
1248 int argc
, int tic
, int tsc
, int cube
)
1250 struct nv_value
*t
[4];
1251 struct nv_instruction
*nvi
;
1254 /* the inputs to a tex instruction must be separate values */
1255 for (c
= 0; c
< argc
; ++c
) {
1256 t
[c
] = bld_insn_1(bld
, NV_OP_MOV
, t_in
[c
]);
1257 t
[c
]->insn
->fixed
= 1;
1260 nvi
= new_instruction(bld
->pc
, opcode
);
1261 for (c
= 0; c
< 4; ++c
)
1262 dst
[c
] = bld_def(nvi
, c
, new_value(bld
->pc
, NV_FILE_GPR
, 4));
1263 for (c
= 0; c
< argc
; ++c
)
1264 nv_reference(bld
->pc
, nvi
, c
, t
[c
]);
1266 nvi
->ext
.tex
.t
= tic
;
1267 nvi
->ext
.tex
.s
= tsc
;
1268 nvi
->tex_mask
= 0xf;
1269 nvi
->tex_cube
= cube
;
1271 nvi
->tex_argc
= argc
;
1278 bld_is_constant(struct nv_value *val)
1280 if (val->reg.file == NV_FILE_IMM)
1282 return val->insn && nvCG_find_constant(val->insn->src[0]);
1287 bld_tex(struct bld_context
*bld
, struct nv_value
*dst0
[4],
1288 const struct tgsi_full_instruction
*insn
)
1290 struct nv_value
*t
[4], *s
[3];
1291 uint opcode
= translate_opcode(insn
->Instruction
.Opcode
);
1293 const int tic
= insn
->Src
[1].Register
.Index
;
1294 const int tsc
= tic
;
1295 const int cube
= (insn
->Texture
.Texture
== TGSI_TEXTURE_CUBE
) ? 1 : 0;
1297 get_tex_dim(insn
, &dim
, &arg
);
1299 if (!cube
&& insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1300 load_proj_tex_coords(bld
, t
, dim
, arg
, insn
);
1302 for (c
= 0; c
< dim
; ++c
)
1303 t
[c
] = emit_fetch(bld
, insn
, 0, c
);
1305 t
[dim
] = emit_fetch(bld
, insn
, 0, 2);
1310 for (c
= 0; c
< 3; ++c
)
1311 s
[c
] = bld_insn_1(bld
, NV_OP_ABS_F32
, t
[c
]);
1313 s
[0] = bld_insn_2(bld
, NV_OP_MAX_F32
, s
[0], s
[1]);
1314 s
[0] = bld_insn_2(bld
, NV_OP_MAX_F32
, s
[0], s
[2]);
1315 s
[0] = bld_insn_1(bld
, NV_OP_RCP
, s
[0]);
1317 for (c
= 0; c
< 3; ++c
)
1318 t
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, t
[c
], s
[0]);
1321 if (opcode
== NV_OP_TXB
|| opcode
== NV_OP_TXL
)
1322 t
[arg
++] = emit_fetch(bld
, insn
, 0, 3);
1323 emit_tex(bld
, opcode
, dst0
, t
, arg
, tic
, tsc
, cube
);
1326 static INLINE
struct nv_value
*
1327 bld_dot(struct bld_context
*bld
, const struct tgsi_full_instruction
*insn
,
1330 struct nv_value
*dotp
, *src0
, *src1
;
1333 src0
= emit_fetch(bld
, insn
, 0, 0);
1334 src1
= emit_fetch(bld
, insn
, 1, 0);
1335 dotp
= bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1337 for (c
= 1; c
< n
; ++c
) {
1338 src0
= emit_fetch(bld
, insn
, 0, c
);
1339 src1
= emit_fetch(bld
, insn
, 1, c
);
1340 dotp
= bld_insn_3(bld
, NV_OP_MAD_F32
, src0
, src1
, dotp
);
1345 #define FOR_EACH_DST0_ENABLED_CHANNEL(chan, inst) \
1346 for (chan = 0; chan < 4; ++chan) \
1347 if ((inst)->Dst[0].Register.WriteMask & (1 << chan))
1350 bld_instruction(struct bld_context
*bld
,
1351 const struct tgsi_full_instruction
*insn
)
1353 struct nv_value
*src0
;
1354 struct nv_value
*src1
;
1355 struct nv_value
*src2
;
1356 struct nv_value
*dst0
[4] = { NULL
};
1357 struct nv_value
*temp
;
1359 uint opcode
= translate_opcode(insn
->Instruction
.Opcode
);
1360 uint8_t mask
= insn
->Dst
[0].Register
.WriteMask
;
1362 #ifdef NOUVEAU_DEBUG_BITS
1363 debug_printf("bld_instruction:"); tgsi_dump_instruction(insn
, 1);
1366 switch (insn
->Instruction
.Opcode
) {
1367 case TGSI_OPCODE_ADD
:
1368 case TGSI_OPCODE_MAX
:
1369 case TGSI_OPCODE_MIN
:
1370 case TGSI_OPCODE_MUL
:
1371 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1372 src0
= emit_fetch(bld
, insn
, 0, c
);
1373 src1
= emit_fetch(bld
, insn
, 1, c
);
1374 dst0
[c
] = bld_insn_2(bld
, opcode
, src0
, src1
);
1377 case TGSI_OPCODE_ARL
:
1378 src1
= bld_imm_u32(bld
, 4);
1379 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1380 src0
= emit_fetch(bld
, insn
, 0, c
);
1381 src0
= bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1382 src0
->insn
->ext
.cvt
.d
= NV_TYPE_S32
;
1383 src0
->insn
->ext
.cvt
.s
= NV_TYPE_F32
;
1384 dst0
[c
] = bld_insn_2(bld
, NV_OP_SHL
, src0
, src1
);
1387 case TGSI_OPCODE_CMP
:
1388 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1389 src0
= emit_fetch(bld
, insn
, 0, c
);
1390 src0
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LT
, src0
, bld
->zero
);
1391 src1
= emit_fetch(bld
, insn
, 1, c
);
1392 src2
= emit_fetch(bld
, insn
, 2, c
);
1393 dst0
[c
] = bld_insn_3(bld
, NV_OP_SELP
, src1
, src2
, src0
);
1396 case TGSI_OPCODE_COS
:
1397 case TGSI_OPCODE_SIN
:
1398 src0
= emit_fetch(bld
, insn
, 0, 0);
1399 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1400 if (insn
->Dst
[0].Register
.WriteMask
& 7)
1401 temp
= bld_insn_1(bld
, opcode
, temp
);
1402 for (c
= 0; c
< 3; ++c
)
1403 if (insn
->Dst
[0].Register
.WriteMask
& (1 << c
))
1405 if (!(insn
->Dst
[0].Register
.WriteMask
& (1 << 3)))
1407 src0
= emit_fetch(bld
, insn
, 0, 3);
1408 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1409 dst0
[3] = bld_insn_1(bld
, opcode
, temp
);
1411 case TGSI_OPCODE_DP2
:
1412 temp
= bld_dot(bld
, insn
, 2);
1413 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1416 case TGSI_OPCODE_DP3
:
1417 temp
= bld_dot(bld
, insn
, 3);
1418 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1421 case TGSI_OPCODE_DP4
:
1422 temp
= bld_dot(bld
, insn
, 4);
1423 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1426 case TGSI_OPCODE_DPH
:
1427 src0
= bld_dot(bld
, insn
, 3);
1428 src1
= emit_fetch(bld
, insn
, 1, 3);
1429 temp
= bld_insn_2(bld
, NV_OP_ADD_F32
, src0
, src1
);
1430 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1433 case TGSI_OPCODE_DST
:
1434 if (insn
->Dst
[0].Register
.WriteMask
& 1)
1435 dst0
[0] = bld_imm_f32(bld
, 1.0f
);
1436 if (insn
->Dst
[0].Register
.WriteMask
& 2) {
1437 src0
= emit_fetch(bld
, insn
, 0, 1);
1438 src1
= emit_fetch(bld
, insn
, 1, 1);
1439 dst0
[1] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1441 if (insn
->Dst
[0].Register
.WriteMask
& 4)
1442 dst0
[2] = emit_fetch(bld
, insn
, 0, 2);
1443 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1444 dst0
[3] = emit_fetch(bld
, insn
, 1, 3);
1446 case TGSI_OPCODE_EXP
:
1447 src0
= emit_fetch(bld
, insn
, 0, 0);
1448 temp
= bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1450 if (insn
->Dst
[0].Register
.WriteMask
& 2)
1451 dst0
[1] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, temp
);
1452 if (insn
->Dst
[0].Register
.WriteMask
& 1) {
1453 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, temp
);
1454 dst0
[0] = bld_insn_1(bld
, NV_OP_EX2
, temp
);
1456 if (insn
->Dst
[0].Register
.WriteMask
& 4) {
1457 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, src0
);
1458 dst0
[2] = bld_insn_1(bld
, NV_OP_EX2
, temp
);
1460 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1461 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1463 case TGSI_OPCODE_EX2
:
1464 src0
= emit_fetch(bld
, insn
, 0, 0);
1465 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, src0
);
1466 temp
= bld_insn_1(bld
, NV_OP_EX2
, temp
);
1467 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1470 case TGSI_OPCODE_FRC
:
1471 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1472 src0
= emit_fetch(bld
, insn
, 0, c
);
1473 dst0
[c
] = bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1474 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, dst0
[c
]);
1477 case TGSI_OPCODE_KIL
:
1478 for (c
= 0; c
< 4; ++c
)
1479 bld_kil(bld
, emit_fetch(bld
, insn
, 0, c
));
1481 case TGSI_OPCODE_KILP
:
1482 (new_instruction(bld
->pc
, NV_OP_KIL
))->fixed
= 1;
1484 case TGSI_OPCODE_IF
:
1486 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1488 assert(bld
->cond_lvl
< BLD_MAX_COND_NESTING
);
1490 nvc0_bblock_attach(bld
->pc
->current_block
, b
, CFG_EDGE_FORWARD
);
1492 bld
->join_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1493 bld
->cond_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1495 src1
= bld_setp(bld
, NV_OP_SET_U32
, NV_CC_NE
,
1496 emit_fetch(bld
, insn
, 0, 0), bld
->zero
);
1498 bld_flow(bld
, NV_OP_BRA
, src1
, NULL
, (bld
->cond_lvl
== 0));
1501 bld_new_block(bld
, b
);
1504 case TGSI_OPCODE_ELSE
:
1506 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1509 nvc0_bblock_attach(bld
->join_bb
[bld
->cond_lvl
], b
, CFG_EDGE_FORWARD
);
1511 bld
->cond_bb
[bld
->cond_lvl
]->exit
->target
= b
;
1512 bld
->cond_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1514 new_instruction(bld
->pc
, NV_OP_BRA
)->terminator
= 1;
1517 bld_new_block(bld
, b
);
1520 case TGSI_OPCODE_ENDIF
:
1522 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1525 nvc0_bblock_attach(bld
->pc
->current_block
, b
, bld
->out_kind
);
1526 nvc0_bblock_attach(bld
->cond_bb
[bld
->cond_lvl
], b
, CFG_EDGE_FORWARD
);
1528 bld
->cond_bb
[bld
->cond_lvl
]->exit
->target
= b
;
1530 bld_new_block(bld
, b
);
1532 if (!bld
->cond_lvl
&& bld
->join_bb
[bld
->cond_lvl
]) {
1533 bld
->join_bb
[bld
->cond_lvl
]->exit
->prev
->target
= b
;
1534 new_instruction(bld
->pc
, NV_OP_JOIN
)->join
= 1;
1538 case TGSI_OPCODE_BGNLOOP
:
1540 struct nv_basic_block
*bl
= new_basic_block(bld
->pc
);
1541 struct nv_basic_block
*bb
= new_basic_block(bld
->pc
);
1543 assert(bld
->loop_lvl
< BLD_MAX_LOOP_NESTING
);
1545 bld
->loop_bb
[bld
->loop_lvl
] = bl
;
1546 bld
->brkt_bb
[bld
->loop_lvl
] = bb
;
1548 nvc0_bblock_attach(bld
->pc
->current_block
, bl
, CFG_EDGE_LOOP_ENTER
);
1550 bld_new_block(bld
, bld
->loop_bb
[bld
->loop_lvl
++]);
1552 if (bld
->loop_lvl
== bld
->pc
->loop_nesting_bound
)
1553 bld
->pc
->loop_nesting_bound
++;
1555 bld_clear_def_use(&bld
->tvs
[0][0], BLD_MAX_TEMPS
, bld
->loop_lvl
);
1556 bld_clear_def_use(&bld
->avs
[0][0], BLD_MAX_ADDRS
, bld
->loop_lvl
);
1557 bld_clear_def_use(&bld
->pvs
[0][0], BLD_MAX_PREDS
, bld
->loop_lvl
);
1560 case TGSI_OPCODE_BRK
:
1562 struct nv_basic_block
*bb
= bld
->brkt_bb
[bld
->loop_lvl
- 1];
1564 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1566 if (bld
->out_kind
== CFG_EDGE_FORWARD
) /* else we already had BRK/CONT */
1567 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_LOOP_LEAVE
);
1569 bld
->out_kind
= CFG_EDGE_FAKE
;
1572 case TGSI_OPCODE_CONT
:
1574 struct nv_basic_block
*bb
= bld
->loop_bb
[bld
->loop_lvl
- 1];
1576 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1578 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_BACK
);
1580 if ((bb
= bld
->join_bb
[bld
->cond_lvl
- 1])) {
1581 bld
->join_bb
[bld
->cond_lvl
- 1] = NULL
;
1582 nvc0_insn_delete(bb
->exit
->prev
);
1584 bld
->out_kind
= CFG_EDGE_FAKE
;
1587 case TGSI_OPCODE_ENDLOOP
:
1589 struct nv_basic_block
*bb
= bld
->loop_bb
[bld
->loop_lvl
- 1];
1591 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1593 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_BACK
);
1595 bld_loop_end(bld
, bb
); /* replace loop-side operand of the phis */
1597 bld_new_block(bld
, bld
->brkt_bb
[--bld
->loop_lvl
]);
1600 case TGSI_OPCODE_ABS
:
1601 case TGSI_OPCODE_CEIL
:
1602 case TGSI_OPCODE_FLR
:
1603 case TGSI_OPCODE_TRUNC
:
1604 case TGSI_OPCODE_DDX
:
1605 case TGSI_OPCODE_DDY
:
1606 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1607 src0
= emit_fetch(bld
, insn
, 0, c
);
1608 dst0
[c
] = bld_insn_1(bld
, opcode
, src0
);
1611 case TGSI_OPCODE_LIT
:
1612 bld_lit(bld
, dst0
, insn
);
1614 case TGSI_OPCODE_LRP
:
1615 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1616 src0
= emit_fetch(bld
, insn
, 0, c
);
1617 src1
= emit_fetch(bld
, insn
, 1, c
);
1618 src2
= emit_fetch(bld
, insn
, 2, c
);
1619 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src1
, src2
);
1620 dst0
[c
] = bld_insn_3(bld
, NV_OP_MAD_F32
, dst0
[c
], src0
, src2
);
1623 case TGSI_OPCODE_MOV
:
1624 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1625 dst0
[c
] = emit_fetch(bld
, insn
, 0, c
);
1627 case TGSI_OPCODE_MAD
:
1628 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1629 src0
= emit_fetch(bld
, insn
, 0, c
);
1630 src1
= emit_fetch(bld
, insn
, 1, c
);
1631 src2
= emit_fetch(bld
, insn
, 2, c
);
1632 dst0
[c
] = bld_insn_3(bld
, opcode
, src0
, src1
, src2
);
1635 case TGSI_OPCODE_POW
:
1636 src0
= emit_fetch(bld
, insn
, 0, 0);
1637 src1
= emit_fetch(bld
, insn
, 1, 0);
1638 temp
= bld_pow(bld
, src0
, src1
);
1639 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1642 case TGSI_OPCODE_LOG
:
1643 src0
= emit_fetch(bld
, insn
, 0, 0);
1644 src0
= bld_insn_1(bld
, NV_OP_ABS_F32
, src0
);
1645 temp
= bld_insn_1(bld
, NV_OP_LG2
, src0
);
1647 if (insn
->Dst
[0].Register
.WriteMask
& 3) {
1648 temp
= bld_insn_1(bld
, NV_OP_FLOOR
, temp
);
1651 if (insn
->Dst
[0].Register
.WriteMask
& 2) {
1652 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, temp
);
1653 temp
= bld_insn_1(bld
, NV_OP_EX2
, temp
);
1654 temp
= bld_insn_1(bld
, NV_OP_RCP
, temp
);
1655 dst0
[1] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, temp
);
1657 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1658 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1660 case TGSI_OPCODE_RCP
:
1661 case TGSI_OPCODE_LG2
:
1662 src0
= emit_fetch(bld
, insn
, 0, 0);
1663 temp
= bld_insn_1(bld
, opcode
, src0
);
1664 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1667 case TGSI_OPCODE_RSQ
:
1668 src0
= emit_fetch(bld
, insn
, 0, 0);
1669 temp
= bld_insn_1(bld
, NV_OP_ABS_F32
, src0
);
1670 temp
= bld_insn_1(bld
, NV_OP_RSQ
, temp
);
1671 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1674 case TGSI_OPCODE_SLT
:
1675 case TGSI_OPCODE_SGE
:
1676 case TGSI_OPCODE_SEQ
:
1677 case TGSI_OPCODE_SGT
:
1678 case TGSI_OPCODE_SLE
:
1679 case TGSI_OPCODE_SNE
:
1680 case TGSI_OPCODE_ISLT
:
1681 case TGSI_OPCODE_ISGE
:
1682 case TGSI_OPCODE_USEQ
:
1683 case TGSI_OPCODE_USGE
:
1684 case TGSI_OPCODE_USLT
:
1685 case TGSI_OPCODE_USNE
:
1686 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1687 src0
= emit_fetch(bld
, insn
, 0, c
);
1688 src1
= emit_fetch(bld
, insn
, 1, c
);
1689 dst0
[c
] = bld_insn_2(bld
, opcode
, src0
, src1
);
1690 dst0
[c
]->insn
->set_cond
= translate_setcc(insn
->Instruction
.Opcode
);
1693 case TGSI_OPCODE_SCS
:
1694 if (insn
->Dst
[0].Register
.WriteMask
& 0x3) {
1695 src0
= emit_fetch(bld
, insn
, 0, 0);
1696 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1697 if (insn
->Dst
[0].Register
.WriteMask
& 0x1)
1698 dst0
[0] = bld_insn_1(bld
, NV_OP_COS
, temp
);
1699 if (insn
->Dst
[0].Register
.WriteMask
& 0x2)
1700 dst0
[1] = bld_insn_1(bld
, NV_OP_SIN
, temp
);
1702 if (insn
->Dst
[0].Register
.WriteMask
& 0x4)
1703 dst0
[2] = bld_imm_f32(bld
, 0.0f
);
1704 if (insn
->Dst
[0].Register
.WriteMask
& 0x8)
1705 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1707 case TGSI_OPCODE_SSG
:
1708 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) { /* XXX: set lt, set gt, sub */
1709 src0
= emit_fetch(bld
, insn
, 0, c
);
1710 src1
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_EQ
, src0
, bld
->zero
);
1711 temp
= bld_insn_2(bld
, NV_OP_AND
, src0
, bld_imm_u32(bld
, 0x80000000));
1712 temp
= bld_insn_2(bld
, NV_OP_OR
, temp
, bld_imm_f32(bld
, 1.0f
));
1713 dst0
[c
] = bld_insn_1(bld
, NV_OP_MOV
, temp
);
1714 bld_src_predicate(bld
, dst0
[c
]->insn
, 1, src1
);
1717 case TGSI_OPCODE_SUB
:
1718 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1719 src0
= emit_fetch(bld
, insn
, 0, c
);
1720 src1
= emit_fetch(bld
, insn
, 1, c
);
1721 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, src1
);
1724 case TGSI_OPCODE_TEX
:
1725 case TGSI_OPCODE_TXB
:
1726 case TGSI_OPCODE_TXL
:
1727 case TGSI_OPCODE_TXP
:
1728 bld_tex(bld
, dst0
, insn
);
1730 case TGSI_OPCODE_XPD
:
1731 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1733 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1736 src0
= emit_fetch(bld
, insn
, 1, (c
+ 1) % 3);
1737 src1
= emit_fetch(bld
, insn
, 0, (c
+ 2) % 3);
1738 dst0
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1740 src0
= emit_fetch(bld
, insn
, 0, (c
+ 1) % 3);
1741 src1
= emit_fetch(bld
, insn
, 1, (c
+ 2) % 3);
1742 dst0
[c
] = bld_insn_3(bld
, NV_OP_MAD_F32
, src0
, src1
, dst0
[c
]);
1744 dst0
[c
]->insn
->src
[2]->mod
^= NV_MOD_NEG
;
1747 case TGSI_OPCODE_RET
:
1748 (new_instruction(bld
->pc
, NV_OP_RET
))->fixed
= 1;
1750 case TGSI_OPCODE_END
:
1751 /* VP outputs are exported in-place as scalars, optimization later */
1752 if (bld
->pc
->is_fragprog
)
1753 bld_export_fp_outputs(bld
);
1756 NOUVEAU_ERR("unhandled opcode %u\n", insn
->Instruction
.Opcode
);
1761 if (insn
->Dst
[0].Register
.File
== TGSI_FILE_OUTPUT
&&
1762 !bld
->pc
->is_fragprog
) {
1763 struct nv_instruction
*mi
= NULL
;
1766 for (c
= 0; c
< 4; ++c
)
1767 if ((mask
& (1 << c
)) &&
1768 ((dst0
[c
]->reg
.file
== NV_FILE_IMM
) ||
1769 (dst0
[c
]->reg
.id
== 63 && dst0
[c
]->reg
.file
== NV_FILE_GPR
)))
1770 dst0
[c
] = bld_insn_1(bld
, NV_OP_MOV
, dst0
[c
]);
1773 if ((mask
& 0x3) == 0x3) {
1776 mi
= bld_insn_2(bld
, NV_OP_BIND
, dst0
[0], dst0
[1])->insn
;
1778 if ((mask
& 0xc) == 0xc) {
1782 nv_reference(bld
->pc
, mi
, 2, dst0
[2]);
1783 nv_reference(bld
->pc
, mi
, 3, dst0
[3]);
1787 mi
= bld_insn_2(bld
, NV_OP_BIND
, dst0
[2], dst0
[3])->insn
;
1790 if (mi
&& (mask
& 0x4)) {
1793 nv_reference(bld
->pc
, mi
, 2, dst0
[2]);
1797 struct nv_instruction
*ex
= new_instruction(bld
->pc
, NV_OP_EXPORT
);
1800 nv_reference(bld
->pc
, ex
, 0, new_value(bld
->pc
, NV_FILE_MEM_V
, 4));
1801 nv_reference(bld
->pc
, ex
, 1, mi
->def
[0]);
1803 for (s
= 1; s
< size
/ 4; ++s
) {
1804 bld_def(mi
, s
, new_value(bld
->pc
, NV_FILE_GPR
, 4));
1805 nv_reference(bld
->pc
, ex
, s
+ 1, mi
->def
[s
]);
1809 ex
->src
[0]->value
->reg
.size
= size
;
1810 ex
->src
[0]->value
->reg
.address
=
1811 bld
->ti
->output_loc
[insn
->Dst
[0].Register
.Index
][c
];
1815 for (c
= 0; c
< 4; ++c
)
1816 if (mask
& (1 << c
))
1817 emit_store(bld
, insn
, c
, dst0
[c
]);
1821 bld_free_registers(struct bld_register
*base
, int n
)
1825 for (i
= 0; i
< n
; ++i
)
1826 for (c
= 0; c
< 4; ++c
)
1827 util_dynarray_fini(&base
[i
* 4 + c
].vals
);
1831 nvc0_tgsi_to_nc(struct nv_pc
*pc
, struct nvc0_translation_info
*ti
)
1833 struct bld_context
*bld
= CALLOC_STRUCT(bld_context
);
1836 pc
->root
[0] = pc
->current_block
= new_basic_block(pc
);
1841 pc
->loop_nesting_bound
= 1;
1843 bld
->zero
= new_value(pc
, NV_FILE_GPR
, 4);
1844 bld
->zero
->reg
.id
= 63;
1846 if (pc
->is_fragprog
) {
1847 struct nv_value
*mem
= new_value(pc
, NV_FILE_MEM_V
, 4);
1848 mem
->reg
.address
= 0x7c;
1850 bld
->frag_coord
[3] = bld_insn_1(bld
, NV_OP_LINTERP
, mem
);
1851 bld
->frag_coord
[3] = bld_insn_1(bld
, NV_OP_RCP
, bld
->frag_coord
[3]);
1854 for (ip
= 0; ip
< ti
->num_insns
; ++ip
)
1855 bld_instruction(bld
, &ti
->insns
[ip
]);
1857 bld_free_registers(&bld
->tvs
[0][0], BLD_MAX_TEMPS
);
1858 bld_free_registers(&bld
->avs
[0][0], BLD_MAX_ADDRS
);
1859 bld_free_registers(&bld
->pvs
[0][0], BLD_MAX_PREDS
);
1860 bld_free_registers(&bld
->ovs
[0][0], PIPE_MAX_SHADER_OUTPUTS
);
1866 /* If a variable is assigned in a loop, replace all references to the value
1867 * from outside the loop with a phi value.
1870 bld_replace_value(struct nv_pc
*pc
, struct nv_basic_block
*b
,
1871 struct nv_value
*old_val
,
1872 struct nv_value
*new_val
)
1874 struct nv_instruction
*nvi
;
1876 for (nvi
= b
->phi
? b
->phi
: b
->entry
; nvi
; nvi
= nvi
->next
) {
1878 for (s
= 0; s
< 6 && nvi
->src
[s
]; ++s
)
1879 if (nvi
->src
[s
]->value
== old_val
)
1880 nv_reference(pc
, nvi
, s
, new_val
);
1883 b
->pass_seq
= pc
->pass_seq
;
1885 if (b
->out
[0] && b
->out
[0]->pass_seq
< pc
->pass_seq
)
1886 bld_replace_value(pc
, b
->out
[0], old_val
, new_val
);
1888 if (b
->out
[1] && b
->out
[1]->pass_seq
< pc
->pass_seq
)
1889 bld_replace_value(pc
, b
->out
[1], old_val
, new_val
);