summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nv50/nv50_program.c
blob: 97e3eab906fb5d40dfa5fcf88abebd8a14b79f3a (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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_inlines.h"

#include "pipe/p_shader_tokens.h"
#include "tgsi/util/tgsi_parse.h"
#include "tgsi/util/tgsi_util.h"

#include "nv50_context.h"
#include "nv50_state.h"

#define OP_MOV 0x001
#define OP_INTERP 0x008
#define OP_RCP 0x009
#define OP_ADD 0x00b
#define OP_MUL 0x00c
#define OP_MAD 0x00e
#define NV50_SU_MAX_TEMP 64

struct nv50_reg {
	enum {
		P_TEMP,
		P_ATTR,
		P_RESULT,
		P_CONST,
		P_IMMD
	} type;
	int index;

	int hw;
	int neg;
};

struct nv50_pc {
	struct nv50_program *p;

	/* hw resources */
	struct nv50_reg *r_temp[NV50_SU_MAX_TEMP];

	/* tgsi resources */
	struct nv50_reg *temp;
	int temp_nr;
	struct nv50_reg *attr;
	int attr_nr;
	struct nv50_reg *result;
	int result_nr;
	struct nv50_reg *param;
	int param_nr;
	struct nv50_reg *immd;
	float *immd_buf;
	int immd_nr;
};

static void
alloc_reg(struct nv50_pc *pc, struct nv50_reg *reg)
{
	int i;

	if (reg->type != P_TEMP || reg->hw >= 0)
		return;

	for (i = 0; i < NV50_SU_MAX_TEMP; i++) {
		if (!(pc->r_temp[i])) {
			pc->r_temp[i] = reg;
			reg->hw = i;
			if (pc->p->cfg.vp.high_temp < (i + 1))
				pc->p->cfg.vp.high_temp = i + 1;
			return;
		}
	}

	assert(0);
}

static struct nv50_reg *
alloc_temp(struct nv50_pc *pc, struct nv50_reg *dst)
{
	struct nv50_reg *r;
	int i;

	if (dst && dst->type == P_TEMP && dst->hw == -1)
		return dst;

	for (i = 0; i < NV50_SU_MAX_TEMP; i++) {
		if (!pc->r_temp[i]) {
			r = CALLOC_STRUCT(nv50_reg);
			r->type = P_TEMP;
			r->index = -1;
			r->hw = i;
			pc->r_temp[i] = r;
			return r;
		}
	}

	assert(0);
	return NULL;
}

static void
free_temp(struct nv50_pc *pc, struct nv50_reg *r)
{
	if (r->index == -1) {
		FREE(pc->r_temp[r->hw]);
		pc->r_temp[r->hw] = NULL;
	}
}

#if 0
static struct nv50_reg *
constant(struct nv50_pc *pc, int pipe, int c, float v)
{
	struct nv50_reg *r = CALLOC_STRUCT(nv50_reg);
	struct nv50_program *p = pc->p;
	struct nv50_program_data *pd;
	int idx;

	if (pipe >= 0) {
		for (idx = 0; idx < p->nr_consts; idx++) {
			if (p->consts[idx].index == pipe)
				return nv40_sr(NV40SR_CONST, idx);
		}
	}

	idx = p->nr_consts++;
	p->consts = realloc(p->consts, sizeof(*pd) * p->nr_consts);
	pd = &p->consts[idx];

	pd->index = pipe;
	pd->component = c;
	pd->value = v;
	return nv40_sr(NV40SR_CONST, idx);
}
#endif

static void
emit(struct nv50_pc *pc, unsigned op, struct nv50_reg *dst,
     struct nv50_reg *src0, struct nv50_reg *src1, struct nv50_reg *src2)
{
	struct nv50_program *p = pc->p;
	struct nv50_reg *tmp0 = NULL, *tmp = NULL, *tmp2 = NULL;
	unsigned inst[2] = { 0, 0 };

	/* Grr.. Fun restrictions on where attribs can be sourced from.. */
	if (src0 && (src0->type == P_CONST || src0->type == P_IMMD) &&
	    (op == OP_MUL || op == OP_MAD)) {
		tmp = src1;
		src1 = src0;
		src0 = tmp;
		tmp = NULL;
	}

	if (src1 && src1->type == P_ATTR) {
		tmp = alloc_temp(pc, dst);
		tmp->neg = src1->neg; src1->neg = 0;
		emit(pc, 1, tmp, src1, NULL, NULL);
		src1 = tmp;
	}

	if (src2 && src2->type == P_ATTR) {
		tmp2 = alloc_temp(pc, dst);
		tmp2->neg = src2->neg; src2->neg = 0;
		emit(pc, 1, tmp2, src2, NULL, NULL);
		src2 = tmp2;
	}

	/* Get this out of the way first.  What type of opcode do we
	 * want/need to build?
	 */
	if ((op & 0x3f0) || dst->type == P_RESULT ||
	    (src0 && src0->type == P_ATTR) || src1 || src2)
		inst[0] |= 0x00000001;

	if (inst[0] & 0x00000001) {
		inst[0] |= ((op & 0xf) << 28);
		inst[1] |= ((op >> 4) << 26);

		alloc_reg(pc, dst);
		if (dst->type == P_RESULT)
			inst[1] |= 0x00000008;
		inst[0] |= (dst->hw << 2);

		if (src0) {
			if (src0->type == P_ATTR)
				inst[1] |= 0x00200000;
			else
			if (src0->type == P_CONST || src0->type == P_IMMD)
				assert(0);
			alloc_reg(pc, src0);
			inst[0] |= (src0->hw << 9);
		}

		if (src1) {
			if (src1->type == P_CONST || src1->type == P_IMMD) {
				if (src1->type == P_IMMD)
					inst[1] |= (NV50_CB_PMISC << 22);
				else
					inst[1] |= (NV50_CB_PVP << 22);
				inst[0] |= 0x00800000; /* src1 is const */
				/*XXX: does src1 come from "src2" now? */
				alloc_reg(pc, src1);
				inst[0] |= (src1->hw << 16);
			} else {
				alloc_reg(pc, src1);
				if (op == OP_MUL || op == OP_MAD)
					inst[0] |= (src1->hw << 16);
				else
					inst[1] |= (src1->hw << 14);
			}
		} else {
			inst[1] |= 0x0003c000; /*XXX FIXME */
		}

		if (src2) {
			if (src2->type == P_CONST || src2->type == P_IMMD) {
				if (src2->type == P_IMMD)
					inst[1] |= (NV50_CB_PMISC << 22);
				else
					inst[1] |= (NV50_CB_PVP << 22);
				inst[0] |= 0x01000000; /* src2 is const */
				inst[1] |= (src2->hw << 14);
			} else {
				alloc_reg(pc, src2);
				if (inst[0] & 0x00800000 || op == OP_MAD)
					inst[1] |= (src2->hw << 14);
				else
					inst[0] |= (src2->hw << 16);
			}
		}

		/*XXX: FIXME */
		switch (op) {
		case OP_ADD:
		case OP_MUL:
		case OP_RCP:
		case OP_MAD:
			/* 0x04000000 negates arg0 */
			/* 0x08000000 negates arg1 */
			/*XXX: true for !0xb also ? */
			if (src0 && src0->neg)
				inst[1] |= 0x04000000;
			if (src1 && src1->neg)
				inst[1] |= 0x08000000;
			inst[1] |= 0x00000780;
			break;
		default:
			/* 0x04000000 == arg0 32 bit, otherwise 16 bit */
			inst[1] |= 0x04000780;
			break;
		}
	} else {
		inst[0] |= ((op & 0xf) << 28);

		alloc_reg(pc, dst);
		inst[0] |= (dst->hw << 2);

		if (src0) {
			alloc_reg(pc, src0);
			inst[0] |= (src0->hw << 9);
		}

		/*XXX: NFI if this even works - probably not.. */
		if (src1) {
			alloc_reg(pc, src1);
			inst[0] |= (src1->hw << 16);
		}
	}

	if (tmp0) free_temp(pc, tmp0);
	if (tmp) free_temp(pc, tmp);
	if (tmp2) free_temp(pc, tmp2);

	if (inst[0] & 1) {
		p->insns_nr += 2;
		p->insns = realloc(p->insns, sizeof(unsigned) * p->insns_nr);
		memcpy(p->insns + (p->insns_nr - 2), inst, sizeof(unsigned)*2);
	} else {
		p->insns_nr += 1;
		p->insns = realloc(p->insns, sizeof(unsigned) * p->insns_nr);
		memcpy(p->insns + (p->insns_nr - 1), inst, sizeof(unsigned));
	}
}

static struct nv50_reg *
tgsi_dst(struct nv50_pc *pc, int c, const struct tgsi_full_dst_register *dst)
{
	switch (dst->DstRegister.File) {
	case TGSI_FILE_TEMPORARY:
		return &pc->temp[dst->DstRegister.Index * 4 + c];
	case TGSI_FILE_OUTPUT:
		return &pc->result[dst->DstRegister.Index * 4 + c];
	case TGSI_FILE_NULL:
		return NULL;
	default:
		break;
	}

	return NULL;
}

static struct nv50_reg *
tgsi_src(struct nv50_pc *pc, int c, const struct tgsi_full_src_register *src)
{
	/* Handle swizzling */
	switch (c) {
	case 0: c = src->SrcRegister.SwizzleX; break;
	case 1: c = src->SrcRegister.SwizzleY; break;
	case 2: c = src->SrcRegister.SwizzleZ; break;
	case 3: c = src->SrcRegister.SwizzleW; break;
	default:
		assert(0);
	}

	switch (src->SrcRegister.File) {
	case TGSI_FILE_INPUT:
		return &pc->attr[src->SrcRegister.Index * 4 + c];
	case TGSI_FILE_TEMPORARY:
		return &pc->temp[src->SrcRegister.Index * 4 + c];
	case TGSI_FILE_CONSTANT:
		return &pc->param[src->SrcRegister.Index * 4 + c];
	case TGSI_FILE_IMMEDIATE:
		return &pc->immd[src->SrcRegister.Index * 4 + c];
	default:
		break;
	}

	return NULL;
}

static boolean
nv50_program_tx_insn(struct nv50_pc *pc, const union tgsi_full_token *tok)
{
	const struct tgsi_full_instruction *inst = &tok->FullInstruction;
	struct nv50_reg *dst[4], *src[3][4], *none = NULL, *tmp;
	unsigned mask;
	int i, c;

	NOUVEAU_ERR("insn %p\n", tok);

	mask = inst->FullDstRegisters[0].DstRegister.WriteMask;

	for (c = 0; c < 4; c++) {
		if (mask & (1 << c))
			dst[c] = tgsi_dst(pc, c, &inst->FullDstRegisters[0]);
		else
			dst[c] = NULL;
	}

	for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
		for (c = 0; c < 4; c++)
			src[i][c] = tgsi_src(pc, c, &inst->FullSrcRegisters[i]);
	}

	switch (inst->Instruction.Opcode) {
	case TGSI_OPCODE_ADD:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c)) {
				emit(pc, OP_ADD, dst[c],
				     src[0][c], src[1][c], none);
			}
		}
		break;
	case TGSI_OPCODE_DP3:
		tmp = alloc_temp(pc, NULL);
		emit(pc, OP_MUL, tmp, src[0][0], src[1][0], NULL);
		emit(pc, OP_MAD, tmp, src[0][1], src[1][1], tmp);
		emit(pc, OP_MAD, tmp, src[0][2], src[1][2], tmp);
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MOV, dst[c], tmp, none, none);
		}
		free_temp(pc, tmp);
		break;
	case TGSI_OPCODE_DP4:
		tmp = alloc_temp(pc, NULL);
		emit(pc, OP_MUL, tmp, src[0][0], src[1][0], NULL);
		emit(pc, OP_MAD, tmp, src[0][1], src[1][1], tmp);
		emit(pc, OP_MAD, tmp, src[0][2], src[1][2], tmp);
		emit(pc, OP_MAD, tmp, src[0][3], src[1][3], tmp);
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MOV, dst[c], tmp, none, none);
		}
		free_temp(pc, tmp);
		break;
	case TGSI_OPCODE_DPH:
		tmp = alloc_temp(pc, NULL);
		emit(pc, OP_MUL, tmp, src[0][0], src[1][0], NULL);
		emit(pc, OP_MAD, tmp, src[0][1], src[1][1], tmp);
		emit(pc, OP_MAD, tmp, src[0][2], src[1][2], tmp);
		emit(pc, OP_ADD, tmp, src[1][3], tmp, NULL);
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MOV, dst[c], tmp, none, none);
		}
		free_temp(pc, tmp);
		break;
	case TGSI_OPCODE_MAD:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MAD, dst[c],
				     src[0][c], src[1][c], src[2][c]);
		}
		break;
	case TGSI_OPCODE_MOV:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MOV, dst[c], src[0][c], none, none);
		}
		break;
	case TGSI_OPCODE_MUL:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_MUL, dst[c],
				     src[0][c], src[1][c], none);
		}
		break;
	case TGSI_OPCODE_RCP:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c))
				emit(pc, OP_RCP, dst[c],
				     src[0][c], none, none);
		}
		break;
	case TGSI_OPCODE_SUB:
		for (c = 0; c < 4; c++) {
			if (mask & (1 << c)) {
				src[1][c]->neg = 1;
				emit(pc, OP_ADD, dst[c],
				     src[0][c], src[1][c], none);
				src[1][c]->neg = 0;
			}
		}
		break;
	case TGSI_OPCODE_END:
		break;
	default:
		NOUVEAU_ERR("invalid opcode %d\n", inst->Instruction.Opcode);
		return FALSE;
	}

	return TRUE;
}

