summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/nouveau/nv40_vtxprog.c
blob: 582bf65ea00100baeab67f7dc3690ca87356227c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "glheader.h"
#include "macros.h"
#include "enums.h"
#include "program.h"
#include "program_instruction.h"

#include "nouveau_reg.h"
#include "nouveau_shader.h"
#include "nouveau_msg.h"

#include "nv40_reg.h"

/* TODO:
 *  - Implement support for constants
 *  - Handle SWZ with 0/1 components and partial negate masks
 *  - Handle ARB_position_invarient
 *  - Relative register addressing
 *  - Implement any missing instructions
 */

static int t_dst_mask(int mask);

static int
alloc_hw_temp(nouveau_vertex_program *vp)
{
	return nvsAllocIndex(vp->hwtemps_in_use, 64);
}

static void
free_hw_temp(nouveau_vertex_program *vp, int id)
{
	nvsBitClear(vp->hwtemps_in_use, id);
}

static int
alloc_temp(nouveau_vertex_program *vp)
{
	int idx;

	idx = nvsAllocIndex(vp->temps_in_use, 64);
	if (!idx)
		return -1;

	vp->temps[idx].file  = HW_TEMP;
	vp->temps[idx].hw_id = -1;
	vp->temps[idx].ref   = -1;

	return idx;
}

static void
free_temp(nouveau_vertex_program *vp, nouveau_srcreg *temp)
{
	if (!temp) return;

	if (vp->temps[temp->idx].hw_id != -1)
		free_hw_temp(vp, vp->temps[temp->idx].hw_id);
	nvsBitClear(vp->temps_in_use, temp->idx);
}

static void
make_srcreg(nouveau_vertex_program *vp,
		nouveau_srcreg *src,
		nouveau_regtype type,
		int id)
{
	switch (type) {
	case HW_INPUT:
		src->hw  = &vp->inputs[id];
		src->idx = id;
		break;
	case HW_TEMP:
		src->hw  = &vp->temps[id];
		src->idx = id;
		break;
	case HW_CONST:
		//FIXME: TODO
		break;
	default:
		assert(0);
		break;
	}

	src->negate  = 0;
	src->swizzle = 0x1B /* 00011011 - XYZW */;
}

static void
make_dstreg(nouveau_vertex_program *vp,
		nouveau_dstreg *dest,
		nouveau_regtype type,
		int id)
{
	if (type == HW_TEMP && id == -1)
		dest->idx = alloc_temp(vp);
	else
		dest->idx = id;
	switch (type) {
	case HW_TEMP:
		dest->idx = id;
		if (dest->idx == -1)
			dest->idx = alloc_temp(vp);
		dest->hw = &vp->temps[dest->idx];
		break;
	case HW_OUTPUT:
		dest->hw  = NULL;
		dest->idx = id;
		break;
	default:
		assert(0);
		break;
	}

	dest->mask     = t_dst_mask(WRITEMASK_XYZW);
	dest->condup   = 0;
	dest->condreg  = 0;
	dest->condtest = NV40_VP_INST_COND_TR;
	dest->condswz  = 0x1B /* 00011011 - XYZW */;
}

