r300/compiler: add new compiler parameter max_alu_insts
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / r3xx_fragprog.c
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_dataflow.h"
28 #include "radeon_emulate_branches.h"
29 #include "radeon_emulate_loops.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
39 static void dataflow_outputs_mark_use(void * userdata, void * data,
40 void (*callback)(void *, unsigned int, unsigned int))
41 {
42 struct r300_fragment_program_compiler * c = userdata;
43 callback(data, c->OutputColor[0], RC_MASK_XYZW);
44 callback(data, c->OutputColor[1], RC_MASK_XYZW);
45 callback(data, c->OutputColor[2], RC_MASK_XYZW);
46 callback(data, c->OutputColor[3], RC_MASK_XYZW);
47 callback(data, c->OutputDepth, RC_MASK_W);
48 }
49
50 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
51 {
52 struct rc_instruction *rci;
53
54 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
55 struct rc_sub_instruction * inst = &rci->U.I;
56
57 if (inst->DstReg.File != RC_FILE_OUTPUT || inst->DstReg.Index != c->OutputDepth)
58 continue;
59
60 if (inst->DstReg.WriteMask & RC_MASK_Z) {
61 inst->DstReg.WriteMask = RC_MASK_W;
62 } else {
63 inst->DstReg.WriteMask = 0;
64 continue;
65 }
66
67 switch (inst->Opcode) {
68 case RC_OPCODE_FRC:
69 case RC_OPCODE_MOV:
70 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
71 break;
72 case RC_OPCODE_ADD:
73 case RC_OPCODE_MAX:
74 case RC_OPCODE_MIN:
75 case RC_OPCODE_MUL:
76 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
77 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
78 break;
79 case RC_OPCODE_CMP:
80 case RC_OPCODE_MAD:
81 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
82 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
83 inst->SrcReg[2] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[2]);
84 break;
85 default:
86 // Scalar instructions needn't be reswizzled
87 break;
88 }
89 }
90 }
91
92 static void debug_program_log(struct r300_fragment_program_compiler* c, const char * where)
93 {
94 if (c->Base.Debug) {
95 fprintf(stderr, "Fragment Program: %s\n", where);
96 rc_print_program(&c->Base.Program);
97 }
98 }
99
100 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
101 {
102 rewrite_depth_out(c);
103
104 /* This transformation needs to be done before any of the IF
105 * instructions are modified. */
106 radeonTransformKILP(&c->Base);
107
108 debug_program_log(c, "before compilation");
109
110 if (c->Base.is_r500) {
111 rc_unroll_loops(&c->Base);
112 debug_program_log(c, "after unroll loops");
113 } else {
114 rc_transform_loops(&c->Base);
115 debug_program_log(c, "after transform loops");
116
117 rc_emulate_branches(&c->Base);
118 debug_program_log(c, "after emulate branches");
119 }
120
121 if (c->Base.is_r500) {
122 struct radeon_program_transformation transformations[] = {
123 { &r500_transform_IF, 0 },
124 { &radeonTransformALU, 0 },
125 { &radeonTransformDeriv, 0 },
126 { &radeonTransformTrigScale, 0 }
127 };
128 radeonLocalTransform(&c->Base, 4, transformations);
129
130 debug_program_log(c, "after native rewrite part 1");
131
132 c->Base.SwizzleCaps = &r500_swizzle_caps;
133 } else {
134 struct radeon_program_transformation transformations[] = {
135 { &radeonTransformALU, 0 },
136 { &radeonTransformTrigSimple, 0 }
137 };
138 radeonLocalTransform(&c->Base, 2, transformations);
139
140 debug_program_log(c, "after native rewrite part 1");
141
142 c->Base.SwizzleCaps = &r300_swizzle_caps;
143 }
144
145 /* Run the common transformations too.
146 * Remember, lowering comes last! */
147 struct radeon_program_transformation common_transformations[] = {
148 { &radeonTransformTEX, c },
149 };
150 radeonLocalTransform(&c->Base, 1, common_transformations);
151
152 common_transformations[0].function = &radeonTransformALU;
153 radeonLocalTransform(&c->Base, 1, common_transformations);
154
155 if (c->Base.Error)
156 return;
157
158 debug_program_log(c, "after native rewrite part 2");
159
160 rc_dataflow_deadcode(&c->Base, &dataflow_outputs_mark_use, c);
161 if (c->Base.Error)
162 return;
163
164 debug_program_log(c, "after deadcode");
165
166 if (!c->Base.is_r500) {
167 rc_emulate_loops(&c->Base);
168 debug_program_log(c, "after emulate loops");
169 }
170
171 rc_optimize(&c->Base);
172
173 debug_program_log(c, "after dataflow optimize");
174
175 rc_dataflow_swizzles(&c->Base);
176 if (c->Base.Error)
177 return;
178
179 debug_program_log(c, "after dataflow passes");
180
181 if (c->Base.remove_unused_constants) {
182 rc_remove_unused_constants(&c->Base,
183 &c->code->constants_remap_table);
184
185 debug_program_log(c, "after constants cleanup");
186 }
187
188 if (!c->Base.is_r500) {
189 /* This pass makes it easier for the scheduler to group TEX
190 * instructions and reduces the chances of creating too
191 * many texture indirections.*/
192 rc_rename_regs(&c->Base);
193 if (c->Base.Error)
194 return;
195 debug_program_log(c, "after register rename");
196 }
197
198 rc_pair_translate(c);
199 if (c->Base.Error)
200 return;
201
202 debug_program_log(c, "after pair translate");
203
204 rc_pair_schedule(c);
205 if (c->Base.Error)
206 return;
207
208 debug_program_log(c, "after pair scheduling");
209
210 rc_pair_regalloc(c, c->Base.max_temp_regs);
211
212 if (c->Base.Error)
213 return;
214
215 debug_program_log(c, "after register allocation");
216
217 if (c->Base.is_r500) {
218 r500BuildFragmentProgramHwCode(c);
219 } else {
220 r300BuildFragmentProgramHwCode(c);
221 }
222
223 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
224
225 if (c->Base.Debug) {
226 if (c->Base.is_r500) {
227 r500FragmentProgramDump(c->code);
228 } else {
229 r300FragmentProgramDump(c->code);
230 }
231 }
232
233 /* Check the number of constants. */
234 if (!c->Base.Error) {
235 unsigned max = c->Base.is_r500 ? R500_PFS_NUM_CONST_REGS : R300_PFS_NUM_CONST_REGS;
236
237 if (c->Base.Program.Constants.Count > max) {
238 rc_error(&c->Base, "Too many constants. Max: %i, Got: %i\n",
239 max, c->Base.Program.Constants.Count);
240 }
241 }
242 }