static boolean
nv50_program_tx_prep(struct nv50_pc *pc)
{
	struct tgsi_parse_context p;
	boolean ret = FALSE;
	unsigned i, c;

	tgsi_parse_init(&p, pc->p->pipe.tokens);
	while (!tgsi_parse_end_of_tokens(&p)) {
		const union tgsi_full_token *tok = &p.FullToken;

		tgsi_parse_token(&p);
		switch (tok->Token.Type) {
		case TGSI_TOKEN_TYPE_IMMEDIATE:
		{
			const struct tgsi_full_immediate *imm =
				&p.FullToken.FullImmediate;

			pc->immd_nr++;
			pc->immd_buf = realloc(pc->immd_buf, 4 * pc->immd_nr *
							     sizeof(float));
			pc->immd_buf[4 * (pc->immd_nr - 1) + 0] =
				imm->u.ImmediateFloat32[0].Float;
			pc->immd_buf[4 * (pc->immd_nr - 1) + 1] =
				imm->u.ImmediateFloat32[1].Float;
			pc->immd_buf[4 * (pc->immd_nr - 1) + 2] =
				imm->u.ImmediateFloat32[2].Float;
			pc->immd_buf[4 * (pc->immd_nr - 1) + 3] =
				imm->u.ImmediateFloat32[3].Float;
		}
			break;
		case TGSI_TOKEN_TYPE_DECLARATION:
		{
			const struct tgsi_full_declaration *d;
			unsigned last;

			d = &p.FullToken.FullDeclaration;
			last = d->u.DeclarationRange.Last;

			switch (d->Declaration.File) {
			case TGSI_FILE_TEMPORARY:
				if (pc->temp_nr < (last + 1))
					pc->temp_nr = last + 1;
				break;
			case TGSI_FILE_OUTPUT:
				if (pc->result_nr < (last + 1))
					pc->result_nr = last + 1;
				break;
			case TGSI_FILE_INPUT:
				if (pc->attr_nr < (last + 1))
					pc->attr_nr = last + 1;
				break;
			case TGSI_FILE_CONSTANT:
				if (pc->param_nr < (last + 1))
					pc->param_nr = last + 1;
				break;
			default:
				NOUVEAU_ERR("bad decl file %d\n",
					    d->Declaration.File);
				goto out_err;
			}
		}
			break;
		case TGSI_TOKEN_TYPE_INSTRUCTION:
			break;
		default:
			break;
		}
	}

	NOUVEAU_ERR("%d temps\n", pc->temp_nr);
	if (pc->temp_nr) {
		pc->temp = calloc(pc->temp_nr * 4, sizeof(struct nv50_reg));
		if (!pc->temp)
			goto out_err;

		for (i = 0; i < pc->temp_nr; i++) {
			for (c = 0; c < 4; c++) {
				pc->temp[i*4+c].type = P_TEMP;
				pc->temp[i*4+c].hw = -1;
				pc->temp[i*4+c].index = i;
			}
		}
	}

	NOUVEAU_ERR("%d attrib regs\n", pc->attr_nr);
	if (pc->attr_nr) {
		int aid = 0;

		pc->attr = calloc(pc->attr_nr * 4, sizeof(struct nv50_reg));
		if (!pc->attr)
			goto out_err;

		for (i = 0; i < pc->attr_nr; i++) {
			for (c = 0; c < 4; c++) {
				pc->p->cfg.vp.attr[aid/32] |= (1 << (aid % 32));
				pc->attr[i*4+c].type = P_ATTR;
				pc->attr[i*4+c].hw = aid++;
				pc->attr[i*4+c].index = i;
			}
		}
	}

	NOUVEAU_ERR("%d result regs\n", pc->result_nr);
	if (pc->result_nr) {
		int rid = 0;

		pc->result = calloc(pc->result_nr * 4, sizeof(struct nv50_reg));
		if (!pc->result)
			goto out_err;

		for (i = 0; i < pc->result_nr; i++) {
			for (c = 0; c < 4; c++) {
				pc->result[i*4+c].type = P_RESULT;
				pc->result[i*4+c].hw = rid++;
				pc->result[i*4+c].index = i;
			}
		}
	}

	NOUVEAU_ERR("%d param regs\n", pc->param_nr);
	if (pc->param_nr) {
		int rid = 0;

		pc->param = calloc(pc->param_nr * 4, sizeof(struct nv50_reg));
		if (!pc->param)
			goto out_err;

		for (i = 0; i < pc->param_nr; i++) {
			for (c = 0; c < 4; c++) {
				pc->param[i*4+c].type = P_CONST;
				pc->param[i*4+c].hw = rid++;
				pc->param[i*4+c].index = i;
			}
		}
	}

	if (pc->immd_nr) {
		int rid = 0;

		pc->immd = calloc(pc->immd_nr * 4, sizeof(struct nv50_reg));
		if (!pc->immd)
			goto out_err;

		for (i = 0; i < pc->immd_nr; i++) {
			for (c = 0; c < 4; c++) {
				pc->immd[i*4+c].type = P_IMMD;
				pc->immd[i*4+c].hw = rid++;
				pc->immd[i*4+c].index = i;
			}
		}
	}

	ret = TRUE;
out_err:
	tgsi_parse_free(&p);
	return ret;
}