static unsigned int
src_to_hw(nouveau_vertex_program *vp, nouveau_srcreg *src,
		unsigned int *is, unsigned int *cs)
{
	unsigned int hs = 0;

	if (!src) {
		/* unused sources seem to be INPUT swz XYZW, dont't know if this
		 * actually matters or not...
		 */
		hs |= (NV40_VP_SRC_REG_TYPE_INPUT << NV40_VP_SRC_REG_TYPE_SHIFT);
		hs |= (0x1B << NV40_VP_SRC_SWZ_ALL_SHIFT);
		return hs;
	}

	switch (src->hw->file) {
	case HW_INPUT:
		if (*is != -1) {
			fprintf(stderr, "multiple inputs detected... not good\n");
			return;
		}
		*is = src->hw->hw_id;
		hs |= (NV40_VP_SRC_REG_TYPE_INPUT << NV40_VP_SRC_REG_TYPE_SHIFT);
		break;
	case HW_CONST:
		if (*cs != -1) {
			fprintf(stderr, "multiple consts detected... not good\n");
			return;
		}
		*cs = src->hw->hw_id;
		hs |= (NV40_VP_SRC_REG_TYPE_CONST << NV40_VP_SRC_REG_TYPE_SHIFT);
		break;
	case HW_TEMP:
		if (src->hw->hw_id == -1) {
			fprintf(stderr, "read from unwritten temp!\n");
			return;
		}
		hs |= (NV40_VP_SRC_REG_TYPE_TEMP << NV40_VP_SRC_REG_TYPE_SHIFT) |
			(src->hw->hw_id << NV40_VP_SRC_TEMP_SRC_SHIFT);

		if (--src->hw->ref == 0)
			free_hw_temp(vp, src->hw->hw_id);
	}

	hs |= (src->swizzle << NV40_VP_SRC_SWZ_ALL_SHIFT);
	if (src->negate)
		hs |= NV40_VP_SRC_NEGATE;

	return hs;
}

static void
instruction_store(nouveau_vertex_program *vp, unsigned int inst[])
{
	if ((vp->inst_count+1) > vp->insns_alloced) {
		vp->insns = realloc(vp->insns, sizeof(unsigned int) * (vp->inst_count+1) * 4);
		vp->insns_alloced = vp->inst_count+1;
	}
	vp->insns[(vp->inst_count*4) + 0] = inst[0];
	vp->insns[(vp->inst_count*4) + 1] = inst[1];
	vp->insns[(vp->inst_count*4) + 2] = inst[2];
	vp->insns[(vp->inst_count*4) + 3] = inst[3];
	vp->inst_count++;
}

static void
emit_arith(nouveau_vertex_program *vp, int op,
		nouveau_dstreg *dest,
		nouveau_srcreg *src0,
		nouveau_srcreg *src1,
		nouveau_srcreg *src2,
		int flags)
{
	nouveau_regrec *hwdest = dest->hw;
	unsigned int hs0, hs1, hs2;
	unsigned int hop[4] = { 0, 0, 0, 0 };
	int insrc = -1, constsrc = -1;

	/* Calculate source reg state */
	hs0 = src_to_hw(vp, src0, &insrc, &constsrc);
	hs1 = src_to_hw(vp, src1, &insrc, &constsrc);
	hs2 = src_to_hw(vp, src2, &insrc, &constsrc);

	/* Append it to the instruction */
	hop[1] |= (((hs0 & NV40_VP_SRC0_HIGH_MASK) >> NV40_VP_SRC0_HIGH_SHIFT)
				<< NV40_VP_INST_SRC0H_SHIFT);
	hop[2] |= ((hs0 & NV40_VP_SRC0_LOW_MASK) << NV40_VP_INST_SRC0L_SHIFT) |
			   (hs1 << NV40_VP_INST_SRC1_SHIFT) |
			   (((hs2 & NV40_VP_SRC2_HIGH_MASK) >> NV40_VP_SRC2_HIGH_SHIFT)
				<< NV40_VP_INST_SRC2H_SHIFT);
	hop[3] |= (hs2 & NV40_VP_SRC2_LOW_MASK) << NV40_VP_INST_SRC2L_SHIFT;

	/* bits 127:96 */
	hop[0] |= (dest->condtest << NV40_VP_INST_COND_SHIFT) |
			  (dest->condswz  << NV40_VP_INST_COND_SWZ_ALL_SHIFT);
	if (dest->condtest != NV40_VP_INST_COND_TR)
		hop[0] |= NV40_VP_INST_COND_TEST_ENABLE;
	if (dest->condreg) hop[0] |= NV40_VP_INST_COND_REG_SELECT_1;
	if (dest->condup ) hop[0] |= NV40_VP_INST_COND_UPDATE_ENABLE;

	if (hwdest->file == HW_OUTPUT)
		hop[0] |= NV40_VP_INST0_UNK0;
	else {
		if (hwdest->hw_id == -1)
			hwdest->hw_id = alloc_hw_temp(vp);

		hop[0] = (hwdest->hw_id << NV40_VP_INST_DEST_TEMP_SHIFT);
		if (flags & NOUVEAU_OUT_ABS)
			hop[0] |= NV40_VP_INST_DEST_TEMP_ABS;

		nvsBitSet(vp->hwtemps_written, hwdest->hw_id);
		if (--hwdest->ref == 0)
			free_hw_temp(vp, hwdest->hw_id);
	}

	/* bits 95:64 */
	if (constsrc == -1) constsrc = 0;
	if (insrc    == -1) insrc    = 0;

	constsrc &= 0xFF;
	insrc    &= 0x0F;
	hop[1] |= (op       << NV40_VP_INST_OPCODE_SHIFT) |
			  (constsrc << NV40_VP_INST_CONST_SRC_SHIFT) |
			  (insrc    << NV40_VP_INST_INPUT_SRC_SHIFT);

	/* bits 31:0 */
	if (hwdest->file == HW_OUTPUT) {
		hop[3] |= (dest->mask | (hwdest->hw_id << NV40_VP_INST_DEST_SHIFT));
	} else {
		hop[3] |= (dest->mask | (NV40_VP_INST_DEST_TEMP << NV40_VP_INST_DEST_SHIFT));
	}
	hop[3] |= (0x3F << 7); /*FIXME: what is this?*/

	printf("0x%08x\n", hop[0]);
	printf("0x%08x\n", hop[1]);
	printf("0x%08x\n", hop[2]);
	printf("0x%08x\n", hop[3]);

	instruction_store(vp, hop);
}

