98fcc928982ec6ebd13d83ba185fc83b17c109ab
[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_POW:
571 tmp = nvfx_src(temp(vpc));
572 nvfx_vp_emit(vpc, arith(SCA, LG2, tmp.reg, NVFX_VP_MASK_X, none, none, swz(src[0], X, X, X, X)));
573 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));
574 nvfx_vp_emit(vpc, arith(SCA, EX2, dst, mask, none, none, swz(tmp, X, X, X, X)));
575 break;
576 case TGSI_OPCODE_RCP:
577 nvfx_vp_emit(vpc, arith(SCA, RCP, dst, mask, none, none, src[0]));
578 break;
579 case TGSI_OPCODE_RSQ:
580 nvfx_vp_emit(vpc, arith(SCA, RSQ, dst, mask, none, none, abs(src[0])));
581 break;
582 case TGSI_OPCODE_SEQ:
583 nvfx_vp_emit(vpc, arith(VEC, SEQ, dst, mask, src[0], src[1], none));
584 break;
585 case TGSI_OPCODE_SFL:
586 nvfx_vp_emit(vpc, arith(VEC, SFL, dst, mask, src[0], src[1], none));
587 break;
588 case TGSI_OPCODE_SGE:
589 nvfx_vp_emit(vpc, arith(VEC, SGE, dst, mask, src[0], src[1], none));
590 break;
591 case TGSI_OPCODE_SGT:
592 nvfx_vp_emit(vpc, arith(VEC, SGT, dst, mask, src[0], src[1], none));
593 break;
594 case TGSI_OPCODE_SIN:
595 nvfx_vp_emit(vpc, arith(SCA, SIN, dst, mask, none, none, src[0]));
596 break;
597 case TGSI_OPCODE_SLE:
598 nvfx_vp_emit(vpc, arith(VEC, SLE, dst, mask, src[0], src[1], none));
599 break;
600 case TGSI_OPCODE_SLT:
601 nvfx_vp_emit(vpc, arith(VEC, SLT, dst, mask, src[0], src[1], none));
602 break;
603 case TGSI_OPCODE_SNE:
604 nvfx_vp_emit(vpc, arith(VEC, SNE, dst, mask, src[0], src[1], none));
605 break;
606 case TGSI_OPCODE_SSG:
607 nvfx_vp_emit(vpc, arith(VEC, SSG, dst, mask, src[0], src[1], none));
608 break;
609 case TGSI_OPCODE_STR:
610 nvfx_vp_emit(vpc, arith(VEC, STR, dst, mask, src[0], src[1], none));
611 break;
612 case TGSI_OPCODE_SUB:
613 nvfx_vp_emit(vpc, arith(VEC, ADD, dst, mask, src[0], none, neg(src[1])));
614 break;
615 case TGSI_OPCODE_XPD:
616 tmp = nvfx_src(temp(vpc));
617 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));
618 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)));
619 break;
620
621 case TGSI_OPCODE_IF:
622 insn = arith(VEC, MOV, none.reg, NVFX_VP_MASK_X, src[0], none, none);
623 insn.cc_update = 1;
624 nvfx_vp_emit(vpc, insn);
625
626 reloc.location = vpc->vp->nr_insns;
627 reloc.target = finst->Label.Label + 1;
628 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
629
630 insn = arith(SCA, BRA, none.reg, 0, none, none, none);
631 insn.cc_test = NVFX_COND_EQ;
632 insn.cc_swz[0] = insn.cc_swz[1] = insn.cc_swz[2] = insn.cc_swz[3] = 0;
633 nvfx_vp_emit(vpc, insn);
634 break;
635
636 case TGSI_OPCODE_ELSE:
637 case TGSI_OPCODE_BRA:
638 case TGSI_OPCODE_CAL:
639 reloc.location = vpc->vp->nr_insns;
640 reloc.target = finst->Label.Label;
641 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
642
643 if(finst->Instruction.Opcode == TGSI_OPCODE_CAL)
644 insn = arith(SCA, CAL, none.reg, 0, none, none, none);
645 else
646 insn = arith(SCA, BRA, none.reg, 0, none, none, none);
647 nvfx_vp_emit(vpc, insn);
648 break;
649
650 case TGSI_OPCODE_RET:
651 tmp = none;
652 tmp.swz[0] = tmp.swz[1] = tmp.swz[2] = tmp.swz[3] = 0;
653 nvfx_vp_emit(vpc, arith(SCA, RET, none.reg, 0, none, none, tmp));
654 break;
655
656 case TGSI_OPCODE_BGNSUB:
657 case TGSI_OPCODE_ENDSUB:
658 case TGSI_OPCODE_ENDIF:
659 /* nothing to do here */
660 break;
661
662 case TGSI_OPCODE_BGNLOOP:
663 loop.cont_target = idx;
664 loop.brk_target = finst->Label.Label + 1;
665 util_dynarray_append(&vpc->loop_stack, struct nvfx_loop_entry, loop);
666 break;
667
668 case TGSI_OPCODE_ENDLOOP:
669 loop = util_dynarray_pop(&vpc->loop_stack, struct nvfx_loop_entry);
670
671 reloc.location = vpc->vp->nr_insns;
672 reloc.target = loop.cont_target;
673 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
674
675 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
676 break;
677
678 case TGSI_OPCODE_CONT:
679 loop = util_dynarray_top(&vpc->loop_stack, struct nvfx_loop_entry);
680
681 reloc.location = vpc->vp->nr_insns;
682 reloc.target = loop.cont_target;
683 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
684
685 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
686 break;
687
688 case TGSI_OPCODE_BRK:
689 loop = util_dynarray_top(&vpc->loop_stack, struct nvfx_loop_entry);
690
691 reloc.location = vpc->vp->nr_insns;
692 reloc.target = loop.brk_target;
693 util_dynarray_append(&vpc->label_relocs, struct nvfx_label_relocation, reloc);
694
695 nvfx_vp_emit(vpc, arith(SCA, BRA, none.reg, 0, none, none, none));
696 break;
697
698 default:
699 NOUVEAU_ERR("invalid opcode %d\n", finst->Instruction.Opcode);
700 return FALSE;
701 }
702
703 release_temps(vpc);
704 return TRUE;
705 }
706
707 static boolean
708 nvfx_vertprog_parse_decl_output(struct nvfx_context* nvfx, struct nvfx_vpc *vpc,
709 const struct tgsi_full_declaration *fdec)
710 {
711 unsigned idx = fdec->Range.First;
712 int hw;
713
714 switch (fdec->Semantic.Name) {
715 case TGSI_SEMANTIC_POSITION:
716 hw = NVFX_VP(INST_DEST_POS);
717 vpc->hpos_idx = idx;
718 break;
719 case TGSI_SEMANTIC_COLOR:
720 if (fdec->Semantic.Index == 0) {
721 hw = NVFX_VP(INST_DEST_COL0);
722 } else
723 if (fdec->Semantic.Index == 1) {
724 hw = NVFX_VP(INST_DEST_COL1);
725 } else {
726 NOUVEAU_ERR("bad colour semantic index\n");
727 return FALSE;
728 }
729 break;
730 case TGSI_SEMANTIC_BCOLOR:
731 if (fdec->Semantic.Index == 0) {
732 hw = NVFX_VP(INST_DEST_BFC0);
733 } else
734 if (fdec->Semantic.Index == 1) {
735 hw = NVFX_VP(INST_DEST_BFC1);
736 } else {
737 NOUVEAU_ERR("bad bcolour semantic index\n");
738 return FALSE;
739 }
740 break;
741 case TGSI_SEMANTIC_FOG:
742 hw = NVFX_VP(INST_DEST_FOGC);
743 break;
744 case TGSI_SEMANTIC_PSIZE:
745 hw = NVFX_VP(INST_DEST_PSZ);
746 break;
747 case TGSI_SEMANTIC_GENERIC:
748 hw = (vpc->vp->generic_to_fp_input[fdec->Semantic.Index] & 0xf)
749 + NVFX_VP(INST_DEST_TC(0)) - NVFX_FP_OP_INPUT_SRC_TC(0);
750 break;
751 case TGSI_SEMANTIC_EDGEFLAG:
752 /* not really an error just a fallback */
753 NOUVEAU_ERR("cannot handle edgeflag output\n");
754 return FALSE;
755 default:
756 NOUVEAU_ERR("bad output semantic\n");
757 return FALSE;
758 }
759
760 vpc->r_result[idx] = nvfx_reg(NVFXSR_OUTPUT, hw);
761 return TRUE;
762 }
763
764 static boolean
765 nvfx_vertprog_prepare(struct nvfx_context* nvfx, struct nvfx_vpc *vpc)
766 {
767 struct tgsi_parse_context p;
768 int high_temp = -1, high_addr = -1, nr_imm = 0, i;
769 struct util_semantic_set set;
770 unsigned char sem_layout[8];
771 unsigned sem_layout_size;
772 unsigned num_outputs;
773
774 num_outputs = util_semantic_set_from_program_file(&set, vpc->vp->pipe.tokens, TGSI_FILE_OUTPUT);
775
776 if(num_outputs > 8) {
777 NOUVEAU_ERR("too many vertex program outputs: %i\n", num_outputs);
778 return FALSE;
779 }
780 util_semantic_layout_from_set(sem_layout, &set, 8, 8);
781
782 /* hope 0xf is (0, 0, 0, 1) initialized; otherwise, we are _probably_ not required to do this */
783 memset(vpc->vp->generic_to_fp_input, 0x0f, sizeof(vpc->vp->generic_to_fp_input));
784 vpc->vp->texcoord_ouput_mask = 0;
785 for(int i = 0; i < 8; ++i) {
786 if(sem_layout[i] == 0xff)
787 continue;
788 vpc->vp->texcoord_ouput_mask |= (1 << i);
789 //printf("vp: GENERIC[%i] to fpreg %i\n", sem_layout[i], NVFX_FP_OP_INPUT_SRC_TC(0) + i);
790 vpc->vp->generic_to_fp_input[sem_layout[i]] = 0xf0 | (NVFX_FP_OP_INPUT_SRC_TC(0) + i);
791 }
792
793 tgsi_parse_init(&p, vpc->vp->pipe.tokens);
794 while (!tgsi_parse_end_of_tokens(&p)) {
795 const union tgsi_full_token *tok = &p.FullToken;
796
797 tgsi_parse_token(&p);
798 switch(tok->Token.Type) {
799 case TGSI_TOKEN_TYPE_IMMEDIATE:
800 nr_imm++;
801 break;
802 case TGSI_TOKEN_TYPE_DECLARATION:
803 {
804 const struct tgsi_full_declaration *fdec;
805
806 fdec = &p.FullToken.FullDeclaration;
807 switch (fdec->Declaration.File) {
808 case TGSI_FILE_TEMPORARY:
809 if (fdec->Range.Last > high_temp) {
810 high_temp =
811 fdec->Range.Last;
812 }
813 break;
814 #if 0 /* this would be nice.. except gallium doesn't track it */
815 case TGSI_FILE_ADDRESS:
816 if (fdec->Range.Last > high_addr) {
817 high_addr =
818 fdec->Range.Last;
819 }
820 break;
821 #endif
822 case TGSI_FILE_OUTPUT:
823 if (!nvfx_vertprog_parse_decl_output(nvfx, vpc, fdec))
824 return FALSE;
825 break;
826 default:
827 break;
828 }
829 }
830 break;
831 #if 1 /* yay, parse instructions looking for address regs instead */
832 case TGSI_TOKEN_TYPE_INSTRUCTION:
833 {
834 const struct tgsi_full_instruction *finst;
835 const struct tgsi_full_dst_register *fdst;
836
837 finst = &p.FullToken.FullInstruction;
838 fdst = &finst->Dst[0];
839
840 if (fdst->Register.File == TGSI_FILE_ADDRESS) {
841 if (fdst->Register.Index > high_addr)
842 high_addr = fdst->Register.Index;
843 }
844
845 }
846 break;
847 #endif
848 default:
849 break;
850 }
851 }
852 tgsi_parse_free(&p);
853
854 if (nr_imm) {
855 vpc->imm = CALLOC(nr_imm, sizeof(struct nvfx_reg));
856 assert(vpc->imm);
857 }
858
859 if (++high_temp) {
860 vpc->r_temp = CALLOC(high_temp, sizeof(struct nvfx_reg));
861 for (i = 0; i < high_temp; i++)
862 vpc->r_temp[i] = temp(vpc);
863 }
864
865 if (++high_addr) {
866 vpc->r_address = CALLOC(high_addr, sizeof(struct nvfx_reg));
867 for (i = 0; i < high_addr; i++)
868 vpc->r_address[i] = temp(vpc);
869 }
870
871 vpc->r_temps_discard = 0;
872 return TRUE;
873 }
874
875 DEBUG_GET_ONCE_BOOL_OPTION(nvfx_dump_vp, "NVFX_DUMP_VP", FALSE)
876
877 static void
878 nvfx_vertprog_translate(struct nvfx_context *nvfx,
879 struct nvfx_vertex_program *vp)
880 {
881 struct tgsi_parse_context parse;
882 struct nvfx_vpc *vpc = NULL;
883 struct nvfx_src none = nvfx_src(nvfx_reg(NVFXSR_NONE, 0));
884 struct util_dynarray insns;
885 int i;
886
887 vpc = CALLOC(1, sizeof(struct nvfx_vpc));
888 if (!vpc)
889 return;
890 vpc->nvfx = nvfx;
891 vpc->vp = vp;
892
893 if (!nvfx_vertprog_prepare(nvfx, vpc)) {
894 FREE(vpc);
895 return;
896 }
897
898 /* Redirect post-transform vertex position to a temp if user clip
899 * planes are enabled. We need to append code to the vtxprog
900 * to handle clip planes later.
901 */
902 if (vp->ucp.nr) {
903 vpc->r_result[vpc->hpos_idx] = temp(vpc);
904 vpc->r_temps_discard = 0;
905 }
906
907 tgsi_parse_init(&parse, vp->pipe.tokens);
908
909 util_dynarray_init(&insns);
910 while (!tgsi_parse_end_of_tokens(&parse)) {
911 tgsi_parse_token(&parse);
912
913 switch (parse.FullToken.Token.Type) {
914 case TGSI_TOKEN_TYPE_IMMEDIATE:
915 {
916 const struct tgsi_full_immediate *imm;
917
918 imm = &parse.FullToken.FullImmediate;
919 assert(imm->Immediate.DataType == TGSI_IMM_FLOAT32);
920 assert(imm->Immediate.NrTokens == 4 + 1);
921 vpc->imm[vpc->nr_imm++] =
922 constant(vpc, -1,
923 imm->u[0].Float,
924 imm->u[1].Float,
925 imm->u[2].Float,
926 imm->u[3].Float);
927 }
928 break;
929 case TGSI_TOKEN_TYPE_INSTRUCTION:
930 {
931 const struct tgsi_full_instruction *finst;
932 unsigned idx = insns.size >> 2;
933 util_dynarray_append(&insns, unsigned, vp->nr_insns);
934 finst = &parse.FullToken.FullInstruction;
935 if (!nvfx_vertprog_parse_instruction(nvfx, vpc, idx, finst))
936 goto out_err;
937 }
938 break;
939 default:
940 break;
941 }
942 }
943
944 util_dynarray_append(&insns, unsigned, vp->nr_insns);
945
946 for(unsigned i = 0; i < vpc->label_relocs.size; i += sizeof(struct nvfx_label_relocation))
947 {
948 struct nvfx_label_relocation* label_reloc = (struct nvfx_label_relocation*)((char*)vpc->label_relocs.data + i);
949 struct nvfx_label_relocation hw_reloc;
950
951 hw_reloc.location = label_reloc->location;
952 hw_reloc.target = ((unsigned*)insns.data)[label_reloc->target];
953
954 //debug_printf("hw %u -> tgsi %u = hw %u\n", hw_reloc.location, label_reloc->target, hw_reloc.target);
955
956 util_dynarray_append(&vp->branch_relocs, struct nvfx_label_relocation, hw_reloc);
957 }
958 util_dynarray_fini(&insns);
959 util_dynarray_trim(&vp->branch_relocs);
960
961 /* XXX: what if we add a RET before?! make sure we jump here...*/
962
963 /* Write out HPOS if it was redirected to a temp earlier */
964 if (vpc->r_result[vpc->hpos_idx].type != NVFXSR_OUTPUT) {
965 struct nvfx_reg hpos = nvfx_reg(NVFXSR_OUTPUT,
966 NVFX_VP(INST_DEST_POS));
967 struct nvfx_src htmp = nvfx_src(vpc->r_result[vpc->hpos_idx]);
968
969 nvfx_vp_emit(vpc, arith(VEC, MOV, hpos, NVFX_VP_MASK_ALL, htmp, none, none));
970 }
971
972 /* Insert code to handle user clip planes */
973 for (i = 0; i < vp->ucp.nr; i++) {
974 struct nvfx_reg cdst = nvfx_reg(NVFXSR_OUTPUT,
975 NVFX_VP_INST_DEST_CLIP(i));
976 struct nvfx_src ceqn = nvfx_src(constant(vpc, -1,
977 nvfx->clip.ucp[i][0],
978 nvfx->clip.ucp[i][1],
979 nvfx->clip.ucp[i][2],
980 nvfx->clip.ucp[i][3]));
981 struct nvfx_src htmp = nvfx_src(vpc->r_result[vpc->hpos_idx]);
982 unsigned mask;
983
984 switch (i) {
985 case 0: case 3: mask = NVFX_VP_MASK_Y; break;
986 case 1: case 4: mask = NVFX_VP_MASK_Z; break;
987 case 2: case 5: mask = NVFX_VP_MASK_W; break;
988 default:
989 NOUVEAU_ERR("invalid clip dist #%d\n", i);
990 goto out_err;
991 }
992
993 nvfx_vp_emit(vpc, arith(VEC, DP4, cdst, mask, htmp, ceqn, none));
994 }
995
996 //vp->insns[vp->nr_insns - 1].data[3] |= NVFX_VP_INST_LAST;
997
998 /* Append NOP + END instruction for branches to the end of the program */
999 nvfx_vp_emit(vpc, arith(VEC, NOP, none.reg, 0, none, none, none));
1000 vp->insns[vp->nr_insns - 1].data[3] |= NVFX_VP_INST_LAST | 0x1000;
1001
1002 if(debug_get_option_nvfx_dump_vp())
1003 {
1004 debug_printf("\n");
1005 tgsi_dump(vp->pipe.tokens, 0);
1006
1007 debug_printf("\n%s vertex program:\n", nvfx->is_nv4x ? "nv4x" : "nv3x");
1008 for (i = 0; i < vp->nr_insns; i++)
1009 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]);
1010 debug_printf("\n");
1011 }
1012
1013 vp->exec_start = -1;
1014 vp->translated = TRUE;
1015 out_err:
1016 tgsi_parse_free(&parse);
1017 util_dynarray_fini(&vpc->label_relocs);
1018 util_dynarray_fini(&vpc->loop_stack);
1019 if (vpc->r_temp)
1020 FREE(vpc->r_temp);
1021 if (vpc->r_address)
1022 FREE(vpc->r_address);
1023 if (vpc->imm)
1024 FREE(vpc->imm);
1025 FREE(vpc);
1026 }
1027
1028 boolean
1029 nvfx_vertprog_validate(struct nvfx_context *nvfx)
1030 {
1031 struct pipe_context *pipe = &nvfx->pipe;
1032 struct nvfx_screen *screen = nvfx->screen;
1033 struct nouveau_channel *chan = screen->base.channel;
1034 struct nouveau_grobj *eng3d = screen->eng3d;
1035 struct nvfx_vertex_program *vp;
1036 struct pipe_resource *constbuf;
1037 boolean upload_code = FALSE, upload_data = FALSE;
1038 int i;
1039
1040 if (nvfx->render_mode == HW) {
1041 vp = nvfx->vertprog;
1042 constbuf = nvfx->constbuf[PIPE_SHADER_VERTEX];
1043
1044 // TODO: ouch! can't we just use constant slots for these?!
1045 if ((nvfx->dirty & NVFX_NEW_UCP) ||
1046 memcmp(&nvfx->clip, &vp->ucp, sizeof(vp->ucp))) {
1047 nvfx_vertprog_destroy(nvfx, vp);
1048 memcpy(&vp->ucp, &nvfx->clip, sizeof(vp->ucp));
1049 }
1050 } else {
1051 vp = nvfx->swtnl.vertprog;
1052 constbuf = NULL;
1053 }
1054
1055 /* Translate TGSI shader into hw bytecode */
1056 if (!vp->translated)
1057 {
1058 nvfx->fallback_swtnl &= ~NVFX_NEW_VERTPROG;
1059 nvfx_vertprog_translate(nvfx, vp);
1060 if (!vp->translated) {
1061 nvfx->fallback_swtnl |= NVFX_NEW_VERTPROG;
1062 return FALSE;
1063 }
1064 }
1065
1066 /* Allocate hw vtxprog exec slots */
1067 if (!vp->exec) {
1068 struct nouveau_resource *heap = nvfx->screen->vp_exec_heap;
1069 uint vplen = vp->nr_insns;
1070
1071 if (nouveau_resource_alloc(heap, vplen, vp, &vp->exec)) {
1072 while (heap->next && heap->size < vplen) {
1073 struct nvfx_vertex_program *evict;
1074
1075 evict = heap->next->priv;
1076 nouveau_resource_free(&evict->exec);
1077 }
1078
1079 if (nouveau_resource_alloc(heap, vplen, vp, &vp->exec))
1080 assert(0);
1081 }
1082
1083 upload_code = TRUE;
1084 }
1085
1086 /* Allocate hw vtxprog const slots */
1087 if (vp->nr_consts && !vp->data) {
1088 struct nouveau_resource *heap = nvfx->screen->vp_data_heap;
1089
1090 if (nouveau_resource_alloc(heap, vp->nr_consts, vp, &vp->data)) {
1091 while (heap->next && heap->size < vp->nr_consts) {
1092 struct nvfx_vertex_program *evict;
1093
1094 evict = heap->next->priv;
1095 nouveau_resource_free(&evict->data);
1096 }
1097
1098 if (nouveau_resource_alloc(heap, vp->nr_consts, vp, &vp->data))
1099 assert(0);
1100 }
1101
1102 /*XXX: handle this some day */
1103 assert(vp->data->start >= vp->data_start_min);
1104
1105 upload_data = TRUE;
1106 if (vp->data_start != vp->data->start)
1107 upload_code = TRUE;
1108 }
1109
1110 /* If exec or data segments moved we need to patch the program to
1111 * fixup offsets and register IDs.
1112 */
1113 if (vp->exec_start != vp->exec->start) {
1114 //printf("vp_relocs %u -> %u\n", vp->exec_start, vp->exec->start);
1115 for(unsigned i = 0; i < vp->branch_relocs.size; i += sizeof(struct nvfx_label_relocation))
1116 {
1117 struct nvfx_label_relocation* reloc = (struct nvfx_label_relocation*)((char*)vp->branch_relocs.data + i);
1118 uint32_t* hw = vp->insns[reloc->location].data;
1119 unsigned target = vp->exec->start + reloc->target;
1120
1121 //debug_printf("vp_reloc hw %u -> hw %u\n", reloc->location, target);
1122
1123 if(!nvfx->is_nv4x)
1124 {
1125 hw[2] &=~ NV30_VP_INST_IADDR_MASK;
1126 hw[2] |= (target & 0x1ff) << NV30_VP_INST_IADDR_SHIFT;
1127 }
1128 else
1129 {
1130 hw[3] &=~ NV40_VP_INST_IADDRL_MASK;
1131 hw[3] |= (target & 7) << NV40_VP_INST_IADDRL_SHIFT;
1132
1133 hw[2] &=~ NV40_VP_INST_IADDRH_MASK;
1134 hw[2] |= ((target >> 3) & 0x3f) << NV40_VP_INST_IADDRH_SHIFT;
1135 }
1136 }
1137
1138 vp->exec_start = vp->exec->start;
1139 }
1140
1141 if (vp->nr_consts && vp->data_start != vp->data->start) {
1142 for (i = 0; i < vp->nr_insns; i++) {
1143 struct nvfx_vertex_program_exec *vpi = &vp->insns[i];
1144
1145 if (vpi->const_index >= 0) {
1146 vpi->data[1] &= ~NVFX_VP(INST_CONST_SRC_MASK);
1147 vpi->data[1] |=
1148 (vpi->const_index + vp->data->start) <<
1149 NVFX_VP(INST_CONST_SRC_SHIFT);
1150
1151 }
1152 }
1153
1154 vp->data_start = vp->data->start;
1155 }
1156
1157 /* Update + Upload constant values */
1158 if (vp->nr_consts) {
1159 float *map = NULL;
1160
1161 if (constbuf)
1162 map = nvfx_buffer(constbuf)->data;
1163
1164 for (i = 0; i < vp->nr_consts; i++) {
1165 struct nvfx_vertex_program_data *vpd = &vp->consts[i];
1166
1167 if (vpd->index >= 0) {
1168 if (!upload_data &&
1169 !memcmp(vpd->value, &map[vpd->index * 4],
1170 4 * sizeof(float)))
1171 continue;
1172 memcpy(vpd->value, &map[vpd->index * 4],
1173 4 * sizeof(float));
1174 }
1175
1176 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_CONST_ID, 5);
1177 OUT_RING (chan, i + vp->data->start);
1178 OUT_RINGp (chan, (uint32_t *)vpd->value, 4);
1179 }
1180 }
1181
1182 /* Upload vtxprog */
1183 if (upload_code) {
1184 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_FROM_ID, 1);
1185 OUT_RING (chan, vp->exec->start);
1186 for (i = 0; i < vp->nr_insns; i++) {
1187 BEGIN_RING(chan, eng3d, NV34TCL_VP_UPLOAD_INST(0), 4);
1188 OUT_RINGp (chan, vp->insns[i].data, 4);
1189 }
1190 }
1191
1192 if(nvfx->dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_UCP))
1193 {
1194 WAIT_RING(chan, 7);
1195 OUT_RING(chan, RING_3D(NV34TCL_VP_START_FROM_ID, 1));
1196 OUT_RING(chan, vp->exec->start);
1197 if(nvfx->is_nv4x) {
1198 OUT_RING(chan, RING_3D(NV40TCL_VP_ATTRIB_EN, 2));
1199 OUT_RING(chan, vp->ir);
1200 OUT_RING(chan, vp->or);
1201 }
1202 OUT_RING(chan, RING_3D(NV34TCL_VP_CLIP_PLANES_ENABLE, 1));
1203 OUT_RING(chan, vp->clip_ctrl);
1204 }
1205
1206 return TRUE;
1207 }
1208
1209 void
1210 nvfx_vertprog_destroy(struct nvfx_context *nvfx, struct nvfx_vertex_program *vp)
1211 {
1212 vp->translated = FALSE;
1213
1214 if (vp->nr_insns) {
1215 FREE(vp->insns);
1216 vp->insns = NULL;
1217 vp->nr_insns = 0;
1218 }
1219
1220 if (vp->nr_consts) {
1221 FREE(vp->consts);
1222 vp->consts = NULL;
1223 vp->nr_consts = 0;
1224 }
1225
1226 nouveau_resource_free(&vp->exec);
1227 vp->exec_start = 0;
1228 nouveau_resource_free(&vp->data);
1229 vp->data_start = 0;
1230 vp->data_start_min = 0;
1231
1232 vp->ir = vp->or = vp->clip_ctrl = 0;
1233 }