static boolean
nv50_program_tx(struct nv50_program *p)
{
	struct tgsi_parse_context parse;
	struct nv50_pc *pc;
	boolean ret;

	pc = CALLOC_STRUCT(nv50_pc);
	if (!pc)
		return FALSE;
	pc->p = p;
	pc->p->cfg.vp.high_temp = 4;

	ret = nv50_program_tx_prep(pc);
	if (ret == FALSE)
		goto out_cleanup;

	tgsi_parse_init(&parse, pc->p->pipe.tokens);
	while (!tgsi_parse_end_of_tokens(&parse)) {
		const union tgsi_full_token *tok = &parse.FullToken;

		tgsi_parse_token(&parse);

		switch (tok->Token.Type) {
		case TGSI_TOKEN_TYPE_INSTRUCTION:
			ret = nv50_program_tx_insn(pc, tok);
			if (ret == FALSE)
				goto out_err;
			break;
		default:
			break;
		}
	}

	p->immd_nr = pc->immd_nr * 4;
	p->immd = pc->immd_buf;

out_err:
	tgsi_parse_free(&parse);

out_cleanup:
	return ret;
}

static void
nv50_program_validate(struct nv50_context *nv50, struct nv50_program *p)
{
	struct tgsi_parse_context pc;

	tgsi_parse_init(&pc, p->pipe.tokens);

	if (pc.FullHeader.Processor.Processor == TGSI_PROCESSOR_FRAGMENT) {
		p->insns_nr = 8;
		p->insns = malloc(p->insns_nr * sizeof(unsigned));
		p->insns[0] = 0x80000000;
		p->insns[1] = 0x9000000c;
		p->insns[2] = 0x82010600;
		p->insns[3] = 0x82020604;
		p->insns[4] = 0x80030609;
		p->insns[5] = 0x00020780;
		p->insns[6] = 0x8004060d;
		p->insns[7] = 0x00020781;
	} else
	if (pc.FullHeader.Processor.Processor == TGSI_PROCESSOR_VERTEX) {
		int i;

		if (nv50_program_tx(p) == FALSE)
			assert(0);
		p->insns[p->insns_nr - 1] |= 0x00000001;

		for (i = 0; i < p->insns_nr; i++)
			NOUVEAU_ERR("%d 0x%08x\n", i, p->insns[i]);
	} else {
		NOUVEAU_ERR("invalid TGSI processor\n");
		tgsi_parse_free(&pc);
		return;
	}

	tgsi_parse_free(&pc);

	p->translated = TRUE;
}

