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);
514 { MASK_Y
, MASK_Z
, MASK_W
, MASK_Y
, MASK_Z
, MASK_W
};
516 /* Some instructions we can get away with swizzling and/or
517 * changing the writemask. Others, we'll use a temp reg.
519 switch (finst
->Instruction
.Opcode
) {
520 case TGSI_OPCODE_DST
:
521 case TGSI_OPCODE_EXP
:
522 case TGSI_OPCODE_LIT
:
523 case TGSI_OPCODE_LOG
:
524 case TGSI_OPCODE_XPD
:
529 case TGSI_OPCODE_DP3
:
530 case TGSI_OPCODE_DP4
:
531 case TGSI_OPCODE_DPH
:
532 case TGSI_OPCODE_POW
:
533 case TGSI_OPCODE_RCP
:
534 case TGSI_OPCODE_RSQ
:
538 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++)
539 src
[i
] = swz(src
[i
], X
, X
, X
, X
);
545 switch (finst
->Instruction
.Opcode
) {
546 case TGSI_OPCODE_ABS
:
547 arith(vpc
, 0, OP_MOV
, dst
, mask
, abs(src
[0]), none
, none
);
549 case TGSI_OPCODE_ADD
:
550 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, src
[1]);
552 case TGSI_OPCODE_ARL
:
553 arith(vpc
, 0, OP_ARL
, dst
, mask
, src
[0], none
, none
);
555 case TGSI_OPCODE_DP3
:
556 arith(vpc
, 0, OP_DP3
, dst
, mask
, src
[0], src
[1], none
);
558 case TGSI_OPCODE_DP4
:
559 arith(vpc
, 0, OP_DP4
, dst
, mask
, src
[0], src
[1], none
);
561 case TGSI_OPCODE_DPH
:
562 arith(vpc
, 0, OP_DPH
, dst
, mask
, src
[0], src
[1], none
);
564 case TGSI_OPCODE_DST
:
565 arith(vpc
, 0, OP_DST
, dst
, mask
, src
[0], src
[1], none
);
567 case TGSI_OPCODE_EX2
:
568 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
, src
[0]);
570 case TGSI_OPCODE_EXP
:
571 arith(vpc
, 1, OP_EXP
, dst
, mask
, none
, none
, src
[0]);
573 case TGSI_OPCODE_FLR
:
574 arith(vpc
, 0, OP_FLR
, dst
, mask
, src
[0], none
, none
);
576 case TGSI_OPCODE_FRC
:
577 arith(vpc
, 0, OP_FRC
, dst
, mask
, src
[0], none
, none
);
579 case TGSI_OPCODE_LG2
:
580 arith(vpc
, 1, OP_LG2
, dst
, mask
, none
, none
, src
[0]);
582 case TGSI_OPCODE_LIT
:
583 arith(vpc
, 1, OP_LIT
, dst
, mask
, none
, none
, src
[0]);
585 case TGSI_OPCODE_LOG
:
586 arith(vpc
, 1, OP_LOG
, dst
, mask
, none
, none
, src
[0]);
588 case TGSI_OPCODE_MAD
:
589 arith(vpc
, 0, OP_MAD
, dst
, mask
, src
[0], src
[1], src
[2]);
591 case TGSI_OPCODE_MAX
:
592 arith(vpc
, 0, OP_MAX
, dst
, mask
, src
[0], src
[1], none
);
594 case TGSI_OPCODE_MIN
:
595 arith(vpc
, 0, OP_MIN
, dst
, mask
, src
[0], src
[1], none
);
597 case TGSI_OPCODE_MOV
:
598 arith(vpc
, 0, OP_MOV
, dst
, mask
, src
[0], none
, none
);
600 case TGSI_OPCODE_MUL
:
601 arith(vpc
, 0, OP_MUL
, dst
, mask
, src
[0], src
[1], none
);
603 case TGSI_OPCODE_POW
:
605 arith(vpc
, 1, OP_LG2
, tmp
, MASK_X
, none
, none
,
606 swz(src
[0], X
, X
, X
, X
));
607 arith(vpc
, 0, OP_MUL
, tmp
, MASK_X
, swz(tmp
, X
, X
, X
, X
),
608 swz(src
[1], X
, X
, X
, X
), none
);
609 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
,
610 swz(tmp
, X
, X
, X
, X
));
612 case TGSI_OPCODE_RCP
:
613 arith(vpc
, 1, OP_RCP
, dst
, mask
, none
, none
, src
[0]);
615 case TGSI_OPCODE_RET
:
617 case TGSI_OPCODE_RSQ
:
618 arith(vpc
, 1, OP_RSQ
, dst
, mask
, none
, none
, src
[0]);
620 case TGSI_OPCODE_SGE
:
621 arith(vpc
, 0, OP_SGE
, dst
, mask
, src
[0], src
[1], none
);
623 case TGSI_OPCODE_SLT
:
624 arith(vpc
, 0, OP_SLT
, dst
, mask
, src
[0], src
[1], none
);
626 case TGSI_OPCODE_SUB
:
627 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, neg(src
[1]));
629 case TGSI_OPCODE_XPD
:
631 arith(vpc
, 0, OP_MUL
, tmp
, mask
,
632 swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
);
633 arith(vpc
, 0, OP_MAD
, dst
, (mask
& ~MASK_W
),
634 swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
),
638 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
642 if (clip
.dst
.type
!= NV40SR_NONE
) {
643 arith(vpc
, 0, OP_MOV
, clip
.dst
, clip
.m
,
644 swz(dst
, X
, X
, X
, X
), none
, none
);
652 nv40_vertprog_parse_decl_output(struct nv40_vpc
*vpc
,
653 const struct tgsi_full_declaration
*fdec
)
655 unsigned idx
= fdec
->u
.DeclarationRange
.First
;
658 switch (fdec
->Semantic
.SemanticName
) {
659 case TGSI_SEMANTIC_POSITION
:
660 hw
= NV40_VP_INST_DEST_POS
;
662 case TGSI_SEMANTIC_COLOR
:
663 if (fdec
->Semantic
.SemanticIndex
== 0) {
664 hw
= NV40_VP_INST_DEST_COL0
;
666 if (fdec
->Semantic
.SemanticIndex
== 1) {
667 hw
= NV40_VP_INST_DEST_COL1
;
669 NOUVEAU_ERR("bad colour semantic index\n");
673 case TGSI_SEMANTIC_BCOLOR
:
674 if (fdec
->Semantic
.SemanticIndex
== 0) {
675 hw
= NV40_VP_INST_DEST_BFC0
;
677 if (fdec
->Semantic
.SemanticIndex
== 1) {
678 hw
= NV40_VP_INST_DEST_BFC1
;
680 NOUVEAU_ERR("bad bcolour semantic index\n");
684 case TGSI_SEMANTIC_FOG
:
685 hw
= NV40_VP_INST_DEST_FOGC
;
687 case TGSI_SEMANTIC_PSIZE
:
688 hw
= NV40_VP_INST_DEST_PSZ
;
690 case TGSI_SEMANTIC_GENERIC
:
691 if (fdec
->Semantic
.SemanticIndex
<= 7) {
692 hw
= NV40_VP_INST_DEST_TC(fdec
->Semantic
.SemanticIndex
);
694 NOUVEAU_ERR("bad generic semantic index\n");
699 case TGSI_SEMANTIC_CLIP
:
700 if (fdec
->Semantic
.SemanticIndex
>= 6) {
701 NOUVEAU_ERR("bad clip distance index\n");
704 hw
= NV40_VP_INST_DEST_CLIP(fdec
->Semantic
.SemanticIndex
);
708 NOUVEAU_ERR("bad output semantic\n");
712 vpc
->r_result
[idx
] = nv40_sr(NV40SR_OUTPUT
, hw
);
717 nv40_vertprog_prepare(struct nv40_vpc
*vpc
)
719 struct tgsi_parse_context p
;
720 int high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
722 tgsi_parse_init(&p
, vpc
->vp
->pipe
.tokens
);
723 while (!tgsi_parse_end_of_tokens(&p
)) {
724 const union tgsi_full_token
*tok
= &p
.FullToken
;
726 tgsi_parse_token(&p
);
727 switch(tok
->Token
.Type
) {
728 case TGSI_TOKEN_TYPE_IMMEDIATE
:
731 case TGSI_TOKEN_TYPE_DECLARATION
:
733 const struct tgsi_full_declaration
*fdec
;
735 fdec
= &p
.FullToken
.FullDeclaration
;
736 switch (fdec
->Declaration
.File
) {
737 case TGSI_FILE_TEMPORARY
:
738 if (fdec
->u
.DeclarationRange
.Last
> high_temp
) {
740 fdec
->u
.DeclarationRange
.Last
;
743 #if 0 /* this would be nice.. except gallium doesn't track it */
744 case TGSI_FILE_ADDRESS
:
745 if (fdec
->u
.DeclarationRange
.Last
> high_addr
) {
747 fdec
->u
.DeclarationRange
.Last
;
756 #if 1 /* yay, parse instructions looking for address regs instead */
757 case TGSI_TOKEN_TYPE_INSTRUCTION
:
759 const struct tgsi_full_instruction
*finst
;
760 const struct tgsi_full_dst_register
*fdst
;
762 finst
= &p
.FullToken
.FullInstruction
;
763 fdst
= &finst
->FullDstRegisters
[0];
765 if (fdst
->DstRegister
.File
== TGSI_FILE_ADDRESS
) {
766 if (fdst
->DstRegister
.Index
> high_addr
)
767 high_addr
= fdst
->DstRegister
.Index
;
780 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nv40_sreg
));
785 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nv40_sreg
));
786 for (i
= 0; i
< high_temp
; i
++)
787 vpc
->r_temp
[i
] = temp(vpc
);
791 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nv40_sreg
));
792 for (i
= 0; i
< high_addr
; i
++)
793 vpc
->r_address
[i
] = temp(vpc
);
796 vpc
->r_temps_discard
= 0;
801 nv40_vertprog_translate(struct nv40_context
*nv40
,
802 struct nv40_vertex_program
*vp
)
804 struct tgsi_parse_context parse
;
805 struct nv40_vpc
*vpc
= NULL
;
807 vpc
= CALLOC(1, sizeof(struct nv40_vpc
));
812 if (!nv40_vertprog_prepare(vpc
)) {
817 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
819 while (!tgsi_parse_end_of_tokens(&parse
)) {
820 tgsi_parse_token(&parse
);
822 switch (parse
.FullToken
.Token
.Type
) {
823 case TGSI_TOKEN_TYPE_DECLARATION
:
825 const struct tgsi_full_declaration
*fdec
;
826 fdec
= &parse
.FullToken
.FullDeclaration
;
827 switch (fdec
->Declaration
.File
) {
828 case TGSI_FILE_OUTPUT
:
829 if (!nv40_vertprog_parse_decl_output(vpc
, fdec
))
837 case TGSI_TOKEN_TYPE_IMMEDIATE
:
839 const struct tgsi_full_immediate
*imm
;
841 imm
= &parse
.FullToken
.FullImmediate
;
842 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
843 // assert(imm->Immediate.Size == 4);
844 vpc
->imm
[vpc
->nr_imm
++] =
846 imm
->u
.ImmediateFloat32
[0].Float
,
847 imm
->u
.ImmediateFloat32
[1].Float
,
848 imm
->u
.ImmediateFloat32
[2].Float
,
849 imm
->u
.ImmediateFloat32
[3].Float
);
852 case TGSI_TOKEN_TYPE_INSTRUCTION
:
854 const struct tgsi_full_instruction
*finst
;
855 finst
= &parse
.FullToken
.FullInstruction
;
856 if (!nv40_vertprog_parse_instruction(vpc
, finst
))
865 vp
->insns
[vp
->nr_insns
- 1].data
[3] |= NV40_VP_INST_LAST
;
866 vp
->translated
= TRUE
;
868 tgsi_parse_free(&parse
);
873 nv40_vertprog_validate(struct nv40_context
*nv40
)
875 struct nouveau_winsys
*nvws
= nv40
->nvws
;
876 struct pipe_winsys
*ws
= nv40
->pipe
.winsys
;
877 struct nouveau_grobj
*curie
= nv40
->screen
->curie
;
878 struct nv40_vertex_program
*vp
;
879 struct pipe_buffer
*constbuf
;
880 boolean upload_code
= FALSE
, upload_data
= FALSE
;
883 if (nv40
->render_mode
== HW
) {
885 constbuf
= nv40
->constbuf
[PIPE_SHADER_VERTEX
];
887 vp
= nv40
->swtnl
.vertprog
;
891 /* Translate TGSI shader into hw bytecode */
893 goto check_gpu_resources
;
895 nv40
->fallback_swtnl
&= ~NV40_NEW_VERTPROG
;
896 nv40_vertprog_translate(nv40
, vp
);
897 if (!vp
->translated
) {
898 nv40
->fallback_swtnl
|= NV40_NEW_VERTPROG
;
903 /* Allocate hw vtxprog exec slots */
905 struct nouveau_resource
*heap
= nv40
->screen
->vp_exec_heap
;
906 struct nouveau_stateobj
*so
;
907 uint vplen
= vp
->nr_insns
;
909 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
)) {
910 while (heap
->next
&& heap
->size
< vplen
) {
911 struct nv40_vertex_program
*evict
;
913 evict
= heap
->next
->priv
;
914 nvws
->res_free(&evict
->exec
);
917 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
))
922 so_method(so
, curie
, NV40TCL_VP_START_FROM_ID
, 1);
923 so_data (so
, vp
->exec
->start
);
924 so_method(so
, curie
, NV40TCL_VP_ATTRIB_EN
, 2);
925 so_data (so
, vp
->ir
);
926 so_data (so
, vp
->or);
927 so_method(so
, curie
, NV40TCL_CLIP_PLANE_ENABLE
, 1);
928 so_data (so
, vp
->clip_ctrl
);
934 /* Allocate hw vtxprog const slots */
935 if (vp
->nr_consts
&& !vp
->data
) {
936 struct nouveau_resource
*heap
= nv40
->screen
->vp_data_heap
;
938 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
)) {
939 while (heap
->next
&& heap
->size
< vp
->nr_consts
) {
940 struct nv40_vertex_program
*evict
;
942 evict
= heap
->next
->priv
;
943 nvws
->res_free(&evict
->data
);
946 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
))
950 /*XXX: handle this some day */
951 assert(vp
->data
->start
>= vp
->data_start_min
);
954 if (vp
->data_start
!= vp
->data
->start
)
958 /* If exec or data segments moved we need to patch the program to
959 * fixup offsets and register IDs.
961 if (vp
->exec_start
!= vp
->exec
->start
) {
962 for (i
= 0; i
< vp
->nr_insns
; i
++) {
963 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
965 if (vpi
->has_branch_offset
) {
970 vp
->exec_start
= vp
->exec
->start
;
973 if (vp
->nr_consts
&& vp
->data_start
!= vp
->data
->start
) {
974 for (i
= 0; i
< vp
->nr_insns
; i
++) {
975 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
977 if (vpi
->const_index
>= 0) {
978 vpi
->data
[1] &= ~NV40_VP_INST_CONST_SRC_MASK
;
980 (vpi
->const_index
+ vp
->data
->start
) <<
981 NV40_VP_INST_CONST_SRC_SHIFT
;
986 vp
->data_start
= vp
->data
->start
;
989 /* Update + Upload constant values */
994 map
= ws
->buffer_map(ws
, constbuf
,
995 PIPE_BUFFER_USAGE_CPU_READ
);
998 for (i
= 0; i
< vp
->nr_consts
; i
++) {
999 struct nv40_vertex_program_data
*vpd
= &vp
->consts
[i
];
1001 if (vpd
->index
>= 0) {
1003 !memcmp(vpd
->value
, &map
[vpd
->index
* 4],
1006 memcpy(vpd
->value
, &map
[vpd
->index
* 4],
1010 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_CONST_ID
, 5);
1011 OUT_RING (i
+ vp
->data
->start
);
1012 OUT_RINGp ((uint32_t *)vpd
->value
, 4);
1016 ws
->buffer_unmap(ws
, constbuf
);
1019 /* Upload vtxprog */
1022 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1023 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[0]);
1024 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[1]);
1025 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[2]);
1026 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[3]);
1029 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_FROM_ID
, 1);
1030 OUT_RING (vp
->exec
->start
);
1031 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1032 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_INST(0), 4);
1033 OUT_RINGp (vp
->insns
[i
].data
, 4);
1037 if (vp
->so
!= nv40
->state
.hw
[NV40_STATE_VERTPROG
]) {
1038 so_ref(vp
->so
, &nv40
->state
.hw
[NV40_STATE_VERTPROG
]);
1046 nv40_vertprog_destroy(struct nv40_context
*nv40
, struct nv40_vertex_program
*vp
)
1054 struct nv40_state_entry nv40_state_vertprog
= {
1055 .validate
= nv40_vertprog_validate
,
1057 .pipe
= NV40_NEW_VERTPROG
,
1058 .hw
= NV40_STATE_VERTPROG
,