Merge remote branch 'origin/master' into nv50-compiler
[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 struct emulate_loop_state loop_state;
103
104 rewrite_depth_out(c);
105
106 /* This transformation needs to be done before any of the IF
107 * instructions are modified. */
108 radeonTransformKILP(&c->Base);
109
110 debug_program_log(c, "before compilation");
111
112 if (c->Base.is_r500){
113 rc_unroll_loops(&c->Base, R500_PFS_MAX_INST);
114 debug_program_log(c, "after unroll loops");
115 }
116 else{
117 rc_transform_loops(&c->Base, &loop_state, -1);
118 debug_program_log(c, "after transform loops");
119
120 rc_emulate_branches(&c->Base);
121 debug_program_log(c, "after emulate branches");
122 }
123
124 if (c->Base.is_r500) {
125 struct radeon_program_transformation transformations[] = {
126 { &r500_transform_IF, 0 },
127 { &radeonTransformALU, 0 },
128 { &radeonTransformDeriv, 0 },
129 { &radeonTransformTrigScale, 0 }
130 };
131 radeonLocalTransform(&c->Base, 4, transformations);
132
133 debug_program_log(c, "after native rewrite part 1");
134
135 c->Base.SwizzleCaps = &r500_swizzle_caps;
136 } else {
137 struct radeon_program_transformation transformations[] = {
138 { &radeonTransformALU, 0 },
139 { &radeonTransformTrigSimple, 0 }
140 };
141 radeonLocalTransform(&c->Base, 2, transformations);
142
143 debug_program_log(c, "after native rewrite part 1");
144
145 c->Base.SwizzleCaps = &r300_swizzle_caps;
146 }
147
148 /* Run the common transformations too.
149 * Remember, lowering comes last! */
150 struct radeon_program_transformation common_transformations[] = {
151 { &radeonTransformTEX, c },
152 };
153 radeonLocalTransform(&c->Base, 1, common_transformations);
154
155 common_transformations[0].function = &radeonTransformALU;
156 radeonLocalTransform(&c->Base, 1, common_transformations);
157
158 if (c->Base.Error)
159 return;
160
161 debug_program_log(c, "after native rewrite part 2");
162
163 rc_dataflow_deadcode(&c->Base, &dataflow_outputs_mark_use, c);
164 if (c->Base.Error)
165 return;
166
167 debug_program_log(c, "after deadcode");
168
169 if(!c->Base.is_r500){
170 rc_emulate_loops(&loop_state, R300_PFS_MAX_ALU_INST);
171 debug_program_log(c, "after emulate loops");
172 }
173
174 rc_optimize(&c->Base);
175
176 debug_program_log(c, "after dataflow optimize");
177
178 rc_dataflow_swizzles(&c->Base);
179 if (c->Base.Error)
180 return;
181
182 debug_program_log(c, "after dataflow passes");
183
184 if (c->Base.remove_unused_constants) {
185 rc_remove_unused_constants(&c->Base,
186 &c->code->constants_remap_table);
187
188 debug_program_log(c, "after constants cleanup");
189 }
190
191 if(!c->Base.is_r500) {
192 /* This pass makes it easier for the scheduler to group TEX
193 * instructions and reduces the chances of creating too
194 * many texture indirections.*/
195 rc_rename_regs(&c->Base);
196 if (c->Base.Error)
197 return;
198 debug_program_log(c, "after register rename");
199 }
200
201 rc_pair_translate(c);
202 if (c->Base.Error)
203 return;
204
205 debug_program_log(c, "after pair translate");
206
207 rc_pair_schedule(c);
208 if (c->Base.Error)
209 return;
210
211 debug_program_log(c, "after pair scheduling");
212
213 rc_pair_regalloc(c, c->Base.max_temp_regs);
214
215 if (c->Base.Error)
216 return;
217
218 debug_program_log(c, "after register allocation");
219
220 if (c->Base.is_r500) {
221 r500BuildFragmentProgramHwCode(c);
222 } else {
223 r300BuildFragmentProgramHwCode(c);
224 }
225
226 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
227
228 if (c->Base.Debug) {
229 if (c->Base.is_r500) {
230 r500FragmentProgramDump(c->code);
231 } else {
232 r300FragmentProgramDump(c->code);
233 }
234 }
235
236 /* Check the number of constants. */
237 if (!c->Base.Error) {
238 unsigned max = c->Base.is_r500 ? R500_PFS_NUM_CONST_REGS : R300_PFS_NUM_CONST_REGS;
239
240 if (c->Base.Program.Constants.Count > max) {
241 rc_error(&c->Base, "Too many constants. Max: %i, Got: %i\n",
242 max, c->Base.Program.Constants.Count);
243 }
244 }
245 }