static void
nv50_program_validate_data(struct nv50_context *nv50, struct nv50_program *p)
{
	int i;

	for (i = 0; i < p->immd_nr; i++) {
		BEGIN_RING(tesla, 0x0f00, 2);
		OUT_RING  ((NV50_CB_PMISC << 16) | (i << 8));
		OUT_RING  (fui(p->immd[i]));
	}
}

static void
nv50_program_validate_code(struct nv50_context *nv50, struct nv50_program *p)
{
	struct pipe_winsys *ws = nv50->pipe.winsys;
	void *map;

	if (!p->buffer)
		p->buffer = ws->buffer_create(ws, 0x100, 0, p->insns_nr * 4);
	map = ws->buffer_map(ws, p->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
	memcpy(map, p->insns, p->insns_nr * 4);
	ws->buffer_unmap(ws, p->buffer);
}

void
nv50_vertprog_validate(struct nv50_context *nv50)
{
	struct nouveau_grobj *tesla = nv50->screen->tesla;
	struct nv50_program *p = nv50->vertprog;
	struct nouveau_stateobj *so;

	if (!p->translated) {
		nv50_program_validate(nv50, p);
		if (!p->translated)
			assert(0);
	}

	nv50_program_validate_data(nv50, p);
	nv50_program_validate_code(nv50, p);

	so = so_new(11, 2);
	so_method(so, tesla, NV50TCL_VP_ADDRESS_HIGH, 2);
	so_reloc (so, p->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
		  NOUVEAU_BO_HIGH, 0, 0);
	so_reloc (so, p->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
		  NOUVEAU_BO_LOW, 0, 0);
	so_method(so, tesla, 0x1650, 2);
	so_data  (so, p->cfg.vp.attr[0]);
	so_data  (so, p->cfg.vp.attr[1]);
	so_method(so, tesla, 0x16ac, 2);
	so_data  (so, 8);
	so_data  (so, p->cfg.vp.high_temp);
	so_method(so, tesla, 0x140c, 1);
	so_data  (so, 0); /* program start offset */
	so_emit(nv50->screen->nvws, so);
	so_ref(NULL, &so);
}

void
nv50_fragprog_validate(struct nv50_context *nv50)
{
	struct pipe_winsys *ws = nv50->pipe.winsys;
	struct nouveau_grobj *tesla = nv50->screen->tesla;
	struct nv50_program *p = nv50->fragprog;
	struct nouveau_stateobj *so;
	void *map;

	if (!p->translated) {
		nv50_program_validate(nv50, p);
		if (!p->translated)
			assert(0);
	}

	if (!p->buffer)
		p->buffer = ws->buffer_create(ws, 0x100, 0, p->insns_nr * 4);
	map = ws->buffer_map(ws, p->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
	memcpy(map, p->insns, p->insns_nr * 4);
	ws->buffer_unmap(ws, p->buffer);

	so = so_new(3, 2);
	so_method(so, tesla, NV50TCL_FP_ADDRESS_HIGH, 2);
	so_reloc (so, p->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
		  NOUVEAU_BO_HIGH, 0, 0);
	so_reloc (so, p->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
		  NOUVEAU_BO_LOW, 0, 0);
	so_emit(nv50->screen->nvws, so);
	so_ref(NULL, &so);
}

void
nv50_program_destroy(struct nv50_context *nv50, struct nv50_program *p)
{
	struct pipe_winsys *ws = nv50->pipe.winsys;

	if (p->insns_nr) {
		if (p->insns)
			FREE(p->insns);
		p->insns_nr = 0;
	}

	if (p->buffer)
		pipe_buffer_reference(ws, &p->buffer, NULL);

	p->translated = 0;
}