nvfx: implement TRUNC in vp and fp
[mesa.git] / src / gallium / drivers / nvfx / nvfx_vertprog.c
1 #include "pipe/p_context.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_state.h"
4 #include "util/u_linkage.h"
5 #include "util/u_debug.h"
6
7 #include "pipe/p_shader_tokens.h"
8 #include "tgsi/tgsi_parse.h"
9 #include "tgsi/tgsi_dump.h"
10 #include "tgsi/tgsi_util.h"
11
12 #include "nvfx_context.h"
13 #include "nvfx_state.h"
14 #include "nvfx_resource.h"
15
16 /* TODO (at least...):
17 * 1. Indexed consts + ARL
18 * 3. NV_vp11, NV_vp2, NV_vp3 features
19 * - extra arith opcodes
20 * - branching
21 * - texture sampling
22 * - indexed attribs
23 * - indexed results
24 * 4. bugs
25 */
26
27 #include "nv30_vertprog.h"
28 #include "nv40_vertprog.h"
29
30 #define NVFX_VP_INST_DEST_CLIP(n) ((~0 - 6) + (n))
31
32 struct nvfx_loop_entry
33 {
34 unsigned brk_target;
35 unsigned cont_target;
36 };
37
38 struct nvfx_vpc {
39 struct nvfx_context* nvfx;
40 struct nvfx_vertex_program *vp;
41
42 struct nvfx_vertex_program_exec *vpi;
43
44 unsigned r_temps;
45 unsigned r_temps_discard;
46 struct nvfx_reg r_result[PIPE_MAX_SHADER_OUTPUTS];
47 struct nvfx_reg *r_address;
48 struct nvfx_reg *r_temp;
49
50 struct nvfx_reg *imm;
51 unsigned nr_imm;
52
53 unsigned hpos_idx;
54
55 struct util_dynarray label_relocs;
56 struct util_dynarray loop_stack;
57 };
58
59 static struct nvfx_reg
60 temp(struct nvfx_vpc *vpc)
61 {
62 int idx = ffs(~vpc->r_temps) - 1;
63
64 if (idx < 0) {
65 NOUVEAU_ERR("out of temps!!\n");
66 assert(0);
67 return nvfx_reg(NVFXSR_TEMP, 0);
68 }
69
70 vpc->r_temps |= (1 << idx);
71 vpc->r_temps_discard |= (1 << idx);
72 return nvfx_reg(NVFXSR_TEMP, idx);
73 }
74
75 static inline void
76 release_temps(struct nvfx_vpc *vpc)
77 {
78 vpc->r_temps &= ~vpc->r_temps_discard;
79 vpc->r_temps_discard = 0;
80 }
81
82 static struct nvfx_reg
83 constant(struct nvfx_vpc *vpc, int pipe, float x, float y, float z, float w)
84 {
85 struct nvfx_vertex_program *vp = vpc->vp;
86 struct nvfx_vertex_program_data *vpd;
87 int idx;
88
89 if (pipe >= 0) {
90 for (idx = 0; idx < vp->nr_consts; idx++) {
91 if (vp->consts[idx].index == pipe)
92 return nvfx_reg(NVFXSR_CONST, idx);
93 }
94 }
95
96 idx = vp->nr_consts++;
97 vp->consts = realloc(vp->consts, sizeof(*vpd) * vp->nr_consts);
98 vpd = &vp->consts[idx];
99
100 vpd->index = pipe;
101 vpd->value[0] = x;
102 vpd->value[1] = y;
103 vpd->value[2] = z;
104 vpd->value[3] = w;
105 return nvfx_reg(NVFXSR_CONST, idx);
106 }
107
108 #define arith(s,o,d,m,s0,s1,s2) \
109 nvfx_insn(0, (NVFX_VP_INST_SLOT_##s << 7) | NVFX_VP_INST_##s##_OP_##o, -1, (d), (m), (s0), (s1), (s2))
110
111 static void
112 emit_src(struct nvfx_context* nvfx, struct nvfx_vpc *vpc, uint32_t *hw, int pos, struct nvfx_src src)
113 {
114 struct nvfx_vertex_program *vp = vpc->vp;
115 uint32_t sr = 0;
116
117 switch (src.reg.type) {
118 case NVFXSR_TEMP:
119 sr |= (NVFX_VP(SRC_REG_TYPE_TEMP) << NVFX_VP(SRC_REG_TYPE_SHIFT));
120 sr |= (src.reg.index << NVFX_VP(SRC_TEMP_SRC_SHIFT));
121 break;
122 case NVFXSR_INPUT:
123 sr |= (NVFX_VP(SRC_REG_TYPE_INPUT) <<
124 NVFX_VP(SRC_REG_TYPE_SHIFT));
125 vp->ir |= (1 << src.reg.index);
126 hw[1] |= (src.reg.index << NVFX_VP(INST_INPUT_SRC_SHIFT));
127 break;
128 case NVFXSR_CONST:
129 sr |= (NVFX_VP(SRC_REG_TYPE_CONST) <<
130 NVFX_VP(SRC_REG_TYPE_SHIFT));
131 assert(vpc->vpi->const_index == -1 ||
132 vpc->vpi->const_index == src.reg.index);
133 vpc->vpi->const_index = src.reg.index;
134 break;
135 case NVFXSR_NONE:
136 sr |= (NVFX_VP(SRC_REG_TYPE_INPUT) <<
137 NVFX_VP(SRC_REG_TYPE_SHIFT));
138 break;
139 default:
140 assert(0);
141 }
142
143 if (src.negate)
144 sr |= NVFX_VP(SRC_NEGATE);
145
146 if (src.abs)
147 hw[0] |= (1 << (21 + pos));
148
149 sr |= ((src.swz[0] << NVFX_VP(SRC_SWZ_X_SHIFT)) |
150 (src.swz[1] << NVFX_VP(SRC_SWZ_Y_SHIFT)) |
151 (src.swz[2] << NVFX_VP(SRC_SWZ_Z_SHIFT)) |
152 (src.swz[3] << NVFX_VP(SRC_SWZ_W_SHIFT)));
153
154 switch (pos) {
155 case 0:
156 hw[1] |= ((sr & NVFX_VP(SRC0_HIGH_MASK)) >>
157 NVFX_VP(SRC0_HIGH_SHIFT)) << NVFX_VP(INST_SRC0H_SHIFT);
158 hw[2] |= (sr & NVFX_VP(SRC0_LOW_MASK)) <<
159 NVFX_VP(INST_SRC0L_SHIFT);
160 break;
161 case 1:
162 hw[2] |= sr << NVFX_VP(INST_SRC1_SHIFT);
163 break;
164 case 2:
165 hw[2] |= ((sr & NVFX_VP(SRC2_HIGH_MASK)) >>
166 NVFX_VP(SRC2_HIGH_SHIFT)) << NVFX_VP(INST_SRC2H_SHIFT);
167 hw[3] |= (sr & NVFX_VP(SRC2_LOW_MASK)) <<
168 NVFX_VP(INST_SRC2L_SHIFT);
169 break;
170 default:
171 assert(0);
172 }
173 }
174
175 static void
176 emit_dst(struct nvfx_context* nvfx, struct nvfx_vpc *vpc, uint32_t *hw, int slot, struct nvfx_reg dst)
177 {
178 struct nvfx_vertex_program *vp = vpc->vp;
179
180 switch (dst.type) {
181 case NVFXSR_NONE:
182 if(!nvfx->is_nv4x)
183 hw[0] |= NV30_VP_INST_DEST_TEMP_ID_MASK;
184 else {
185 hw[3] |= NV40_VP_INST_DEST_MASK;
186 if (slot == 0)
187 hw[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK;
188 else
189 hw[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK;
190 }
191 break;
192 case NVFXSR_TEMP:
193 if(!nvfx->is_nv4x)
194 hw[0] |= (dst.index << NV30_VP_INST_DEST_TEMP_ID_SHIFT);
195 else {
196 hw[3] |= NV40_VP_INST_DEST_MASK;
197 if (slot == 0)
198 hw[0] |= (dst.index << NV40_VP_INST_VEC_DEST_TEMP_SHIFT);
199 else
200 hw[3] |= (dst.index << NV40_VP_INST_SCA_DEST_TEMP_SHIFT);
201 }
202 break;
203 case NVFXSR_OUTPUT:
204 /* TODO: this may be wrong because on nv30 COL0 and BFC0 are swapped */
205 switch (dst.index) {
206 case NVFX_VP_INST_DEST_CLIP(0):
207 vp->or |= (1 << 6);
208 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE0;
209 dst.index = NVFX_VP(INST_DEST_FOGC);
210 break;
211 case NVFX_VP_INST_DEST_CLIP(1):
212 vp->or |= (1 << 7);
213 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE1;
214 dst.index = NVFX_VP(INST_DEST_FOGC);
215 break;
216 case NVFX_VP_INST_DEST_CLIP(2):
217 vp->or |= (1 << 8);
218 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE2;
219 dst.index = NVFX_VP(INST_DEST_FOGC);
220 break;
221 case NVFX_VP_INST_DEST_CLIP(3):
222 vp->or |= (1 << 9);
223 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE3;
224 dst.index = NVFX_VP(INST_DEST_PSZ);
225 break;
226 case NVFX_VP_INST_DEST_CLIP(4):
227 vp->or |= (1 << 10);
228 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE4;
229 dst.index = NVFX_VP(INST_DEST_PSZ);
230 break;
231 case NVFX_VP_INST_DEST_CLIP(5):
232 vp->or |= (1 << 11);
233 vp->clip_ctrl |= NV34TCL_VP_CLIP_PLANES_ENABLE_PLANE5;
234 dst.index = NVFX_VP(INST_DEST_PSZ);
235 break;
236 default:
237 if(!nvfx->is_nv4x) {
238 switch (dst.index) {
239 case NV30_VP_INST_DEST_COL0 : vp->or |= (1 << 0); break;
240 case NV30_VP_INST_DEST_COL1 : vp->or |= (1 << 1); break;
241 case NV30_VP_INST_DEST_BFC0 : vp->or |= (1 << 2); break;
242 case NV30_VP_INST_DEST_BFC1 : vp->or |= (1 << 3); break;
243 case NV30_VP_INST_DEST_FOGC: vp->or |= (1 << 4); break;
244 case NV30_VP_INST_DEST_PSZ : vp->or |= (1 << 5); break;
245 case NV30_VP_INST_DEST_TC(0): vp->or |= (1 << 14); break;
246 case NV30_VP_INST_DEST_TC(1): vp->or |= (1 << 15); break;
247 case NV30_VP_INST_DEST_TC(2): vp->or |= (1 << 16); break;
248 case NV30_VP_INST_DEST_TC(3): vp->or |= (1 << 17); break;
249 case NV30_VP_INST_DEST_TC(4): vp->or |= (1 << 18); break;
250 case NV30_VP_INST_DEST_TC(5): vp->or |= (1 << 19); break;
251 case NV30_VP_INST_DEST_TC(6): vp->or |= (1 << 20); break;
252 case NV30_VP_INST_DEST_TC(7): vp->or |= (1 << 21); break;
253 }
254 } else {
255 switch (dst.index) {
256 case NV40_VP_INST_DEST_COL0 : vp->or |= (1 << 0); break;
257 case NV40_VP_INST_DEST_COL1 : vp->or |= (1 << 1); break;
258 case NV40_VP_INST_DEST_BFC0 : vp->or |= (1 << 2); break;
259 case NV40_VP_INST_DEST_BFC1 : vp->or |= (1 << 3); break;
260 case NV40_VP_INST_DEST_FOGC: vp->or |= (1 << 4); break;
261 case NV40_VP_INST_DEST_PSZ : vp->or |= (1 << 5); break;
262 case NV40_VP_INST_DEST_TC(0): vp->or |= (1 << 14); break;
263 case NV40_VP_INST_DEST_TC(1): vp->or |= (1 << 15); break;
264 case NV40_VP_INST_DEST_TC(2): vp->or |= (1 << 16); break;
265 case NV40_VP_INST_DEST_TC(3): vp->or |= (1 << 17); break;
266 case NV40_VP_INST_DEST_TC(4): vp->or |= (1 << 18); break;
267 case NV40_VP_INST_DEST_TC(5): vp->or |= (1 << 19); break;
268 case NV40_VP_INST_DEST_TC(6): vp->or |= (1 << 20); break;
269 case NV40_VP_INST_DEST_TC(7): vp->or |= (1 << 21); break;
270 }
271 }
272 break;
273 }
274
275 if(!nvfx->is_nv4x) {
276 hw[3] |= (dst.index << NV30_VP_INST_DEST_SHIFT);
277 hw[0] |= NV30_VP_INST_VEC_DEST_TEMP_MASK;
278
279 /*XXX: no way this is entirely correct, someone needs to
280 * figure out what exactly it is.
281 */
282 hw[3] |= 0x800;
283 } else {
284 hw[3] |= (dst.index << NV40_VP_INST_DEST_SHIFT);
285 if (slot == 0) {
286 hw[0] |= NV40_VP_INST_VEC_RESULT;
287 hw[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK;
288 } else {
289 hw[3] |= NV40_VP_INST_SCA_RESULT;
290 hw[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK;
291 }
292 }
293 break;
294 default:
295 assert(0);
296 }
297 }
298
299 static void
300 nvfx_vp_emit(struct nvfx_vpc *vpc, struct nvfx_insn insn)
301 {
302 struct nvfx_context* nvfx = vpc->nvfx;
303 struct nvfx_vertex_program *vp = vpc->vp;
304 unsigned slot = insn.op >> 7;
305 unsigned op = insn.op & 0x7f;
306 uint32_t *hw;
307
308 vp->insns = realloc(vp->insns, ++vp->nr_insns * sizeof(*vpc->vpi));
309 vpc->vpi = &vp->insns[vp->nr_insns - 1];
310 memset(vpc->vpi, 0, sizeof(*vpc->vpi));
311 vpc->vpi->const_index = -1;
312
313 hw = vpc->vpi->data;
314
315 hw[0] |= (insn.cc_test << NVFX_VP(INST_COND_SHIFT));
316 hw[0] |= ((insn.cc_swz[0] << NVFX_VP(INST_COND_SWZ_X_SHIFT)) |
317 (insn.cc_swz[1] << NVFX_VP(INST_COND_SWZ_Y_SHIFT)) |
318 (insn.cc_swz[2] << NVFX_VP(INST_COND_SWZ_Z_SHIFT)) |
319 (insn.cc_swz[3] << NVFX_VP(INST_COND_SWZ_W_SHIFT)));
320 if(insn.cc_update)
321 hw[0] |= NVFX_VP(INST_COND_UPDATE_ENABLE);
322
323 if(!nvfx->is_nv4x) {
324 if(slot == 0)
325 hw[1] |= (op << NV30_VP_INST_VEC_OPCODE_SHIFT);
326 else
327 {
328 hw[0] |= ((op >> 4) << NV30_VP_INST_SCA_OPCODEH_SHIFT);
329 hw[1] |= ((op & 0xf) << NV30_VP_INST_SCA_OPCODEL_SHIFT);
330 }
331 // hw[3] |= NVFX_VP(INST_SCA_DEST_TEMP_MASK);
332 // hw[3] |= (mask << NVFX_VP(INST_VEC_WRITEMASK_SHIFT));
333
334 if (insn.dst.type == NVFXSR_OUTPUT) {
335 if (slot)
336 hw[3] |= (insn.mask << NV30_VP_INST_SDEST_WRITEMASK_SHIFT);
337 else
338 hw[3] |= (insn.mask << NV30_VP_INST_VDEST_WRITEMASK_SHIFT);
339 } else {
340 if (slot)
341 hw[3] |= (insn.mask << NV30_VP_INST_STEMP_WRITEMASK_SHIFT);
342 else
343 hw[3] |= (insn.mask << NV30_VP_INST_VTEMP_WRITEMASK_SHIFT);
344 }
345 } else {
346 if (slot == 0) {
347 hw[1] |= (op << NV40_VP_INST_VEC_OPCODE_SHIFT);
348 hw[3] |= NV40_VP_INST_SCA_DEST_TEMP_MASK;
349 hw[3] |= (insn.mask << NV40_VP_INST_VEC_WRITEMASK_SHIFT);
350 } else {
351 hw[1] |= (op << NV40_VP_INST_SCA_OPCODE_SHIFT);
352 hw[0] |= NV40_VP_INST_VEC_DEST_TEMP_MASK ;
353 hw[3] |= (insn.mask << NV40_VP_INST_SCA_WRITEMASK_SHIFT);
354 }
355 }
356
357 emit_dst(nvfx, vpc, hw, slot, insn.dst);
358 emit_src(nvfx, vpc, hw, 0, insn.src[0]);
359 emit_src(nvfx, vpc, hw, 1, insn.src[1]);
360 emit_src(nvfx, vpc, hw, 2, insn.src[2]);
361 }
362
363 static inline struct nvfx_src
364 tgsi_src(struct nvfx_vpc *vpc, const struct tgsi_full_src_register *fsrc) {
365 struct nvfx_src src;
366
367 switch (fsrc->Register.File) {
368 case TGSI_FILE_INPUT:
369 src.reg = nvfx_reg(NVFXSR_INPUT, fsrc->Register.Index);
370 break;
371 case TGSI_FILE_CONSTANT:
372 src.reg = constant(vpc, fsrc->Register.Index, 0, 0, 0, 0);
373 break;
374 case TGSI_FILE_IMMEDIATE:
375 src.reg = vpc->imm[fsrc->Register.Index];
376 break;
377 case TGSI_FILE_TEMPORARY:
378 src.reg = vpc->r_temp[fsrc->Register.Index];
379 break;
380 default:
381 NOUVEAU_ERR("bad src file\n");
382 break;
383 }
384
385 src.abs = fsrc->Register.Absolute;
386 src.negate = fsrc->Register.Negate;
387 src.swz[0] = fsrc->Register.SwizzleX;
388 src.swz[1] = fsrc->Register.SwizzleY;
389 src.swz[2] = fsrc->Register.SwizzleZ;
390 src.swz[3] = fsrc->Register.SwizzleW;
391 return src;
392 }
393
394 static INLINE struct nvfx_reg
395 tgsi_dst(struct nvfx_vpc *vpc, const struct tgsi_full_dst_register *fdst) {
396 struct nvfx_reg dst;
397
398 switch (fdst->Register.File) {
399 case TGSI_FILE_NULL:
400 dst = nvfx_reg(NVFXSR_NONE, 0);
401 break;
402 case TGSI_FILE_OUTPUT:
403 dst = vpc->r_result[fdst->Register.Index];
404 break;
405 case TGSI_FILE_TEMPORARY:
406 dst = vpc->r_temp[fdst->Register.Index];
407 break;
408 case TGSI_FILE_ADDRESS:
409 dst = vpc->r_address[fdst->Register.Index];
410 break;
411 default:
412 NOUVEAU_ERR("bad dst file %i\n", fdst->Register.File);
413 break;
414 }
415
416 return dst;
417 }
418
419 static inline int
420 tgsi_mask(uint tgsi)
421 {
422 int mask = 0;
423
424 if (tgsi & TGSI_WRITEMASK_X) mask |= NVFX_VP_MASK_X;
425 if (tgsi & TGSI_WRITEMASK_Y) mask |= NVFX_VP_MASK_Y;
426 if (tgsi & TGSI_WRITEMASK_Z) mask |= NVFX_VP_MASK_Z;
427 if (tgsi & TGSI_WRITEMASK_W) mask |= NVFX_VP_MASK_W;
428 return mask;
429 }
430
431 static boolean
432 nvfx_vertprog_parse_instruction(struct nvfx_context* nvfx, struct nvfx_vpc *vpc,
433 unsigned idx, const struct tgsi_full_instruction *finst)
434 {
435 struct nvfx_src src[3], tmp;
436 struct nvfx_reg dst;
437 struct nvfx_src none = nvfx_src(nvfx_reg(NVFXSR_NONE, 0));
438 struct nvfx_insn insn;
439 struct nvfx_label_relocation reloc;
440 struct nvfx_loop_entry loop;
441 int mask;
442 int ai = -1, ci = -1, ii = -1;
443 int i;
444
445 if (finst->Instruction.Opcode == TGSI_OPCODE_END)
446 return TRUE;
447
448 for (i = 0; i < finst->Instruction.NumSrcRegs; i++) {
449 const struct tgsi_full_src_register *fsrc;
450
451 fsrc = &finst->Src[i];
452 if (fsrc->Register.File == TGSI_FILE_TEMPORARY) {
453 src[i] = tgsi_src(vpc, fsrc);
454 }
455 }
456
457 for (i = 0; i < finst->Instruction.NumSrcRegs; i++) {
458 const struct tgsi_full_src_register *fsrc;
459
460 fsrc = &finst->Src[i];
461
462 switch (fsrc->Register.File) {
463 case TGSI_FILE_INPUT:
464 if (ai == -1 || ai == fsrc->Register.Index) {
465 ai = fsrc->Register.Index;
466 src[i] = tgsi_src(vpc, fsrc);
467 } else {
468 src[i] = nvfx_src(temp(vpc));
469 nvfx_vp_emit(vpc, arith(VEC, MOV, src[i].reg, NVFX_VP_MASK_ALL, tgsi_src(vpc, fsrc), none, none));
470 }
471 break;
472 case TGSI_FILE_CONSTANT:
473 if ((ci == -1 && ii == -1) ||
474 ci == fsrc->Register.Index) {
475 ci = fsrc->Register.Index;
476 src[i] = tgsi_src(vpc, fsrc);
477 } else {
478 src[i] = nvfx_src(temp(vpc));
479 nvfx_vp_emit(vpc, arith(VEC, MOV, src[i].reg, NVFX_VP_MASK_ALL, tgsi_src(vpc, fsrc), none, none));
480 }
481 break;
482 case TGSI_FILE_IMMEDIATE:
483 if ((ci == -1 && ii == -1) ||
484 ii == fsrc->Register.Index) {
485 ii = fsrc->Register.Index;
486 src[i] = tgsi_src(vpc, fsrc);
487 } else {
488 src[i] = nvfx_src(temp(vpc));
489 nvfx_vp_emit(vpc, arith(VEC, MOV, src[i].reg, NVFX_VP_MASK_ALL, tgsi_src(vpc, fsrc), none, none));
490 }
491 break;
492 case TGSI_FILE_TEMPORARY:
493 /* handled above */
494 break;
495 default:
496 NOUVEAU_ERR("bad src file\n");
497 return FALSE;
498 }
499 }
500
501 dst = tgsi_dst(vpc, &finst->Dst[0]);
502 mask = tgsi_mask(finst->Dst[0].Register.WriteMask);
503
504 switch (finst->Instruction.Opcode) {
505 case TGSI_OPCODE_ABS:
506 nvfx_vp_emit(vpc, arith(VEC, MOV, dst, mask, abs(src[0]), none, none));
507 break;
508 case TGSI_OPCODE_ADD:
509 nvfx_vp_emit(vpc, arith(VEC, ADD, dst, mask, src[0], none, src[1]));
510 break;
511 case TGSI_OPCODE_ARL:
512 nvfx_vp_emit(vpc, arith(VEC, ARL, dst, mask, src[0], none, none));
513 break;
514 case TGSI_OPCODE_COS:
515 nvfx_vp_emit(vpc, arith(SCA, COS, dst, mask, none, none, src[0]));
516 break;
517 case TGSI_OPCODE_DP3:
518 nvfx_vp_emit(vpc, arith(VEC, DP3, dst, mask, src[0], src[1], none));
519 break;
520 case TGSI_OPCODE_DP4:
521 nvfx_vp_emit(vpc, arith(VEC, DP4, dst, mask, src[0], src[1], none));
522 break;
523 case TGSI_OPCODE_DPH:
524 nvfx_vp_emit(vpc, arith(VEC, DPH, dst, mask, src[0], src[1], none));
525 break;
526 case TGSI_OPCODE_DST:
527 nvfx_vp_emit(vpc, arith(VEC, DST, dst, mask, src[0], src[1], none));
528 break;
529 case TGSI_OPCODE_EX2:
530 nvfx_vp_emit(vpc, arith(SCA, EX2, dst, mask, none, none, src[0]));
531 break;
532 case TGSI_OPCODE_EXP:
533 nvfx_vp_emit(vpc, arith(SCA, EXP, dst, mask, none, none, src[0]));
534 break;
535 case TGSI_OPCODE_FLR:
536 nvfx_vp_emit(vpc, arith(VEC, FLR, dst, mask, src[0], none, none));
537 break;
538 case TGSI_OPCODE_FRC:
539 nvfx_vp_emit(vpc, arith(VEC, FRC, dst, mask, src[0], none, none));
540 break;
541 case TGSI_OPCODE_LG2:
542 nvfx_vp_emit(vpc, arith(SCA, LG2, dst, mask, none, none, src[0]));
543 break;
544 case TGSI_OPCODE_LIT:
545 nvfx_vp_emit(vpc, arith(SCA, LIT, dst, mask, none, none, src[0]));
546 break;
547 case TGSI_OPCODE_LOG:
548 nvfx_vp_emit(vpc, arith(SCA, LOG, dst, mask, none, none, src[0]));
549 break;
550 case TGSI_OPCODE_LRP:
551 tmp = nvfx_src(temp(vpc));
552 nvfx_vp_emit(vpc, arith(VEC, MAD, tmp.reg, mask, neg(src[0]), src[2], src[2]));
553 nvfx_vp_emit(vpc, arith(VEC, MAD, dst, mask, src[0], src[1], tmp));
554 break;
555 case TGSI_OPCODE_MAD:
556 nvfx_vp_emit(vpc, arith(VEC, MAD, dst, mask, src[0], src[1], src[2]));
557 break;
558 case TGSI_OPCODE_MAX:
559 nvfx_vp_emit(vpc, arith(VEC, MAX, dst, mask, src[0], src[1], none));
560 break;
561 case TGSI_OPCODE_MIN:
562 nvfx_vp_emit(vpc, arith(VEC, MIN, dst, mask, src[0], src[1], none));
563 break;
564 case TGSI_OPCODE_MOV:
565 nvfx_vp_emit(vpc, arith(VEC, MOV, dst, mask, src[0], none, none));
566 break;
567 case TGSI_OPCODE_MUL:
568 nvfx_vp_emit(vpc, arith(VEC, MUL, dst, mask, src[0], src[1], none));
569 break;
570 case TGSI_OPCODE_NOP:
571 break;
572 case TGSI_OPCODE_POW:
573 tmp = nvfx_src(temp(vpc));
574 nvfx_vp_emit(vpc, arith(SCA, LG2, tmp.reg, NVFX_VP_MASK_X, none, none, swz(src[0], X, X, X, X)));
575 nvfx_vp_emit(vpc, arith(VEC, MUL, tmp.reg, NVFX_VP_MASK_X, swz(tmp, X, X, X, X), swz(src[1], X, X, X, X), none));
576 nvfx_vp_emit(vpc, arith(SCA, EX2, dst, mask, none, none, swz(tmp, X, X, X, X)));
577 break;
578 case TGSI_OPCODE_RCP:
579 nvfx_vp_emit(vpc, arith(SCA, RCP, dst, mask, none, none, src[0]));
580 break;
581 case TGSI_OPCODE_RSQ:
582 nvfx_vp_emit(vpc, arith(SCA, RSQ, dst, mask, none, none, abs(src[0])));
583 break;
584 case TGSI_OPCODE_SEQ:
585 nvfx_vp_emit(vpc, arith(VEC, SEQ, dst, mask, src[0], src[1], none));
586 break;
587 case TGSI_OPCODE_SFL:
588 nvfx_vp_emit(vpc, arith(VEC, SFL, dst, mask, src[0], src[1], none));
589 break;
590 case TGSI_OPCODE_SGE:
591 nvfx_vp_emit(vpc, arith(VEC, SGE, dst, mask, src[0], src[1], none));
592 break;
593 case TGSI_OPCODE_SGT:
594 nvfx_vp_emit(vpc, arith(VEC, SGT, dst, mask, src[0], src[1], none));
595 break;
596 case TGSI_OPCODE_SIN:
597 nvfx_vp_emit(vpc, arith(SCA, SIN, dst, mask, none, none, src[0]));
598 break;
599 case TGSI_OPCODE_SLE:
600 nvfx_vp_emit(vpc, arith(VEC, SLE, dst, mask, src[0], src[1], none));
601 break;
602 case TGSI_OPCODE_SLT:
603 nvfx_vp_emit(vpc, arith(VEC, SLT, dst, mask, src[0], src[1], none));
604 break;
605 case TGSI_OPCODE_SNE:
606 nvfx_vp_emit(vpc, arith(VEC, SNE, dst, mask, src[0], src[1], none));
607 break;
608 case TGSI_OPCODE_SSG:
609 nvfx_vp_emit(vpc, arith(VEC, SSG, dst, mask, src[0], src[1], none));
610 break;
611 case TGSI_OPCODE_STR:
612 nvfx_vp_emit(vpc, arith(VEC, STR, dst, mask, src[0], src[1], none));
613 break;
614 case TGSI_OPCODE_SUB:
615 nvfx_vp_emit(vpc, arith(VEC, ADD, dst, mask, src[0], none, neg(src[1])));
616 break;
617 case TGSI_OPCODE_TRUNC:
618 tmp = nvfx_src(temp(vpc));
619 insn = arith(VEC, MOV, none.reg, mask, src[0], none, none);
620 insn.cc_update = 1;
621 nvfx_vp_emit(vpc, insn);
622
623 nvfx_vp_emit(vpc, arith(VEC, FLR, tmp.reg, mask, abs(src[0]), none, none));
624 nvfx_vp_emit(vpc, arith(VEC, MOV, dst, mask, tmp, none, none));
625
626 insn = arith(VEC, MOV, dst, mask, neg(tmp), none, none);
627 insn.cc_test = NVFX_COND_LT;
628 nvfx_vp_emit(vpc, insn);
629 break;
630 case TGSI_OPCODE_XPD:
631 tmp = nvfx_src(temp(vpc));
632 nvfx_vp_emit(vpc, arith(VEC, MUL, tmp.reg, mask, swz(src[0], Z, X, Y, Y), swz(src[1], Y, Z, X, X), none));
633 nvfx_vp_emit(vpc, arith(VEC, MAD, dst, (mask & ~NVFX_VP_MASK_W), swz(src[0], Y, Z, X, X), swz(src[1], Z, X, Y, Y), neg(tmp)));
634 break;
635
636 case TGSI_OPCODE_IF:
637 insn = arith(VEC, MOV, none.reg, NVFX_VP_MASK_X, src[0], none, none);
638 insn.cc_update = 1;
639 nvfx_vp_emit(vpc, insn);
640
641 reloc.location = vpc->vp->nr_insns;
642 reloc.target = finst->Label.Label + 1;
643 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
644
645 insn = arith(SCA, BRA, none.reg, 0, none, none, none);
646 insn.cc_test = NVFX_COND_EQ;
647 insn.cc_swz[0] = insn.cc_swz[1] = insn.cc_swz[2] = insn.cc_swz[3] = 0;
648 nvfx_vp_emit(vpc, insn);
649 break;
650
651 case TGSI_OPCODE_ELSE:
652 case TGSI_OPCODE_BRA:
653 case TGSI_OPCODE_CAL:
654 reloc.location = vpc->vp->nr_insns;
655 reloc.target = finst->Label.Label;
656 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
657
658 if(finst->Instruction.Opcode == TGSI_OPCODE_CAL)
659 insn = arith(SCA, CAL, none.reg, 0, none, none, none);
660 else
661 insn = arith(SCA, BRA, none.reg, 0, none, none, none);
662 nvfx_vp_emit(vpc, insn);
663 break;
664
665 case TGSI_OPCODE_RET:
666 tmp = none;
667 tmp.swz[0] = tmp.swz[1] = tmp.swz[2] = tmp.swz[3] = 0;
668 nvfx_vp_emit(vpc, arith(SCA, RET, none.reg, 0, none, none, tmp));
669 break;
670
671 case TGSI_OPCODE_BGNSUB:
672 case TGSI_OPCODE_ENDSUB:
673 case TGSI_OPCODE_ENDIF:
674 /* nothing to do here */
675 break;
676
677 case TGSI_OPCODE_BGNLOOP:
678 loop.cont_target = idx;
679 loop.brk_target = finst->Label.Label + 1;
680 util_dynarray_append(&vpc->loop_stack, struct nvfx_loop_entry, loop);
681 break;
682
683 case TGSI_OPCODE_ENDLOOP:
684 loop = util_dynarray_pop(&vpc->loop_stack, struct nvfx_loop_entry);
685
686 reloc.location = vpc->vp->nr_insns;
687 reloc.target = loop.cont_target;
688 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
689
690 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
691 break;
692
693 case TGSI_OPCODE_CONT:
694 loop = util_dynarray_top(&vpc->loop_stack, struct nvfx_loop_entry);
695
696 reloc.location = vpc->vp->nr_insns;
697 reloc.target = loop.cont_target;
698 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
699
700 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
701 break;
702
703 case TGSI_OPCODE_BRK:
704 loop = util_dynarray_top(&vpc->loop_stack, struct nvfx_loop_entry);
705
706 reloc.location = vpc->vp->nr_insns;
707 reloc.target = loop.brk_target;
708 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
709
710 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
711 break;
712
713 default:
714 NOUVEAU_ERR("invalid opcode %d\n", finst->Instruction.Opcode);
715 return FALSE;
716 }
717
718 release_temps(vpc);
719 return TRUE;
720 }
721
722 static boolean
723 nvfx_vertprog_parse_decl_output(struct nvfx_context* nvfx, struct nvfx_vpc *vpc,
724 const struct tgsi_full_declaration *fdec)
725 {
726 unsigned idx = fdec->Range.First;
727 int hw;
728
729 switch (fdec->Semantic.Name) {
730 case TGSI_SEMANTIC_POSITION:
731 hw = NVFX_VP(INST_DEST_POS);
732 vpc->hpos_idx = idx;
733 break;
734 case TGSI_SEMANTIC_COLOR:
735 if (fdec->Semantic.Index == 0) {
736 hw = NVFX_VP(INST_DEST_COL0);
737 } else
738 if (fdec->Semantic.Index == 1) {
739 hw = NVFX_VP(INST_DEST_COL1);
740 } else {
741 NOUVEAU_ERR("bad colour semantic index\n");
742 return FALSE;
743 }
744 break;
745 case TGSI_SEMANTIC_BCOLOR:
746 if (fdec->Semantic.Index == 0) {
747 hw = NVFX_VP(INST_DEST_BFC0);
748 } else
749 if (fdec->Semantic.Index == 1) {
750 hw = NVFX_VP(INST_DEST_BFC1);
751 } else {
752 NOUVEAU_ERR("bad bcolour semantic index\n");
753 return FALSE;
754 }
755 break;
756 case TGSI_SEMANTIC_FOG:
757 hw = NVFX_VP(INST_DEST_FOGC);
758 break;
759 case TGSI_SEMANTIC_PSIZE:
760 hw = NVFX_VP(INST_DEST_PSZ);
761 break;
762 case TGSI_SEMANTIC_GENERIC:
763 hw = (vpc->vp->generic_to_fp_input[fdec->Semantic.Index] & 0xf)
764 + NVFX_VP(INST_DEST_TC(0)) - NVFX_FP_OP_INPUT_SRC_TC(0);
765 break;
766 case TGSI_SEMANTIC_EDGEFLAG:
767 /* not really an error just a fallback */
768 NOUVEAU_ERR("cannot handle edgeflag output\n");
769 return FALSE;
770 default:
771 NOUVEAU_ERR("bad output semantic\n");
772 return FALSE;
773 }
774
775 vpc->r_result[idx] = nvfx_reg(NVFXSR_OUTPUT, hw);
776 return TRUE;
777 }
778
779 static boolean
780 nvfx_vertprog_prepare(struct nvfx_context* nvfx, struct nvfx_vpc *vpc)
781 {
782 struct tgsi_parse_context p;
783 int high_temp = -1, high_addr = -1, nr_imm = 0, i;
784 struct util_semantic_set set;
785 unsigned char sem_layout[8];
786 unsigned sem_layout_size;
787 unsigned num_outputs;
788
789 num_outputs = util_semantic_set_from_program_file(&set, vpc->vp->pipe.tokens, TGSI_FILE_OUTPUT);
790
791 if(num_outputs > 8) {
792 NOUVEAU_ERR("too many vertex program outputs: %i\n", num_outputs);
793 return FALSE;
794 }
795 util_semantic_layout_from_set(sem_layout, &set, 8, 8);
796
797 /* hope 0xf is (0, 0, 0, 1) initialized; otherwise, we are _probably_ not required to do this */
798 memset(vpc->vp->generic_to_fp_input, 0x0f, sizeof(vpc->vp->generic_to_fp_input));
799 vpc->vp->texcoord_ouput_mask = 0;
800 for(int i = 0; i < 8; ++i) {
801 if(sem_layout[i] == 0xff)
802 continue;
803 vpc->vp->texcoord_ouput_mask |= (1 << i);
804 //printf("vp: GENERIC[%i] to fpreg %i\n", sem_layout[i], NVFX_FP_OP_INPUT_SRC_TC(0) + i);
805 vpc->vp->generic_to_fp_input[sem_layout[i]] = 0xf0 | (NVFX_FP_OP_INPUT_SRC_TC(0) + i);
806 }
807
808 tgsi_parse_init(&p, vpc->vp->pipe.tokens);
809 while (!tgsi_parse_end_of_tokens(&p)) {
810 const union tgsi_full_token *tok = &p.FullToken;
811
812 tgsi_parse_token(&p);
813 switch(tok->Token.Type) {
814 case TGSI_TOKEN_TYPE_IMMEDIATE:
815 nr_imm++;
816 break;
817 case TGSI_TOKEN_TYPE_DECLARATION:
818 {
819 const struct tgsi_full_declaration *fdec;
820
821 fdec = &p.FullToken.FullDeclaration;
822 switch (fdec->Declaration.File) {
823 case TGSI_FILE_TEMPORARY:
824 if (fdec->Range.Last > high_temp) {
825 high_temp =
826 fdec->Range.Last;
827 }
828 break;
829 #if 0 /* this would be nice.. except gallium doesn't track it */
830 case TGSI_FILE_ADDRESS:
831 if (fdec->Range.Last > high_addr) {
832 high_addr =
833 fdec->Range.Last;
834 }
835 break;
836 #endif
837 case TGSI_FILE_OUTPUT:
838 if (!nvfx_vertprog_parse_decl_output(nvfx, vpc, fdec))
839 return FALSE;
840 break;
841 default:
842 break;
843 }
844 }
845 break;
846 #if 1 /* yay, parse instructions looking for address regs instead */
847 case TGSI_TOKEN_TYPE_INSTRUCTION:
848 {
849 const struct tgsi_full_instruction *finst;
850 const struct tgsi_full_dst_register *fdst;
851
852 finst = &p.FullToken.FullInstruction;
853 fdst = &finst->Dst[0];
854
855 if (fdst->Register.File == TGSI_FILE_ADDRESS) {
856 if (fdst->Register.Index > high_addr)
857 high_addr = fdst->Register.Index;
858 }
859
860 }
861 break;
862 #endif
863 default:
864 break;
865 }
866 }
867 tgsi_parse_free(&p);
868
869 if (nr_imm) {
870 vpc->imm = CALLOC(nr_imm, sizeof(struct nvfx_reg));
871 assert(vpc->imm);
872 }
873
874 if (++high_temp) {
875 vpc->r_temp = CALLOC(high_temp, sizeof(struct nvfx_reg));
876 for (i = 0; i < high_temp; i++)
877 vpc->r_temp[i] = temp(vpc);
878 }
879
880 if (++high_addr) {
881 vpc->r_address = CALLOC(high_addr, sizeof(struct nvfx_reg));
882 for (i = 0; i < high_addr; i++)
883 vpc->r_address[i] = temp(vpc);
884 }
885
886 vpc->r_temps_discard = 0;
887 return TRUE;
888 }
889
890 DEBUG_GET_ONCE_BOOL_OPTION(nvfx_dump_vp, "NVFX_DUMP_VP", FALSE)
891
892 static void
893 nvfx_vertprog_translate(struct nvfx_context *nvfx,
894 struct nvfx_vertex_program *vp)
895 {
896 struct tgsi_parse_context parse;
897 struct nvfx_vpc *vpc = NULL;
898 struct nvfx_src none = nvfx_src(nvfx_reg(NVFXSR_NONE, 0));
899 struct util_dynarray insns;
900 int i;
901
902 vpc = CALLOC(1, sizeof(struct nvfx_vpc));
903 if (!vpc)
904 return;
905 vpc->nvfx = nvfx;
906 vpc->vp = vp;
907
908 if (!nvfx_vertprog_prepare(nvfx, vpc)) {
909 FREE(vpc);
910 return;
911 }
912
913 /* Redirect post-transform vertex position to a temp if user clip
914 * planes are enabled. We need to append code to the vtxprog
915 * to handle clip planes later.
916 */
917 if (vp->ucp.nr) {
918 vpc->r_result[vpc->hpos_idx] = temp(vpc);
919 vpc->r_temps_discard = 0;
920 }
921
922 tgsi_parse_init(&parse, vp->pipe.tokens);
923
924 util_dynarray_init(&insns);
925 while (!tgsi_parse_end_of_tokens(&parse)) {
926 tgsi_parse_token(&parse);
927
928 switch (parse.FullToken.Token.Type) {
929 case TGSI_TOKEN_TYPE_IMMEDIATE:
930 {
931 const struct tgsi_full_immediate *imm;
932
933 imm = &parse.FullToken.FullImmediate;
934 assert(imm->Immediate.DataType == TGSI_IMM_FLOAT32);
935 assert(imm->Immediate.NrTokens == 4 + 1);
936 vpc->imm[vpc->nr_imm++] =
937 constant(vpc, -1,
938 imm->u[0].Float,
939 imm->u[1].Float,
940 imm->u[2].Float,
941 imm->u[3].Float);
942 }
943 break;
944 case TGSI_TOKEN_TYPE_INSTRUCTION:
945 {
946 const struct tgsi_full_instruction *finst;
947 unsigned idx = insns.size >> 2;
948 util_dynarray_append(&insns, unsigned, vp->nr_insns);
949 finst = &parse.FullToken.FullInstruction;
950 if (!nvfx_vertprog_parse_instruction(nvfx, vpc, idx, finst))
951 goto out_err;
952 }
953 break;
954 default:
955 break;
956 }
957 }
958
959 util_dynarray_append(&insns, unsigned, vp->nr_insns);
960
961 for(unsigned i = 0; i < vpc->label_relocs.size; i += sizeof(struct nvfx_label_relocation))
962 {
963 struct nvfx_label_relocation* label_reloc = (struct nvfx_label_relocation*)((char*)vpc->label_relocs.data + i);
964 struct nvfx_label_relocation hw_reloc;
965
966 hw_reloc.location = label_reloc->location;
967 hw_reloc.target = ((unsigned*)insns.data)[label_reloc->target];
968
969 //debug_printf("hw %u -> tgsi %u = hw %u\n", hw_reloc.location, label_reloc->target, hw_reloc.target);
970
971 util_dynarray_append(&vp->branch_relocs, struct nvfx_label_relocation, hw_reloc);
972 }
973 util_dynarray_fini(&insns);
974 util_dynarray_trim(&vp->branch_relocs);
975
976 /* XXX: what if we add a RET before?! make sure we jump here...*/
977
978 /* Write out HPOS if it was redirected to a temp earlier */
979 if (vpc->r_result[vpc->hpos_idx].type != NVFXSR_OUTPUT) {
980 struct nvfx_reg hpos = nvfx_reg(NVFXSR_OUTPUT,
981 NVFX_VP(INST_DEST_POS));
982 struct nvfx_src htmp = nvfx_src(vpc->r_result[vpc->hpos_idx]);
983
984 nvfx_vp_emit(vpc, arith(VEC, MOV, hpos, NVFX_VP_MASK_ALL, htmp, none, none));
985 }
986
987 /* Insert code to handle user clip planes */
988 for (i = 0; i < vp->ucp.nr; i++) {
989 struct nvfx_reg cdst = nvfx_reg(NVFXSR_OUTPUT,
990 NVFX_VP_INST_DEST_CLIP(i));
991 struct nvfx_src ceqn = nvfx_src(constant(vpc, -1,
992 nvfx->clip.ucp[i][0],
993 nvfx->clip.ucp[i][1],
994 nvfx->clip.ucp[i][2],
995 nvfx->clip.ucp[i][3]));
996 struct nvfx_src htmp = nvfx_src(vpc->r_result[vpc->hpos_idx]);
997 unsigned mask;
998
999 switch (i) {
1000 case 0: case 3: mask = NVFX_VP_MASK_Y; break;
1001 case 1: case 4: mask = NVFX_VP_MASK_Z; break;
1002 case 2: case 5: mask = NVFX_VP_MASK_W; break;
1003 default:
1004 NOUVEAU_ERR("invalid clip dist #%d\n", i);
1005 goto out_err;
1006 }
1007
1008 nvfx_vp_emit(vpc, arith(VEC, DP4, cdst, mask, htmp, ceqn, none));
1009 }
1010
1011 //vp->insns[vp->nr_insns - 1].data[3] |= NVFX_VP_INST_LAST;
1012
1013 /* Append NOP + END instruction for branches to the end of the program */
1014 nvfx_vp_emit(vpc, arith(VEC, NOP, none.reg, 0, none, none, none));
1015 vp->insns[vp->nr_insns - 1].data[3] |= NVFX_VP_INST_LAST | 0x1000;
1016
1017 if(debug_get_option_nvfx_dump_vp())
1018 {
1019 debug_printf("\n");
1020 tgsi_dump(vp->pipe.tokens, 0);
1021
1022 debug_printf("\n%s vertex program:\n", nvfx->is_nv4x ? "nv4x" : "nv3x");
1023 for (i = 0; i < vp->nr_insns; i++)
1024 debug_printf("%3u: %08x %08x %08x %08x\n", i, vp->insns[i].data[0], vp->insns[i].data[1], vp->insns[i].data[2], vp->insns[i].data[3]);
1025 debug_printf("\n");
1026 }
1027
1028 vp->exec_start = -1;
1029 vp->translated = TRUE;
1030 out_err:
1031 tgsi_parse_free(&parse);
1032 util_dynarray_fini(&vpc->label_relocs);
1033 util_dynarray_fini(&vpc->loop_stack);
1034 if (vpc->r_temp)
1035 FREE(vpc->r_temp);
1036 if (vpc->r_address)
1037 FREE(vpc->r_address);
1038 if (vpc->imm)
1039 FREE(vpc->imm);
1040 FREE(vpc);
1041 }
1042
1043 boolean
1044 nvfx_vertprog_validate(struct nvfx_context *nvfx)
1045 {
1046 struct pipe_context *pipe = &nvfx->pipe;
1047 struct nvfx_screen *screen = nvfx->screen;
1048 struct nouveau_channel *chan = screen->base.channel;
1049 struct nouveau_grobj *eng3d = screen->eng3d;
1050 struct nvfx_vertex_program *vp;
1051 struct pipe_resource *constbuf;
1052 boolean upload_code = FALSE, upload_data = FALSE;
1053 int i;
1054
1055 if (nvfx->render_mode == HW) {
1056 vp = nvfx->vertprog;
1057 constbuf = nvfx->constbuf[PIPE_SHADER_VERTEX];
1058
1059 // TODO: ouch! can't we just use constant slots for these?!
1060 if ((nvfx->dirty & NVFX_NEW_UCP) ||
1061 memcmp(&nvfx->clip, &vp->ucp, sizeof(vp->ucp))) {
1062 nvfx_vertprog_destroy(nvfx, vp);
1063 memcpy(&vp->ucp, &nvfx->clip, sizeof(vp->ucp));
1064 }
1065 } else {
1066 vp = nvfx->swtnl.vertprog;
1067 constbuf = NULL;
1068 }
1069
1070 /* Translate TGSI shader into hw bytecode */
1071 if (!vp->translated)
1072 {
1073 nvfx->fallback_swtnl &= ~NVFX_NEW_VERTPROG;
1074 nvfx_vertprog_translate(nvfx, vp);
1075 if (!vp->translated) {
1076 nvfx->fallback_swtnl |= NVFX_NEW_VERTPROG;
1077 return FALSE;
1078 }
1079 }
1080
1081 /* Allocate hw vtxprog exec slots */
1082 if (!vp->exec) {
1083 struct nouveau_resource *heap = nvfx->screen->vp_exec_heap;
1084 uint vplen = vp->nr_insns;
1085
1086 if (nouveau_resource_alloc(heap, vplen, vp, &vp->exec)) {
1087 while (heap->next && heap->size < vplen) {
1088 struct nvfx_vertex_program *evict;
1089
1090 evict = heap->next->priv;
1091 nouveau_resource_free(&evict->exec);
1092 }
1093
1094 if (nouveau_resource_alloc(heap, vplen, vp, &vp->exec))
1095 assert(0);
1096 }
1097
1098 upload_code = TRUE;
1099 }
1100
1101 /* Allocate hw vtxprog const slots */
1102 if (vp->nr_consts && !vp->data) {
1103 struct nouveau_resource *heap = nvfx->screen->vp_data_heap;
1104
1105 if (nouveau_resource_alloc(heap, vp->nr_consts, vp, &vp->data)) {
1106 while (heap->next && heap->size < vp->nr_consts) {
1107 struct nvfx_vertex_program *evict;
1108
1109 evict = heap->next->priv;
1110 nouveau_resource_free(&evict->data);
1111 }
1112
1113 if (nouveau_resource_alloc(heap, vp->nr_consts, vp, &vp->data))
1114 assert(0);
1115 }
1116
1117 /*XXX: handle this some day */
1118 assert(vp->data->start >= vp->data_start_min);
1119
1120 upload_data = TRUE;
1121 if (vp->data_start != vp->data->start)
1122 upload_code = TRUE;
1123 }
1124
1125 /* If exec or data segments moved we need to patch the program to
1126 * fixup offsets and register IDs.
1127 */
1128 if (vp->exec_start != vp->exec->start) {
1129 //printf("vp_relocs %u -> %u\n", vp->exec_start, vp->exec->start);
1130 for(unsigned i = 0; i < vp->branch_relocs.size; i += sizeof(struct nvfx_label_relocation))
1131 {
1132 struct nvfx_label_relocation* reloc = (struct nvfx_label_relocation*)((char*)vp->branch_relocs.data + i);
1133 uint32_t* hw = vp->insns[reloc->location].data;
1134 unsigned target = vp->exec->start + reloc->target;
1135
1136 //debug_printf("vp_reloc hw %u -> hw %u\n", reloc->location, target);
1137
1138 if(!nvfx->is_nv4x)
1139 {
1140 hw[2] &=~ NV30_VP_INST_IADDR_MASK;
1141 hw[2] |= (target & 0x1ff) << NV30_VP_INST_IADDR_SHIFT;
1142 }
1143 else
1144 {
1145 hw[3] &=~ NV40_VP_INST_IADDRL_MASK;
1146 hw[3] |= (target & 7) << NV40_VP_INST_IADDRL_SHIFT;
1147
1148 hw[2] &=~ NV40_VP_INST_IADDRH_MASK;
1149 hw[2] |= ((target >> 3) & 0x3f) << NV40_VP_INST_IADDRH_SHIFT;
1150 }
1151 }
1152
1153 vp->exec_start = vp->exec->start;
1154 }
1155
1156 if (vp->nr_consts && vp->data_start != vp->data->start) {
1157 for (i = 0; i < vp->nr_insns; i++) {
1158 struct nvfx_vertex_program_exec *vpi = &vp->insns[i];
1159
1160 if (vpi->const_index >= 0) {
1161 vpi->data[1] &= ~NVFX_VP(INST_CONST_SRC_MASK);
1162 vpi->data[1] |=
1163 (vpi->const_index + vp->data->start) <<
1164 NVFX_VP(INST_CONST_SRC_SHIFT);
1165
1166 }
1167 }
1168
1169 vp->data_start = vp->data->start;
1170 }
1171
1172 /* Update + Upload constant values */
1173 if (vp->nr_consts) {
1174 float *map = NULL;
1175
1176 if (constbuf)
1177 map = nvfx_buffer(constbuf)->data;
1178
1179 for (i = 0; i < vp->nr_consts; i++) {
1180 struct nvfx_vertex_program_data *vpd = &vp->consts[i];
1181
1182 if (vpd->index >= 0) {
1183 if (!upload_data &&
1184 !memcmp(vpd->value, &map[vpd->index * 4],
1185 4 * sizeof(float)))
1186 continue;
1187 memcpy(vpd->value, &map[vpd->index * 4],
1188 4 * sizeof(float));
1189 }
1190
1191 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_CONST_ID, 5);
1192 OUT_RING (chan, i + vp->data->start);
1193 OUT_RINGp (chan, (uint32_t *)vpd->value, 4);
1194 }
1195 }
1196
1197 /* Upload vtxprog */
1198 if (upload_code) {
1199 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_FROM_ID, 1);
1200 OUT_RING (chan, vp->exec->start);
1201 for (i = 0; i < vp->nr_insns; i++) {
1202 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_INST(0), 4);
1203 OUT_RINGp (chan, vp->insns[i].data, 4);
1204 }
1205 }
1206
1207 if(nvfx->dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_UCP))
1208 {
1209 WAIT_RING(chan, 7);
1210 OUT_RING(chan, RING_3D(NV34TCL_VP_START_FROM_ID, 1));
1211 OUT_RING(chan, vp->exec->start);
1212 if(nvfx->is_nv4x) {
1213 OUT_RING(chan, RING_3D(NV40TCL_VP_ATTRIB_EN, 2));
1214 OUT_RING(chan, vp->ir);
1215 OUT_RING(chan, vp->or);
1216 }
1217 OUT_RING(chan, RING_3D(NV34TCL_VP_CLIP_PLANES_ENABLE, 1));
1218 OUT_RING(chan, vp->clip_ctrl);
1219 }
1220
1221 return TRUE;
1222 }
1223
1224 void
1225 nvfx_vertprog_destroy(struct nvfx_context *nvfx, struct nvfx_vertex_program *vp)
1226 {
1227 vp->translated = FALSE;
1228
1229 if (vp->nr_insns) {
1230 FREE(vp->insns);
1231 vp->insns = NULL;
1232 vp->nr_insns = 0;
1233 }
1234
1235 if (vp->nr_consts) {
1236 FREE(vp->consts);
1237 vp->consts = NULL;
1238 vp->nr_consts = 0;
1239 }
1240
1241 nouveau_resource_free(&vp->exec);
1242 vp->exec_start = 0;
1243 nouveau_resource_free(&vp->data);
1244 vp->data_start = 0;
1245 vp->data_start_min = 0;
1246
1247 vp->ir = vp->or = vp->clip_ctrl = 0;
1248 }