1/*
2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#include "radeon_compiler.h"
24
25#include <stdio.h>
26
27#include "radeon_compiler_util.h"
28#include "radeon_dataflow.h"
29#include "radeon_emulate_branches.h"
30#include "radeon_program_alu.h"
31#include "radeon_program_tex.h"
32#include "radeon_rename_regs.h"
33#include "radeon_remove_constants.h"
34#include "r300_fragprog.h"
35#include "r300_fragprog_swizzle.h"
36#include "r500_fragprog.h"
37
38
39static void rc_rewrite_depth_out(struct radeon_compiler *cc, void *user)
40{
41	struct r300_fragment_program_compiler *c = (struct r300_fragment_program_compiler*)cc;
42	struct rc_instruction *rci;
43
44	for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
45		struct rc_sub_instruction * inst = &rci->U.I;
46		unsigned i;
47		const struct rc_opcode_info *info = rc_get_opcode_info(inst->Opcode);
48
49		if (inst->DstReg.File != RC_FILE_OUTPUT || inst->DstReg.Index != c->OutputDepth)
50			continue;
51
52		if (inst->DstReg.WriteMask & RC_MASK_Z) {
53			inst->DstReg.WriteMask = RC_MASK_W;
54		} else {
55			inst->DstReg.WriteMask = 0;
56			continue;
57		}
58
59		if (!info->IsComponentwise) {
60			continue;
61		}
62
63		for (i = 0; i < info->NumSrcRegs; i++) {
64			inst->SrcReg[i] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[i]);
65		}
66	}
67}
68
69void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
70{
71	int is_r500 = c->Base.is_r500;
72	int opt = !c->Base.disable_optimizations;
73	int alpha2one = c->state.alpha_to_one;
74
75	/* Lists of instruction transformations. */
76	struct radeon_program_transformation force_alpha_to_one[] = {
77		{ &rc_force_output_alpha_to_one, c },
78		{ NULL, NULL }
79	};
80
81	struct radeon_program_transformation rewrite_tex[] = {
82		{ &radeonTransformTEX, c },
83		{ NULL, NULL }
84	};
85
86	struct radeon_program_transformation rewrite_if[] = {
87		{ &r500_transform_IF, NULL },
88		{ NULL, NULL }
89	};
90
91	struct radeon_program_transformation native_rewrite_r500[] = {
92		{ &radeonTransformALU, NULL },
93		{ &radeonTransformDeriv, NULL },
94		{ &radeonTransformTrigScale, NULL },
95		{ NULL, NULL }
96	};
97
98	struct radeon_program_transformation native_rewrite_r300[] = {
99		{ &radeonTransformALU, NULL },
100		{ &radeonStubDeriv, NULL },
101		{ &r300_transform_trig_simple, NULL },
102		{ NULL, NULL }
103	};
104
105	/* List of compiler passes. */
106	struct radeon_compiler_pass fs_list[] = {
107		/* NAME				DUMP PREDICATE	FUNCTION			PARAM */
108		{"rewrite depth out",		1, 1,		rc_rewrite_depth_out,		NULL},
109		/* This transformation needs to be done before any of the IF
110		 * instructions are modified. */
111		{"transform KILP",		1, 1,		rc_transform_KILL,		NULL},
112		{"emulate branches",		1, !is_r500,	rc_emulate_branches,		NULL},
113		{"force alpha to one",		1, alpha2one,	rc_local_transform,		force_alpha_to_one},
114		{"transform TEX",		1, 1,		rc_local_transform,		rewrite_tex},
115		{"transform IF",		1, is_r500,	rc_local_transform,		rewrite_if},
116		{"native rewrite",		1, is_r500,	rc_local_transform,		native_rewrite_r500},
117		{"native rewrite",		1, !is_r500,	rc_local_transform,		native_rewrite_r300},
118		{"deadcode",			1, opt,		rc_dataflow_deadcode,		NULL},
119		{"register rename",		1, !is_r500 || opt,		rc_rename_regs,			NULL},
120		{"dataflow optimize",		1, opt,		rc_optimize,			NULL},
121		{"inline literals",		1, is_r500 && opt,		rc_inline_literals,			NULL},
122		{"dataflow swizzles",		1, 1,		rc_dataflow_swizzles,		NULL},
123		{"dead constants",		1, 1,		rc_remove_unused_constants,	&c->code->constants_remap_table},
124		{"pair translate",		1, 1,		rc_pair_translate,		NULL},
125		{"pair scheduling",		1, 1,		rc_pair_schedule,		&opt},
126		{"dead sources",		1, 1,		rc_pair_remove_dead_sources, NULL},
127		{"register allocation",		1, 1,		rc_pair_regalloc,		&opt},
128		{"final code validation",	0, 1,		rc_validate_final_shader,	NULL},
129		{"machine code generation",	0, is_r500,	r500BuildFragmentProgramHwCode,	NULL},
130		{"machine code generation",	0, !is_r500,	r300BuildFragmentProgramHwCode,	NULL},
131		{"dump machine code",		0, is_r500  && (c->Base.Debug & RC_DBG_LOG), r500FragmentProgramDump, NULL},
132		{"dump machine code",		0, !is_r500 && (c->Base.Debug & RC_DBG_LOG), r300FragmentProgramDump, NULL},
133		{NULL, 0, 0, NULL, NULL}
134	};
135
136	c->Base.type = RC_FRAGMENT_PROGRAM;
137	c->Base.SwizzleCaps = c->Base.is_r500 ? &r500_swizzle_caps : &r300_swizzle_caps;
138
139	rc_run_compiler(&c->Base, fs_list);
140
141	rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
142}
143