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/tgsi_parse.h"
7 #include "tgsi/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
;
59 static struct nv40_sreg
60 temp(struct nv40_vpc
*vpc
)
62 int idx
= ffs(~vpc
->r_temps
) - 1;
65 NOUVEAU_ERR("out of temps!!\n");
67 return nv40_sr(NV40SR_TEMP
, 0);
70 vpc
->r_temps
|= (1 << idx
);
71 vpc
->r_temps_discard
|= (1 << idx
);
72 return nv40_sr(NV40SR_TEMP
, idx
);
76 release_temps(struct nv40_vpc
*vpc
)
78 vpc
->r_temps
&= ~vpc
->r_temps_discard
;
79 vpc
->r_temps_discard
= 0;
82 static struct nv40_sreg
83 constant(struct nv40_vpc
*vpc
, int pipe
, float x
, float y
, float z
, float w
)
85 struct nv40_vertex_program
*vp
= vpc
->vp
;
86 struct nv40_vertex_program_data
*vpd
;
90 for (idx
= 0; idx
< vp
->nr_consts
; idx
++) {
91 if (vp
->consts
[idx
].index
== pipe
)
92 return nv40_sr(NV40SR_CONST
, idx
);
96 idx
= vp
->nr_consts
++;
97 vp
->consts
= realloc(vp
->consts
, sizeof(*vpd
) * vp
->nr_consts
);
98 vpd
= &vp
->consts
[idx
];
105 return nv40_sr(NV40SR_CONST
, idx
);
108 #define arith(cc,s,o,d,m,s0,s1,s2) \
109 nv40_vp_arith((cc), (s), NV40_VP_INST_##o, (d), (m), (s0), (s1), (s2))
112 emit_src(struct nv40_vpc
*vpc
, uint32_t *hw
, int pos
, struct nv40_sreg src
)
114 struct nv40_vertex_program
*vp
= vpc
->vp
;
119 sr
|= (NV40_VP_SRC_REG_TYPE_TEMP
<< NV40_VP_SRC_REG_TYPE_SHIFT
);
120 sr
|= (src
.index
<< NV40_VP_SRC_TEMP_SRC_SHIFT
);
123 sr
|= (NV40_VP_SRC_REG_TYPE_INPUT
<<
124 NV40_VP_SRC_REG_TYPE_SHIFT
);
125 vp
->ir
|= (1 << src
.index
);
126 hw
[1] |= (src
.index
<< NV40_VP_INST_INPUT_SRC_SHIFT
);
129 sr
|= (NV40_VP_SRC_REG_TYPE_CONST
<<
130 NV40_VP_SRC_REG_TYPE_SHIFT
);
131 assert(vpc
->vpi
->const_index
== -1 ||
132 vpc
->vpi
->const_index
== src
.index
);
133 vpc
->vpi
->const_index
= src
.index
;
136 sr
|= (NV40_VP_SRC_REG_TYPE_INPUT
<<
137 NV40_VP_SRC_REG_TYPE_SHIFT
);
144 sr
|= NV40_VP_SRC_NEGATE
;
147 hw
[0] |= (1 << (21 + pos
));
149 sr
|= ((src
.swz
[0] << NV40_VP_SRC_SWZ_X_SHIFT
) |
150 (src
.swz
[1] << NV40_VP_SRC_SWZ_Y_SHIFT
) |
151 (src
.swz
[2] << NV40_VP_SRC_SWZ_Z_SHIFT
) |
152 (src
.swz
[3] << NV40_VP_SRC_SWZ_W_SHIFT
));
156 hw
[1] |= ((sr
& NV40_VP_SRC0_HIGH_MASK
) >>
157 NV40_VP_SRC0_HIGH_SHIFT
) << NV40_VP_INST_SRC0H_SHIFT
;
158 hw
[2] |= (sr
& NV40_VP_SRC0_LOW_MASK
) <<
159 NV40_VP_INST_SRC0L_SHIFT
;
162 hw
[2] |= sr
<< NV40_VP_INST_SRC1_SHIFT
;
165 hw
[2] |= ((sr
& NV40_VP_SRC2_HIGH_MASK
) >>
166 NV40_VP_SRC2_HIGH_SHIFT
) << NV40_VP_INST_SRC2H_SHIFT
;
167 hw
[3] |= (sr
& NV40_VP_SRC2_LOW_MASK
) <<
168 NV40_VP_INST_SRC2L_SHIFT
;
176 emit_dst(struct nv40_vpc
*vpc
, uint32_t *hw
, int slot
, struct nv40_sreg dst
)
178 struct nv40_vertex_program
*vp
= vpc
->vp
;
182 hw
[3] |= NV40_VP_INST_DEST_MASK
;
184 hw
[0] |= (dst
.index
<<
185 NV40_VP_INST_VEC_DEST_TEMP_SHIFT
);
187 hw
[3] |= (dst
.index
<<
188 NV40_VP_INST_SCA_DEST_TEMP_SHIFT
);
193 case NV40_VP_INST_DEST_COL0
: vp
->or |= (1 << 0); break;
194 case NV40_VP_INST_DEST_COL1
: vp
->or |= (1 << 1); break;
195 case NV40_VP_INST_DEST_BFC0
: vp
->or |= (1 << 2); break;
196 case NV40_VP_INST_DEST_BFC1
: vp
->or |= (1 << 3); break;
197 case NV40_VP_INST_DEST_FOGC
: vp
->or |= (1 << 4); break;
198 case NV40_VP_INST_DEST_PSZ
: vp
->or |= (1 << 5); break;
199 case NV40_VP_INST_DEST_TC(0): vp
->or |= (1 << 14); break;
200 case NV40_VP_INST_DEST_TC(1): vp
->or |= (1 << 15); break;
201 case NV40_VP_INST_DEST_TC(2): vp
->or |= (1 << 16); break;
202 case NV40_VP_INST_DEST_TC(3): vp
->or |= (1 << 17); break;
203 case NV40_VP_INST_DEST_TC(4): vp
->or |= (1 << 18); break;
204 case NV40_VP_INST_DEST_TC(5): vp
->or |= (1 << 19); break;
205 case NV40_VP_INST_DEST_TC(6): vp
->or |= (1 << 20); break;
206 case NV40_VP_INST_DEST_TC(7): vp
->or |= (1 << 21); break;
207 case NV40_VP_INST_DEST_CLIP(0):
209 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE0
;
210 dst
.index
= NV40_VP_INST_DEST_FOGC
;
212 case NV40_VP_INST_DEST_CLIP(1):
214 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE1
;
215 dst
.index
= NV40_VP_INST_DEST_FOGC
;
217 case NV40_VP_INST_DEST_CLIP(2):
219 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE2
;
220 dst
.index
= NV40_VP_INST_DEST_FOGC
;
222 case NV40_VP_INST_DEST_CLIP(3):
224 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE3
;
225 dst
.index
= NV40_VP_INST_DEST_PSZ
;
227 case NV40_VP_INST_DEST_CLIP(4):
229 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE4
;
230 dst
.index
= NV40_VP_INST_DEST_PSZ
;
232 case NV40_VP_INST_DEST_CLIP(5):
234 vp
->clip_ctrl
|= NV40TCL_CLIP_PLANE_ENABLE_PLANE5
;
235 dst
.index
= NV40_VP_INST_DEST_PSZ
;
241 hw
[3] |= (dst
.index
<< NV40_VP_INST_DEST_SHIFT
);
243 hw
[0] |= NV40_VP_INST_VEC_RESULT
;
244 hw
[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1<<20);
246 hw
[3] |= NV40_VP_INST_SCA_RESULT
;
247 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
256 nv40_vp_arith(struct nv40_vpc
*vpc
, int slot
, int op
,
257 struct nv40_sreg dst
, int mask
,
258 struct nv40_sreg s0
, struct nv40_sreg s1
,
261 struct nv40_vertex_program
*vp
= vpc
->vp
;
264 vp
->insns
= realloc(vp
->insns
, ++vp
->nr_insns
* sizeof(*vpc
->vpi
));
265 vpc
->vpi
= &vp
->insns
[vp
->nr_insns
- 1];
266 memset(vpc
->vpi
, 0, sizeof(*vpc
->vpi
));
267 vpc
->vpi
->const_index
= -1;
271 hw
[0] |= (NV40_VP_INST_COND_TR
<< NV40_VP_INST_COND_SHIFT
);
272 hw
[0] |= ((0 << NV40_VP_INST_COND_SWZ_X_SHIFT
) |
273 (1 << NV40_VP_INST_COND_SWZ_Y_SHIFT
) |
274 (2 << NV40_VP_INST_COND_SWZ_Z_SHIFT
) |
275 (3 << NV40_VP_INST_COND_SWZ_W_SHIFT
));
278 hw
[1] |= (op
<< NV40_VP_INST_VEC_OPCODE_SHIFT
);
279 hw
[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK
;
280 hw
[3] |= (mask
<< NV40_VP_INST_VEC_WRITEMASK_SHIFT
);
282 hw
[1] |= (op
<< NV40_VP_INST_SCA_OPCODE_SHIFT
);
283 hw
[0] |= (NV40_VP_INST_VEC_DEST_TEMP_MASK
| (1 << 20));
284 hw
[3] |= (mask
<< NV40_VP_INST_SCA_WRITEMASK_SHIFT
);
287 emit_dst(vpc
, hw
, slot
, dst
);
288 emit_src(vpc
, hw
, 0, s0
);
289 emit_src(vpc
, hw
, 1, s1
);
290 emit_src(vpc
, hw
, 2, s2
);
293 static INLINE
struct nv40_sreg
294 tgsi_src(struct nv40_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
) {
295 struct nv40_sreg src
;
297 switch (fsrc
->SrcRegister
.File
) {
298 case TGSI_FILE_INPUT
:
299 src
= nv40_sr(NV40SR_INPUT
, fsrc
->SrcRegister
.Index
);
301 case TGSI_FILE_CONSTANT
:
302 src
= constant(vpc
, fsrc
->SrcRegister
.Index
, 0, 0, 0, 0);
304 case TGSI_FILE_IMMEDIATE
:
305 src
= vpc
->imm
[fsrc
->SrcRegister
.Index
];
307 case TGSI_FILE_TEMPORARY
:
308 src
= vpc
->r_temp
[fsrc
->SrcRegister
.Index
];
311 NOUVEAU_ERR("bad src file\n");
315 src
.abs
= fsrc
->SrcRegisterExtMod
.Absolute
;
316 src
.negate
= fsrc
->SrcRegister
.Negate
;
317 src
.swz
[0] = fsrc
->SrcRegister
.SwizzleX
;
318 src
.swz
[1] = fsrc
->SrcRegister
.SwizzleY
;
319 src
.swz
[2] = fsrc
->SrcRegister
.SwizzleZ
;
320 src
.swz
[3] = fsrc
->SrcRegister
.SwizzleW
;
324 static INLINE
struct nv40_sreg
325 tgsi_dst(struct nv40_vpc
*vpc
, const struct tgsi_full_dst_register
*fdst
) {
326 struct nv40_sreg dst
;
328 switch (fdst
->DstRegister
.File
) {
329 case TGSI_FILE_OUTPUT
:
330 dst
= vpc
->r_result
[fdst
->DstRegister
.Index
];
332 case TGSI_FILE_TEMPORARY
:
333 dst
= vpc
->r_temp
[fdst
->DstRegister
.Index
];
335 case TGSI_FILE_ADDRESS
:
336 dst
= vpc
->r_address
[fdst
->DstRegister
.Index
];
339 NOUVEAU_ERR("bad dst file\n");
351 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= MASK_X
;
352 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= MASK_Y
;
353 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= MASK_Z
;
354 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= MASK_W
;
359 src_native_swz(struct nv40_vpc
*vpc
, const struct tgsi_full_src_register
*fsrc
,
360 struct nv40_sreg
*src
)
362 const struct nv40_sreg none
= nv40_sr(NV40SR_NONE
, 0);
363 struct nv40_sreg tgsi
= tgsi_src(vpc
, fsrc
);
364 uint mask
= 0, zero_mask
= 0, one_mask
= 0, neg_mask
= 0;
365 uint neg
[4] = { fsrc
->SrcRegisterExtSwz
.NegateX
,
366 fsrc
->SrcRegisterExtSwz
.NegateY
,
367 fsrc
->SrcRegisterExtSwz
.NegateZ
,
368 fsrc
->SrcRegisterExtSwz
.NegateW
};
371 for (c
= 0; c
< 4; c
++) {
372 switch (tgsi_util_get_full_src_register_extswizzle(fsrc
, c
)) {
373 case TGSI_EXTSWIZZLE_X
:
374 case TGSI_EXTSWIZZLE_Y
:
375 case TGSI_EXTSWIZZLE_Z
:
376 case TGSI_EXTSWIZZLE_W
:
377 mask
|= tgsi_mask(1 << c
);
379 case TGSI_EXTSWIZZLE_ZERO
:
380 zero_mask
|= tgsi_mask(1 << c
);
383 case TGSI_EXTSWIZZLE_ONE
:
384 one_mask
|= tgsi_mask(1 << c
);
391 if (!tgsi
.negate
&& neg
[c
])
392 neg_mask
|= tgsi_mask(1 << c
);
395 if (mask
== MASK_ALL
&& !neg_mask
)
401 arith(vpc
, 0, OP_MOV
, *src
, mask
, tgsi
, none
, none
);
404 arith(vpc
, 0, OP_SFL
, *src
, zero_mask
, *src
, none
, none
);
407 arith(vpc
, 0, OP_STR
, *src
, one_mask
, *src
, none
, none
);
410 struct nv40_sreg one
= temp(vpc
);
411 arith(vpc
, 0, OP_STR
, one
, neg_mask
, one
, none
, none
);
412 arith(vpc
, 0, OP_MUL
, *src
, neg_mask
, *src
, neg(one
), none
);
419 nv40_vertprog_parse_instruction(struct nv40_vpc
*vpc
,
420 const struct tgsi_full_instruction
*finst
)
422 struct nv40_sreg src
[3], dst
, tmp
;
423 struct nv40_sreg none
= nv40_sr(NV40SR_NONE
, 0);
425 int ai
= -1, ci
= -1, ii
= -1;
428 if (finst
->Instruction
.Opcode
== TGSI_OPCODE_END
)
431 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
432 const struct tgsi_full_src_register
*fsrc
;
434 fsrc
= &finst
->FullSrcRegisters
[i
];
435 if (fsrc
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
) {
436 src
[i
] = tgsi_src(vpc
, fsrc
);
440 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
441 const struct tgsi_full_src_register
*fsrc
;
443 fsrc
= &finst
->FullSrcRegisters
[i
];
445 switch (fsrc
->SrcRegister
.File
) {
446 case TGSI_FILE_INPUT
:
447 case TGSI_FILE_CONSTANT
:
448 case TGSI_FILE_TEMPORARY
:
449 if (!src_native_swz(vpc
, fsrc
, &src
[i
]))
456 switch (fsrc
->SrcRegister
.File
) {
457 case TGSI_FILE_INPUT
:
458 if (ai
== -1 || ai
== fsrc
->SrcRegister
.Index
) {
459 ai
= fsrc
->SrcRegister
.Index
;
460 src
[i
] = tgsi_src(vpc
, fsrc
);
463 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
464 tgsi_src(vpc
, fsrc
), none
, none
);
467 case TGSI_FILE_CONSTANT
:
468 if ((ci
== -1 && ii
== -1) ||
469 ci
== fsrc
->SrcRegister
.Index
) {
470 ci
= fsrc
->SrcRegister
.Index
;
471 src
[i
] = tgsi_src(vpc
, fsrc
);
474 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
475 tgsi_src(vpc
, fsrc
), none
, none
);
478 case TGSI_FILE_IMMEDIATE
:
479 if ((ci
== -1 && ii
== -1) ||
480 ii
== fsrc
->SrcRegister
.Index
) {
481 ii
= fsrc
->SrcRegister
.Index
;
482 src
[i
] = tgsi_src(vpc
, fsrc
);
485 arith(vpc
, 0, OP_MOV
, src
[i
], MASK_ALL
,
486 tgsi_src(vpc
, fsrc
), none
, none
);
489 case TGSI_FILE_TEMPORARY
:
493 NOUVEAU_ERR("bad src file\n");
498 dst
= tgsi_dst(vpc
, &finst
->FullDstRegisters
[0]);
499 mask
= tgsi_mask(finst
->FullDstRegisters
[0].DstRegister
.WriteMask
);
501 switch (finst
->Instruction
.Opcode
) {
502 case TGSI_OPCODE_ABS
:
503 arith(vpc
, 0, OP_MOV
, dst
, mask
, abs(src
[0]), none
, none
);
505 case TGSI_OPCODE_ADD
:
506 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, src
[1]);
508 case TGSI_OPCODE_ARL
:
509 arith(vpc
, 0, OP_ARL
, dst
, mask
, src
[0], none
, none
);
511 case TGSI_OPCODE_DP3
:
512 arith(vpc
, 0, OP_DP3
, dst
, mask
, src
[0], src
[1], none
);
514 case TGSI_OPCODE_DP4
:
515 arith(vpc
, 0, OP_DP4
, dst
, mask
, src
[0], src
[1], none
);
517 case TGSI_OPCODE_DPH
:
518 arith(vpc
, 0, OP_DPH
, dst
, mask
, src
[0], src
[1], none
);
520 case TGSI_OPCODE_DST
:
521 arith(vpc
, 0, OP_DST
, dst
, mask
, src
[0], src
[1], none
);
523 case TGSI_OPCODE_EX2
:
524 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
, src
[0]);
526 case TGSI_OPCODE_EXP
:
527 arith(vpc
, 1, OP_EXP
, dst
, mask
, none
, none
, src
[0]);
529 case TGSI_OPCODE_FLR
:
530 arith(vpc
, 0, OP_FLR
, dst
, mask
, src
[0], none
, none
);
532 case TGSI_OPCODE_FRC
:
533 arith(vpc
, 0, OP_FRC
, dst
, mask
, src
[0], none
, none
);
535 case TGSI_OPCODE_LG2
:
536 arith(vpc
, 1, OP_LG2
, dst
, mask
, none
, none
, src
[0]);
538 case TGSI_OPCODE_LIT
:
539 arith(vpc
, 1, OP_LIT
, dst
, mask
, none
, none
, src
[0]);
541 case TGSI_OPCODE_LOG
:
542 arith(vpc
, 1, OP_LOG
, dst
, mask
, none
, none
, src
[0]);
544 case TGSI_OPCODE_MAD
:
545 arith(vpc
, 0, OP_MAD
, dst
, mask
, src
[0], src
[1], src
[2]);
547 case TGSI_OPCODE_MAX
:
548 arith(vpc
, 0, OP_MAX
, dst
, mask
, src
[0], src
[1], none
);
550 case TGSI_OPCODE_MIN
:
551 arith(vpc
, 0, OP_MIN
, dst
, mask
, src
[0], src
[1], none
);
553 case TGSI_OPCODE_MOV
:
554 arith(vpc
, 0, OP_MOV
, dst
, mask
, src
[0], none
, none
);
556 case TGSI_OPCODE_MUL
:
557 arith(vpc
, 0, OP_MUL
, dst
, mask
, src
[0], src
[1], none
);
559 case TGSI_OPCODE_POW
:
561 arith(vpc
, 1, OP_LG2
, tmp
, MASK_X
, none
, none
,
562 swz(src
[0], X
, X
, X
, X
));
563 arith(vpc
, 0, OP_MUL
, tmp
, MASK_X
, swz(tmp
, X
, X
, X
, X
),
564 swz(src
[1], X
, X
, X
, X
), none
);
565 arith(vpc
, 1, OP_EX2
, dst
, mask
, none
, none
,
566 swz(tmp
, X
, X
, X
, X
));
568 case TGSI_OPCODE_RCP
:
569 arith(vpc
, 1, OP_RCP
, dst
, mask
, none
, none
, src
[0]);
571 case TGSI_OPCODE_RET
:
573 case TGSI_OPCODE_RSQ
:
574 arith(vpc
, 1, OP_RSQ
, dst
, mask
, none
, none
, abs(src
[0]));
576 case TGSI_OPCODE_SGE
:
577 arith(vpc
, 0, OP_SGE
, dst
, mask
, src
[0], src
[1], none
);
579 case TGSI_OPCODE_SLT
:
580 arith(vpc
, 0, OP_SLT
, dst
, mask
, src
[0], src
[1], none
);
582 case TGSI_OPCODE_SUB
:
583 arith(vpc
, 0, OP_ADD
, dst
, mask
, src
[0], none
, neg(src
[1]));
585 case TGSI_OPCODE_XPD
:
587 arith(vpc
, 0, OP_MUL
, tmp
, mask
,
588 swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
);
589 arith(vpc
, 0, OP_MAD
, dst
, (mask
& ~MASK_W
),
590 swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
),
594 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
603 nv40_vertprog_parse_decl_output(struct nv40_vpc
*vpc
,
604 const struct tgsi_full_declaration
*fdec
)
606 unsigned idx
= fdec
->DeclarationRange
.First
;
609 switch (fdec
->Semantic
.SemanticName
) {
610 case TGSI_SEMANTIC_POSITION
:
611 hw
= NV40_VP_INST_DEST_POS
;
614 case TGSI_SEMANTIC_COLOR
:
615 if (fdec
->Semantic
.SemanticIndex
== 0) {
616 hw
= NV40_VP_INST_DEST_COL0
;
618 if (fdec
->Semantic
.SemanticIndex
== 1) {
619 hw
= NV40_VP_INST_DEST_COL1
;
621 NOUVEAU_ERR("bad colour semantic index\n");
625 case TGSI_SEMANTIC_BCOLOR
:
626 if (fdec
->Semantic
.SemanticIndex
== 0) {
627 hw
= NV40_VP_INST_DEST_BFC0
;
629 if (fdec
->Semantic
.SemanticIndex
== 1) {
630 hw
= NV40_VP_INST_DEST_BFC1
;
632 NOUVEAU_ERR("bad bcolour semantic index\n");
636 case TGSI_SEMANTIC_FOG
:
637 hw
= NV40_VP_INST_DEST_FOGC
;
639 case TGSI_SEMANTIC_PSIZE
:
640 hw
= NV40_VP_INST_DEST_PSZ
;
642 case TGSI_SEMANTIC_GENERIC
:
643 if (fdec
->Semantic
.SemanticIndex
<= 7) {
644 hw
= NV40_VP_INST_DEST_TC(fdec
->Semantic
.SemanticIndex
);
646 NOUVEAU_ERR("bad generic semantic index\n");
651 NOUVEAU_ERR("bad output semantic\n");
655 vpc
->r_result
[idx
] = nv40_sr(NV40SR_OUTPUT
, hw
);
660 nv40_vertprog_prepare(struct nv40_vpc
*vpc
)
662 struct tgsi_parse_context p
;
663 int high_temp
= -1, high_addr
= -1, nr_imm
= 0, i
;
665 tgsi_parse_init(&p
, vpc
->vp
->pipe
.tokens
);
666 while (!tgsi_parse_end_of_tokens(&p
)) {
667 const union tgsi_full_token
*tok
= &p
.FullToken
;
669 tgsi_parse_token(&p
);
670 switch(tok
->Token
.Type
) {
671 case TGSI_TOKEN_TYPE_IMMEDIATE
:
674 case TGSI_TOKEN_TYPE_DECLARATION
:
676 const struct tgsi_full_declaration
*fdec
;
678 fdec
= &p
.FullToken
.FullDeclaration
;
679 switch (fdec
->Declaration
.File
) {
680 case TGSI_FILE_TEMPORARY
:
681 if (fdec
->DeclarationRange
.Last
> high_temp
) {
683 fdec
->DeclarationRange
.Last
;
686 #if 0 /* this would be nice.. except gallium doesn't track it */
687 case TGSI_FILE_ADDRESS
:
688 if (fdec
->DeclarationRange
.Last
> high_addr
) {
690 fdec
->DeclarationRange
.Last
;
694 case TGSI_FILE_OUTPUT
:
695 if (!nv40_vertprog_parse_decl_output(vpc
, fdec
))
703 #if 1 /* yay, parse instructions looking for address regs instead */
704 case TGSI_TOKEN_TYPE_INSTRUCTION
:
706 const struct tgsi_full_instruction
*finst
;
707 const struct tgsi_full_dst_register
*fdst
;
709 finst
= &p
.FullToken
.FullInstruction
;
710 fdst
= &finst
->FullDstRegisters
[0];
712 if (fdst
->DstRegister
.File
== TGSI_FILE_ADDRESS
) {
713 if (fdst
->DstRegister
.Index
> high_addr
)
714 high_addr
= fdst
->DstRegister
.Index
;
727 vpc
->imm
= CALLOC(nr_imm
, sizeof(struct nv40_sreg
));
732 vpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nv40_sreg
));
733 for (i
= 0; i
< high_temp
; i
++)
734 vpc
->r_temp
[i
] = temp(vpc
);
738 vpc
->r_address
= CALLOC(high_addr
, sizeof(struct nv40_sreg
));
739 for (i
= 0; i
< high_addr
; i
++)
740 vpc
->r_address
[i
] = temp(vpc
);
743 vpc
->r_temps_discard
= 0;
748 nv40_vertprog_translate(struct nv40_context
*nv40
,
749 struct nv40_vertex_program
*vp
)
751 struct tgsi_parse_context parse
;
752 struct nv40_vpc
*vpc
= NULL
;
753 struct nv40_sreg none
= nv40_sr(NV40SR_NONE
, 0);
756 vpc
= CALLOC(1, sizeof(struct nv40_vpc
));
761 if (!nv40_vertprog_prepare(vpc
)) {
766 /* Redirect post-transform vertex position to a temp if user clip
767 * planes are enabled. We need to append code the the vtxprog
768 * to handle clip planes later.
771 vpc
->r_result
[vpc
->hpos_idx
] = temp(vpc
);
772 vpc
->r_temps_discard
= 0;
775 tgsi_parse_init(&parse
, vp
->pipe
.tokens
);
777 while (!tgsi_parse_end_of_tokens(&parse
)) {
778 tgsi_parse_token(&parse
);
780 switch (parse
.FullToken
.Token
.Type
) {
781 case TGSI_TOKEN_TYPE_IMMEDIATE
:
783 const struct tgsi_full_immediate
*imm
;
785 imm
= &parse
.FullToken
.FullImmediate
;
786 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
787 assert(imm
->Immediate
.NrTokens
== 4 + 1);
788 vpc
->imm
[vpc
->nr_imm
++] =
790 imm
->u
.ImmediateFloat32
[0].Float
,
791 imm
->u
.ImmediateFloat32
[1].Float
,
792 imm
->u
.ImmediateFloat32
[2].Float
,
793 imm
->u
.ImmediateFloat32
[3].Float
);
796 case TGSI_TOKEN_TYPE_INSTRUCTION
:
798 const struct tgsi_full_instruction
*finst
;
799 finst
= &parse
.FullToken
.FullInstruction
;
800 if (!nv40_vertprog_parse_instruction(vpc
, finst
))
809 /* Write out HPOS if it was redirected to a temp earlier */
810 if (vpc
->r_result
[vpc
->hpos_idx
].type
!= NV40SR_OUTPUT
) {
811 struct nv40_sreg hpos
= nv40_sr(NV40SR_OUTPUT
,
812 NV40_VP_INST_DEST_POS
);
813 struct nv40_sreg htmp
= vpc
->r_result
[vpc
->hpos_idx
];
815 arith(vpc
, 0, OP_MOV
, hpos
, MASK_ALL
, htmp
, none
, none
);
818 /* Insert code to handle user clip planes */
819 for (i
= 0; i
< vp
->ucp
.nr
; i
++) {
820 struct nv40_sreg cdst
= nv40_sr(NV40SR_OUTPUT
,
821 NV40_VP_INST_DEST_CLIP(i
));
822 struct nv40_sreg ceqn
= constant(vpc
, -1,
823 nv40
->clip
.ucp
[i
][0],
824 nv40
->clip
.ucp
[i
][1],
825 nv40
->clip
.ucp
[i
][2],
826 nv40
->clip
.ucp
[i
][3]);
827 struct nv40_sreg htmp
= vpc
->r_result
[vpc
->hpos_idx
];
831 case 0: case 3: mask
= MASK_Y
; break;
832 case 1: case 4: mask
= MASK_Z
; break;
833 case 2: case 5: mask
= MASK_W
; break;
835 NOUVEAU_ERR("invalid clip dist #%d\n", i
);
839 arith(vpc
, 0, OP_DP4
, cdst
, mask
, htmp
, ceqn
, none
);
842 vp
->insns
[vp
->nr_insns
- 1].data
[3] |= NV40_VP_INST_LAST
;
843 vp
->translated
= TRUE
;
845 tgsi_parse_free(&parse
);
849 FREE(vpc
->r_address
);
856 nv40_vertprog_validate(struct nv40_context
*nv40
)
858 struct nouveau_winsys
*nvws
= nv40
->nvws
;
859 struct pipe_winsys
*ws
= nv40
->pipe
.winsys
;
860 struct nouveau_grobj
*curie
= nv40
->screen
->curie
;
861 struct nv40_vertex_program
*vp
;
862 struct pipe_buffer
*constbuf
;
863 boolean upload_code
= FALSE
, upload_data
= FALSE
;
866 if (nv40
->render_mode
== HW
) {
868 constbuf
= nv40
->constbuf
[PIPE_SHADER_VERTEX
];
870 if ((nv40
->dirty
& NV40_NEW_UCP
) ||
871 memcmp(&nv40
->clip
, &vp
->ucp
, sizeof(vp
->ucp
))) {
872 nv40_vertprog_destroy(nv40
, vp
);
873 memcpy(&vp
->ucp
, &nv40
->clip
, sizeof(vp
->ucp
));
876 vp
= nv40
->swtnl
.vertprog
;
880 /* Translate TGSI shader into hw bytecode */
882 goto check_gpu_resources
;
884 nv40
->fallback_swtnl
&= ~NV40_NEW_VERTPROG
;
885 nv40_vertprog_translate(nv40
, vp
);
886 if (!vp
->translated
) {
887 nv40
->fallback_swtnl
|= NV40_NEW_VERTPROG
;
892 /* Allocate hw vtxprog exec slots */
894 struct nouveau_resource
*heap
= nv40
->screen
->vp_exec_heap
;
895 struct nouveau_stateobj
*so
;
896 uint vplen
= vp
->nr_insns
;
898 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
)) {
899 while (heap
->next
&& heap
->size
< vplen
) {
900 struct nv40_vertex_program
*evict
;
902 evict
= heap
->next
->priv
;
903 nvws
->res_free(&evict
->exec
);
906 if (nvws
->res_alloc(heap
, vplen
, vp
, &vp
->exec
))
911 so_method(so
, curie
, NV40TCL_VP_START_FROM_ID
, 1);
912 so_data (so
, vp
->exec
->start
);
913 so_method(so
, curie
, NV40TCL_VP_ATTRIB_EN
, 2);
914 so_data (so
, vp
->ir
);
915 so_data (so
, vp
->or);
916 so_method(so
, curie
, NV40TCL_CLIP_PLANE_ENABLE
, 1);
917 so_data (so
, vp
->clip_ctrl
);
924 /* Allocate hw vtxprog const slots */
925 if (vp
->nr_consts
&& !vp
->data
) {
926 struct nouveau_resource
*heap
= nv40
->screen
->vp_data_heap
;
928 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
)) {
929 while (heap
->next
&& heap
->size
< vp
->nr_consts
) {
930 struct nv40_vertex_program
*evict
;
932 evict
= heap
->next
->priv
;
933 nvws
->res_free(&evict
->data
);
936 if (nvws
->res_alloc(heap
, vp
->nr_consts
, vp
, &vp
->data
))
940 /*XXX: handle this some day */
941 assert(vp
->data
->start
>= vp
->data_start_min
);
944 if (vp
->data_start
!= vp
->data
->start
)
948 /* If exec or data segments moved we need to patch the program to
949 * fixup offsets and register IDs.
951 if (vp
->exec_start
!= vp
->exec
->start
) {
952 for (i
= 0; i
< vp
->nr_insns
; i
++) {
953 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
955 if (vpi
->has_branch_offset
) {
960 vp
->exec_start
= vp
->exec
->start
;
963 if (vp
->nr_consts
&& vp
->data_start
!= vp
->data
->start
) {
964 for (i
= 0; i
< vp
->nr_insns
; i
++) {
965 struct nv40_vertex_program_exec
*vpi
= &vp
->insns
[i
];
967 if (vpi
->const_index
>= 0) {
968 vpi
->data
[1] &= ~NV40_VP_INST_CONST_SRC_MASK
;
970 (vpi
->const_index
+ vp
->data
->start
) <<
971 NV40_VP_INST_CONST_SRC_SHIFT
;
976 vp
->data_start
= vp
->data
->start
;
979 /* Update + Upload constant values */
984 map
= ws
->buffer_map(ws
, constbuf
,
985 PIPE_BUFFER_USAGE_CPU_READ
);
988 for (i
= 0; i
< vp
->nr_consts
; i
++) {
989 struct nv40_vertex_program_data
*vpd
= &vp
->consts
[i
];
991 if (vpd
->index
>= 0) {
993 !memcmp(vpd
->value
, &map
[vpd
->index
* 4],
996 memcpy(vpd
->value
, &map
[vpd
->index
* 4],
1000 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_CONST_ID
, 5);
1001 OUT_RING (i
+ vp
->data
->start
);
1002 OUT_RINGp ((uint32_t *)vpd
->value
, 4);
1006 ws
->buffer_unmap(ws
, constbuf
);
1009 /* Upload vtxprog */
1012 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1013 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[0]);
1014 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[1]);
1015 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[2]);
1016 NOUVEAU_MSG("VP %d: 0x%08x\n", i
, vp
->insns
[i
].data
[3]);
1019 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_FROM_ID
, 1);
1020 OUT_RING (vp
->exec
->start
);
1021 for (i
= 0; i
< vp
->nr_insns
; i
++) {
1022 BEGIN_RING(curie
, NV40TCL_VP_UPLOAD_INST(0), 4);
1023 OUT_RINGp (vp
->insns
[i
].data
, 4);
1027 if (vp
->so
!= nv40
->state
.hw
[NV40_STATE_VERTPROG
]) {
1028 so_ref(vp
->so
, &nv40
->state
.hw
[NV40_STATE_VERTPROG
]);
1036 nv40_vertprog_destroy(struct nv40_context
*nv40
, struct nv40_vertex_program
*vp
)
1038 struct nouveau_winsys
*nvws
= nv40
->screen
->nvws
;
1040 vp
->translated
= FALSE
;
1048 if (vp
->nr_consts
) {
1054 nvws
->res_free(&vp
->exec
);
1056 nvws
->res_free(&vp
->data
);
1058 vp
->data_start_min
= 0;
1060 vp
->ir
= vp
->or = vp
->clip_ctrl
= 0;
1061 so_ref(NULL
, &vp
->so
);
1064 struct nv40_state_entry nv40_state_vertprog
= {
1065 .validate
= nv40_vertprog_validate
,
1067 .pipe
= NV40_NEW_VERTPROG
| NV40_NEW_UCP
,
1068 .hw
= NV40_STATE_VERTPROG
,