static int
t_swizzle(GLuint swz)
{
	int x, y, z, w;
	x = GET_SWZ(swz, 0);
	y = GET_SWZ(swz, 1);
	z = GET_SWZ(swz, 2);
	w = GET_SWZ(swz, 3);

	if ((x<SWIZZLE_ZERO) &&
		(y<SWIZZLE_ZERO) &&
		(z<SWIZZLE_ZERO) &&
		(w<SWIZZLE_ZERO))
		return (x << 6) | (y << 4) | (z << 2) | w;
	return -1;
}

static void
t_src_reg(nouveau_vertex_program *vp, struct prog_src_register *src,
		nouveau_srcreg *ns)
{
	switch (src->File) {
	case PROGRAM_TEMPORARY:
		ns->hw = &vp->temps[src->Index];
		break;
	case PROGRAM_INPUT:
		ns->hw = &vp->inputs[src->Index];
		break;
	default:
		fprintf(stderr, "Unhandled source register file!\n");
		break;
	}

	ns->swizzle = t_swizzle(src->Swizzle);
	if ((src->NegateBase != 0xF && src->NegateBase != 0x0) ||
			ns->swizzle == -1) {
		WARN_ONCE("Unhandled source swizzle/negate, results will be incorrect\n");
		ns->swizzle = 0x1B; // 00 01 10 11 - XYZW
		ns->negate  = (src->NegateBase) ? 1 : 0;
	} else
		ns->negate  = (src->NegateBase) ? 1 : 0;

}

static int
t_dst_mask(int mask)
{
	int hwmask = 0;

	if (mask & WRITEMASK_X) hwmask |= NV40_VP_INST_WRITEMASK_X;
	if (mask & WRITEMASK_Y) hwmask |= NV40_VP_INST_WRITEMASK_Y;
	if (mask & WRITEMASK_Z) hwmask |= NV40_VP_INST_WRITEMASK_Z;
	if (mask & WRITEMASK_W) hwmask |= NV40_VP_INST_WRITEMASK_W;

	return hwmask;
}

