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