2 #include "pipe/p_context.h"
3 #include "pipe/p_defines.h"
4 #include "pipe/p_state.h"
5 #include "util/u_dynarray.h"
6 #include "util/u_debug.h"
7 #include "util/u_memory.h"
9 #include "pipe/p_shader_tokens.h"
10 #include "tgsi/tgsi_parse.h"
11 #include "tgsi/tgsi_dump.h"
12 #include "tgsi/tgsi_util.h"
13 #include "tgsi/tgsi_ureg.h"
15 #include "draw/draw_context.h"
17 #include "nv_object.xml.h"
18 #include "nouveau_debug.h"
19 #include "nv30/nv30-40_3d.xml.h"
20 #include "nv30/nv30_state.h"
22 /* TODO (at least...):
23 * 1. Indexed consts + ARL
24 * 3. NV_vp11, NV_vp2, NV_vp3 features
25 * - extra arith opcodes
33 #include "nv30/nv30_vertprog.h"
34 #include "nv30/nv40_vertprog.h"
36 struct nvfx_loop_entry
{
42 struct pipe_shader_state pipe
;
43 struct nv30_vertprog
*vp
;
44 struct tgsi_shader_info
* info
;
46 struct nv30_vertprog_exec
*vpi
;
49 unsigned r_temps_discard
;
50 struct nvfx_reg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
51 struct nvfx_reg
*r_address
;
52 struct nvfx_reg
*r_temp
;
53 struct nvfx_reg
*r_const
;
54 struct nvfx_reg r_0_1
;
64 struct util_dynarray label_relocs
;
65 struct util_dynarray loop_stack
;
68 static struct nvfx_reg
69 temp(struct nvfx_vpc
*vpc
)
71 int idx
= ffs(~vpc
->r_temps
) - 1;
73 if (idx
< 0 || (!vpc
->is_nv4x
&& idx
>= 16)) {
74 NOUVEAU_ERR("out of temps!!\n");
75 return nvfx_reg(NVFXSR_TEMP
, 0);
78 vpc
->r_temps
|= (1 << idx
);
79 vpc
->r_temps_discard
|= (1 << idx
);
80 return nvfx_reg(NVFXSR_TEMP
, idx
);
84 release_temps(struct nvfx_vpc
*vpc
)
86 vpc
->r_temps
&= ~vpc
->r_temps_discard
;
87 vpc
->r_temps_discard
= 0;
90 static struct nvfx_reg
91 constant(struct nvfx_vpc
*vpc
, int pipe
, float x
, float y
, float z
, float w
)
93 struct nv30_vertprog
*vp
= vpc
->vp
;
94 struct nv30_vertprog_data
*vpd
;
98 for (idx
= 0; idx
< vp
->nr_consts
; idx
++) {
99 if (vp
->consts
[idx
].index
== pipe
)
100 return nvfx_reg(NVFXSR_CONST
, idx
);
104 idx
= vp
->nr_consts
++;
105 vp
->consts
= realloc(vp
->consts
, sizeof(*vpd
) * vp
->nr_consts
);
106 vpd
= &vp
->consts
[idx
];
113 return nvfx_reg(NVFXSR_CONST
, idx
);
116 #define arith(s,t,o,d,m,s0,s1,s2) \
117 nvfx_insn((s), (NVFX_VP_INST_SLOT_##t << 7) | NVFX_VP_INST_##t##_OP_##o, -1, (d), (m), (s0), (s1), (s2))
120 emit_src(struct nvfx_vpc
*vpc
, uint32_t *hw
,
121 int pos
, struct nvfx_src src
)
123 struct nv30_vertprog
*vp
= vpc
->vp
;
125 struct nvfx_relocation reloc
;
127 switch (src
.reg
.type
) {
129 sr
|= (NVFX_VP(SRC_REG_TYPE_TEMP
) << NVFX_VP(SRC_REG_TYPE_SHIFT
));
130 sr
|= (src
.reg
.index
<< NVFX_VP(SRC_TEMP_SRC_SHIFT
));
133 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
134 NVFX_VP(SRC_REG_TYPE_SHIFT
));
135 vp
->ir
|= (1 << src
.reg
.index
);
136 hw
[1] |= (src
.reg
.index
<< NVFX_VP(INST_INPUT_SRC_SHIFT
));
139 sr
|= (NVFX_VP(SRC_REG_TYPE_CONST
) <<
140 NVFX_VP(SRC_REG_TYPE_SHIFT
));
141 if (src
.reg
.index
< 256 && src
.reg
.index
>= -256) {
142 reloc
.location
= vp
->nr_insns
- 1;
143 reloc
.target
= src
.reg
.index
;
144 util_dynarray_append(&vp
->const_relocs
, struct nvfx_relocation
, reloc
);
146 hw
[1] |= (src
.reg
.index
<< NVFX_VP(INST_CONST_SRC_SHIFT
)) &
147 NVFX_VP(INST_CONST_SRC_MASK
);
151 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
152 NVFX_VP(SRC_REG_TYPE_SHIFT
));
159 sr
|= NVFX_VP(SRC_NEGATE
);
162 hw
[0] |= (1 << (21 + pos
));
164 sr
|= ((src
.swz
[0] << NVFX_VP(SRC_SWZ_X_SHIFT
)) |
165 (src
.swz
[1] << NVFX_VP(SRC_SWZ_Y_SHIFT
)) |
166 (src
.swz
[2] << NVFX_VP(SRC_SWZ_Z_SHIFT
)) |
167 (src
.swz
[3] << NVFX_VP(SRC_SWZ_W_SHIFT
)));
170 if(src
.reg
.type
== NVFXSR_CONST
)
171 hw
[3] |= NVFX_VP(INST_INDEX_CONST
);
172 else if(src
.reg
.type
== NVFXSR_INPUT
)
173 hw
[0] |= NVFX_VP(INST_INDEX_INPUT
);
178 hw
[0] |= NVFX_VP(INST_ADDR_REG_SELECT_1
);
179 hw
[0] |= src
.indirect_swz
<< NVFX_VP(INST_ADDR_SWZ_SHIFT
);
184 hw
[1] |= ((sr
& NVFX_VP(SRC0_HIGH_MASK
)) >>
185 NVFX_VP(SRC0_HIGH_SHIFT
)) << NVFX_VP(INST_SRC0H_SHIFT
);
186 hw
[2] |= (sr
& NVFX_VP(SRC0_LOW_MASK
)) <<
187 NVFX_VP(INST_SRC0L_SHIFT
);
190 hw
[2] |= sr
<< NVFX_VP(INST_SRC1_SHIFT
);
193 hw
[2] |= ((sr
& NVFX_VP(SRC2_HIGH_MASK
)) >>
194 NVFX_VP(SRC2_HIGH_SHIFT
)) << NVFX_VP(INST_SRC2H_SHIFT
);
195 hw
[3] |= (sr
& NVFX_VP(SRC2_LOW_MASK
)) <<
196 NVFX_VP(INST_SRC2L_SHIFT
);
204 emit_dst(struct nvfx_vpc
*vpc
, uint32_t *hw
,
205 int slot
, struct nvfx_reg dst
)
207 struct nv30_vertprog
*vp
= vpc
->vp
;
212 hw
[0] |= NV30_VP_INST_DEST_TEMP_ID_MASK
;
214 hw
[3] |= NV40_VP_INST_DEST_MASK
;
216 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
218 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
223 hw
[0] |= (dst
.index
<< NV30_VP_INST_DEST_TEMP_ID_SHIFT
);
225 hw
[3] |= NV40_VP_INST_DEST_MASK
;
227 hw
[0] |= (dst
.index
<< NV40_VP_INST_VEC_DEST_TEMP_SHIFT
);
229 hw
[3] |= (dst
.index
<< NV40_VP_INST_SCA_DEST_TEMP_SHIFT
);
233 /* TODO: this may be wrong because on nv30 COL0 and BFC0 are swapped */
236 case NV30_VP_INST_DEST_CLP(0):
237 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
240 case NV30_VP_INST_DEST_CLP(1):
241 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
244 case NV30_VP_INST_DEST_CLP(2):
245 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
248 case NV30_VP_INST_DEST_CLP(3):
249 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
252 case NV30_VP_INST_DEST_CLP(4):
253 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
256 case NV30_VP_INST_DEST_CLP(5):
257 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
260 case NV40_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
261 case NV40_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
262 case NV40_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
263 case NV40_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
264 case NV40_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
265 case NV40_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
270 hw
[3] |= (dst
.index
<< NV30_VP_INST_DEST_SHIFT
);
271 hw
[0] |= NV30_VP_INST_VEC_DEST_TEMP_MASK
;
273 /*XXX: no way this is entirely correct, someone needs to
274 * figure out what exactly it is.
278 hw
[3] |= (dst
.index
<< NV40_VP_INST_DEST_SHIFT
);
280 hw
[0] |= NV40_VP_INST_VEC_RESULT
;
281 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
283 hw
[3] |= NV40_VP_INST_SCA_RESULT
;
284 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
294 nvfx_vp_emit(struct nvfx_vpc
*vpc
, struct nvfx_insn insn
)
296 struct nv30_vertprog
*vp
= vpc
->vp
;
297 unsigned slot
= insn
.op
>> 7;
298 unsigned op
= insn
.op
& 0x7f;
301 vp
->insns
= realloc(vp
->insns
, ++vp
->nr_insns
* sizeof(*vpc
->vpi
));
302 vpc
->vpi
= &vp
->insns
[vp
->nr_insns
- 1];
303 memset(vpc
->vpi
, 0, sizeof(*vpc
->vpi
));
307 if (insn
.cc_test
!= NVFX_COND_TR
)
308 hw
[0] |= NVFX_VP(INST_COND_TEST_ENABLE
);
309 hw
[0] |= (insn
.cc_test
<< NVFX_VP(INST_COND_SHIFT
));
310 hw
[0] |= ((insn
.cc_swz
[0] << NVFX_VP(INST_COND_SWZ_X_SHIFT
)) |
311 (insn
.cc_swz
[1] << NVFX_VP(INST_COND_SWZ_Y_SHIFT
)) |
312 (insn
.cc_swz
[2] << NVFX_VP(INST_COND_SWZ_Z_SHIFT
)) |
313 (insn
.cc_swz
[3] << NVFX_VP(INST_COND_SWZ_W_SHIFT
)));
315 hw
[0] |= NVFX_VP(INST_COND_UPDATE_ENABLE
);
318 assert(vpc
->is_nv4x
);
320 hw
[0] |= NV40_VP_INST_SATURATE
;
325 hw
[1] |= (op
<< NV30_VP_INST_VEC_OPCODE_SHIFT
);
327 hw
[0] |= ((op
>> 4) << NV30_VP_INST_SCA_OPCODEH_SHIFT
);
328 hw
[1] |= ((op
& 0xf) << NV30_VP_INST_SCA_OPCODEL_SHIFT
);
330 // hw[3] |= NVFX_VP(INST_SCA_DEST_TEMP_MASK);
331 // hw[3] |= (mask << NVFX_VP(INST_VEC_WRITEMASK_SHIFT));
333 if (insn
.dst
.type
== NVFXSR_OUTPUT
) {
335 hw
[3] |= (insn
.mask
<< NV30_VP_INST_SDEST_WRITEMASK_SHIFT
);
337 hw
[3] |= (insn
.mask
<< NV30_VP_INST_VDEST_WRITEMASK_SHIFT
);
340 hw
[3] |= (insn
.mask
<< NV30_VP_INST_STEMP_WRITEMASK_SHIFT
);
342 hw
[3] |= (insn
.mask
<< NV30_VP_INST_VTEMP_WRITEMASK_SHIFT
);
346 hw
[1] |= (op
<< NV40_VP_INST_VEC_OPCODE_SHIFT
);
347 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
348 hw
[3] |= (insn
.mask
<< NV40_VP_INST_VEC_WRITEMASK_SHIFT
);
350 hw
[1] |= (op
<< NV40_VP_INST_SCA_OPCODE_SHIFT
);
351 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
;
352 hw
[3] |= (insn
.mask
<< NV40_VP_INST_SCA_WRITEMASK_SHIFT
);
356 emit_dst(vpc
, hw
, slot
, insn
.dst
);
357 emit_src(vpc
, hw
, 0, insn
.src
[0]);
358 emit_src(vpc
, hw
, 1, insn
.src
[1]);
359 emit_src(vpc
, hw
, 2, insn
.src
[2]);
361 // if(insn.src[0].indirect || op == NVFX_VP_INST_VEC_OP_ARL)
362 // hw[3] |= NV40_VP_INST_SCA_RESULT;
365 static inline struct nvfx_src
366 tgsi_src(struct nvfx_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
) {
369 switch (fsrc
->Register
.File
) {
370 case TGSI_FILE_INPUT
:
371 src
.reg
= nvfx_reg(NVFXSR_INPUT
, fsrc
->Register
.Index
);
373 case TGSI_FILE_CONSTANT
:
374 if(fsrc
->Register
.Indirect
) {
375 src
.reg
= vpc
->r_const
[0];
376 src
.reg
.index
= fsrc
->Register
.Index
;
378 src
.reg
= vpc
->r_const
[fsrc
->Register
.Index
];
381 case TGSI_FILE_IMMEDIATE
:
382 src
.reg
= vpc
->imm
[fsrc
->Register
.Index
];
384 case TGSI_FILE_TEMPORARY
:
385 src
.reg
= vpc
->r_temp
[fsrc
->Register
.Index
];
388 NOUVEAU_ERR("bad src file\n");
394 src
.abs
= fsrc
->Register
.Absolute
;
395 src
.negate
= fsrc
->Register
.Negate
;
396 src
.swz
[0] = fsrc
->Register
.SwizzleX
;
397 src
.swz
[1] = fsrc
->Register
.SwizzleY
;
398 src
.swz
[2] = fsrc
->Register
.SwizzleZ
;
399 src
.swz
[3] = fsrc
->Register
.SwizzleW
;
401 src
.indirect_reg
= 0;
402 src
.indirect_swz
= 0;
404 if(fsrc
->Register
.Indirect
) {
405 if(fsrc
->Indirect
.File
== TGSI_FILE_ADDRESS
&&
406 (fsrc
->Register
.File
== TGSI_FILE_CONSTANT
||
407 fsrc
->Register
.File
== TGSI_FILE_INPUT
)) {
409 src
.indirect_reg
= fsrc
->Indirect
.Index
;
410 src
.indirect_swz
= fsrc
->Indirect
.Swizzle
;
420 static inline struct nvfx_reg
421 tgsi_dst(struct nvfx_vpc
*vpc
, const struct tgsi_full_dst_register
*fdst
) {
424 switch (fdst
->Register
.File
) {
426 dst
= nvfx_reg(NVFXSR_NONE
, 0);
428 case TGSI_FILE_OUTPUT
:
429 dst
= vpc
->r_result
[fdst
->Register
.Index
];
431 case TGSI_FILE_TEMPORARY
:
432 dst
= vpc
->r_temp
[fdst
->Register
.Index
];
434 case TGSI_FILE_ADDRESS
:
435 dst
= vpc
->r_address
[fdst
->Register
.Index
];
438 NOUVEAU_ERR("bad dst file %i\n", fdst
->Register
.File
);
452 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= NVFX_VP_MASK_X
;
453 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= NVFX_VP_MASK_Y
;
454 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= NVFX_VP_MASK_Z
;
455 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= NVFX_VP_MASK_W
;
460 nvfx_vertprog_parse_instruction(struct nvfx_vpc
*vpc
,
461 unsigned idx
, const struct tgsi_full_instruction
*finst
)
463 struct nvfx_src src
[3], tmp
;
465 struct nvfx_reg final_dst
;
466 struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
467 struct nvfx_insn insn
;
468 struct nvfx_relocation reloc
;
469 struct nvfx_loop_entry loop
;
472 int ai
= -1, ci
= -1, ii
= -1;
474 unsigned sub_depth
= 0;
476 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
477 const struct tgsi_full_src_register
*fsrc
;
479 fsrc
= &finst
->Src
[i
];
480 if (fsrc
->Register
.File
== TGSI_FILE_TEMPORARY
) {
481 src
[i
] = tgsi_src(vpc
, fsrc
);
485 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
486 const struct tgsi_full_src_register
*fsrc
;
488 fsrc
= &finst
->Src
[i
];
490 switch (fsrc
->Register
.File
) {
491 case TGSI_FILE_INPUT
:
492 if (ai
== -1 || ai
== fsrc
->Register
.Index
) {
493 ai
= fsrc
->Register
.Index
;
494 src
[i
] = tgsi_src(vpc
, fsrc
);
496 src
[i
] = nvfx_src(temp(vpc
));
497 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
498 tgsi_src(vpc
, fsrc
), none
, none
));
501 case TGSI_FILE_CONSTANT
:
502 if ((ci
== -1 && ii
== -1) ||
503 ci
== fsrc
->Register
.Index
) {
504 ci
= fsrc
->Register
.Index
;
505 src
[i
] = tgsi_src(vpc
, fsrc
);
507 src
[i
] = nvfx_src(temp(vpc
));
508 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
509 tgsi_src(vpc
, fsrc
), none
, none
));
512 case TGSI_FILE_IMMEDIATE
:
513 if ((ci
== -1 && ii
== -1) ||
514 ii
== fsrc
->Register
.Index
) {
515 ii
= fsrc
->Register
.Index
;
516 src
[i
] = tgsi_src(vpc
, fsrc
);
518 src
[i
] = nvfx_src(temp(vpc
));
519 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, src
[i
].reg
, NVFX_VP_MASK_ALL
,
520 tgsi_src(vpc
, fsrc
), none
, none
));
523 case TGSI_FILE_TEMPORARY
:
527 NOUVEAU_ERR("bad src file\n");
532 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
533 if(src
[i
].reg
.type
< 0)
537 if(finst
->Dst
[0].Register
.File
== TGSI_FILE_ADDRESS
&&
538 finst
->Instruction
.Opcode
!= TGSI_OPCODE_ARL
)
541 final_dst
= dst
= tgsi_dst(vpc
, &finst
->Dst
[0]);
542 mask
= tgsi_mask(finst
->Dst
[0].Register
.WriteMask
);
543 if(finst
->Instruction
.Saturate
) {
544 assert(finst
->Instruction
.Opcode
!= TGSI_OPCODE_ARL
);
548 if(dst
.type
!= NVFXSR_TEMP
)
552 switch (finst
->Instruction
.Opcode
) {
553 case TGSI_OPCODE_ADD
:
554 nvfx_vp_emit(vpc
, arith(sat
, VEC
, ADD
, dst
, mask
, src
[0], none
, src
[1]));
556 case TGSI_OPCODE_ARL
:
557 nvfx_vp_emit(vpc
, arith(0, VEC
, ARL
, dst
, mask
, src
[0], none
, none
));
559 case TGSI_OPCODE_CEIL
:
560 tmp
= nvfx_src(temp(vpc
));
561 nvfx_vp_emit(vpc
, arith(0, VEC
, FLR
, tmp
.reg
, mask
, neg(src
[0]), none
, none
));
562 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, neg(tmp
), none
, none
));
564 case TGSI_OPCODE_CMP
:
565 insn
= arith(0, VEC
, MOV
, none
.reg
, mask
, src
[0], none
, none
);
567 nvfx_vp_emit(vpc
, insn
);
569 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, src
[2], none
, none
);
570 insn
.cc_test
= NVFX_COND_GE
;
571 nvfx_vp_emit(vpc
, insn
);
573 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, src
[1], none
, none
);
574 insn
.cc_test
= NVFX_COND_LT
;
575 nvfx_vp_emit(vpc
, insn
);
577 case TGSI_OPCODE_COS
:
578 nvfx_vp_emit(vpc
, arith(sat
, SCA
, COS
, dst
, mask
, none
, none
, src
[0]));
580 case TGSI_OPCODE_DP2
:
581 tmp
= nvfx_src(temp(vpc
));
582 nvfx_vp_emit(vpc
, arith(0, VEC
, MUL
, tmp
.reg
, NVFX_VP_MASK_X
| NVFX_VP_MASK_Y
, src
[0], src
[1], none
));
583 nvfx_vp_emit(vpc
, arith(sat
, VEC
, ADD
, dst
, mask
, swz(tmp
, X
, X
, X
, X
), none
, swz(tmp
, Y
, Y
, Y
, Y
)));
585 case TGSI_OPCODE_DP3
:
586 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DP3
, dst
, mask
, src
[0], src
[1], none
));
588 case TGSI_OPCODE_DP4
:
589 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DP4
, dst
, mask
, src
[0], src
[1], none
));
591 case TGSI_OPCODE_DST
:
592 nvfx_vp_emit(vpc
, arith(sat
, VEC
, DST
, dst
, mask
, src
[0], src
[1], none
));
594 case TGSI_OPCODE_EX2
:
595 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EX2
, dst
, mask
, none
, none
, src
[0]));
597 case TGSI_OPCODE_EXP
:
598 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EXP
, dst
, mask
, none
, none
, src
[0]));
600 case TGSI_OPCODE_FLR
:
601 nvfx_vp_emit(vpc
, arith(sat
, VEC
, FLR
, dst
, mask
, src
[0], none
, none
));
603 case TGSI_OPCODE_FRC
:
604 nvfx_vp_emit(vpc
, arith(sat
, VEC
, FRC
, dst
, mask
, src
[0], none
, none
));
606 case TGSI_OPCODE_LG2
:
607 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LG2
, dst
, mask
, none
, none
, src
[0]));
609 case TGSI_OPCODE_LIT
:
610 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LIT
, dst
, mask
, none
, none
, src
[0]));
612 case TGSI_OPCODE_LOG
:
613 nvfx_vp_emit(vpc
, arith(sat
, SCA
, LOG
, dst
, mask
, none
, none
, src
[0]));
615 case TGSI_OPCODE_LRP
:
616 tmp
= nvfx_src(temp(vpc
));
617 nvfx_vp_emit(vpc
, arith(0, VEC
, MAD
, tmp
.reg
, mask
, neg(src
[0]), src
[2], src
[2]));
618 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], tmp
));
620 case TGSI_OPCODE_MAD
:
621 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], src
[2]));
623 case TGSI_OPCODE_MAX
:
624 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MAX
, dst
, mask
, src
[0], src
[1], none
));
626 case TGSI_OPCODE_MIN
:
627 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MIN
, dst
, mask
, src
[0], src
[1], none
));
629 case TGSI_OPCODE_MOV
:
630 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, src
[0], none
, none
));
632 case TGSI_OPCODE_MUL
:
633 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MUL
, dst
, mask
, src
[0], src
[1], none
));
635 case TGSI_OPCODE_NOP
:
637 case TGSI_OPCODE_POW
:
638 tmp
= nvfx_src(temp(vpc
));
639 nvfx_vp_emit(vpc
, arith(0, SCA
, LG2
, tmp
.reg
, NVFX_VP_MASK_X
, none
, none
, swz(src
[0], X
, X
, X
, X
)));
640 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
));
641 nvfx_vp_emit(vpc
, arith(sat
, SCA
, EX2
, dst
, mask
, none
, none
, swz(tmp
, X
, X
, X
, X
)));
643 case TGSI_OPCODE_RCP
:
644 nvfx_vp_emit(vpc
, arith(sat
, SCA
, RCP
, dst
, mask
, none
, none
, src
[0]));
646 case TGSI_OPCODE_RSQ
:
647 nvfx_vp_emit(vpc
, arith(sat
, SCA
, RSQ
, dst
, mask
, none
, none
, abs(src
[0])));
649 case TGSI_OPCODE_SEQ
:
650 nvfx_vp_emit(vpc
, arith(sat
, VEC
, SEQ
, 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], none
, none
));
673 case TGSI_OPCODE_TRUNC
:
674 tmp
= nvfx_src(temp(vpc
));
675 insn
= arith(0, VEC
, MOV
, none
.reg
, mask
, src
[0], none
, none
);
677 nvfx_vp_emit(vpc
, insn
);
679 nvfx_vp_emit(vpc
, arith(0, VEC
, FLR
, tmp
.reg
, mask
, abs(src
[0]), none
, none
));
680 nvfx_vp_emit(vpc
, arith(sat
, VEC
, MOV
, dst
, mask
, tmp
, none
, none
));
682 insn
= arith(sat
, VEC
, MOV
, dst
, mask
, neg(tmp
), none
, none
);
683 insn
.cc_test
= NVFX_COND_LT
;
684 nvfx_vp_emit(vpc
, insn
);
687 insn
= arith(0, VEC
, MOV
, none
.reg
, NVFX_VP_MASK_X
, src
[0], none
, none
);
689 nvfx_vp_emit(vpc
, insn
);
691 reloc
.location
= vpc
->vp
->nr_insns
;
692 reloc
.target
= finst
->Label
.Label
+ 1;
693 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
695 insn
= arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
);
696 insn
.cc_test
= NVFX_COND_EQ
;
697 insn
.cc_swz
[0] = insn
.cc_swz
[1] = insn
.cc_swz
[2] = insn
.cc_swz
[3] = 0;
698 nvfx_vp_emit(vpc
, insn
);
700 case TGSI_OPCODE_ELSE
:
701 case TGSI_OPCODE_CAL
:
702 reloc
.location
= vpc
->vp
->nr_insns
;
703 reloc
.target
= finst
->Label
.Label
;
704 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
706 if(finst
->Instruction
.Opcode
== TGSI_OPCODE_CAL
)
707 insn
= arith(0, SCA
, CAL
, none
.reg
, 0, none
, none
, none
);
709 insn
= arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
);
710 nvfx_vp_emit(vpc
, insn
);
712 case TGSI_OPCODE_RET
:
713 if(sub_depth
|| !vpc
->vp
->enabled_ucps
) {
715 tmp
.swz
[0] = tmp
.swz
[1] = tmp
.swz
[2] = tmp
.swz
[3] = 0;
716 nvfx_vp_emit(vpc
, arith(0, SCA
, RET
, none
.reg
, 0, none
, none
, tmp
));
718 reloc
.location
= vpc
->vp
->nr_insns
;
719 reloc
.target
= vpc
->info
->num_instructions
;
720 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
721 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
724 case TGSI_OPCODE_BGNSUB
:
727 case TGSI_OPCODE_ENDSUB
:
730 case TGSI_OPCODE_ENDIF
:
731 /* nothing to do here */
733 case TGSI_OPCODE_BGNLOOP
:
734 loop
.cont_target
= idx
;
735 loop
.brk_target
= finst
->Label
.Label
+ 1;
736 util_dynarray_append(&vpc
->loop_stack
, struct nvfx_loop_entry
, loop
);
738 case TGSI_OPCODE_ENDLOOP
:
739 loop
= util_dynarray_pop(&vpc
->loop_stack
, struct nvfx_loop_entry
);
741 reloc
.location
= vpc
->vp
->nr_insns
;
742 reloc
.target
= loop
.cont_target
;
743 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
745 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
747 case TGSI_OPCODE_CONT
:
748 loop
= util_dynarray_top(&vpc
->loop_stack
, struct nvfx_loop_entry
);
750 reloc
.location
= vpc
->vp
->nr_insns
;
751 reloc
.target
= loop
.cont_target
;
752 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
754 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
756 case TGSI_OPCODE_BRK
:
757 loop
= util_dynarray_top(&vpc
->loop_stack
, struct nvfx_loop_entry
);
759 reloc
.location
= vpc
->vp
->nr_insns
;
760 reloc
.target
= loop
.brk_target
;
761 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
763 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
765 case TGSI_OPCODE_END
:
767 if(vpc
->vp
->enabled_ucps
) {
768 if(idx
!= (vpc
->info
->num_instructions
- 1)) {
769 reloc
.location
= vpc
->vp
->nr_insns
;
770 reloc
.target
= vpc
->info
->num_instructions
;
771 util_dynarray_append(&vpc
->label_relocs
, struct nvfx_relocation
, reloc
);
772 nvfx_vp_emit(vpc
, arith(0, SCA
, BRA
, none
.reg
, 0, none
, none
, none
));
775 if(vpc
->vp
->nr_insns
)
776 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
777 nvfx_vp_emit(vpc
, arith(0, VEC
, NOP
, none
.reg
, 0, none
, none
, none
));
778 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
782 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
786 if(finst
->Instruction
.Saturate
&& !vpc
->is_nv4x
) {
787 if (!vpc
->r_0_1
.type
)
788 vpc
->r_0_1
= constant(vpc
, -1, 0, 1, 0, 0);
789 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
));
790 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
));
798 nvfx_vertprog_parse_decl_output(struct nvfx_vpc
*vpc
,
799 const struct tgsi_full_declaration
*fdec
)
801 unsigned num_texcoords
= vpc
->is_nv4x
? 10 : 8;
802 unsigned idx
= fdec
->Range
.First
;
803 unsigned semantic_index
= fdec
->Semantic
.Index
;
806 switch (fdec
->Semantic
.Name
) {
807 case TGSI_SEMANTIC_POSITION
:
808 hw
= NVFX_VP(INST_DEST_POS
);
811 case TGSI_SEMANTIC_CLIPVERTEX
:
812 vpc
->r_result
[idx
] = temp(vpc
);
813 vpc
->r_temps_discard
= 0;
816 case TGSI_SEMANTIC_COLOR
:
817 if (fdec
->Semantic
.Index
== 0) {
818 hw
= NVFX_VP(INST_DEST_COL0
);
820 if (fdec
->Semantic
.Index
== 1) {
821 hw
= NVFX_VP(INST_DEST_COL1
);
823 NOUVEAU_ERR("bad colour semantic index\n");
827 case TGSI_SEMANTIC_BCOLOR
:
828 if (fdec
->Semantic
.Index
== 0) {
829 hw
= NVFX_VP(INST_DEST_BFC0
);
831 if (fdec
->Semantic
.Index
== 1) {
832 hw
= NVFX_VP(INST_DEST_BFC1
);
834 NOUVEAU_ERR("bad bcolour semantic index\n");
838 case TGSI_SEMANTIC_FOG
:
839 hw
= NVFX_VP(INST_DEST_FOGC
);
841 case TGSI_SEMANTIC_PSIZE
:
842 hw
= NVFX_VP(INST_DEST_PSZ
);
844 case TGSI_SEMANTIC_GENERIC
:
845 /* this is really an identifier for VP/FP linkage */
848 case TGSI_SEMANTIC_TEXCOORD
:
849 for (i
= 0; i
< num_texcoords
; i
++) {
850 if (vpc
->vp
->texcoord
[i
] == semantic_index
) {
851 hw
= NVFX_VP(INST_DEST_TC(i
));
856 if (i
== num_texcoords
) {
857 vpc
->r_result
[idx
] = nvfx_reg(NVFXSR_NONE
, 0);
861 case TGSI_SEMANTIC_EDGEFLAG
:
862 vpc
->r_result
[idx
] = nvfx_reg(NVFXSR_NONE
, 0);
865 NOUVEAU_ERR("bad output semantic\n");
869 vpc
->r_result
[idx
] = nvfx_reg(NVFXSR_OUTPUT
, hw
);
874 nvfx_vertprog_prepare(struct nvfx_vpc
*vpc
)
876 struct tgsi_parse_context p
;
877 int high_const
= -1, high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
879 tgsi_parse_init(&p
, vpc
->pipe
.tokens
);
880 while (!tgsi_parse_end_of_tokens(&p
)) {
881 const union tgsi_full_token
*tok
= &p
.FullToken
;
883 tgsi_parse_token(&p
);
884 switch(tok
->Token
.Type
) {
885 case TGSI_TOKEN_TYPE_IMMEDIATE
:
888 case TGSI_TOKEN_TYPE_DECLARATION
:
890 const struct tgsi_full_declaration
*fdec
;
892 fdec
= &p
.FullToken
.FullDeclaration
;
893 switch (fdec
->Declaration
.File
) {
894 case TGSI_FILE_TEMPORARY
:
895 if (fdec
->Range
.Last
> high_temp
) {
900 case TGSI_FILE_ADDRESS
:
901 if (fdec
->Range
.Last
> high_addr
) {
906 case TGSI_FILE_CONSTANT
:
907 if (fdec
->Range
.Last
> high_const
) {
912 case TGSI_FILE_OUTPUT
:
913 if (!nvfx_vertprog_parse_decl_output(vpc
, fdec
))
928 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nvfx_reg
));
933 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nvfx_reg
));
934 for (i
= 0; i
< high_temp
; i
++)
935 vpc
->r_temp
[i
] = temp(vpc
);
939 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nvfx_reg
));
940 for (i
= 0; i
< high_addr
; i
++)
941 vpc
->r_address
[i
] = nvfx_reg(NVFXSR_TEMP
, i
);
945 vpc
->r_const
= CALLOC(high_const
, sizeof(struct nvfx_reg
));
946 for (i
= 0; i
< high_const
; i
++)
947 vpc
->r_const
[i
] = constant(vpc
, i
, 0, 0, 0, 0);
950 vpc
->r_temps_discard
= 0;
954 DEBUG_GET_ONCE_BOOL_OPTION(nvfx_dump_vp
, "NVFX_DUMP_VP", false)
957 _nvfx_vertprog_translate(uint16_t oclass
, struct nv30_vertprog
*vp
)
959 struct tgsi_parse_context parse
;
960 struct nvfx_vpc
*vpc
= NULL
;
961 struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
962 struct util_dynarray insns
;
965 vp
->translated
= false;
969 vpc
= CALLOC_STRUCT(nvfx_vpc
);
972 vpc
->is_nv4x
= (oclass
>= NV40_3D_CLASS
) ? ~0 : 0;
974 vpc
->pipe
= vp
->pipe
;
975 vpc
->info
= &vp
->info
;
978 if (!nvfx_vertprog_prepare(vpc
)) {
983 /* Redirect post-transform vertex position to a temp if user clip
984 * planes are enabled. We need to append code to the vtxprog
985 * to handle clip planes later.
987 if (vp
->enabled_ucps
&& vpc
->cvtx_idx
< 0) {
988 vpc
->r_result
[vpc
->hpos_idx
] = temp(vpc
);
989 vpc
->r_temps_discard
= 0;
990 vpc
->cvtx_idx
= vpc
->hpos_idx
;
993 util_dynarray_init(&insns
, NULL
);
995 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
996 while (!tgsi_parse_end_of_tokens(&parse
)) {
997 tgsi_parse_token(&parse
);
999 switch (parse
.FullToken
.Token
.Type
) {
1000 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1002 const struct tgsi_full_immediate
*imm
;
1004 imm
= &parse
.FullToken
.FullImmediate
;
1005 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
1006 assert(imm
->Immediate
.NrTokens
== 4 + 1);
1007 vpc
->imm
[vpc
->nr_imm
++] =
1015 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1017 const struct tgsi_full_instruction
*finst
;
1018 unsigned idx
= insns
.size
>> 2;
1019 util_dynarray_append(&insns
, unsigned, vp
->nr_insns
);
1020 finst
= &parse
.FullToken
.FullInstruction
;
1021 if (!nvfx_vertprog_parse_instruction(vpc
, idx
, finst
))
1030 util_dynarray_append(&insns
, unsigned, vp
->nr_insns
);
1032 for(unsigned i
= 0; i
< vpc
->label_relocs
.size
; i
+= sizeof(struct nvfx_relocation
))
1034 struct nvfx_relocation
* label_reloc
= (struct nvfx_relocation
*)((char*)vpc
->label_relocs
.data
+ i
);
1035 struct nvfx_relocation hw_reloc
;
1037 hw_reloc
.location
= label_reloc
->location
;
1038 hw_reloc
.target
= ((unsigned*)insns
.data
)[label_reloc
->target
];
1040 //debug_printf("hw %u -> tgsi %u = hw %u\n", hw_reloc.location, label_reloc->target, hw_reloc.target);
1042 util_dynarray_append(&vp
->branch_relocs
, struct nvfx_relocation
, hw_reloc
);
1044 util_dynarray_fini(&insns
);
1045 util_dynarray_trim(&vp
->branch_relocs
);
1047 /* XXX: what if we add a RET before?! make sure we jump here...*/
1049 /* Write out HPOS if it was redirected to a temp earlier */
1050 if (vpc
->r_result
[vpc
->hpos_idx
].type
!= NVFXSR_OUTPUT
) {
1051 struct nvfx_reg hpos
= nvfx_reg(NVFXSR_OUTPUT
,
1052 NVFX_VP(INST_DEST_POS
));
1053 struct nvfx_src htmp
= nvfx_src(vpc
->r_result
[vpc
->hpos_idx
]);
1055 nvfx_vp_emit(vpc
, arith(0, VEC
, MOV
, hpos
, NVFX_VP_MASK_ALL
, htmp
, none
, none
));
1058 /* Insert code to handle user clip planes */
1059 ucps
= vp
->enabled_ucps
;
1061 int i
= ffs(ucps
) - 1; ucps
&= ~(1 << i
);
1062 struct nvfx_reg cdst
= nvfx_reg(NVFXSR_OUTPUT
, NV30_VP_INST_DEST_CLP(i
));
1063 struct nvfx_src ceqn
= nvfx_src(nvfx_reg(NVFXSR_CONST
, 512 + i
));
1064 struct nvfx_src htmp
= nvfx_src(vpc
->r_result
[vpc
->cvtx_idx
]);
1070 case 0: case 3: mask
= NVFX_VP_MASK_Y
; break;
1071 case 1: case 4: mask
= NVFX_VP_MASK_Z
; break;
1072 case 2: case 5: mask
= NVFX_VP_MASK_W
; break;
1074 NOUVEAU_ERR("invalid clip dist #%d\n", i
);
1079 mask
= NVFX_VP_MASK_X
;
1081 nvfx_vp_emit(vpc
, arith(0, VEC
, DP4
, cdst
, mask
, htmp
, ceqn
, none
));
1084 if (vpc
->vp
->nr_insns
)
1085 vpc
->vp
->insns
[vpc
->vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
1087 if(debug_get_option_nvfx_dump_vp())
1090 tgsi_dump(vpc
->pipe
.tokens
, 0);
1092 debug_printf("\n%s vertex program:\n", vpc
->is_nv4x
? "nv4x" : "nv3x");
1093 for (i
= 0; i
< vp
->nr_insns
; i
++)
1094 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]);
1098 vp
->translated
= true;
1101 tgsi_parse_free(&parse
);
1103 util_dynarray_fini(&vpc
->label_relocs
);
1104 util_dynarray_fini(&vpc
->loop_stack
);
1106 FREE(vpc
->r_address
);
1112 return vp
->translated
;