static int
t_dst_index(int idx)
{
	int hwidx;

	switch (idx) {
	case VERT_RESULT_HPOS:
		return NV40_VP_INST_DEST_POS;
	case VERT_RESULT_COL0:
		return NV40_VP_INST_DEST_COL0;
	case VERT_RESULT_COL1:
		return NV40_VP_INST_DEST_COL1;
	case VERT_RESULT_FOGC:
		return NV40_VP_INST_DEST_FOGC;
	case VERT_RESULT_TEX0:
	case VERT_RESULT_TEX1:
	case VERT_RESULT_TEX2:
	case VERT_RESULT_TEX3:
	case VERT_RESULT_TEX4:
	case VERT_RESULT_TEX5:
	case VERT_RESULT_TEX6:
	case VERT_RESULT_TEX7:
		return NV40_VP_INST_DEST_TC(idx - VERT_RESULT_TEX0);
	case VERT_RESULT_PSIZ:
		return NV40_VP_INST_DEST_PSZ;
	case VERT_RESULT_BFC0:
		return NV40_VP_INST_DEST_BFC0;
	case VERT_RESULT_BFC1:
		return NV40_VP_INST_DEST_BFC1;
	default:
		fprintf(stderr, "Unknown result reg index!\n");
		return -1;
	}
}

static int
t_cond_test(GLuint test)
{
	switch(test) {
	case COND_GT: return NV40_VP_INST_COND_GT;
	case COND_EQ: return NV40_VP_INST_COND_EQ;
	case COND_LT: return NV40_VP_INST_COND_LT;
	case COND_GE: return NV40_VP_INST_COND_GE;
	case COND_LE: return NV40_VP_INST_COND_LE;
	case COND_NE: return NV40_VP_INST_COND_NE;
	case COND_TR: return NV40_VP_INST_COND_TR;
	case COND_FL: return NV40_VP_INST_COND_FL;
	default:
		WARN_ONCE("unknown CondMask!\n");
		return -1;
	}
}

#define ARITH_1OP(op) do { \
			t_src_reg(vp, &vpi->SrcReg[0], &src0); \
			emit_arith(vp, op, &dest, &src0, NULL, NULL, 0); \
} while(0);
#define ARITH_1OP_SCALAR(op) do { \
			t_src_reg(vp, &vpi->SrcReg[0], &src0); \
			emit_arith(vp, op, &dest, NULL, NULL, &src0, 0); \
} while(0);
#define ARITH_2OP(op) do { \
			t_src_reg(vp, &vpi->SrcReg[0], &src0); \
			t_src_reg(vp, &vpi->SrcReg[1], &src1); \
			emit_arith(vp, op, &dest, &src0, &src1, NULL, 0); \
} while(0);
#define ARITH_3OP(op) do { \
			t_src_reg(vp, &vpi->SrcReg[0], &src0); \
			t_src_reg(vp, &vpi->SrcReg[1], &src1); \
			t_src_reg(vp, &vpi->SrcReg[2], &src2); \
			emit_arith(vp, op, &dest, &src0, &src1, &src2, 0); \
} while(0);

