aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/r300/compiler/r300_fragprog_emit.c
blob: 841d46f720388031c26faac97d26324d58707bb5 (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
/*
 * Copyright (C) 2005 Ben Skeggs.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * \file
 *
 * Emit the r300_fragment_program_code that can be understood by the hardware.
 * Input is a pre-transformed radeon_program.
 *
 * \author Ben Skeggs <darktama@iinet.net.au>
 *
 * \author Jerome Glisse <j.glisse@gmail.com>
 */

#include "r300_fragprog.h"

#include "../r300_reg.h"

#include "radeon_program_pair.h"
#include "r300_fragprog_swizzle.h"


struct r300_emit_state {
	struct r300_fragment_program_compiler * compiler;

	unsigned current_node : 2;
	unsigned node_first_tex : 8;
	unsigned node_first_alu : 8;
	uint32_t node_flags;
};

#define PROG_CODE \
	struct r300_fragment_program_compiler *c = emit->compiler; \
	struct r300_fragment_program_code *code = &c->code->code.r300

#define error(fmt, args...) do {			\
		rc_error(&c->Base, "%s::%s(): " fmt "\n",	\
			__FILE__, __FUNCTION__, ##args);	\
	} while(0)

static unsigned int get_msbs_alu(unsigned int bits)
{
	return (bits >> 6) & 0x7;
}

/**
 * @param lsbs The number of least significant bits
 */
static unsigned int get_msbs_tex(unsigned int bits, unsigned int lsbs)
{
	return (bits >> lsbs) & 0x15;
}

#define R400_EXT_GET_MSBS(x, lsbs, mask) (((x) >> lsbs) & mask)

/**
 * Mark a temporary register as used.
 */
static void use_temporary(struct r300_fragment_program_code *code, unsigned int index)
{
	if (index > code->pixsize)
		code->pixsize = index;
}

static unsigned int use_source(struct r300_fragment_program_code* code, struct rc_pair_instruction_source src)
{
	if (!src.Used)
		return 0;

	if (src.File == RC_FILE_CONSTANT) {
		return src.Index | (1 << 5);
	} else if (src.File == RC_FILE_TEMPORARY || src.File == RC_FILE_INPUT) {
		use_temporary(code, src.Index);
		return src.Index & 0x1f;
	}

	return 0;
}


static unsigned int translate_rgb_opcode(struct r300_fragment_program_compiler * c, rc_opcode opcode)
{
	switch(opcode) {
	case RC_OPCODE_CMP: return R300_ALU_OUTC_CMP;
	case RC_OPCODE_CND: return R300_ALU_OUTC_CND;
	case RC_OPCODE_DP3: return R300_ALU_OUTC_DP3;
	case RC_OPCODE_DP4: return R300_ALU_OUTC_DP4;
	case RC_OPCODE_FRC: return R300_ALU_OUTC_FRC;
	default:
		error("translate_rgb_opcode: Unknown opcode %s", rc_get_opcode_info(opcode)->Name);
		/* fall through */
	case RC_OPCODE_NOP:
		/* fall through */
	case RC_OPCODE_MAD: return R300_ALU_OUTC_MAD;
	case RC_OPCODE_MAX: return R300_ALU_OUTC_MAX;
	case RC_OPCODE_MIN: return R300_ALU_OUTC_MIN;
	case RC_OPCODE_REPL_ALPHA: return R300_ALU_OUTC_REPL_ALPHA;
	}
}

static unsigned int translate_alpha_opcode(struct r300_fragment_program_compiler * c, rc_opcode opcode)
{
	switch(opcode) {
	case RC_OPCODE_CMP: return R300_ALU_OUTA_CMP;
	case RC_OPCODE_CND: return R300_ALU_OUTA_CND;
	case RC_OPCODE_DP3: return R300_ALU_OUTA_DP4;
	case RC_OPCODE_DP4: return R300_ALU_OUTA_DP4;
	case RC_OPCODE_EX2: return R300_ALU_OUTA_EX2;
	case RC_OPCODE_FRC: return R300_ALU_OUTA_FRC;
	case RC_OPCODE_LG2: return R300_ALU_OUTA_LG2;
	default:
		error("translate_rgb_opcode: Unknown opcode %s", rc_get_opcode_info(opcode)->Name);
		/* fall through */
	case RC_OPCODE_NOP:
		/* fall through */
	case RC_OPCODE_MAD: return R300_ALU_OUTA_MAD;
	case RC_OPCODE_MAX: return R300_ALU_OUTA_MAX;
	case RC_OPCODE_MIN: return R300_ALU_OUTA_MIN;
	case RC_OPCODE_RCP: return R300_ALU_OUTA_RCP;
	case RC_OPCODE_RSQ: return R300_ALU_OUTA_RSQ;
	}
}

/**
 * Emit one paired ALU instruction.
 */
static int emit_alu(struct r300_emit_state * emit, struct rc_pair_instruction* inst)
{
	int ip;
	int j;
	PROG_CODE;

	if (code->alu.length >= c->Base.max_alu_insts) {
		error("Too many ALU instructions");
		return 0;
	}

	ip = code->alu.length++;

	code->alu.inst[ip].rgb_inst = translate_rgb_opcode(c, inst->RGB.Opcode);
	code->alu.inst[ip].alpha_inst = translate_alpha_opcode(c, inst->Alpha.Opcode);

	for(j = 0; j < 3; ++j) {
		/* Set the RGB address */
		unsigned int src = use_source(code, inst->RGB.Src[j]);
		unsigned int arg;
		if (inst->RGB.Src[j].Index >= R300_PFS_NUM_TEMP_REGS)
			code->alu.inst[ip].r400_ext_addr |= R400_ADDR_EXT_RGB_MSB_BIT(j);

		code->alu.inst[ip].rgb_addr |= src << (6*j);

		/* Set the Alpha address */
		src = use_source(code, inst->Alpha.Src[j]);
		if (inst->Alpha.Src[j].Index >= R300_PFS_NUM_TEMP_REGS)
			code->alu.inst[ip].r400_ext_addr |= R400_ADDR_EXT_A_MSB_BIT(j);

		code->alu.inst[ip].alpha_addr |= src << (6*j);

		arg = r300FPTranslateRGBSwizzle(inst->RGB.Arg[j].Source, inst->RGB.Arg[j].Swizzle);
		arg |= inst->RGB.Arg[j].Abs << 6;
		arg |= inst->RGB.Arg[j].Negate << 5;
		code->alu.inst[ip].rgb_inst |= arg << (7*j);

		arg = r300FPTranslateAlphaSwizzle(inst->Alpha.Arg[j].Source, inst->Alpha.Arg[j].Swizzle);
		arg |= inst->Alpha.Arg[j].Abs << 6;
		arg |= inst->Alpha.Arg[j].Negate << 5;
		code->alu.inst[ip].alpha_inst |= arg << (7*j);
	}

	/* Presubtract */
	if (inst->RGB.Src[RC_PAIR_PRESUB_SRC].Used) {
		switch(inst->RGB.Src[RC_PAIR_PRESUB_SRC].Index) {
		case RC_PRESUB_BIAS:
			code->alu.inst[ip].rgb_inst |=
						R300_ALU_SRCP_1_MINUS_2_SRC0;
			break;
		case RC_PRESUB_ADD:
			code->alu.inst[ip].rgb_inst |=
						R300_ALU_SRCP_SRC1_PLUS_SRC0;
			break;
		case RC_PRESUB_SUB:
			code->alu.inst[ip].rgb_inst |=
						R300_ALU_SRCP_SRC1_MINUS_SRC0;
			break;
		case RC_PRESUB_INV:
			code->alu.inst[ip].rgb_inst |=
						R300_ALU_SRCP_1_MINUS_SRC0;
			break;
		default:
			break;
		}
	}

	if (inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Used) {
		switch(inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Index) {
		case RC_PRESUB_BIAS:
			code->alu.inst[ip].alpha_inst |=
						R300_ALU_SRCP_1_MINUS_2_SRC0;
			break;
		case RC_PRESUB_ADD:
			code->alu.inst[ip].alpha_inst |=
						R300_ALU_SRCP_SRC1_PLUS_SRC0;
			break;
		case RC_PRESUB_SUB:
			code->alu.inst[ip].alpha_inst |=
						R300_ALU_SRCP_SRC1_MINUS_SRC0;
			break;
		case RC_PRESUB_INV:
			code->alu.inst[ip].alpha_inst |=
						R300_ALU_SRCP_1_MINUS_SRC0;
			break;
		default:
			break;
		}
	}

	if (inst->RGB.Saturate)
		code->alu.inst[ip].rgb_inst |= R300_ALU_OUTC_CLAMP;
	if (inst->Alpha.Saturate)
		code->alu.inst[ip].alpha_inst |= R300_ALU_OUTA_CLAMP;

	if (inst->RGB.WriteMask) {
		use_temporary(code, inst->RGB.DestIndex);
		if (inst->RGB.DestIndex >= R300_PFS_NUM_TEMP_REGS)
			code->alu.inst[ip].r400_ext_addr |= R400_ADDRD_EXT_RGB_MSB_BIT;
		code->alu.inst[ip].rgb_addr |=
			((inst->RGB.DestIndex & 0x1f) << R300_ALU_DSTC_SHIFT) |
			(inst->RGB.WriteMask << R300_ALU_DSTC_REG_MASK_SHIFT);
	}
	if (inst->RGB.OutputWriteMask) {
		code->alu.inst[ip].rgb_addr |=
            (inst->RGB.OutputWriteMask << R300_ALU_DSTC_OUTPUT_MASK_SHIFT) |
            R300_RGB_TARGET(inst->RGB.Target);
		emit->node_flags |= R300_RGBA_OUT;
	}

	if (inst->Alpha.WriteMask) {
		use_temporary(code, inst->Alpha.DestIndex);
		if (inst->Alpha.DestIndex >= R300_PFS_NUM_TEMP_REGS)
			code->alu.inst[ip].r400_ext_addr |= R400_ADDRD_EXT_A_MSB_BIT;
		code->alu.inst[ip].alpha_addr |=
			((inst->Alpha.DestIndex & 0x1f) << R300_ALU_DSTA_SHIFT) |
			R300_ALU_DSTA_REG;
	}
	if (inst->Alpha.OutputWriteMask) {
		code->alu.inst[ip].alpha_addr |= R300_ALU_DSTA_OUTPUT |
            R300_ALPHA_TARGET(inst->Alpha.Target);
		emit->node_flags |= R300_RGBA_OUT;
	}
	if (inst->Alpha.DepthWriteMask) {
		code->alu.inst[ip].alpha_addr |= R300_ALU_DSTA_DEPTH;
		emit->node_flags |= R300_W_OUT;
		c->code->writes_depth = 1;
	}
	if (inst->Nop)
		code->alu.inst[ip].rgb_inst |= R300_ALU_INSERT_NOP;

	/* Handle Output Modifier
	 * According to the r300 docs, there is no RC_OMOD_DISABLE for r300 */
	if (inst->RGB.Omod) {
		if (inst->RGB.Omod == RC_OMOD_DISABLE) {
			rc_error(&c->Base, "RC_OMOD_DISABLE not supported");
		}
		code->alu.inst[ip].rgb_inst |=
			(inst->RGB.Omod << R300_ALU_OUTC_MOD_SHIFT);
	}
	if (inst->Alpha.Omod) {
		if (inst->Alpha.Omod == RC_OMOD_DISABLE) {
			rc_error(&c->Base, "RC_OMOD_DISABLE not supported");
		}
		code->alu.inst[ip].alpha_inst |=
			(inst->Alpha.Omod << R300_ALU_OUTC_MOD_SHIFT);
	}
	return 1;
}


/**
 * Finish the current node without advancing to the next one.
 */
static int finish_node(struct r300_emit_state * emit)
{
	struct r300_fragment_program_compiler * c = emit->compiler;
	struct r300_fragment_program_code *code = &emit->compiler->code->code.r300;
	unsigned alu_offset;
	unsigned alu_end;
	unsigned tex_offset;
	unsigned tex_end;

	unsigned int alu_offset_msbs, alu_end_msbs;

	if (code->alu.length == emit->node_first_alu) {
		/* Generate a single NOP for this node */
		struct rc_pair_instruction inst;
		memset(&inst, 0, sizeof(inst));
		if (!emit_alu(emit, &inst))
			return 0;
	}

	alu_offset = emit->node_first_alu;
	alu_end = code->alu.length - alu_offset - 1;
	tex_offset = emit->node_first_tex;
	tex_end = code->tex.length - tex_offset - 1;

	if (code->tex.length == emit->node_first_tex) {
		if (emit->current_node > 0) {
			error("Node %i has no TEX instructions", emit->current_node);
			return 0;
		}

		tex_end = 0;
	} else {
		if (emit->current_node == 0)
			code->config |= R300_PFS_CNTL_FIRST_NODE_HAS_TEX;
	}

	/* Write the config register.
	 * Note: The order in which the words for each node are written
	 * is not correct here and needs to be fixed up once we're entirely
	 * done
	 *
	 * Also note that the register specification from AMD is slightly
	 * incorrect in its description of this register. */
	code->code_addr[emit->current_node]  =
			((alu_offset << R300_ALU_START_SHIFT)
				& R300_ALU_START_MASK)
			| ((alu_end << R300_ALU_SIZE_SHIFT)
				& R300_ALU_SIZE_MASK)
			| ((tex_offset << R300_TEX_START_SHIFT)
				& R300_TEX_START_MASK)
			| ((tex_end << R300_TEX_SIZE_SHIFT)
				& R300_TEX_SIZE_MASK)
			| emit->node_flags
			| (get_msbs_tex(tex_offset, 5)
				<< R400_TEX_START_MSB_SHIFT)
			| (get_msbs_tex(tex_end, 5)
				<< R400_TEX_SIZE_MSB_SHIFT)
			;

	/* Write r400 extended instruction fields.  These will be ignored on
	 * r300 cards.  */
	alu_offset_msbs = get_msbs_alu(alu_offset);
	alu_end_msbs = get_msbs_alu(alu_end);
	switch(emit->current_node) {
	case 0:
		code->r400_code_offset_ext |=
			alu_offset_msbs << R400_ALU_START3_MSB_SHIFT
			| alu_end_msbs << R400_ALU_SIZE3_MSB_SHIFT;
		break;
	case 1:
		code->r400_code_offset_ext |=
			alu_offset_msbs << R400_ALU_START2_MSB_SHIFT
			| alu_end_msbs << R400_ALU_SIZE2_MSB_SHIFT;
		break;
	case 2:
		code->r400_code_offset_ext |=
			alu_offset_msbs << R400_ALU_START1_MSB_SHIFT
			| alu_end_msbs << R400_ALU_SIZE1_MSB_SHIFT;
		break;
	case 3:
		code->r400_code_offset_ext |=
			alu_offset_msbs << R400_ALU_START0_MSB_SHIFT
			| alu_end_msbs << R400_ALU_SIZE0_MSB_SHIFT;
		break;
	}
	return 1;
}


/**
 * Begin a block of texture instructions.
 * Create the necessary indirection.
 */
static int begin_tex(struct r300_emit_state * emit)
{
	PROG_CODE;

	if (code->alu.length == emit->node_first_alu &&
	    code->tex.length == emit->node_first_tex) {
		return 1;
	}

	if (emit->current_node == 3) {
		error("Too many texture indirections");
		return 0;
	}

	if (!finish_node(emit))
		return 0;

	emit->current_node++;
	emit->node_first_tex = code->tex.length;
	emit->node_first_alu = code->alu.length;
	emit->node_flags = 0;
	return 1;
}


static int emit_tex(struct r300_emit_state * emit, struct rc_instruction * inst)
{
	unsigned int unit;
	unsigned int dest;
	unsigned int opcode;
	PROG_CODE;

	if (code->tex.length >= emit->compiler->Base.max_tex_insts) {
		error("Too many TEX instructions");
		return 0;
	}

	unit = inst->U.I.TexSrcUnit;
	dest = inst->U.I.DstReg.Index;

	switch(inst->U.I.Opcode) {
	case RC_OPCODE_KIL: opcode = R300_TEX_OP_KIL; break;
	case RC_OPCODE_TEX: opcode = R300_TEX_OP_LD; break;
	case RC_OPCODE_TXB: opcode = R300_TEX_OP_TXB; break;
	case RC_OPCODE_TXP: opcode = R300_TEX_OP_TXP; break;
	default:
		error("Unknown texture opcode %s", rc_get_opcode_info(inst->U.I.Opcode)->Name);
		return 0;
	}

	if (inst->U.I.Opcode == RC_OPCODE_KIL) {
		unit = 0;
		dest = 0;
	} else {
		use_temporary(code, dest);
	}

	use_temporary(code, inst->U.I.SrcReg[0].Index);

	code->tex.inst[code->tex.length++] =
		((inst->U.I.SrcReg[0].Index << R300_SRC_ADDR_SHIFT)
			& R300_SRC_ADDR_MASK)
		| ((dest << R300_DST_ADDR_SHIFT)
			& R300_DST_ADDR_MASK)
		| (unit << R300_TEX_ID_SHIFT)
		| (opcode << R300_TEX_INST_SHIFT)
		| (inst->U.I.SrcReg[0].Index >= R300_PFS_NUM_TEMP_REGS ?
			R400_SRC_ADDR_EXT_BIT : 0)
		| (dest >= R300_PFS_NUM_TEMP_REGS ?
			R400_DST_ADDR_EXT_BIT : 0)
		;
	return 1;
}


/**
 * Final compilation step: Turn the intermediate radeon_program into
 * machine-readable instructions.
 */
void r300BuildFragmentProgramHwCode(struct radeon_compiler *c, void *user)
{
	struct r300_fragment_program_compiler *compiler = (struct r300_fragment_program_compiler*)c;
	struct r300_emit_state emit;
	struct r300_fragment_program_code *code = &compiler->code->code.r300;
	unsigned int tex_end;

	memset(&emit, 0, sizeof(emit));
	emit.compiler = compiler;

	memset(code, 0, sizeof(struct r300_fragment_program_code));

	for(struct rc_instruction * inst = compiler->Base.Program.Instructions.Next;
	    inst != &compiler->Base.Program.Instructions && !compiler->Base.Error;
	    inst = inst->Next) {
		if (inst->Type == RC_INSTRUCTION_NORMAL) {
			if (inst->U.I.Opcode == RC_OPCODE_BEGIN_TEX) {
				begin_tex(&emit);
				continue;
			}

			emit_tex(&emit, inst);
		} else {
			emit_alu(&emit, &inst->U.P);
		}
	}

	if (code->pixsize >= compiler->Base.max_temp_regs)
		rc_error(&compiler->Base, "Too many hardware temporaries used.\n");

	if (compiler->Base.Error)
		return;

	/* Finish the program */
	finish_node(&emit);

	code->config |= emit.current_node; /* FIRST_NODE_HAS_TEX set by finish_node */

	/* Set r400 extended instruction fields.  These values will be ignored
	 * on r300 cards. */
	code->r400_code_offset_ext |=
		(get_msbs_alu(0)
				<< R400_ALU_OFFSET_MSB_SHIFT)
		| (get_msbs_alu(code->alu.length - 1)
				<< R400_ALU_SIZE_MSB_SHIFT);

	tex_end = code->tex.length ? code->tex.length - 1 : 0;
	code->code_offset =
		((0 << R300_PFS_CNTL_ALU_OFFSET_SHIFT)
			& R300_PFS_CNTL_ALU_OFFSET_MASK)
		| (((code->alu.length - 1) << R300_PFS_CNTL_ALU_END_SHIFT)
			& R300_PFS_CNTL_ALU_END_MASK)
		| ((0 << R300_PFS_CNTL_TEX_OFFSET_SHIFT)
			& R300_PFS_CNTL_TEX_OFFSET_MASK)
		| ((tex_end << R300_PFS_CNTL_TEX_END_SHIFT)
			& R300_PFS_CNTL_TEX_END_MASK)
		| (get_msbs_tex(0, 5) << R400_TEX_START_MSB_SHIFT)
		| (get_msbs_tex(tex_end, 6) << R400_TEX_SIZE_MSB_SHIFT)
		;

	if (emit.current_node < 3) {
		int shift = 3 - emit.current_node;
		int i;
		for(i = emit.current_node; i >= 0; --i)
			code->code_addr[shift + i] = code->code_addr[i];
		for(i = 0; i < shift; ++i)
			code->code_addr[i] = 0;
	}

	if (code->pixsize >= R300_PFS_NUM_TEMP_REGS
	    || code->alu.length > R300_PFS_MAX_ALU_INST
	    || code->tex.length > R300_PFS_MAX_TEX_INST) {

		code->r390_mode = 1;
	}
}