1 #include "pipe/p_context.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_state.h"
4 #include "util/u_linkage.h"
6 #include "pipe/p_shader_tokens.h"
7 #include "tgsi/tgsi_parse.h"
8 #include "tgsi/tgsi_dump.h"
9 #include "tgsi/tgsi_util.h"
11 #include "nvfx_context.h"
12 #include "nvfx_state.h"
13 #include "nvfx_resource.h"
15 /* TODO (at least...):
16 * 1. Indexed consts + ARL
17 * 3. NV_vp11, NV_vp2, NV_vp3 features
18 * - extra arith opcodes
26 #include "nv30_vertprog.h"
27 #include "nv40_vertprog.h"
29 #define NVFX_VP_INST_DEST_CLIP(n) ((~0 - 6) + (n))
32 struct nvfx_vertex_program
*vp
;
34 struct nvfx_vertex_program_exec
*vpi
;
37 unsigned r_temps_discard
;
38 struct nvfx_sreg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
39 struct nvfx_sreg
*r_address
;
40 struct nvfx_sreg
*r_temp
;
42 struct nvfx_sreg
*imm
;
48 static struct nvfx_sreg
49 temp(struct nvfx_vpc
*vpc
)
51 int idx
= ffs(~vpc
->r_temps
) - 1;
54 NOUVEAU_ERR("out of temps!!\n");
56 return nvfx_sr(NVFXSR_TEMP
, 0);
59 vpc
->r_temps
|= (1 << idx
);
60 vpc
->r_temps_discard
|= (1 << idx
);
61 return nvfx_sr(NVFXSR_TEMP
, idx
);
65 release_temps(struct nvfx_vpc
*vpc
)
67 vpc
->r_temps
&= ~vpc
->r_temps_discard
;
68 vpc
->r_temps_discard
= 0;
71 static struct nvfx_sreg
72 constant(struct nvfx_vpc
*vpc
, int pipe
, float x
, float y
, float z
, float w
)
74 struct nvfx_vertex_program
*vp
= vpc
->vp
;
75 struct nvfx_vertex_program_data
*vpd
;
79 for (idx
= 0; idx
< vp
->nr_consts
; idx
++) {
80 if (vp
->consts
[idx
].index
== pipe
)
81 return nvfx_sr(NVFXSR_CONST
, idx
);
85 idx
= vp
->nr_consts
++;
86 vp
->consts
= realloc(vp
->consts
, sizeof(*vpd
) * vp
->nr_consts
);
87 vpd
= &vp
->consts
[idx
];
94 return nvfx_sr(NVFXSR_CONST
, idx
);
97 #define arith(cc,s,o,d,m,s0,s1,s2) \
98 nvfx_vp_arith(nvfx, (cc), NVFX_VP_INST_SLOT_##s, NVFX_VP_INST_##s##_OP_##o, (d), (m), (s0), (s1), (s2))
101 emit_src(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
, uint32_t *hw
, int pos
, struct nvfx_sreg src
)
103 struct nvfx_vertex_program
*vp
= vpc
->vp
;
108 sr
|= (NVFX_VP(SRC_REG_TYPE_TEMP
) << NVFX_VP(SRC_REG_TYPE_SHIFT
));
109 sr
|= (src
.index
<< NVFX_VP(SRC_TEMP_SRC_SHIFT
));
112 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
113 NVFX_VP(SRC_REG_TYPE_SHIFT
));
114 vp
->ir
|= (1 << src
.index
);
115 hw
[1] |= (src
.index
<< NVFX_VP(INST_INPUT_SRC_SHIFT
));
118 sr
|= (NVFX_VP(SRC_REG_TYPE_CONST
) <<
119 NVFX_VP(SRC_REG_TYPE_SHIFT
));
120 assert(vpc
->vpi
->const_index
== -1 ||
121 vpc
->vpi
->const_index
== src
.index
);
122 vpc
->vpi
->const_index
= src
.index
;
125 sr
|= (NVFX_VP(SRC_REG_TYPE_INPUT
) <<
126 NVFX_VP(SRC_REG_TYPE_SHIFT
));
133 sr
|= NVFX_VP(SRC_NEGATE
);
136 hw
[0] |= (1 << (21 + pos
));
138 sr
|= ((src
.swz
[0] << NVFX_VP(SRC_SWZ_X_SHIFT
)) |
139 (src
.swz
[1] << NVFX_VP(SRC_SWZ_Y_SHIFT
)) |
140 (src
.swz
[2] << NVFX_VP(SRC_SWZ_Z_SHIFT
)) |
141 (src
.swz
[3] << NVFX_VP(SRC_SWZ_W_SHIFT
)));
145 hw
[1] |= ((sr
& NVFX_VP(SRC0_HIGH_MASK
)) >>
146 NVFX_VP(SRC0_HIGH_SHIFT
)) << NVFX_VP(INST_SRC0H_SHIFT
);
147 hw
[2] |= (sr
& NVFX_VP(SRC0_LOW_MASK
)) <<
148 NVFX_VP(INST_SRC0L_SHIFT
);
151 hw
[2] |= sr
<< NVFX_VP(INST_SRC1_SHIFT
);
154 hw
[2] |= ((sr
& NVFX_VP(SRC2_HIGH_MASK
)) >>
155 NVFX_VP(SRC2_HIGH_SHIFT
)) << NVFX_VP(INST_SRC2H_SHIFT
);
156 hw
[3] |= (sr
& NVFX_VP(SRC2_LOW_MASK
)) <<
157 NVFX_VP(INST_SRC2L_SHIFT
);
165 emit_dst(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
, uint32_t *hw
, int slot
, struct nvfx_sreg dst
)
167 struct nvfx_vertex_program
*vp
= vpc
->vp
;
172 hw
[0] |= (dst
.index
<< NV30_VP_INST_DEST_TEMP_ID_SHIFT
);
174 hw
[3] |= NV40_VP_INST_DEST_MASK
;
176 hw
[0] |= (dst
.index
<<
177 NV40_VP_INST_VEC_DEST_TEMP_SHIFT
);
179 hw
[3] |= (dst
.index
<<
180 NV40_VP_INST_SCA_DEST_TEMP_SHIFT
);
185 /* TODO: this may be wrong because on nv30 COL0 and BFC0 are swapped */
187 case NVFX_VP_INST_DEST_CLIP(0):
189 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE0
;
190 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
192 case NVFX_VP_INST_DEST_CLIP(1):
194 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE1
;
195 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
197 case NVFX_VP_INST_DEST_CLIP(2):
199 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE2
;
200 dst
.index
= NVFX_VP(INST_DEST_FOGC
);
202 case NVFX_VP_INST_DEST_CLIP(3):
204 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE3
;
205 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
207 case NVFX_VP_INST_DEST_CLIP(4):
209 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE4
;
210 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
212 case NVFX_VP_INST_DEST_CLIP(5):
214 vp
->clip_ctrl
|= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE5
;
215 dst
.index
= NVFX_VP(INST_DEST_PSZ
);
220 case NV30_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
221 case NV30_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
222 case NV30_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
223 case NV30_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
224 case NV30_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
225 case NV30_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
226 case NV30_VP_INST_DEST_TC(0): vp
->or |= (1 << 14); break;
227 case NV30_VP_INST_DEST_TC(1): vp
->or |= (1 << 15); break;
228 case NV30_VP_INST_DEST_TC(2): vp
->or |= (1 << 16); break;
229 case NV30_VP_INST_DEST_TC(3): vp
->or |= (1 << 17); break;
230 case NV30_VP_INST_DEST_TC(4): vp
->or |= (1 << 18); break;
231 case NV30_VP_INST_DEST_TC(5): vp
->or |= (1 << 19); break;
232 case NV30_VP_INST_DEST_TC(6): vp
->or |= (1 << 20); break;
233 case NV30_VP_INST_DEST_TC(7): vp
->or |= (1 << 21); break;
237 case NV40_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
238 case NV40_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
239 case NV40_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
240 case NV40_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
241 case NV40_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
242 case NV40_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
243 case NV40_VP_INST_DEST_TC(0): vp
->or |= (1 << 14); break;
244 case NV40_VP_INST_DEST_TC(1): vp
->or |= (1 << 15); break;
245 case NV40_VP_INST_DEST_TC(2): vp
->or |= (1 << 16); break;
246 case NV40_VP_INST_DEST_TC(3): vp
->or |= (1 << 17); break;
247 case NV40_VP_INST_DEST_TC(4): vp
->or |= (1 << 18); break;
248 case NV40_VP_INST_DEST_TC(5): vp
->or |= (1 << 19); break;
249 case NV40_VP_INST_DEST_TC(6): vp
->or |= (1 << 20); break;
250 case NV40_VP_INST_DEST_TC(7): vp
->or |= (1 << 21); break;
257 hw
[3] |= (dst
.index
<< NV30_VP_INST_DEST_SHIFT
);
258 hw
[0] |= NV30_VP_INST_VEC_DEST_TEMP_MASK
| (1<<20);
260 /*XXX: no way this is entirely correct, someone needs to
261 * figure out what exactly it is.
265 hw
[3] |= (dst
.index
<< NV40_VP_INST_DEST_SHIFT
);
267 hw
[0] |= NV40_VP_INST_VEC_RESULT
;
268 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1<<20);
270 hw
[3] |= NV40_VP_INST_SCA_RESULT
;
271 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
281 nvfx_vp_arith(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
, int slot
, int op
,
282 struct nvfx_sreg dst
, int mask
,
283 struct nvfx_sreg s0
, struct nvfx_sreg s1
,
286 struct nvfx_vertex_program
*vp
= vpc
->vp
;
289 vp
->insns
= realloc(vp
->insns
, ++vp
->nr_insns
* sizeof(*vpc
->vpi
));
290 vpc
->vpi
= &vp
->insns
[vp
->nr_insns
- 1];
291 memset(vpc
->vpi
, 0, sizeof(*vpc
->vpi
));
292 vpc
->vpi
->const_index
= -1;
296 hw
[0] |= (NVFX_COND_TR
<< NVFX_VP(INST_COND_SHIFT
));
297 hw
[0] |= ((0 << NVFX_VP(INST_COND_SWZ_X_SHIFT
)) |
298 (1 << NVFX_VP(INST_COND_SWZ_Y_SHIFT
)) |
299 (2 << NVFX_VP(INST_COND_SWZ_Z_SHIFT
)) |
300 (3 << NVFX_VP(INST_COND_SWZ_W_SHIFT
)));
304 hw
[1] |= (op
<< NV30_VP_INST_VEC_OPCODE_SHIFT
);
307 hw
[0] |= ((op
>> 4) << NV30_VP_INST_SCA_OPCODEH_SHIFT
);
308 hw
[1] |= ((op
& 0xf) << NV30_VP_INST_SCA_OPCODEL_SHIFT
);
310 // hw[3] |= NVFX_VP(INST_SCA_DEST_TEMP_MASK);
311 // hw[3] |= (mask << NVFX_VP(INST_VEC_WRITEMASK_SHIFT));
313 if (dst
.type
== NVFXSR_OUTPUT
) {
315 hw
[3] |= (mask
<< NV30_VP_INST_SDEST_WRITEMASK_SHIFT
);
317 hw
[3] |= (mask
<< NV30_VP_INST_VDEST_WRITEMASK_SHIFT
);
320 hw
[3] |= (mask
<< NV30_VP_INST_STEMP_WRITEMASK_SHIFT
);
322 hw
[3] |= (mask
<< NV30_VP_INST_VTEMP_WRITEMASK_SHIFT
);
326 hw
[1] |= (op
<< NV40_VP_INST_VEC_OPCODE_SHIFT
);
327 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
328 hw
[3] |= (mask
<< NV40_VP_INST_VEC_WRITEMASK_SHIFT
);
330 hw
[1] |= (op
<< NV40_VP_INST_SCA_OPCODE_SHIFT
);
331 hw
[0] |= (NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1 << 20));
332 hw
[3] |= (mask
<< NV40_VP_INST_SCA_WRITEMASK_SHIFT
);
336 emit_dst(nvfx
, vpc
, hw
, slot
, dst
);
337 emit_src(nvfx
, vpc
, hw
, 0, s0
);
338 emit_src(nvfx
, vpc
, hw
, 1, s1
);
339 emit_src(nvfx
, vpc
, hw
, 2, s2
);
342 static inline struct nvfx_sreg
343 tgsi_src(struct nvfx_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
) {
344 struct nvfx_sreg src
= { 0 };
346 switch (fsrc
->Register
.File
) {
347 case TGSI_FILE_INPUT
:
348 src
= nvfx_sr(NVFXSR_INPUT
, fsrc
->Register
.Index
);
350 case TGSI_FILE_CONSTANT
:
351 src
= constant(vpc
, fsrc
->Register
.Index
, 0, 0, 0, 0);
353 case TGSI_FILE_IMMEDIATE
:
354 src
= vpc
->imm
[fsrc
->Register
.Index
];
356 case TGSI_FILE_TEMPORARY
:
357 src
= vpc
->r_temp
[fsrc
->Register
.Index
];
360 NOUVEAU_ERR("bad src file\n");
364 src
.abs
= fsrc
->Register
.Absolute
;
365 src
.negate
= fsrc
->Register
.Negate
;
366 src
.swz
[0] = fsrc
->Register
.SwizzleX
;
367 src
.swz
[1] = fsrc
->Register
.SwizzleY
;
368 src
.swz
[2] = fsrc
->Register
.SwizzleZ
;
369 src
.swz
[3] = fsrc
->Register
.SwizzleW
;
373 static INLINE
struct nvfx_sreg
374 tgsi_dst(struct nvfx_vpc
*vpc
, const struct tgsi_full_dst_register
*fdst
) {
375 struct nvfx_sreg dst
= { 0 };
377 switch (fdst
->Register
.File
) {
378 case TGSI_FILE_OUTPUT
:
379 dst
= vpc
->r_result
[fdst
->Register
.Index
];
381 case TGSI_FILE_TEMPORARY
:
382 dst
= vpc
->r_temp
[fdst
->Register
.Index
];
384 case TGSI_FILE_ADDRESS
:
385 dst
= vpc
->r_address
[fdst
->Register
.Index
];
388 NOUVEAU_ERR("bad dst file %i\n", fdst
->Register
.File
);
400 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= NVFX_VP_MASK_X
;
401 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= NVFX_VP_MASK_Y
;
402 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= NVFX_VP_MASK_Z
;
403 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= NVFX_VP_MASK_W
;
408 nvfx_vertprog_parse_instruction(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
,
409 const struct tgsi_full_instruction
*finst
)
411 struct nvfx_sreg src
[3], dst
, tmp
;
412 struct nvfx_sreg none
= nvfx_sr(NVFXSR_NONE
, 0);
414 int ai
= -1, ci
= -1, ii
= -1;
417 if (finst
->Instruction
.Opcode
== TGSI_OPCODE_END
)
420 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
421 const struct tgsi_full_src_register
*fsrc
;
423 fsrc
= &finst
->Src
[i
];
424 if (fsrc
->Register
.File
== TGSI_FILE_TEMPORARY
) {
425 src
[i
] = tgsi_src(vpc
, fsrc
);
429 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
430 const struct tgsi_full_src_register
*fsrc
;
432 fsrc
= &finst
->Src
[i
];
434 switch (fsrc
->Register
.File
) {
435 case TGSI_FILE_INPUT
:
436 if (ai
== -1 || ai
== fsrc
->Register
.Index
) {
437 ai
= fsrc
->Register
.Index
;
438 src
[i
] = tgsi_src(vpc
, fsrc
);
441 arith(vpc
, VEC
, MOV
, src
[i
], NVFX_VP_MASK_ALL
,
442 tgsi_src(vpc
, fsrc
), none
, none
);
445 case TGSI_FILE_CONSTANT
:
446 if ((ci
== -1 && ii
== -1) ||
447 ci
== fsrc
->Register
.Index
) {
448 ci
= fsrc
->Register
.Index
;
449 src
[i
] = tgsi_src(vpc
, fsrc
);
452 arith(vpc
, VEC
, MOV
, src
[i
], NVFX_VP_MASK_ALL
,
453 tgsi_src(vpc
, fsrc
), none
, none
);
456 case TGSI_FILE_IMMEDIATE
:
457 if ((ci
== -1 && ii
== -1) ||
458 ii
== fsrc
->Register
.Index
) {
459 ii
= fsrc
->Register
.Index
;
460 src
[i
] = tgsi_src(vpc
, fsrc
);
463 arith(vpc
, VEC
, MOV
, src
[i
], NVFX_VP_MASK_ALL
,
464 tgsi_src(vpc
, fsrc
), none
, none
);
467 case TGSI_FILE_TEMPORARY
:
471 NOUVEAU_ERR("bad src file\n");
476 dst
= tgsi_dst(vpc
, &finst
->Dst
[0]);
477 mask
= tgsi_mask(finst
->Dst
[0].Register
.WriteMask
);
479 switch (finst
->Instruction
.Opcode
) {
480 case TGSI_OPCODE_ABS
:
481 arith(vpc
, VEC
, MOV
, dst
, mask
, abs(src
[0]), none
, none
);
483 case TGSI_OPCODE_ADD
:
484 arith(vpc
, VEC
, ADD
, dst
, mask
, src
[0], none
, src
[1]);
486 case TGSI_OPCODE_ARL
:
487 arith(vpc
, VEC
, ARL
, dst
, mask
, src
[0], none
, none
);
489 case TGSI_OPCODE_COS
:
490 arith(vpc
, SCA
, COS
, dst
, mask
, none
, none
, src
[0]);
492 case TGSI_OPCODE_DP3
:
493 arith(vpc
, VEC
, DP3
, dst
, mask
, src
[0], src
[1], none
);
495 case TGSI_OPCODE_DP4
:
496 arith(vpc
, VEC
, DP4
, dst
, mask
, src
[0], src
[1], none
);
498 case TGSI_OPCODE_DPH
:
499 arith(vpc
, VEC
, DPH
, dst
, mask
, src
[0], src
[1], none
);
501 case TGSI_OPCODE_DST
:
502 arith(vpc
, VEC
, DST
, dst
, mask
, src
[0], src
[1], none
);
504 case TGSI_OPCODE_EX2
:
505 arith(vpc
, SCA
, EX2
, dst
, mask
, none
, none
, src
[0]);
507 case TGSI_OPCODE_EXP
:
508 arith(vpc
, SCA
, EXP
, dst
, mask
, none
, none
, src
[0]);
510 case TGSI_OPCODE_FLR
:
511 arith(vpc
, VEC
, FLR
, dst
, mask
, src
[0], none
, none
);
513 case TGSI_OPCODE_FRC
:
514 arith(vpc
, VEC
, FRC
, dst
, mask
, src
[0], none
, none
);
516 case TGSI_OPCODE_LG2
:
517 arith(vpc
, SCA
, LG2
, dst
, mask
, none
, none
, src
[0]);
519 case TGSI_OPCODE_LIT
:
520 arith(vpc
, SCA
, LIT
, dst
, mask
, none
, none
, src
[0]);
522 case TGSI_OPCODE_LOG
:
523 arith(vpc
, SCA
, LOG
, dst
, mask
, none
, none
, src
[0]);
525 case TGSI_OPCODE_LRP
:
527 arith(vpc
, VEC
, MAD
, tmp
, mask
, neg(src
[0]), src
[2], src
[2]);
528 arith(vpc
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], tmp
);
530 case TGSI_OPCODE_MAD
:
531 arith(vpc
, VEC
, MAD
, dst
, mask
, src
[0], src
[1], src
[2]);
533 case TGSI_OPCODE_MAX
:
534 arith(vpc
, VEC
, MAX
, dst
, mask
, src
[0], src
[1], none
);
536 case TGSI_OPCODE_MIN
:
537 arith(vpc
, VEC
, MIN
, dst
, mask
, src
[0], src
[1], none
);
539 case TGSI_OPCODE_MOV
:
540 arith(vpc
, VEC
, MOV
, dst
, mask
, src
[0], none
, none
);
542 case TGSI_OPCODE_MUL
:
543 arith(vpc
, VEC
, MUL
, dst
, mask
, src
[0], src
[1], none
);
545 case TGSI_OPCODE_POW
:
547 arith(vpc
, SCA
, LG2
, tmp
, NVFX_VP_MASK_X
, none
, none
,
548 swz(src
[0], X
, X
, X
, X
));
549 arith(vpc
, VEC
, MUL
, tmp
, NVFX_VP_MASK_X
, swz(tmp
, X
, X
, X
, X
),
550 swz(src
[1], X
, X
, X
, X
), none
);
551 arith(vpc
, SCA
, EX2
, dst
, mask
, none
, none
,
552 swz(tmp
, X
, X
, X
, X
));
554 case TGSI_OPCODE_RCP
:
555 arith(vpc
, SCA
, RCP
, dst
, mask
, none
, none
, src
[0]);
557 case TGSI_OPCODE_RET
:
559 case TGSI_OPCODE_RSQ
:
560 arith(vpc
, SCA
, RSQ
, dst
, mask
, none
, none
, abs(src
[0]));
562 case TGSI_OPCODE_SEQ
:
563 arith(vpc
, VEC
, SEQ
, dst
, mask
, src
[0], src
[1], none
);
565 case TGSI_OPCODE_SFL
:
566 arith(vpc
, VEC
, SFL
, dst
, mask
, src
[0], src
[1], none
);
568 case TGSI_OPCODE_SGE
:
569 arith(vpc
, VEC
, SGE
, dst
, mask
, src
[0], src
[1], none
);
571 case TGSI_OPCODE_SGT
:
572 arith(vpc
, VEC
, SGT
, dst
, mask
, src
[0], src
[1], none
);
574 case TGSI_OPCODE_SIN
:
575 arith(vpc
, SCA
, SIN
, dst
, mask
, none
, none
, src
[0]);
577 case TGSI_OPCODE_SLE
:
578 arith(vpc
, VEC
, SLE
, dst
, mask
, src
[0], src
[1], none
);
580 case TGSI_OPCODE_SLT
:
581 arith(vpc
, VEC
, SLT
, dst
, mask
, src
[0], src
[1], none
);
583 case TGSI_OPCODE_SNE
:
584 arith(vpc
, VEC
, SNE
, dst
, mask
, src
[0], src
[1], none
);
586 case TGSI_OPCODE_SSG
:
587 arith(vpc
, VEC
, SSG
, dst
, mask
, src
[0], src
[1], none
);
589 case TGSI_OPCODE_STR
:
590 arith(vpc
, VEC
, STR
, dst
, mask
, src
[0], src
[1], none
);
592 case TGSI_OPCODE_SUB
:
593 arith(vpc
, VEC
, ADD
, dst
, mask
, src
[0], none
, neg(src
[1]));
595 case TGSI_OPCODE_XPD
:
597 arith(vpc
, VEC
, MUL
, tmp
, mask
,
598 swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
);
599 arith(vpc
, VEC
, MAD
, dst
, (mask
& ~NVFX_VP_MASK_W
),
600 swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
),
604 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
613 nvfx_vertprog_parse_decl_output(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
,
614 const struct tgsi_full_declaration
*fdec
)
616 unsigned idx
= fdec
->Range
.First
;
619 switch (fdec
->Semantic
.Name
) {
620 case TGSI_SEMANTIC_POSITION
:
621 hw
= NVFX_VP(INST_DEST_POS
);
624 case TGSI_SEMANTIC_COLOR
:
625 if (fdec
->Semantic
.Index
== 0) {
626 hw
= NVFX_VP(INST_DEST_COL0
);
628 if (fdec
->Semantic
.Index
== 1) {
629 hw
= NVFX_VP(INST_DEST_COL1
);
631 NOUVEAU_ERR("bad colour semantic index\n");
635 case TGSI_SEMANTIC_BCOLOR
:
636 if (fdec
->Semantic
.Index
== 0) {
637 hw
= NVFX_VP(INST_DEST_BFC0
);
639 if (fdec
->Semantic
.Index
== 1) {
640 hw
= NVFX_VP(INST_DEST_BFC1
);
642 NOUVEAU_ERR("bad bcolour semantic index\n");
646 case TGSI_SEMANTIC_FOG
:
647 hw
= NVFX_VP(INST_DEST_FOGC
);
649 case TGSI_SEMANTIC_PSIZE
:
650 hw
= NVFX_VP(INST_DEST_PSZ
);
652 case TGSI_SEMANTIC_GENERIC
:
653 hw
= (vpc
->vp
->generic_to_fp_input
[fdec
->Semantic
.Index
] & 0xf)
654 + NVFX_VP(INST_DEST_TC(0)) - NVFX_FP_OP_INPUT_SRC_TC(0);
656 case TGSI_SEMANTIC_EDGEFLAG
:
657 /* not really an error just a fallback */
658 NOUVEAU_ERR("cannot handle edgeflag output\n");
661 NOUVEAU_ERR("bad output semantic\n");
665 vpc
->r_result
[idx
] = nvfx_sr(NVFXSR_OUTPUT
, hw
);
670 nvfx_vertprog_prepare(struct nvfx_context
* nvfx
, struct nvfx_vpc
*vpc
)
672 struct tgsi_parse_context p
;
673 int high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
674 struct util_semantic_set set
;
675 unsigned char sem_layout
[8];
676 unsigned sem_layout_size
;
677 unsigned num_outputs
;
679 num_outputs
= util_semantic_set_from_program_file(&set
, vpc
->vp
->pipe
.tokens
, TGSI_FILE_OUTPUT
);
681 if(num_outputs
> 8) {
682 NOUVEAU_ERR("too many vertex program outputs: %i\n", num_outputs
);
685 util_semantic_layout_from_set(sem_layout
, &set
, 8, 8);
687 /* hope 0xf is (0, 0, 0, 1) initialized; otherwise, we are _probably_ not required to do this */
688 memset(vpc
->vp
->generic_to_fp_input
, 0x0f, sizeof(vpc
->vp
->generic_to_fp_input
));
689 vpc
->vp
->texcoord_ouput_mask
= 0;
690 for(int i
= 0; i
< 8; ++i
) {
691 if(sem_layout
[i
] == 0xff)
693 vpc
->vp
->texcoord_ouput_mask
|= (1 << i
);
694 //printf("vp: GENERIC[%i] to fpreg %i\n", sem_layout[i], NVFX_FP_OP_INPUT_SRC_TC(0) + i);
695 vpc
->vp
->generic_to_fp_input
[sem_layout
[i
]] = 0xf0 | (NVFX_FP_OP_INPUT_SRC_TC(0) + i
);
698 tgsi_parse_init(&p
, vpc
->vp
->pipe
.tokens
);
699 while (!tgsi_parse_end_of_tokens(&p
)) {
700 const union tgsi_full_token
*tok
= &p
.FullToken
;
702 tgsi_parse_token(&p
);
703 switch(tok
->Token
.Type
) {
704 case TGSI_TOKEN_TYPE_IMMEDIATE
:
707 case TGSI_TOKEN_TYPE_DECLARATION
:
709 const struct tgsi_full_declaration
*fdec
;
711 fdec
= &p
.FullToken
.FullDeclaration
;
712 switch (fdec
->Declaration
.File
) {
713 case TGSI_FILE_TEMPORARY
:
714 if (fdec
->Range
.Last
> high_temp
) {
719 #if 0 /* this would be nice.. except gallium doesn't track it */
720 case TGSI_FILE_ADDRESS
:
721 if (fdec
->Range
.Last
> high_addr
) {
727 case TGSI_FILE_OUTPUT
:
728 if (!nvfx_vertprog_parse_decl_output(nvfx
, vpc
, fdec
))
736 #if 1 /* yay, parse instructions looking for address regs instead */
737 case TGSI_TOKEN_TYPE_INSTRUCTION
:
739 const struct tgsi_full_instruction
*finst
;
740 const struct tgsi_full_dst_register
*fdst
;
742 finst
= &p
.FullToken
.FullInstruction
;
743 fdst
= &finst
->Dst
[0];
745 if (fdst
->Register
.File
== TGSI_FILE_ADDRESS
) {
746 if (fdst
->Register
.Index
> high_addr
)
747 high_addr
= fdst
->Register
.Index
;
760 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nvfx_sreg
));
765 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nvfx_sreg
));
766 for (i
= 0; i
< high_temp
; i
++)
767 vpc
->r_temp
[i
] = temp(vpc
);
771 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nvfx_sreg
));
772 for (i
= 0; i
< high_addr
; i
++)
773 vpc
->r_address
[i
] = temp(vpc
);
776 vpc
->r_temps_discard
= 0;
781 nvfx_vertprog_translate(struct nvfx_context
*nvfx
,
782 struct nvfx_vertex_program
*vp
)
784 struct tgsi_parse_context parse
;
785 struct nvfx_vpc
*vpc
= NULL
;
786 struct nvfx_sreg none
= nvfx_sr(NVFXSR_NONE
, 0);
789 vpc
= CALLOC(1, sizeof(struct nvfx_vpc
));
794 if (!nvfx_vertprog_prepare(nvfx
, vpc
)) {
799 /* Redirect post-transform vertex position to a temp if user clip
800 * planes are enabled. We need to append code to the vtxprog
801 * to handle clip planes later.
804 vpc
->r_result
[vpc
->hpos_idx
] = temp(vpc
);
805 vpc
->r_temps_discard
= 0;
808 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
810 while (!tgsi_parse_end_of_tokens(&parse
)) {
811 tgsi_parse_token(&parse
);
813 switch (parse
.FullToken
.Token
.Type
) {
814 case TGSI_TOKEN_TYPE_IMMEDIATE
:
816 const struct tgsi_full_immediate
*imm
;
818 imm
= &parse
.FullToken
.FullImmediate
;
819 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
820 assert(imm
->Immediate
.NrTokens
== 4 + 1);
821 vpc
->imm
[vpc
->nr_imm
++] =
829 case TGSI_TOKEN_TYPE_INSTRUCTION
:
831 const struct tgsi_full_instruction
*finst
;
832 finst
= &parse
.FullToken
.FullInstruction
;
833 if (!nvfx_vertprog_parse_instruction(nvfx
, vpc
, finst
))
842 /* Write out HPOS if it was redirected to a temp earlier */
843 if (vpc
->r_result
[vpc
->hpos_idx
].type
!= NVFXSR_OUTPUT
) {
844 struct nvfx_sreg hpos
= nvfx_sr(NVFXSR_OUTPUT
,
845 NVFX_VP(INST_DEST_POS
));
846 struct nvfx_sreg htmp
= vpc
->r_result
[vpc
->hpos_idx
];
848 arith(vpc
, VEC
, MOV
, hpos
, NVFX_VP_MASK_ALL
, htmp
, none
, none
);
851 /* Insert code to handle user clip planes */
852 for (i
= 0; i
< vp
->ucp
.nr
; i
++) {
853 struct nvfx_sreg cdst
= nvfx_sr(NVFXSR_OUTPUT
,
854 NVFX_VP_INST_DEST_CLIP(i
));
855 struct nvfx_sreg ceqn
= constant(vpc
, -1,
856 nvfx
->clip
.ucp
[i
][0],
857 nvfx
->clip
.ucp
[i
][1],
858 nvfx
->clip
.ucp
[i
][2],
859 nvfx
->clip
.ucp
[i
][3]);
860 struct nvfx_sreg htmp
= vpc
->r_result
[vpc
->hpos_idx
];
864 case 0: case 3: mask
= NVFX_VP_MASK_Y
; break;
865 case 1: case 4: mask
= NVFX_VP_MASK_Z
; break;
866 case 2: case 5: mask
= NVFX_VP_MASK_W
; break;
868 NOUVEAU_ERR("invalid clip dist #%d\n", i
);
872 arith(vpc
, VEC
, DP4
, cdst
, mask
, htmp
, ceqn
, none
);
875 vp
->insns
[vp
->nr_insns
- 1].data
[3] |= NVFX_VP_INST_LAST
;
876 vp
->translated
= TRUE
;
878 tgsi_parse_free(&parse
);
882 FREE(vpc
->r_address
);
889 nvfx_vertprog_validate(struct nvfx_context
*nvfx
)
891 struct pipe_context
*pipe
= &nvfx
->pipe
;
892 struct nvfx_screen
*screen
= nvfx
->screen
;
893 struct nouveau_channel
*chan
= screen
->base
.channel
;
894 struct nouveau_grobj
*eng3d
= screen
->eng3d
;
895 struct nvfx_vertex_program
*vp
;
896 struct pipe_resource
*constbuf
;
897 boolean upload_code
= FALSE
, upload_data
= FALSE
;
900 if (nvfx
->render_mode
== HW
) {
902 constbuf
= nvfx
->constbuf
[PIPE_SHADER_VERTEX
];
904 // TODO: ouch! can't we just use constant slots for these?!
905 if ((nvfx
->dirty
& NVFX_NEW_UCP
) ||
906 memcmp(&nvfx
->clip
, &vp
->ucp
, sizeof(vp
->ucp
))) {
907 nvfx_vertprog_destroy(nvfx
, vp
);
908 memcpy(&vp
->ucp
, &nvfx
->clip
, sizeof(vp
->ucp
));
911 vp
= nvfx
->swtnl
.vertprog
;
915 /* Translate TGSI shader into hw bytecode */
918 nvfx
->fallback_swtnl
&= ~NVFX_NEW_VERTPROG
;
919 nvfx_vertprog_translate(nvfx
, vp
);
920 if (!vp
->translated
) {
921 nvfx
->fallback_swtnl
|= NVFX_NEW_VERTPROG
;
926 /* Allocate hw vtxprog exec slots */
928 struct nouveau_resource
*heap
= nvfx
->screen
->vp_exec_heap
;
929 uint vplen
= vp
->nr_insns
;
931 if (nouveau_resource_alloc(heap
, vplen
, vp
, &vp
->exec
)) {
932 while (heap
->next
&& heap
->size
< vplen
) {
933 struct nvfx_vertex_program
*evict
;
935 evict
= heap
->next
->priv
;
936 nouveau_resource_free(&evict
->exec
);
939 if (nouveau_resource_alloc(heap
, vplen
, vp
, &vp
->exec
))
946 /* Allocate hw vtxprog const slots */
947 if (vp
->nr_consts
&& !vp
->data
) {
948 struct nouveau_resource
*heap
= nvfx
->screen
->vp_data_heap
;
950 if (nouveau_resource_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
)) {
951 while (heap
->next
&& heap
->size
< vp
->nr_consts
) {
952 struct nvfx_vertex_program
*evict
;
954 evict
= heap
->next
->priv
;
955 nouveau_resource_free(&evict
->data
);
958 if (nouveau_resource_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
))
962 /*XXX: handle this some day */
963 assert(vp
->data
->start
>= vp
->data_start_min
);
966 if (vp
->data_start
!= vp
->data
->start
)
970 /* If exec or data segments moved we need to patch the program to
971 * fixup offsets and register IDs.
973 if (vp
->exec_start
!= vp
->exec
->start
) {
974 for (i
= 0; i
< vp
->nr_insns
; i
++) {
975 struct nvfx_vertex_program_exec
*vpi
= &vp
->insns
[i
];
977 if (vpi
->has_branch_offset
) {
982 vp
->exec_start
= vp
->exec
->start
;
985 if (vp
->nr_consts
&& vp
->data_start
!= vp
->data
->start
) {
986 for (i
= 0; i
< vp
->nr_insns
; i
++) {
987 struct nvfx_vertex_program_exec
*vpi
= &vp
->insns
[i
];
989 if (vpi
->const_index
>= 0) {
990 vpi
->data
[1] &= ~NVFX_VP(INST_CONST_SRC_MASK
);
992 (vpi
->const_index
+ vp
->data
->start
) <<
993 NVFX_VP(INST_CONST_SRC_SHIFT
);
998 vp
->data_start
= vp
->data
->start
;
1001 /* Update + Upload constant values */
1002 if (vp
->nr_consts
) {
1006 map
= nvfx_buffer(constbuf
)->data
;
1008 for (i
= 0; i
< vp
->nr_consts
; i
++) {
1009 struct nvfx_vertex_program_data
*vpd
= &vp
->consts
[i
];
1011 if (vpd
->index
>= 0) {
1013 !memcmp(vpd
->value
, &map
[vpd
->index
* 4],
1016 memcpy(vpd
->value
, &map
[vpd
->index
* 4],
1020 BEGIN_RING(chan
, eng3d
, NV34TCL_VP_UPLOAD_CONST_ID
, 5);
1021 OUT_RING (chan
, i
+ vp
->data
->start
);
1022 OUT_RINGp (chan
, (uint32_t *)vpd
->value
, 4);
1026 /* Upload vtxprog */
1029 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1030 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[0]);
1031 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[1]);
1032 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[2]);
1033 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[3]);
1036 BEGIN_RING(chan
, eng3d
, NV34TCL_VP_UPLOAD_FROM_ID
, 1);
1037 OUT_RING (chan
, vp
->exec
->start
);
1038 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1039 BEGIN_RING(chan
, eng3d
, NV34TCL_VP_UPLOAD_INST(0), 4);
1040 OUT_RINGp (chan
, vp
->insns
[i
].data
, 4);
1044 if(nvfx
->dirty
& (NVFX_NEW_VERTPROG
| NVFX_NEW_UCP
))
1047 OUT_RING(chan
, RING_3D(NV34TCL_VP_START_FROM_ID
, 1));
1048 OUT_RING(chan
, vp
->exec
->start
);
1050 OUT_RING(chan
, RING_3D(NV40TCL_VP_ATTRIB_EN
, 2));
1051 OUT_RING(chan
, vp
->ir
);
1052 OUT_RING(chan
, vp
->or);
1054 OUT_RING(chan
, RING_3D(NV34TCL_VP_CLIP_PLANES_ENABLE
, 1));
1055 OUT_RING(chan
, vp
->clip_ctrl
);
1062 nvfx_vertprog_destroy(struct nvfx_context
*nvfx
, struct nvfx_vertex_program
*vp
)
1064 vp
->translated
= FALSE
;
1072 if (vp
->nr_consts
) {
1078 nouveau_resource_free(&vp
->exec
);
1080 nouveau_resource_free(&vp
->data
);
1082 vp
->data_start_min
= 0;
1084 vp
->ir
= vp
->or = vp
->clip_ctrl
= 0;