static int
translate(nouveau_vertex_program *vp)
{
	struct vertex_program   *mvp = &vp->mesa_program;
	struct prog_instruction *vpi;

	
	for (vpi=mvp->Base.Instructions; vpi->Opcode!=OPCODE_END; vpi++) {
		nouveau_srcreg src0, src1, src2, sT0;
		nouveau_dstreg dest, dT0;

		switch (vpi->DstReg.File) {
		case PROGRAM_OUTPUT:
			make_dstreg(vp, &dest, HW_OUTPUT, t_dst_index(vpi->DstReg.Index));
			break;
		case PROGRAM_TEMPORARY:
			make_dstreg(vp, &dest, HW_TEMP, vpi->DstReg.Index);
			break;
		default:
			assert(0);
		}
		dest.mask     = t_dst_mask(vpi->DstReg.WriteMask);
		dest.condtest = t_cond_test(vpi->DstReg.CondMask);
		dest.condswz  = t_swizzle(vpi->DstReg.CondSwizzle);
		dest.condreg  = vpi->DstReg.CondSrc;

		switch (vpi->Opcode) {
	/* ARB_vertex_program requirements */
		case OPCODE_ABS:
			t_src_reg(vp, &vpi->SrcReg[0], &src0);
			emit_arith(vp, NV40_VP_INST_OP_MOV, &dest,
					&src0, NULL, NULL,
					NOUVEAU_OUT_ABS
					);
			break;
		case OPCODE_ADD:
			t_src_reg(vp, &vpi->SrcReg[0], &src0);
			t_src_reg(vp, &vpi->SrcReg[1], &src1);
			emit_arith(vp, NV40_VP_INST_OP_ADD,	&dest,
					&src0, NULL, &src1,
					0
					);
			break;
		case OPCODE_ARL:
			break;
		case OPCODE_DP3:
			ARITH_2OP(NV40_VP_INST_OP_DP3);
			break;
		case OPCODE_DP4:
			ARITH_2OP(NV40_VP_INST_OP_DP4);
			break;
		case OPCODE_DPH:
			ARITH_2OP(NV40_VP_INST_OP_DPH);
			break;
		case OPCODE_DST:
			ARITH_2OP(NV40_VP_INST_OP_DST);
			break;
		case OPCODE_EX2:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_EX2);
			break;
		case OPCODE_EXP:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_EXP);
			break;
		case OPCODE_FLR:
			ARITH_1OP(NV40_VP_INST_OP_FLR);
			break;
		case OPCODE_FRC:
			ARITH_1OP(NV40_VP_INST_OP_FRC);
			break;
		case OPCODE_LG2:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_LG2);
			break;
		case OPCODE_LIT:
			t_src_reg(vp, &vpi->SrcReg[0], &src0);
			t_src_reg(vp, &vpi->SrcReg[1], &src1);
			t_src_reg(vp, &vpi->SrcReg[2], &src2);
			emit_arith(vp, NV40_VP_INST_OP_LIT,	&dest,
					&src0, &src1, &src2,
					0
					);
			break;
		case OPCODE_LOG:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_LOG);
			break;
		case OPCODE_MAD:
			ARITH_3OP(NV40_VP_INST_OP_MAD);
			break;
		case OPCODE_MAX:
			ARITH_2OP(NV40_VP_INST_OP_MAX);
			break;
		case OPCODE_MIN:
			ARITH_2OP(NV40_VP_INST_OP_MIN);
			break;
		case OPCODE_MOV:
			ARITH_1OP(NV40_VP_INST_OP_MOV);
			break;
		case OPCODE_MUL:
			ARITH_2OP(NV40_VP_INST_OP_MOV);
			break;
		case OPCODE_POW:
			t_src_reg(vp, &vpi->SrcReg[0], &src0);
			t_src_reg(vp, &vpi->SrcReg[1], &src1);
			make_dstreg(vp, &dT0, HW_TEMP, -1);
			make_srcreg(vp, &sT0, HW_TEMP, dT0.idx);

			dT0.mask = t_dst_mask(WRITEMASK_X);
			emit_arith(vp, NV40_VP_INST_OP_LG2, &dT0,
					NULL, NULL, &src0,
					0);
			sT0.swizzle = 0x0; /* 00000000 - XXXX */
			emit_arith(vp, NV40_VP_INST_OP_MUL, &dT0,
					&sT0, &src1, NULL,
					0);
			emit_arith(vp, NV40_VP_INST_OP_EX2, &dest,
					NULL, NULL, &sT0,
					0);
			break;
		case OPCODE_RCP:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_RCP);
			break;
		case OPCODE_RSQ:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_RSQ);
			break;
		case OPCODE_SGE:
			ARITH_2OP(NV40_VP_INST_OP_SGE);
			break;
		case OPCODE_SLT:
			ARITH_2OP(NV40_VP_INST_OP_SLT);
			break;
		case OPCODE_SUB:
			t_src_reg(vp, &vpi->SrcReg[0], &src0);
			t_src_reg(vp, &vpi->SrcReg[1], &src1);
			src1.negate = !src1.negate;

			emit_arith(vp, NV40_VP_INST_OP_ADD,	&dest,
					&src0, NULL, &src1,
					0
					);
			break;
		case OPCODE_SWZ:
			ARITH_1OP(NV40_VP_INST_OP_MOV);
			break;

		case OPCODE_XPD:
			break;
	/* NV_vertex_program3 requirements */
		case OPCODE_SEQ:
			ARITH_2OP(NV40_VP_INST_OP_SEQ);
			break;
		case OPCODE_SFL:
			ARITH_2OP(NV40_VP_INST_OP_SFL);
			break;
		case OPCODE_SGT:
			ARITH_2OP(NV40_VP_INST_OP_SGT);
			break;
		case OPCODE_SLE:
			ARITH_2OP(NV40_VP_INST_OP_SLE);
			break;
		case OPCODE_SNE:
			ARITH_2OP(NV40_VP_INST_OP_SNE);
			break;
		case OPCODE_STR:
			ARITH_2OP(NV40_VP_INST_OP_STR);
			break;
		case OPCODE_SSG:
			ARITH_1OP(NV40_VP_INST_OP_SSG);
			break;
		case OPCODE_ARL_NV:
			break;
		case OPCODE_ARR:
			break;
		case OPCODE_ARA:
			break;
		case OPCODE_RCC:
			ARITH_1OP_SCALAR(NV40_VP_INST_OP_SSG);
			break;
		case OPCODE_BRA:
			break;
		case OPCODE_CAL:
			break;
		case OPCODE_RET:
			break;
		case OPCODE_PUSHA:
			break;
		case OPCODE_POPA:
			break;
		default:
			break;
		}
	}

	return 0;
}

