1 #include "pipe/p_context.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_state.h"
4 #include "util/u_linkage.h"
5 #include "util/u_debug.h"
7 #include "pipe/p_shader_tokens.h"
8 #include "tgsi/tgsi_parse.h"
9 #include "tgsi/tgsi_dump.h"
10 #include "tgsi/tgsi_util.h"
11 #include "tgsi/tgsi_ureg.h"
13 #include "draw/draw_context.h"
15 #include "nv30-40_3d.xml.h"
16 #include "nv30_context.h"
17 #include "nv30_resource.h"
19 /* TODO (at least...):
20 * 1. Indexed consts + ARL
21 * 3. NV_vp11, NV_vp2, NV_vp3 features
22 * - extra arith opcodes
30 #include "nv30_vertprog.h"
31 #include "nv40_vertprog.h"
33 struct nvfx_loop_entry
{
39 struct nv30_context
* nv30
;
40 struct pipe_shader_state pipe
;
41 struct nv30_vertprog
*vp
;
42 struct tgsi_shader_info
* info
;
44 struct nv30_vertprog_exec
*vpi
;
47 unsigned r_temps_discard
;
48 struct nvfx_reg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
49 struct nvfx_reg
*r_address
;
50 struct nvfx_reg
*r_temp
;
51 struct nvfx_reg
*r_const
;
52 struct nvfx_reg r_0_1
;
60 struct util_dynarray label_relocs
;
61 struct util_dynarray loop_stack
;
64 static struct nvfx_reg
65 temp(struct nvfx_vpc
*vpc
)
67 int idx
= ffs(~vpc
->r_temps
) - 1;
70 NOUVEAU_ERR("out of temps!!\n");
72 return nvfx_reg(NVFXSR_TEMP
, 0);
75 vpc
->r_temps
|= (1 << idx
);
76 vpc
->r_temps_discard
|= (1 << idx
);
77 return nvfx_reg(NVFXSR_TEMP
, idx
);
81 release_temps(struct nvfx_vpc
*vpc
)
83 vpc
->r_temps
&= ~vpc
->r_temps_discard
;
84 vpc
->r_temps_discard
= 0;
87 static struct nvfx_reg
88 constant(struct nvfx_vpc
*vpc
, int pipe
, float x
, float y
, float z
, float w
)
90 struct nv30_vertprog
*vp
= vpc
->vp
;
91 struct nv30_vertprog_data
*vpd
;
95 for (idx
= 0; idx
< vp
->nr_consts
; idx
++) {
96 if (vp
->consts
[idx
].index
== pipe
)
97 return nvfx_reg(NVFXSR_CONST
, idx
);
101 idx
= vp
->nr_consts
++;
102 vp
->consts
= realloc(vp
->consts
, sizeof(*vpd
) * vp
->nr_consts
);
103 vpd
= &vp
->consts
[idx
];
110 return nvfx_reg(NVFXSR_CONST
, idx
);
113 #define arith(s,t,o,d,m,s0,s1,s2) \
114 nvfx_insn((s), (NVFX_VP_INST_SLOT_##t << 7) | NVFX_VP_INST_##t##_OP_##o, -1, (d), (m), (s0), (s1), (s2))
117 emit_src(struct nv30_context
*nv30
, struct nvfx_vpc
*vpc
, uint32_t *hw
,
118 int pos
, struct nvfx_src src
)
120 struct nv30_vertprog
*vp
= vpc
->vp
;
122 struct nvfx_relocation reloc
;
124 switch (src
.reg
.type
) {
126 sr
|= (NVFX_VP(SRC_REG_TYPE_TEMP
) << NVFX_VP(SRC_REG_TYPE_SHIFT
));
127 sr
|= (src
.reg
.index
<< NVFX_VP(SRC_TEMP_SRC_SHIFT
));
130 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
131 NVFX_VP(SRC_REG_TYPE_SHIFT
));
132 vp
->ir
|= (1 << src
.reg
.index
);
133 hw
[1] |= (src
.reg
.index
<< NVFX_VP(INST_INPUT_SRC_SHIFT
));
136 sr
|= (NVFX_VP(SRC_REG_TYPE_CONST
) <<
137 NVFX_VP(SRC_REG_TYPE_SHIFT
));
138 if (src
.reg
.index
< 512) {
139 reloc
.location
= vp
->nr_insns
- 1;
140 reloc
.target
= src
.reg
.index
;
141 util_dynarray_append(&vp
->const_relocs
, struct nvfx_relocation
, reloc
);
143 hw
[1] |= (src
.reg
.index
- 512) << NVFX_VP(INST_CONST_SRC_SHIFT
);
147 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
148 NVFX_VP(SRC_REG_TYPE_SHIFT
));
155 sr
|= NVFX_VP(SRC_NEGATE
);
158 hw
[0] |= (1 << (21 + pos
));
160 sr
|= ((src
.swz
[0] << NVFX_VP(SRC_SWZ_X_SHIFT
)) |
161 (src
.swz
[1] << NVFX_VP(SRC_SWZ_Y_SHIFT
)) |
162 (src
.swz
[2] << NVFX_VP(SRC_SWZ_Z_SHIFT
)) |
163 (src
.swz
[3] << NVFX_VP(SRC_SWZ_W_SHIFT
)));
166 if(src
.reg
.type
== NVFXSR_CONST
)
167 hw
[3] |= NVFX_VP(INST_INDEX_CONST
);
168 else if(src
.reg
.type
== NVFXSR_INPUT
)
169 hw
[0] |= NVFX_VP(INST_INDEX_INPUT
);
173 hw
[0] |= NVFX_VP(INST_ADDR_REG_SELECT_1
);
174 hw
[0] |= src
.indirect_swz
<< NVFX_VP(INST_ADDR_SWZ_SHIFT
);
179 hw
[1] |= ((sr
& NVFX_VP(SRC0_HIGH_MASK
)) >>
180 NVFX_VP(SRC0_HIGH_SHIFT
)) << NVFX_VP(INST_SRC0H_SHIFT
);
181 hw
[2] |= (sr
& NVFX_VP(SRC0_LOW_MASK
)) <<
182 NVFX_VP(INST_SRC0L_SHIFT
);
185 hw
[2] |= sr
<< NVFX_VP(INST_SRC1_SHIFT
);
188 hw
[2] |= ((sr
& NVFX_VP(SRC2_HIGH_MASK
)) >>
189 NVFX_VP(SRC2_HIGH_SHIFT
)) << NVFX_VP(INST_SRC2H_SHIFT
);
190 hw
[3] |= (sr
& NVFX_VP(SRC2_LOW_MASK
)) <<
191 NVFX_VP(INST_SRC2L_SHIFT
);
199 emit_dst(struct nv30_context
*nv30
, struct nvfx_vpc
*vpc
, uint32_t *hw
,
200 int slot
, struct nvfx_reg dst
)
202 struct nv30_vertprog
*vp
= vpc
->vp
;
207 hw
[0] |= NV30_VP_INST_DEST_TEMP_ID_MASK
;
209 hw
[3] |= NV40_VP_INST_DEST_MASK
;
211 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
213 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
218 hw
[0] |= (dst
.index
<< NV30_VP_INST_DEST_TEMP_ID_SHIFT
);
220 hw
[3] |= NV40_VP_INST_DEST_MASK
;
222 hw
[0] |= (dst
.index
<< NV40_VP_INST_VEC_DEST_TEMP_SHIFT
);
224 hw
[3] |= (dst
.index
<< NV40_VP_INST_SCA_DEST_TEMP_SHIFT
);
228 /* TODO: this may be wrong because on nv30 COL0 and BFC0 are swapped */
231 case NV30_VP_INST_DEST_CLP(0):
232 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
235 case NV30_VP_INST_DEST_CLP(1):
236 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
239 case NV30_VP_INST_DEST_CLP(2):
240 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
243 case NV30_VP_INST_DEST_CLP(3):
244 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
247 case NV30_VP_INST_DEST_CLP(4):
248 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
251 case NV30_VP_INST_DEST_CLP(5):
252 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
255 case NV40_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
256 case NV40_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
257 case NV40_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
258 case NV40_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
259 case NV40_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
260 case NV40_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
265 hw
[3] |= (dst
.index
<< NV30_VP_INST_DEST_SHIFT
);
266 hw
[0] |= NV30_VP_INST_VEC_DEST_TEMP_MASK
;
268 /*XXX: no way this is entirely correct, someone needs to
269 * figure out what exactly it is.
273 hw
[3] |= (dst
.index
<< NV40_VP_INST_DEST_SHIFT
);
275 hw
[0] |= NV40_VP_INST_VEC_RESULT
;
276 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
278 hw
[3] |= NV40_VP_INST_SCA_RESULT
;
279 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
289 nvfx_vp_emit(struct nvfx_vpc
*vpc
, struct nvfx_insn insn
)
291 struct nv30_context
*nv30
= vpc
->nv30
;
292 struct nv30_vertprog
*vp
= vpc
->vp
;
293 unsigned slot
= insn
.op
>> 7;
294 unsigned op
= insn
.op
& 0x7f;
297 vp
->insns
= realloc(vp
->insns
, ++vp
->nr_insns
* sizeof(*vpc
->vpi
));
298 vpc
->vpi
= &vp
->insns
[vp
->nr_insns
- 1];
299 memset(vpc
->vpi
, 0, sizeof(*vpc
->vpi
));
303 if (insn
.cc_test
!= NVFX_COND_TR
)
304 hw
[0] |= NVFX_VP(INST_COND_TEST_ENABLE
);
305 hw
[0] |= (insn
.cc_test
<< NVFX_VP(INST_COND_SHIFT
));
306 hw
[0] |= ((insn
.cc_swz
[0] << NVFX_VP(INST_COND_SWZ_X_SHIFT
)) |
307 (insn
.cc_swz
[1] << NVFX_VP(INST_COND_SWZ_Y_SHIFT
)) |
308 (insn
.cc_swz
[2] << NVFX_VP(INST_COND_SWZ_Z_SHIFT
)) |
309 (insn
.cc_swz
[3] << NVFX_VP(INST_COND_SWZ_W_SHIFT
)));
311 hw
[0] |= NVFX_VP(INST_COND_UPDATE_ENABLE
);
314 assert(nv30
->use_nv4x
);
316 hw
[0] |= NV40_VP_INST_SATURATE
;
321 hw
[1] |= (op
<< NV30_VP_INST_VEC_OPCODE_SHIFT
);
323 hw
[0] |= ((op
>> 4) << NV30_VP_INST_SCA_OPCODEH_SHIFT
);
324 hw
[1] |= ((op
& 0xf) << NV30_VP_INST_SCA_OPCODEL_SHIFT
);
326 // hw[3] |= NVFX_VP(INST_SCA_DEST_TEMP_MASK);
327 // hw[3] |= (mask << NVFX_VP(INST_VEC_WRITEMASK_SHIFT));
329 if (insn
.dst
.type
== NVFXSR_OUTPUT
) {
331 hw
[3] |= (insn
.mask
<< NV30_VP_INST_SDEST_WRITEMASK_SHIFT
);
333 hw
[3] |= (insn
.mask
<< NV30_VP_INST_VDEST_WRITEMASK_SHIFT
);
336 hw
[3] |= (insn
.mask
<< NV30_VP_INST_STEMP_WRITEMASK_SHIFT
);
338 hw
[3] |= (insn
.mask
<< NV30_VP_INST_VTEMP_WRITEMASK_SHIFT
);
342 hw
[1] |= (op
<< NV40_VP_INST_VEC_OPCODE_SHIFT
);
343 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
344 hw
[3] |= (insn
.mask
<< NV40_VP_INST_VEC_WRITEMASK_SHIFT
);
346 hw
[1] |= (op
<< NV40_VP_INST_SCA_OPCODE_SHIFT
);
347 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
348 hw
[3] |= (insn
.mask
<< NV40_VP_INST_SCA_WRITEMASK_SHIFT
);
352 emit_dst(nv30
, vpc
, hw
, slot
, insn
.dst
);
353 emit_src(nv30
, vpc
, hw
, 0, insn
.src
[0]);
354 emit_src(nv30
, vpc
, hw
, 1, insn
.src
[1]);
355 emit_src(nv30
, vpc
, hw
, 2, insn
.src
[2]);
357 // if(insn.src[0].indirect || op == NVFX_VP_INST_VEC_OP_ARL)
358 // hw[3] |= NV40_VP_INST_SCA_RESULT;
361 static inline struct nvfx_src
362 tgsi_src(struct nvfx_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
) {
365 switch (fsrc
->Register
.File
) {
366 case TGSI_FILE_INPUT
:
367 src
.reg
= nvfx_reg(NVFXSR_INPUT
, fsrc
->Register
.Index
);
369 case TGSI_FILE_CONSTANT
:
370 src
.reg
= vpc
->r_const
[fsrc
->Register
.Index
];
372 case TGSI_FILE_IMMEDIATE
:
373 src
.reg
= vpc
->imm
[fsrc
->Register
.Index
];
375 case TGSI_FILE_TEMPORARY
:
376 src
.reg
= vpc
->r_temp
[fsrc
->Register
.Index
];
379 NOUVEAU_ERR("bad src file\n");
385 src
.abs
= fsrc
->Register
.Absolute
;
386 src
.negate
= fsrc
->Register
.Negate
;
387 src
.swz
[0] = fsrc
->Register
.SwizzleX
;
388 src
.swz
[1] = fsrc
->Register
.SwizzleY
;
389 src
.swz
[2] = fsrc
->Register
.SwizzleZ
;
390 src
.swz
[3] = fsrc
->Register
.SwizzleW
;
392 src
.indirect_reg
= 0;
393 src
.indirect_swz
= 0;
395 if(fsrc
->Register
.Indirect
) {
396 if(fsrc
->Indirect
.File
== TGSI_FILE_ADDRESS
&&
397 (fsrc
->Register
.File
== TGSI_FILE_CONSTANT
||
398 fsrc
->Register
.File
== TGSI_FILE_INPUT
)) {
400 src
.indirect_reg
= fsrc
->Indirect
.Index
;
401 src
.indirect_swz
= fsrc
->Indirect
.SwizzleX
;
411 static INLINE
struct nvfx_reg
412 tgsi_dst(struct nvfx_vpc
*vpc
, const struct tgsi_full_dst_register
*fdst
) {
415 switch (fdst
->Register
.File
) {
417 dst
= nvfx_reg(NVFXSR_NONE
, 0);
419 case TGSI_FILE_OUTPUT
:
420 dst
= vpc
->r_result
[fdst
->Register
.Index
];
422 case TGSI_FILE_TEMPORARY
:
423 dst
= vpc
->r_temp
[fdst
->Register
.Index
];
425 case TGSI_FILE_ADDRESS
:
426 dst
= vpc
->r_address
[fdst
->Register
.Index
];
429 NOUVEAU_ERR("bad dst file %i\n", fdst
->Register
.File
);
443 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= NVFX_VP_MASK_X
;
444 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= NVFX_VP_MASK_Y
;
445 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= NVFX_VP_MASK_Z
;
446 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= NVFX_VP_MASK_W
;
451 nvfx_vertprog_parse_instruction(struct nv30_context
*nv30
, struct nvfx_vpc
*vpc
,
452 unsigned idx
, const struct tgsi_full_instruction
*finst
)
454 struct nvfx_src src
[3], tmp
;
456 struct nvfx_reg final_dst
;
457 struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
458 struct nvfx_insn insn
;
459 struct nvfx_relocation reloc
;
460 struct nvfx_loop_entry loop
;
463 int ai
= -1, ci
= -1, ii
= -1;
465 unsigned sub_depth
= 0;
467 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
468 const struct tgsi_full_src_register
*fsrc
;
470 fsrc
= &finst
->Src
[i
];
471 if (fsrc
->Register
.File
== TGSI_FILE_TEMPORARY
) {
472 src
[i
] = tgsi_src(vpc
, fsrc
);
476 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
477 const struct tgsi_full_src_register
*fsrc
;
479 fsrc
= &finst
->Src
[i
];
481 switch (fsrc
->Register
.File
) {
482 case TGSI_FILE_INPUT
:
483 if (ai
== -1 || ai
== fsrc
->Register
.Index
) {
484 ai
= fsrc
->Register
.Index
;
485 src
[i
] = tgsi_src(vpc
, fsrc
);
487 src
[i
] = nvfx_src(temp(vpc
));
488 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
489 tgsi_src(vpc
, fsrc
), none
, none
));
492 case TGSI_FILE_CONSTANT
:
493 if ((ci
== -1 && ii
== -1) ||
494 ci
== fsrc
->Register
.Index
) {
495 ci
= fsrc
->Register
.Index
;
496 src
[i
] = tgsi_src(vpc
, fsrc
);
498 src
[i
] = nvfx_src(temp(vpc
));
499 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
500 tgsi_src(vpc
, fsrc
), none
, none
));
503 case TGSI_FILE_IMMEDIATE
:
504 if ((ci
== -1 && ii
== -1) ||
505 ii
== fsrc
->Register
.Index
) {
506 ii
= fsrc
->Register
.Index
;
507 src
[i
] = tgsi_src(vpc
, fsrc
);
509 src
[i
] = nvfx_src(temp(vpc
));
510 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
511 tgsi_src(vpc
, fsrc
), none
, none
));
514 case TGSI_FILE_TEMPORARY
:
518 NOUVEAU_ERR("bad src file\n");
523 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
524 if(src
[i
].reg
.type
< 0)
528 if(finst
->Dst
[0].Register
.File
== TGSI_FILE_ADDRESS
&&
529 finst
->Instruction
.Opcode
!= TGSI_OPCODE_ARL
)
532 final_dst
= dst
= tgsi_dst(vpc
, &finst
->Dst
[0]);
533 mask
= tgsi_mask(finst
->Dst
[0].Register
.WriteMask
);
534 if(finst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
) {
535 assert(finst
->Instruction
.Opcode
!= TGSI_OPCODE_ARL
);
539 if(dst
.type
!= NVFXSR_TEMP
)
543 switch (finst
->Instruction
.Opcode
) {
544 case TGSI_OPCODE_ABS
:
545 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, abs(src
[0]), none
, none
));
547 case TGSI_OPCODE_ADD
:
548 nvfx_vp_emit(vpc
, arith(sat
, VEC
, ADD
, dst
, mask
, src
[0], none
, src
[1]));
550 case TGSI_OPCODE_ARL
:
551 nvfx_vp_emit(vpc
, arith(0, VEC
, ARL
, dst
, mask
, src
[0], none
, none
));
553 case TGSI_OPCODE_CEIL
:
554 tmp
= nvfx_src(temp(vpc
));
555 nvfx_vp_emit(vpc
, arith(0, VEC
, FLR
, tmp
.reg
, mask
, neg(src
[0]), none
, none
));
556 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, neg(tmp
), none
, none
));
558 case TGSI_OPCODE_CMP
:
559 insn
= arith(0, VEC
, MOV
, none
.reg
, mask
, src
[0], none
, none
);
561 nvfx_vp_emit(vpc
, insn
);
563 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, src
[2], none
, none
);
564 insn
.cc_test
= NVFX_COND_GE
;
565 nvfx_vp_emit(vpc
, insn
);
567 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, src
[1], none
, none
);
568 insn
.cc_test
= NVFX_COND_LT
;
569 nvfx_vp_emit(vpc
, insn
);
571 case TGSI_OPCODE_COS
:
572 nvfx_vp_emit(vpc
, arith(sat
, SCA
, COS
, dst
, mask
, none
, none
, src
[0]));
574 case TGSI_OPCODE_DP2
:
575 tmp
= nvfx_src(temp(vpc
));
576 nvfx_vp_emit(vpc
, arith(0, VEC
, MUL
, tmp
.reg
, NVFX_VP_MASK_X
| NVFX_VP_MASK_Y
, src
[0], src
[1], none
));
577 nvfx_vp_emit(vpc
, arith(sat
, VEC
, ADD
, dst
, mask
, swz(tmp
, X
, X
, X
, X
), none
, swz(tmp
, Y
, Y
, Y
, Y
)));
579 case TGSI_OPCODE_DP3
:
580 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DP3
, dst
, mask
, src
[0], src
[1], none
));
582 case TGSI_OPCODE_DP4
:
583 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DP4
, dst
, mask
, src
[0], src
[1], none
));
585 case TGSI_OPCODE_DPH
:
586 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DPH
, dst
, mask
, src
[0], src
[1], none
));
588 case TGSI_OPCODE_DST
:
589 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DST
, dst
, mask
, src
[0], src
[1], none
));
591 case TGSI_OPCODE_EX2
:
592 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EX2
, dst
, mask
, none
, none
, src
[0]));
594 case TGSI_OPCODE_EXP
:
595 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EXP
, dst
, mask
, none
, none
, src
[0]));
597 case TGSI_OPCODE_FLR
:
598 nvfx_vp_emit(vpc
, arith(sat
, VEC
, FLR
, dst
, mask
, src
[0], none
, none
));
600 case TGSI_OPCODE_FRC
:
601 nvfx_vp_emit(vpc
, arith(sat
, VEC
, FRC
, dst
, mask
, src
[0], none
, none
));
603 case TGSI_OPCODE_LG2
:
604 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LG2
, dst
, mask
, none
, none
, src
[0]));
606 case TGSI_OPCODE_LIT
:
607 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LIT
, dst
, mask
, none
, none
, src
[0]));
609 case TGSI_OPCODE_LOG
:
610 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LOG
, dst
, mask
, none
, none
, src
[0]));
612 case TGSI_OPCODE_LRP
:
613 tmp
= nvfx_src(temp(vpc
));
614 nvfx_vp_emit(vpc
, arith(0, VEC
, MAD
, tmp
.reg
, mask
, neg(src
[0]), src
[2], src
[2]));
615 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], tmp
));
617 case TGSI_OPCODE_MAD
:
618 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], src
[2]));
620 case TGSI_OPCODE_MAX
:
621 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAX
, dst
, mask
, src
[0], src
[1], none
));
623 case TGSI_OPCODE_MIN
:
624 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MIN
, dst
, mask
, src
[0], src
[1], none
));
626 case TGSI_OPCODE_MOV
:
627 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, src
[0], none
, none
));
629 case TGSI_OPCODE_MUL
:
630 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MUL
, dst
, mask
, src
[0], src
[1], none
));
632 case TGSI_OPCODE_NOP
:
634 case TGSI_OPCODE_POW
:
635 tmp
= nvfx_src(temp(vpc
));
636 nvfx_vp_emit(vpc
, arith(0, SCA
, LG2
, tmp
.reg
, NVFX_VP_MASK_X
, none
, none
, swz(src
[0], X
, X
, X
, X
)));
637 nvfx_vp_emit(vpc
, arith(0, VEC
, MUL
, tmp
.reg
, NVFX_VP_MASK_X
, swz(tmp
, X
, X
, X
, X
), swz(src
[1], X
, X
, X
, X
), none
));
638 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EX2
, dst
, mask
, none
, none
, swz(tmp
, X
, X
, X
, X
)));
640 case TGSI_OPCODE_RCP
:
641 nvfx_vp_emit(vpc
, arith(sat
, SCA
, RCP
, dst
, mask
, none
, none
, src
[0]));
643 case TGSI_OPCODE_RSQ
:
644 nvfx_vp_emit(vpc
, arith(sat
, SCA
, RSQ
, dst
, mask
, none
, none
, abs(src
[0])));
646 case TGSI_OPCODE_SEQ
:
647 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SEQ
, dst
, mask
, src
[0], src
[1], none
));
649 case TGSI_OPCODE_SFL
:
650 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SFL
, dst
, mask
, src
[0], src
[1], none
));
652 case TGSI_OPCODE_SGE
:
653 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SGE
, dst
, mask
, src
[0], src
[1], none
));
655 case TGSI_OPCODE_SGT
:
656 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SGT
, dst
, mask
, src
[0], src
[1], none
));
658 case TGSI_OPCODE_SIN
:
659 nvfx_vp_emit(vpc
, arith(sat
, SCA
, SIN
, dst
, mask
, none
, none
, src
[0]));
661 case TGSI_OPCODE_SLE
:
662 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SLE
, dst
, mask
, src
[0], src
[1], none
));
664 case TGSI_OPCODE_SLT
:
665 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SLT
, dst
, mask
, src
[0], src
[1], none
));
667 case TGSI_OPCODE_SNE
:
668 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SNE
, dst
, mask
, src
[0], src
[1], none
));
670 case TGSI_OPCODE_SSG
:
671 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SSG
, dst
, mask
, src
[0], src
[1], none
));
673 case TGSI_OPCODE_STR
:
674 nvfx_vp_emit(vpc
, arith(sat
, VEC
, STR
, dst
, mask
, src
[0], src
[1], none
));
676 case TGSI_OPCODE_SUB
:
677 nvfx_vp_emit(vpc
, arith(sat
, VEC
, ADD
, dst
, mask
, src
[0], none
, neg(src
[1])));
679 case TGSI_OPCODE_TRUNC
:
680 tmp
= nvfx_src(temp(vpc
));
681 insn
= arith(0, VEC
, MOV
, none
.reg
, mask
, src
[0], none
, none
);
683 nvfx_vp_emit(vpc
, insn
);
685 nvfx_vp_emit(vpc
, arith(0, VEC
, FLR
, tmp
.reg
, mask
, abs(src
[0]), none
, none
));
686 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, tmp
, none
, none
));
688 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, neg(tmp
), none
, none
);
689 insn
.cc_test
= NVFX_COND_LT
;
690 nvfx_vp_emit(vpc
, insn
);
692 case TGSI_OPCODE_XPD
:
693 tmp
= nvfx_src(temp(vpc
));
694 nvfx_vp_emit(vpc
, arith(0, VEC
, MUL
, tmp
.reg
, mask
, swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
));
695 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAD
, dst
, (mask
& ~NVFX_VP_MASK_W
), swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
), neg(tmp
)));
698 insn
= arith(0, VEC
, MOV
, none
.reg
, NVFX_VP_MASK_X
, src
[0], none
, none
);
700 nvfx_vp_emit(vpc
, insn
);
702 reloc
.location
= vpc
->vp
->nr_insns
;
703 reloc
.target
= finst
->Label
.Label
+ 1;
704 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
706 insn
= arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
);
707 insn
.cc_test
= NVFX_COND_EQ
;
708 insn
.cc_swz
[0] = insn
.cc_swz
[1] = insn
.cc_swz
[2] = insn
.cc_swz
[3] = 0;
709 nvfx_vp_emit(vpc
, insn
);
711 case TGSI_OPCODE_ELSE
:
712 case TGSI_OPCODE_BRA
:
713 case TGSI_OPCODE_CAL
:
714 reloc
.location
= vpc
->vp
->nr_insns
;
715 reloc
.target
= finst
->Label
.Label
;
716 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
718 if(finst
->Instruction
.Opcode
== TGSI_OPCODE_CAL
)
719 insn
= arith(0, SCA
, CAL
, none
.reg
, 0, none
, none
, none
);
721 insn
= arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
);
722 nvfx_vp_emit(vpc
, insn
);
724 case TGSI_OPCODE_RET
:
725 if(sub_depth
|| !vpc
->vp
->enabled_ucps
) {
727 tmp
.swz
[0] = tmp
.swz
[1] = tmp
.swz
[2] = tmp
.swz
[3] = 0;
728 nvfx_vp_emit(vpc
, arith(0, SCA
, RET
, none
.reg
, 0, none
, none
, tmp
));
730 reloc
.location
= vpc
->vp
->nr_insns
;
731 reloc
.target
= vpc
->info
->num_instructions
;
732 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
733 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
736 case TGSI_OPCODE_BGNSUB
:
739 case TGSI_OPCODE_ENDSUB
:
742 case TGSI_OPCODE_ENDIF
:
743 /* nothing to do here */
745 case TGSI_OPCODE_BGNLOOP
:
746 loop
.cont_target
= idx
;
747 loop
.brk_target
= finst
->Label
.Label
+ 1;
748 util_dynarray_append(&vpc
->loop_stack
, struct nvfx_loop_entry
, loop
);
750 case TGSI_OPCODE_ENDLOOP
:
751 loop
= util_dynarray_pop(&vpc
->loop_stack
, struct nvfx_loop_entry
);
753 reloc
.location
= vpc
->vp
->nr_insns
;
754 reloc
.target
= loop
.cont_target
;
755 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
757 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
759 case TGSI_OPCODE_CONT
:
760 loop
= util_dynarray_top(&vpc
->loop_stack
, struct nvfx_loop_entry
);
762 reloc
.location
= vpc
->vp
->nr_insns
;
763 reloc
.target
= loop
.cont_target
;
764 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
766 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
768 case TGSI_OPCODE_BRK
:
769 loop
= util_dynarray_top(&vpc
->loop_stack
, struct nvfx_loop_entry
);
771 reloc
.location
= vpc
->vp
->nr_insns
;
772 reloc
.target
= loop
.brk_target
;
773 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
775 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
777 case TGSI_OPCODE_END
:
779 if(vpc
->vp
->enabled_ucps
) {
780 if(idx
!= (vpc
->info
->num_instructions
- 1)) {
781 reloc
.location
= vpc
->vp
->nr_insns
;
782 reloc
.target
= vpc
->info
->num_instructions
;
783 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
784 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
787 if(vpc
->vp
->nr_insns
)
788 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
789 nvfx_vp_emit(vpc
, arith(0, VEC
, NOP
, none
.reg
, 0, none
, none
, none
));
790 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
794 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
798 if(finst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
&& !nv30
->use_nv4x
) {
799 if (!vpc
->r_0_1
.type
)
800 vpc
->r_0_1
= constant(vpc
, -1, 0, 1, 0, 0);
801 nvfx_vp_emit(vpc
, arith(0, VEC
, MAX
, dst
, mask
, nvfx_src(dst
), swz(nvfx_src(vpc
->r_0_1
), X
, X
, X
, X
), none
));
802 nvfx_vp_emit(vpc
, arith(0, VEC
, MIN
, final_dst
, mask
, nvfx_src(dst
), swz(nvfx_src(vpc
->r_0_1
), Y
, Y
, Y
, Y
), none
));
810 nvfx_vertprog_parse_decl_output(struct nv30_context
*nv30
, struct nvfx_vpc
*vpc
,
811 const struct tgsi_full_declaration
*fdec
)
813 unsigned num_texcoords
= nv30
->is_nv4x
? 10 : 8;
814 unsigned idx
= fdec
->Range
.First
;
817 switch (fdec
->Semantic
.Name
) {
818 case TGSI_SEMANTIC_POSITION
:
819 hw
= NVFX_VP(INST_DEST_POS
);
822 case TGSI_SEMANTIC_CLIPVERTEX
:
823 vpc
->r_result
[idx
] = temp(vpc
);
824 vpc
->r_temps_discard
= 0;
827 case TGSI_SEMANTIC_COLOR
:
828 if (fdec
->Semantic
.Index
== 0) {
829 hw
= NVFX_VP(INST_DEST_COL0
);
831 if (fdec
->Semantic
.Index
== 1) {
832 hw
= NVFX_VP(INST_DEST_COL1
);
834 NOUVEAU_ERR("bad colour semantic index\n");
838 case TGSI_SEMANTIC_BCOLOR
:
839 if (fdec
->Semantic
.Index
== 0) {
840 hw
= NVFX_VP(INST_DEST_BFC0
);
842 if (fdec
->Semantic
.Index
== 1) {
843 hw
= NVFX_VP(INST_DEST_BFC1
);
845 NOUVEAU_ERR("bad bcolour semantic index\n");
849 case TGSI_SEMANTIC_FOG
:
850 hw
= NVFX_VP(INST_DEST_FOGC
);
852 case TGSI_SEMANTIC_PSIZE
:
853 hw
= NVFX_VP(INST_DEST_PSZ
);
855 case TGSI_SEMANTIC_GENERIC
:
856 for (i
= 0; i
< num_texcoords
; i
++) {
857 if (vpc
->vp
->texcoord
[i
] == fdec
->Semantic
.Index
) {
858 hw
= NVFX_VP(INST_DEST_TC(i
));
863 if (i
== num_texcoords
) {
864 vpc
->r_result
[idx
] = nvfx_reg(NVFXSR_NONE
, 0);
868 case TGSI_SEMANTIC_EDGEFLAG
:
869 /* not really an error just a fallback */
870 NOUVEAU_ERR("cannot handle edgeflag output\n");
873 NOUVEAU_ERR("bad output semantic\n");
877 vpc
->r_result
[idx
] = nvfx_reg(NVFXSR_OUTPUT
, hw
);
882 nvfx_vertprog_prepare(struct nv30_context
*nv30
, struct nvfx_vpc
*vpc
)
884 struct tgsi_parse_context p
;
885 int high_const
= -1, high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
887 tgsi_parse_init(&p
, vpc
->pipe
.tokens
);
888 while (!tgsi_parse_end_of_tokens(&p
)) {
889 const union tgsi_full_token
*tok
= &p
.FullToken
;
891 tgsi_parse_token(&p
);
892 switch(tok
->Token
.Type
) {
893 case TGSI_TOKEN_TYPE_IMMEDIATE
:
896 case TGSI_TOKEN_TYPE_DECLARATION
:
898 const struct tgsi_full_declaration
*fdec
;
900 fdec
= &p
.FullToken
.FullDeclaration
;
901 switch (fdec
->Declaration
.File
) {
902 case TGSI_FILE_TEMPORARY
:
903 if (fdec
->Range
.Last
> high_temp
) {
908 case TGSI_FILE_ADDRESS
:
909 if (fdec
->Range
.Last
> high_addr
) {
914 case TGSI_FILE_CONSTANT
:
915 if (fdec
->Range
.Last
> high_const
) {
920 case TGSI_FILE_OUTPUT
:
921 if (!nvfx_vertprog_parse_decl_output(nv30
, vpc
, fdec
))
936 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nvfx_reg
));
941 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nvfx_reg
));
942 for (i
= 0; i
< high_temp
; i
++)
943 vpc
->r_temp
[i
] = temp(vpc
);
947 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nvfx_reg
));
948 for (i
= 0; i
< high_addr
; i
++)
949 vpc
->r_address
[i
] = nvfx_reg(NVFXSR_TEMP
, i
);
953 vpc
->r_const
= CALLOC(high_const
, sizeof(struct nvfx_reg
));
954 for (i
= 0; i
< high_const
; i
++)
955 vpc
->r_const
[i
] = constant(vpc
, i
, 0, 0, 0, 0);
958 vpc
->r_temps_discard
= 0;
962 DEBUG_GET_ONCE_BOOL_OPTION(nvfx_dump_vp
, "NVFX_DUMP_VP", FALSE
)
965 _nvfx_vertprog_translate(struct nv30_context
*nv30
, struct nv30_vertprog
*vp
)
967 struct tgsi_parse_context parse
;
968 struct nvfx_vpc
*vpc
= NULL
;
969 struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
970 struct util_dynarray insns
;
973 vp
->translated
= FALSE
;
977 vpc
= CALLOC_STRUCT(nvfx_vpc
);
982 vpc
->pipe
= vp
->pipe
;
983 vpc
->info
= &vp
->info
;
986 if (!nvfx_vertprog_prepare(nv30
, vpc
)) {
991 /* Redirect post-transform vertex position to a temp if user clip
992 * planes are enabled. We need to append code to the vtxprog
993 * to handle clip planes later.
995 if (vp
->enabled_ucps
&& vpc
->cvtx_idx
< 0) {
996 vpc
->r_result
[vpc
->hpos_idx
] = temp(vpc
);
997 vpc
->r_temps_discard
= 0;
998 vpc
->cvtx_idx
= vpc
->hpos_idx
;
1001 util_dynarray_init(&insns
);
1003 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
1004 while (!tgsi_parse_end_of_tokens(&parse
)) {
1005 tgsi_parse_token(&parse
);
1007 switch (parse
.FullToken
.Token
.Type
) {
1008 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1010 const struct tgsi_full_immediate
*imm
;
1012 imm
= &parse
.FullToken
.FullImmediate
;
1013 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
1014 assert(imm
->Immediate
.NrTokens
== 4 + 1);
1015 vpc
->imm
[vpc
->nr_imm
++] =
1023 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1025 const struct tgsi_full_instruction
*finst
;
1026 unsigned idx
= insns
.size
>> 2;
1027 util_dynarray_append(&insns
, unsigned, vp
->nr_insns
);
1028 finst
= &parse
.FullToken
.FullInstruction
;
1029 if (!nvfx_vertprog_parse_instruction(nv30
, vpc
, idx
, finst
))
1038 util_dynarray_append(&insns
, unsigned, vp
->nr_insns
);
1040 for(unsigned i
= 0; i
< vpc
->label_relocs
.size
; i
+= sizeof(struct nvfx_relocation
))
1042 struct nvfx_relocation
* label_reloc
= (struct nvfx_relocation
*)((char*)vpc
->label_relocs
.data
+ i
);
1043 struct nvfx_relocation hw_reloc
;
1045 hw_reloc
.location
= label_reloc
->location
;
1046 hw_reloc
.target
= ((unsigned*)insns
.data
)[label_reloc
->target
];
1048 //debug_printf("hw %u -> tgsi %u = hw %u\n", hw_reloc.location, label_reloc->target, hw_reloc.target);
1050 util_dynarray_append(&vp
->branch_relocs
, struct nvfx_relocation
, hw_reloc
);
1052 util_dynarray_fini(&insns
);
1053 util_dynarray_trim(&vp
->branch_relocs
);
1055 /* XXX: what if we add a RET before?! make sure we jump here...*/
1057 /* Write out HPOS if it was redirected to a temp earlier */
1058 if (vpc
->r_result
[vpc
->hpos_idx
].type
!= NVFXSR_OUTPUT
) {
1059 struct nvfx_reg hpos
= nvfx_reg(NVFXSR_OUTPUT
,
1060 NVFX_VP(INST_DEST_POS
));
1061 struct nvfx_src htmp
= nvfx_src(vpc
->r_result
[vpc
->hpos_idx
]);
1063 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, hpos
, NVFX_VP_MASK_ALL
, htmp
, none
, none
));
1066 /* Insert code to handle user clip planes */
1067 ucps
= vp
->enabled_ucps
;
1069 int i
= ffs(ucps
) - 1; ucps
&= ~(1 << i
);
1070 struct nvfx_reg cdst
= nvfx_reg(NVFXSR_OUTPUT
, NV30_VP_INST_DEST_CLP(i
));
1071 struct nvfx_src ceqn
= nvfx_src(nvfx_reg(NVFXSR_CONST
, 512 + i
));
1072 struct nvfx_src htmp
= nvfx_src(vpc
->r_result
[vpc
->cvtx_idx
]);
1078 case 0: case 3: mask
= NVFX_VP_MASK_Y
; break;
1079 case 1: case 4: mask
= NVFX_VP_MASK_Z
; break;
1080 case 2: case 5: mask
= NVFX_VP_MASK_W
; break;
1082 NOUVEAU_ERR("invalid clip dist #%d\n", i
);
1087 mask
= NVFX_VP_MASK_X
;
1089 nvfx_vp_emit(vpc
, arith(0, VEC
, DP4
, cdst
, mask
, htmp
, ceqn
, none
));
1092 if (vpc
->vp
->nr_insns
)
1093 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
1095 if(debug_get_option_nvfx_dump_vp())
1098 tgsi_dump(vpc
->pipe
.tokens
, 0);
1100 debug_printf("\n%s vertex program:\n", nv30
->is_nv4x
? "nv4x" : "nv3x");
1101 for (i
= 0; i
< vp
->nr_insns
; i
++)
1102 debug_printf("%3u: %08x %08x %08x %08x\n", i
, vp
->insns
[i
].data
[0], vp
->insns
[i
].data
[1], vp
->insns
[i
].data
[2], vp
->insns
[i
].data
[3]);
1106 vp
->translated
= TRUE
;
1109 tgsi_parse_free(&parse
);
1111 util_dynarray_fini(&vpc
->label_relocs
);
1112 util_dynarray_fini(&vpc
->loop_stack
);
1114 FREE(vpc
->r_address
);
1120 return vp
->translated
;