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