/* Pre-init vertex program
 *  - Grab reference counts on temps
 *  - Where multiple inputs are used in a single instruction,
 *    emit instructions to move the extras into temps
 */
static int
init(nouveau_vertex_program *vp)
{
	struct vertex_program   *mvp = &vp->mesa_program;
	struct prog_instruction *vpi;
	int i;

	nvsRecInit(&vp->temps_in_use, 64);
	nvsRecInit(&vp->hwtemps_written, 64);
	nvsRecInit(&vp->hwtemps_in_use , 64);

	for (vpi=mvp->Base.Instructions; vpi->Opcode!=OPCODE_END; vpi++) {
		int in_done = 0;
		int in_idx;

		for (i=0;i<3;i++) {
			struct prog_src_register *src = &vpi->SrcReg[i];
			/*FIXME: does not handle relative addressing!*/
			int	idx = src->Index;

			switch (src->File) {
			case PROGRAM_TEMPORARY:
				vp->temps[idx].file  = HW_TEMP;
				vp->temps[idx].hw_id = -1;
				vp->temps[idx].ref++;
				nvsBitSet(vp->temps_in_use, idx);
				break;
			case PROGRAM_INPUT:
				if (vp->inputs[idx].file == HW_TEMP) {
					vp->inputs[idx].ref++;
					break;
				}

				if (!in_done || (in_idx == idx)) {
					vp->inputs[idx].file  = HW_INPUT;
					vp->inputs[idx].hw_id = idx;
					vp->inputs[idx].ref++;
					in_done = 1;
					in_idx  = idx;
				} else {
					vp->inputs[idx].file  = HW_TEMP;
					vp->inputs[idx].ref++;
				}
				break;
			default:
				break;
			}
		}

		switch (vpi->DstReg.File) {
		case PROGRAM_TEMPORARY:
			vp->temps[vpi->DstReg.Index].file  = HW_TEMP;
			vp->temps[vpi->DstReg.Index].hw_id = -1;
			vp->temps[vpi->DstReg.Index].ref++;
			nvsBitSet(vp->temps_in_use, vpi->DstReg.Index);
			break;
		default:
			break;
		}
	}

	/* Now we can move any inputs that need it into temps */
	for (i=0; i<14; i++) {
		if (vp->inputs[i].file == HW_TEMP) {
			nouveau_srcreg src;
			nouveau_dstreg dest;

			make_dstreg(vp, &dest, HW_TEMP , -1);
			make_srcreg(vp, &src , HW_INPUT,  i); 

			emit_arith(vp, NV40_VP_INST_OP_MOV, &dest,
					&src, NULL, NULL,
					0
					);

			vp->inputs[i].file  = HW_TEMP;
			vp->inputs[i].hw_id = dest.hw->hw_id;
		}
	}

	return 0;
}

