1 #include "pipe/p_context.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_state.h"
5 #include "pipe/p_shader_tokens.h"
6 #include "tgsi/util/tgsi_parse.h"
7 #include "tgsi/util/tgsi_util.h"
9 #include "nv40_context.h"
10 #include "nv40_state.h"
12 /* TODO (at least...):
13 * 1. Indexed consts + ARL
14 * 3. NV_vp11, NV_vp2, NV_vp3 features
15 * - extra arith opcodes
31 #define MASK_ALL (MASK_X|MASK_Y|MASK_Z|MASK_W)
34 #include "nv40_shader.h"
36 #define swz(s,x,y,z,w) nv40_sr_swz((s), SWZ_##x, SWZ_##y, SWZ_##z, SWZ_##w)
37 #define neg(s) nv40_sr_neg((s))
38 #define abs(s) nv40_sr_abs((s))
40 #define NV40_VP_INST_DEST_CLIP(n) ((~0 - 6) + (n))
43 struct nv40_vertex_program
*vp
;
45 struct nv40_vertex_program_exec
*vpi
;
48 unsigned r_temps_discard
;
49 struct nv40_sreg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
50 struct nv40_sreg
*r_address
;
51 struct nv40_sreg
*r_temp
;
53 struct nv40_sreg
*imm
;
57 static struct nv40_sreg
58 temp(struct nv40_vpc
*vpc
)
60 int idx
= ffs(~vpc
->r_temps
) - 1;
63 NOUVEAU_ERR("out of temps!!\n");
65 return nv40_sr(NV40SR_TEMP
, 0);
68 vpc
->r_temps
|= (1 << idx
);
69 vpc
->r_temps_discard
|= (1 << idx
);
70 return nv40_sr(NV40SR_TEMP
, idx
);
74 release_temps(struct nv40_vpc
*vpc
)
76 vpc
->r_temps
&= ~vpc
->r_temps_discard
;
77 vpc
->r_temps_discard
= 0;
80 static struct nv40_sreg
81 constant(struct nv40_vpc
*vpc
, int pipe
, float x
, float y
, float z
, float w
)
83 struct nv40_vertex_program
*vp
= vpc
->vp
;
84 struct nv40_vertex_program_data
*vpd
;
88 for (idx
= 0; idx
< vp
->nr_consts
; idx
++) {
89 if (vp
->consts
[idx
].index
== pipe
)
90 return nv40_sr(NV40SR_CONST
, idx
);
94 idx
= vp
->nr_consts
++;
95 vp
->consts
= realloc(vp
->consts
, sizeof(*vpd
) * vp
->nr_consts
);
96 vpd
= &vp
->consts
[idx
];
103 return nv40_sr(NV40SR_CONST
, idx
);
106 #define arith(cc,s,o,d,m,s0,s1,s2) \
107 nv40_vp_arith((cc), (s), NV40_VP_INST_##o, (d), (m), (s0), (s1), (s2))
110 emit_src(struct nv40_vpc
*vpc
, uint32_t *hw
, int pos
, struct nv40_sreg src
)
112 struct nv40_vertex_program
*vp
= vpc
->vp
;
117 sr
|= (NV40_VP_SRC_REG_TYPE_TEMP
<< NV40_VP_SRC_REG_TYPE_SHIFT
);
118 sr
|= (src
.index
<< NV40_VP_SRC_TEMP_SRC_SHIFT
);
121 sr
|= (NV40_VP_SRC_REG_TYPE_INPUT
<<
122 NV40_VP_SRC_REG_TYPE_SHIFT
);
123 vp
->ir
|= (1 << src
.index
);
124 hw
[1] |= (src
.index
<< NV40_VP_INST_INPUT_SRC_SHIFT
);
127 sr
|= (NV40_VP_SRC_REG_TYPE_CONST
<<
128 NV40_VP_SRC_REG_TYPE_SHIFT
);
129 assert(vpc
->vpi
->const_index
== -1 ||
130 vpc
->vpi
->const_index
== src
.index
);
131 vpc
->vpi
->const_index
= src
.index
;
134 sr
|= (NV40_VP_SRC_REG_TYPE_INPUT
<<
135 NV40_VP_SRC_REG_TYPE_SHIFT
);
142 sr
|= NV40_VP_SRC_NEGATE
;
145 hw
[0] |= (1 << (21 + pos
));
147 sr
|= ((src
.swz
[0] << NV40_VP_SRC_SWZ_X_SHIFT
) |
148 (src
.swz
[1] << NV40_VP_SRC_SWZ_Y_SHIFT
) |
149 (src
.swz
[2] << NV40_VP_SRC_SWZ_Z_SHIFT
) |
150 (src
.swz
[3] << NV40_VP_SRC_SWZ_W_SHIFT
));
154 hw
[1] |= ((sr
& NV40_VP_SRC0_HIGH_MASK
) >>
155 NV40_VP_SRC0_HIGH_SHIFT
) << NV40_VP_INST_SRC0H_SHIFT
;
156 hw
[2] |= (sr
& NV40_VP_SRC0_LOW_MASK
) <<
157 NV40_VP_INST_SRC0L_SHIFT
;
160 hw
[2] |= sr
<< NV40_VP_INST_SRC1_SHIFT
;
163 hw
[2] |= ((sr
& NV40_VP_SRC2_HIGH_MASK
) >>
164 NV40_VP_SRC2_HIGH_SHIFT
) << NV40_VP_INST_SRC2H_SHIFT
;
165 hw
[3] |= (sr
& NV40_VP_SRC2_LOW_MASK
) <<
166 NV40_VP_INST_SRC2L_SHIFT
;
174 emit_dst(struct nv40_vpc
*vpc
, uint32_t *hw
, int slot
, struct nv40_sreg dst
)
176 struct nv40_vertex_program
*vp
= vpc
->vp
;
180 hw
[3] |= NV40_VP_INST_DEST_MASK
;
182 hw
[0] |= (dst
.index
<<
183 NV40_VP_INST_VEC_DEST_TEMP_SHIFT
);
185 hw
[3] |= (dst
.index
<<
186 NV40_VP_INST_SCA_DEST_TEMP_SHIFT
);
191 case NV40_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
192 case NV40_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
193 case NV40_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
194 case NV40_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
195 case NV40_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
196 case NV40_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
197 case NV40_VP_INST_DEST_TC(0): vp
->or |= (1 << 14); break;
198 case NV40_VP_INST_DEST_TC(1): vp
->or |= (1 << 15); break;
199 case NV40_VP_INST_DEST_TC(2): vp
->or |= (1 << 16); break;
200 case NV40_VP_INST_DEST_TC(3): vp
->or |= (1 << 17); break;
201 case NV40_VP_INST_DEST_TC(4): vp
->or |= (1 << 18); break;
202 case NV40_VP_INST_DEST_TC(5): vp
->or |= (1 << 19); break;
203 case NV40_VP_INST_DEST_TC(6): vp
->or |= (1 << 20); break;
204 case NV40_VP_INST_DEST_TC(7): vp
->or |= (1 << 21); break;
205 case NV40_VP_INST_DEST_CLIP(0):
207 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE0
;
208 dst
.index
= NV40_VP_INST_DEST_FOGC
;
210 case NV40_VP_INST_DEST_CLIP(1):
212 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE1
;
213 dst
.index
= NV40_VP_INST_DEST_FOGC
;
215 case NV40_VP_INST_DEST_CLIP(2):
217 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE2
;
218 dst
.index
= NV40_VP_INST_DEST_FOGC
;
220 case NV40_VP_INST_DEST_CLIP(3):
222 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE3
;
223 dst
.index
= NV40_VP_INST_DEST_PSZ
;
225 case NV40_VP_INST_DEST_CLIP(4):
227 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE4
;
228 dst
.index
= NV40_VP_INST_DEST_PSZ
;
230 case NV40_VP_INST_DEST_CLIP(5):
232 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE5
;
233 dst
.index
= NV40_VP_INST_DEST_PSZ
;
239 hw
[3] |= (dst
.index
<< NV40_VP_INST_DEST_SHIFT
);
241 hw
[0] |= NV40_VP_INST_VEC_RESULT
;
242 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1<<20);
244 hw
[3] |= NV40_VP_INST_SCA_RESULT
;
245 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
254 nv40_vp_arith(struct nv40_vpc
*vpc
, int slot
, int op
,
255 struct nv40_sreg dst
, int mask
,
256 struct nv40_sreg s0
, struct nv40_sreg s1
,
259 struct nv40_vertex_program
*vp
= vpc
->vp
;
262 vp
->insns
= realloc(vp
->insns
, ++vp
->nr_insns
* sizeof(*vpc
->vpi
));
263 vpc
->vpi
= &vp
->insns
[vp
->nr_insns
- 1];
264 memset(vpc
->vpi
, 0, sizeof(*vpc
->vpi
));
265 vpc
->vpi
->const_index
= -1;
269 hw
[0] |= (NV40_VP_INST_COND_TR
<< NV40_VP_INST_COND_SHIFT
);
270 hw
[0] |= ((0 << NV40_VP_INST_COND_SWZ_X_SHIFT
) |
271 (1 << NV40_VP_INST_COND_SWZ_Y_SHIFT
) |
272 (2 << NV40_VP_INST_COND_SWZ_Z_SHIFT
) |
273 (3 << NV40_VP_INST_COND_SWZ_W_SHIFT
));
276 hw
[1] |= (op
<< NV40_VP_INST_VEC_OPCODE_SHIFT
);
277 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
278 hw
[3] |= (mask
<< NV40_VP_INST_VEC_WRITEMASK_SHIFT
);
280 hw
[1] |= (op
<< NV40_VP_INST_SCA_OPCODE_SHIFT
);
281 hw
[0] |= (NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1 << 20));
282 hw
[3] |= (mask
<< NV40_VP_INST_SCA_WRITEMASK_SHIFT
);
285 emit_dst(vpc
, hw
, slot
, dst
);
286 emit_src(vpc
, hw
, 0, s0
);
287 emit_src(vpc
, hw
, 1, s1
);
288 emit_src(vpc
, hw
, 2, s2
);
291 static INLINE
struct nv40_sreg
292 tgsi_src(struct nv40_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
) {
293 struct nv40_sreg src
;
295 switch (fsrc
->SrcRegister
.File
) {
296 case TGSI_FILE_INPUT
:
297 src
= nv40_sr(NV40SR_INPUT
, fsrc
->SrcRegister
.Index
);
299 case TGSI_FILE_CONSTANT
:
300 src
= constant(vpc
, fsrc
->SrcRegister
.Index
, 0, 0, 0, 0);
302 case TGSI_FILE_IMMEDIATE
:
303 src
= vpc
->imm
[fsrc
->SrcRegister
.Index
];
305 case TGSI_FILE_TEMPORARY
:
306 src
= vpc
->r_temp
[fsrc
->SrcRegister
.Index
];
309 NOUVEAU_ERR("bad src file\n");
313 src
.abs
= fsrc
->SrcRegisterExtMod
.Absolute
;
314 src
.negate
= fsrc
->SrcRegister
.Negate
;
315 src
.swz
[0] = fsrc
->SrcRegister
.SwizzleX
;
316 src
.swz
[1] = fsrc
->SrcRegister
.SwizzleY
;
317 src
.swz
[2] = fsrc
->SrcRegister
.SwizzleZ
;
318 src
.swz
[3] = fsrc
->SrcRegister
.SwizzleW
;
322 static INLINE
struct nv40_sreg
323 tgsi_dst(struct nv40_vpc
*vpc
, const struct tgsi_full_dst_register
*fdst
) {
324 struct nv40_sreg dst
;
326 switch (fdst
->DstRegister
.File
) {
327 case TGSI_FILE_OUTPUT
:
328 dst
= vpc
->r_result
[fdst
->DstRegister
.Index
];
330 case TGSI_FILE_TEMPORARY
:
331 dst
= vpc
->r_temp
[fdst
->DstRegister
.Index
];
333 case TGSI_FILE_ADDRESS
:
334 dst
= vpc
->r_address
[fdst
->DstRegister
.Index
];
337 NOUVEAU_ERR("bad dst file\n");
349 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= MASK_X
;
350 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= MASK_Y
;
351 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= MASK_Z
;
352 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= MASK_W
;
357 src_native_swz(struct nv40_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
,
358 struct nv40_sreg
*src
)
360 const struct nv40_sreg none
= nv40_sr(NV40SR_NONE
, 0);
361 struct nv40_sreg tgsi
= tgsi_src(vpc
, fsrc
);
362 uint mask
= 0, zero_mask
= 0, one_mask
= 0, neg_mask
= 0;
363 uint neg
[4] = { fsrc
->SrcRegisterExtSwz
.NegateX
,
364 fsrc
->SrcRegisterExtSwz
.NegateY
,
365 fsrc
->SrcRegisterExtSwz
.NegateZ
,
366 fsrc
->SrcRegisterExtSwz
.NegateW
};
369 for (c
= 0; c
< 4; c
++) {
370 switch (tgsi_util_get_full_src_register_extswizzle(fsrc
, c
)) {
371 case TGSI_EXTSWIZZLE_X
:
372 case TGSI_EXTSWIZZLE_Y
:
373 case TGSI_EXTSWIZZLE_Z
:
374 case TGSI_EXTSWIZZLE_W
:
375 mask
|= tgsi_mask(1 << c
);
377 case TGSI_EXTSWIZZLE_ZERO
:
378 zero_mask
|= tgsi_mask(1 << c
);
381 case TGSI_EXTSWIZZLE_ONE
:
382 one_mask
|= tgsi_mask(1 << c
);
389 if (!tgsi
.negate
&& neg
[c
])
390 neg_mask
|= tgsi_mask(1 << c
);
393 if (mask
== MASK_ALL
&& !neg_mask
)
399 arith(vpc
, 0, OP_MOV
, *src
, mask
, tgsi
, none
, none
);
402 arith(vpc
, 0, OP_SFL
, *src
, zero_mask
, *src
, none
, none
);
405 arith(vpc
, 0, OP_STR
, *src
, one_mask
, *src
, none
, none
);
408 struct nv40_sreg one
= temp(vpc
);
409 arith(vpc
, 0, OP_STR
, one
, neg_mask
, one
, none
, none
);
410 arith(vpc
, 0, OP_MUL
, *src
, neg_mask
, *src
, neg(one
), none
);
417 nv40_vertprog_parse_instruction(struct nv40_vpc
*vpc
,
418 const struct tgsi_full_instruction
*finst
)
420 struct nv40_sreg src
[3], dst
, tmp
;
421 struct nv40_sreg none
= nv40_sr(NV40SR_NONE
, 0);
423 int ai
= -1, ci
= -1, ii
= -1;
427 struct nv40_sreg dst
;
431 if (finst
->Instruction
.Opcode
== TGSI_OPCODE_END
)
434 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
435 const struct tgsi_full_src_register
*fsrc
;
437 fsrc
= &finst
->FullSrcRegisters
[i
];
438 if (fsrc
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
) {
439 src
[i
] = tgsi_src(vpc
, fsrc
);
443 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
444 const struct tgsi_full_src_register
*fsrc
;
446 fsrc
= &finst
->FullSrcRegisters
[i
];
448 switch (fsrc
->SrcRegister
.File
) {
449 case TGSI_FILE_INPUT
:
450 case TGSI_FILE_CONSTANT
:
451 case TGSI_FILE_TEMPORARY
:
452 if (!src_native_swz(vpc
, fsrc
, &src
[i
]))
459 switch (fsrc
->SrcRegister
.File
) {
460 case TGSI_FILE_INPUT
:
461 if (ai
== -1 || ai
== fsrc
->SrcRegister
.Index
) {
462 ai
= fsrc
->SrcRegister
.Index
;
463 src
[i
] = tgsi_src(vpc
, fsrc
);
466 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
467 tgsi_src(vpc
, fsrc
), none
, none
);
470 case TGSI_FILE_CONSTANT
:
471 if ((ci
== -1 && ii
== -1) ||
472 ci
== fsrc
->SrcRegister
.Index
) {
473 ci
= fsrc
->SrcRegister
.Index
;
474 src
[i
] = tgsi_src(vpc
, fsrc
);
477 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
478 tgsi_src(vpc
, fsrc
), none
, none
);
481 case TGSI_FILE_IMMEDIATE
:
482 if ((ci
== -1 && ii
== -1) ||
483 ii
== fsrc
->SrcRegister
.Index
) {
484 ii
= fsrc
->SrcRegister
.Index
;
485 src
[i
] = tgsi_src(vpc
, fsrc
);
488 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
489 tgsi_src(vpc
, fsrc
), none
, none
);
492 case TGSI_FILE_TEMPORARY
:
496 NOUVEAU_ERR("bad src file\n");
501 dst
= tgsi_dst(vpc
, &finst
->FullDstRegisters
[0]);
502 mask
= tgsi_mask(finst
->FullDstRegisters
[0].DstRegister
.WriteMask
);
504 /* If writing to clip distance regs, need to modify instruction to
505 * change which component is written to. On NV40 the clip regs
506 * are the unused components (yzw) of FOGC/PSZ
509 if (dst
.type
== NV40SR_OUTPUT
&&
510 dst
.index
>= NV40_VP_INST_DEST_CLIP(0) &&
511 dst
.index
<= NV40_VP_INST_DEST_CLIP(5)) {
512 unsigned n
= dst
.index
- NV40_VP_INST_DEST_CLIP(0);
513 unsigned c
[] = { SWZ_Y
, SWZ_Z
, SWZ_W
, SWZ_Y
, SWZ_Z
, SWZ_W
};
515 { MASK_Y
, MASK_Z
, MASK_W
, MASK_Y
, MASK_Z
, MASK_W
};
517 /* Some instructions we can get away with swizzling and/or
518 * changing the writemask. Others, we'll use a temp reg.
520 switch (finst
->Instruction
.Opcode
) {
521 case TGSI_OPCODE_DST
:
522 case TGSI_OPCODE_EXP
:
523 case TGSI_OPCODE_LIT
:
524 case TGSI_OPCODE_LOG
:
525 case TGSI_OPCODE_XPD
:
531 case TGSI_OPCODE_DP3
:
532 case TGSI_OPCODE_DP4
:
533 case TGSI_OPCODE_DPH
:
534 case TGSI_OPCODE_POW
:
535 case TGSI_OPCODE_RCP
:
536 case TGSI_OPCODE_RSQ
:
540 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
541 src
[i
] = nv40_sr_swz(src
[i
],
542 c
[n
], c
[n
], c
[n
], c
[n
]);
549 switch (finst
->Instruction
.Opcode
) {
550 case TGSI_OPCODE_ABS
:
551 arith(vpc
, 0, OP_MOV
, dst
, mask
, abs(src
[0]), none
, none
);
553 case TGSI_OPCODE_ADD
:
554 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, src
[1]);
556 case TGSI_OPCODE_ARL
:
557 arith(vpc
, 0, OP_ARL
, dst
, mask
, src
[0], none
, none
);
559 case TGSI_OPCODE_DP3
:
560 arith(vpc
, 0, OP_DP3
, dst
, mask
, src
[0], src
[1], none
);
562 case TGSI_OPCODE_DP4
:
563 arith(vpc
, 0, OP_DP4
, dst
, mask
, src
[0], src
[1], none
);
565 case TGSI_OPCODE_DPH
:
566 arith(vpc
, 0, OP_DPH
, dst
, mask
, src
[0], src
[1], none
);
568 case TGSI_OPCODE_DST
:
569 arith(vpc
, 0, OP_DST
, dst
, mask
, src
[0], src
[1], none
);
571 case TGSI_OPCODE_EX2
:
572 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
, src
[0]);
574 case TGSI_OPCODE_EXP
:
575 arith(vpc
, 1, OP_EXP
, dst
, mask
, none
, none
, src
[0]);
577 case TGSI_OPCODE_FLR
:
578 arith(vpc
, 0, OP_FLR
, dst
, mask
, src
[0], none
, none
);
580 case TGSI_OPCODE_FRC
:
581 arith(vpc
, 0, OP_FRC
, dst
, mask
, src
[0], none
, none
);
583 case TGSI_OPCODE_LG2
:
584 arith(vpc
, 1, OP_LG2
, dst
, mask
, none
, none
, src
[0]);
586 case TGSI_OPCODE_LIT
:
587 arith(vpc
, 1, OP_LIT
, dst
, mask
, none
, none
, src
[0]);
589 case TGSI_OPCODE_LOG
:
590 arith(vpc
, 1, OP_LOG
, dst
, mask
, none
, none
, src
[0]);
592 case TGSI_OPCODE_MAD
:
593 arith(vpc
, 0, OP_MAD
, dst
, mask
, src
[0], src
[1], src
[2]);
595 case TGSI_OPCODE_MAX
:
596 arith(vpc
, 0, OP_MAX
, dst
, mask
, src
[0], src
[1], none
);
598 case TGSI_OPCODE_MIN
:
599 arith(vpc
, 0, OP_MIN
, dst
, mask
, src
[0], src
[1], none
);
601 case TGSI_OPCODE_MOV
:
602 arith(vpc
, 0, OP_MOV
, dst
, mask
, src
[0], none
, none
);
604 case TGSI_OPCODE_MUL
:
605 arith(vpc
, 0, OP_MUL
, dst
, mask
, src
[0], src
[1], none
);
607 case TGSI_OPCODE_POW
:
609 arith(vpc
, 1, OP_LG2
, tmp
, MASK_X
, none
, none
,
610 swz(src
[0], X
, X
, X
, X
));
611 arith(vpc
, 0, OP_MUL
, tmp
, MASK_X
, swz(tmp
, X
, X
, X
, X
),
612 swz(src
[1], X
, X
, X
, X
), none
);
613 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
,
614 swz(tmp
, X
, X
, X
, X
));
616 case TGSI_OPCODE_RCP
:
617 arith(vpc
, 1, OP_RCP
, dst
, mask
, none
, none
, src
[0]);
619 case TGSI_OPCODE_RET
:
621 case TGSI_OPCODE_RSQ
:
622 arith(vpc
, 1, OP_RSQ
, dst
, mask
, none
, none
, src
[0]);
624 case TGSI_OPCODE_SGE
:
625 arith(vpc
, 0, OP_SGE
, dst
, mask
, src
[0], src
[1], none
);
627 case TGSI_OPCODE_SLT
:
628 arith(vpc
, 0, OP_SLT
, dst
, mask
, src
[0], src
[1], none
);
630 case TGSI_OPCODE_SUB
:
631 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, neg(src
[1]));
633 case TGSI_OPCODE_XPD
:
635 arith(vpc
, 0, OP_MUL
, tmp
, mask
,
636 swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
);
637 arith(vpc
, 0, OP_MAD
, dst
, (mask
& ~MASK_W
),
638 swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
),
642 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
646 if (clip
.dst
.type
!= NV40SR_NONE
) {
647 arith(vpc
, 0, OP_MOV
, clip
.dst
, clip
.m
,
648 nv40_sr_swz(dst
, clip
.c
, clip
.c
, clip
.c
, clip
.c
),
657 nv40_vertprog_parse_decl_output(struct nv40_vpc
*vpc
,
658 const struct tgsi_full_declaration
*fdec
)
660 unsigned idx
= fdec
->u
.DeclarationRange
.First
;
663 switch (fdec
->Semantic
.SemanticName
) {
664 case TGSI_SEMANTIC_POSITION
:
665 hw
= NV40_VP_INST_DEST_POS
;
667 case TGSI_SEMANTIC_COLOR
:
668 if (fdec
->Semantic
.SemanticIndex
== 0) {
669 hw
= NV40_VP_INST_DEST_COL0
;
671 if (fdec
->Semantic
.SemanticIndex
== 1) {
672 hw
= NV40_VP_INST_DEST_COL1
;
674 NOUVEAU_ERR("bad colour semantic index\n");
678 case TGSI_SEMANTIC_BCOLOR
:
679 if (fdec
->Semantic
.SemanticIndex
== 0) {
680 hw
= NV40_VP_INST_DEST_BFC0
;
682 if (fdec
->Semantic
.SemanticIndex
== 1) {
683 hw
= NV40_VP_INST_DEST_BFC1
;
685 NOUVEAU_ERR("bad bcolour semantic index\n");
689 case TGSI_SEMANTIC_FOG
:
690 hw
= NV40_VP_INST_DEST_FOGC
;
692 case TGSI_SEMANTIC_PSIZE
:
693 hw
= NV40_VP_INST_DEST_PSZ
;
695 case TGSI_SEMANTIC_GENERIC
:
696 if (fdec
->Semantic
.SemanticIndex
<= 7) {
697 hw
= NV40_VP_INST_DEST_TC(fdec
->Semantic
.SemanticIndex
);
699 NOUVEAU_ERR("bad generic semantic index\n");
704 case TGSI_SEMANTIC_CLIP
:
705 if (fdec
->Semantic
.SemanticIndex
>= 6) {
706 NOUVEAU_ERR("bad clip distance index\n");
709 hw
= NV40_VP_INST_DEST_CLIP(fdec
->Semantic
.SemanticIndex
);
713 NOUVEAU_ERR("bad output semantic\n");
717 vpc
->r_result
[idx
] = nv40_sr(NV40SR_OUTPUT
, hw
);
722 nv40_vertprog_prepare(struct nv40_vpc
*vpc
)
724 struct tgsi_parse_context p
;
725 int high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
727 tgsi_parse_init(&p
, vpc
->vp
->pipe
.tokens
);
728 while (!tgsi_parse_end_of_tokens(&p
)) {
729 const union tgsi_full_token
*tok
= &p
.FullToken
;
731 tgsi_parse_token(&p
);
732 switch(tok
->Token
.Type
) {
733 case TGSI_TOKEN_TYPE_IMMEDIATE
:
736 case TGSI_TOKEN_TYPE_DECLARATION
:
738 const struct tgsi_full_declaration
*fdec
;
740 fdec
= &p
.FullToken
.FullDeclaration
;
741 switch (fdec
->Declaration
.File
) {
742 case TGSI_FILE_TEMPORARY
:
743 if (fdec
->u
.DeclarationRange
.Last
> high_temp
) {
745 fdec
->u
.DeclarationRange
.Last
;
748 #if 0 /* this would be nice.. except gallium doesn't track it */
749 case TGSI_FILE_ADDRESS
:
750 if (fdec
->u
.DeclarationRange
.Last
> high_addr
) {
752 fdec
->u
.DeclarationRange
.Last
;
761 #if 1 /* yay, parse instructions looking for address regs instead */
762 case TGSI_TOKEN_TYPE_INSTRUCTION
:
764 const struct tgsi_full_instruction
*finst
;
765 const struct tgsi_full_dst_register
*fdst
;
767 finst
= &p
.FullToken
.FullInstruction
;
768 fdst
= &finst
->FullDstRegisters
[0];
770 if (fdst
->DstRegister
.File
== TGSI_FILE_ADDRESS
) {
771 if (fdst
->DstRegister
.Index
> high_addr
)
772 high_addr
= fdst
->DstRegister
.Index
;
785 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nv40_sreg
));
790 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nv40_sreg
));
791 for (i
= 0; i
< high_temp
; i
++)
792 vpc
->r_temp
[i
] = temp(vpc
);
796 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nv40_sreg
));
797 for (i
= 0; i
< high_addr
; i
++)
798 vpc
->r_address
[i
] = temp(vpc
);
801 vpc
->r_temps_discard
= 0;
806 nv40_vertprog_translate(struct nv40_context
*nv40
,
807 struct nv40_vertex_program
*vp
)
809 struct tgsi_parse_context parse
;
810 struct nv40_vpc
*vpc
= NULL
;
812 vpc
= CALLOC(1, sizeof(struct nv40_vpc
));
817 if (!nv40_vertprog_prepare(vpc
)) {
822 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
824 while (!tgsi_parse_end_of_tokens(&parse
)) {
825 tgsi_parse_token(&parse
);
827 switch (parse
.FullToken
.Token
.Type
) {
828 case TGSI_TOKEN_TYPE_DECLARATION
:
830 const struct tgsi_full_declaration
*fdec
;
831 fdec
= &parse
.FullToken
.FullDeclaration
;
832 switch (fdec
->Declaration
.File
) {
833 case TGSI_FILE_OUTPUT
:
834 if (!nv40_vertprog_parse_decl_output(vpc
, fdec
))
842 case TGSI_TOKEN_TYPE_IMMEDIATE
:
844 const struct tgsi_full_immediate
*imm
;
846 imm
= &parse
.FullToken
.FullImmediate
;
847 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
848 // assert(imm->Immediate.Size == 4);
849 vpc
->imm
[vpc
->nr_imm
++] =
851 imm
->u
.ImmediateFloat32
[0].Float
,
852 imm
->u
.ImmediateFloat32
[1].Float
,
853 imm
->u
.ImmediateFloat32
[2].Float
,
854 imm
->u
.ImmediateFloat32
[3].Float
);
857 case TGSI_TOKEN_TYPE_INSTRUCTION
:
859 const struct tgsi_full_instruction
*finst
;
860 finst
= &parse
.FullToken
.FullInstruction
;
861 if (!nv40_vertprog_parse_instruction(vpc
, finst
))
870 vp
->insns
[vp
->nr_insns
- 1].data
[3] |= NV40_VP_INST_LAST
;
871 vp
->translated
= TRUE
;
873 tgsi_parse_free(&parse
);
878 nv40_vertprog_validate(struct nv40_context
*nv40
)
880 struct nouveau_winsys
*nvws
= nv40
->nvws
;
881 struct pipe_winsys
*ws
= nv40
->pipe
.winsys
;
882 struct nouveau_grobj
*curie
= nv40
->screen
->curie
;
883 struct nv40_vertex_program
*vp
;
884 struct pipe_buffer
*constbuf
;
885 boolean upload_code
= FALSE
, upload_data
= FALSE
;
888 if (nv40
->render_mode
== HW
) {
890 constbuf
= nv40
->constbuf
[PIPE_SHADER_VERTEX
];
892 vp
= nv40
->swtnl
.vertprog
;
896 /* Translate TGSI shader into hw bytecode */
898 goto check_gpu_resources
;
900 nv40
->fallback_swtnl
&= ~NV40_NEW_VERTPROG
;
901 nv40_vertprog_translate(nv40
, vp
);
902 if (!vp
->translated
) {
903 nv40
->fallback_swtnl
|= NV40_NEW_VERTPROG
;
908 /* Allocate hw vtxprog exec slots */
910 struct nouveau_resource
*heap
= nv40
->screen
->vp_exec_heap
;
911 struct nouveau_stateobj
*so
;
912 uint vplen
= vp
->nr_insns
;
914 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
)) {
915 while (heap
->next
&& heap
->size
< vplen
) {
916 struct nv40_vertex_program
*evict
;
918 evict
= heap
->next
->priv
;
919 nvws
->res_free(&evict
->exec
);
922 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
))
927 so_method(so
, curie
, NV40TCL_VP_START_FROM_ID
, 1);
928 so_data (so
, vp
->exec
->start
);
929 so_method(so
, curie
, NV40TCL_VP_ATTRIB_EN
, 2);
930 so_data (so
, vp
->ir
);
931 so_data (so
, vp
->or);
932 so_method(so
, curie
, NV40TCL_CLIP_PLANE_ENABLE
, 1);
933 so_data (so
, vp
->clip_ctrl
);
939 /* Allocate hw vtxprog const slots */
940 if (vp
->nr_consts
&& !vp
->data
) {
941 struct nouveau_resource
*heap
= nv40
->screen
->vp_data_heap
;
943 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
)) {
944 while (heap
->next
&& heap
->size
< vp
->nr_consts
) {
945 struct nv40_vertex_program
*evict
;
947 evict
= heap
->next
->priv
;
948 nvws
->res_free(&evict
->data
);
951 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
))
955 /*XXX: handle this some day */
956 assert(vp
->data
->start
>= vp
->data_start_min
);
959 if (vp
->data_start
!= vp
->data
->start
)
963 /* If exec or data segments moved we need to patch the program to
964 * fixup offsets and register IDs.
966 if (vp
->exec_start
!= vp
->exec
->start
) {
967 for (i
= 0; i
< vp
->nr_insns
; i
++) {
968 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
970 if (vpi
->has_branch_offset
) {
975 vp
->exec_start
= vp
->exec
->start
;
978 if (vp
->nr_consts
&& vp
->data_start
!= vp
->data
->start
) {
979 for (i
= 0; i
< vp
->nr_insns
; i
++) {
980 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
982 if (vpi
->const_index
>= 0) {
983 vpi
->data
[1] &= ~NV40_VP_INST_CONST_SRC_MASK
;
985 (vpi
->const_index
+ vp
->data
->start
) <<
986 NV40_VP_INST_CONST_SRC_SHIFT
;
991 vp
->data_start
= vp
->data
->start
;
994 /* Update + Upload constant values */
999 map
= ws
->buffer_map(ws
, constbuf
,
1000 PIPE_BUFFER_USAGE_CPU_READ
);
1003 for (i
= 0; i
< vp
->nr_consts
; i
++) {
1004 struct nv40_vertex_program_data
*vpd
= &vp
->consts
[i
];
1006 if (vpd
->index
>= 0) {
1008 !memcmp(vpd
->value
, &map
[vpd
->index
* 4],
1011 memcpy(vpd
->value
, &map
[vpd
->index
* 4],
1015 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_CONST_ID
, 5);
1016 OUT_RING (i
+ vp
->data
->start
);
1017 OUT_RINGp ((uint32_t *)vpd
->value
, 4);
1021 ws
->buffer_unmap(ws
, constbuf
);
1024 /* Upload vtxprog */
1027 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1028 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[0]);
1029 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[1]);
1030 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[2]);
1031 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[3]);
1034 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_FROM_ID
, 1);
1035 OUT_RING (vp
->exec
->start
);
1036 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1037 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_INST(0), 4);
1038 OUT_RINGp (vp
->insns
[i
].data
, 4);
1042 if (vp
->so
!= nv40
->state
.hw
[NV40_STATE_VERTPROG
]) {
1043 so_ref(vp
->so
, &nv40
->state
.hw
[NV40_STATE_VERTPROG
]);
1051 nv40_vertprog_destroy(struct nv40_context
*nv40
, struct nv40_vertex_program
*vp
)
1059 struct nv40_state_entry nv40_state_vertprog
= {
1060 .validate
= nv40_vertprog_validate
,
1062 .pipe
= NV40_NEW_VERTPROG
,
1063 .hw
= NV40_STATE_VERTPROG
,