int
nv40TranslateVertexProgram(nouveau_vertex_program *vp)
{
	int ret;

	ret = init(vp);
	if (ret)
		return ret;

	ret = translate(vp);
	if (ret)
		return ret;

	return 0;
}

int
main(int argc, char **argv)
{
	nouveau_vertex_program *vp  = calloc(1, sizeof(nouveau_vertex_program));
	struct vertex_program  *mvp = &vp->mesa_program;
	struct prog_instruction inst[3];

	/*
        "ADD t0, vertex.color, vertex.position;\n"
        "ADD result.position, t0, vertex.position;\n"
	*/

	inst[0].Opcode = OPCODE_ADD;
	inst[0].SrcReg[0].File       = PROGRAM_INPUT;
	inst[0].SrcReg[0].Index      = VERT_ATTRIB_COLOR0;
	inst[0].SrcReg[0].NegateBase = 0;
	inst[0].SrcReg[0].Swizzle    = MAKE_SWIZZLE4(0, 1, 2, 3);
	inst[0].SrcReg[1].File       = PROGRAM_INPUT;
	inst[0].SrcReg[1].Index      = VERT_ATTRIB_POS;
	inst[0].SrcReg[1].NegateBase = 0;
	inst[0].SrcReg[1].Swizzle    = MAKE_SWIZZLE4(0, 1, 2, 3);
	inst[0].SrcReg[2].File       = PROGRAM_UNDEFINED;
	inst[0].DstReg.File      = PROGRAM_TEMPORARY;
	inst[0].DstReg.Index     = 0;
	inst[0].DstReg.WriteMask = WRITEMASK_XYZW;

	inst[1].Opcode = OPCODE_ADD;
	inst[1].SrcReg[0].File       = PROGRAM_TEMPORARY;
	inst[1].SrcReg[0].Index      = 0;
	inst[1].SrcReg[0].NegateBase = 0;
	inst[1].SrcReg[0].Swizzle    = MAKE_SWIZZLE4(0, 1, 2, 3);
	inst[1].SrcReg[1].File       = PROGRAM_INPUT;
	inst[1].SrcReg[1].Index      = VERT_ATTRIB_POS;
	inst[1].SrcReg[1].NegateBase = 0;
	inst[1].SrcReg[1].Swizzle    = MAKE_SWIZZLE4(0, 1, 2, 3);
	inst[0].SrcReg[2].File       = PROGRAM_UNDEFINED;
	inst[1].DstReg.File      = PROGRAM_OUTPUT;
	inst[1].DstReg.Index     = VERT_RESULT_HPOS;
	inst[1].DstReg.WriteMask = WRITEMASK_XYZW;

	inst[2].Opcode = OPCODE_END;

	mvp->Base.Instructions = inst;

	nv40TranslateVertexProgram(vp);
}