Merge branch 'shader-file-reorg'
[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 "r300_fragprog.h"
33 #include "r300_fragprog_swizzle.h"
34 #include "r500_fragprog.h"
35
36
37 static void dataflow_outputs_mark_use(void * userdata, void * data,
38 void (*callback)(void *, unsigned int, unsigned int))
39 {
40 struct r300_fragment_program_compiler * c = userdata;
41 callback(data, c->OutputColor[0], RC_MASK_XYZW);
42 callback(data, c->OutputColor[1], RC_MASK_XYZW);
43 callback(data, c->OutputColor[2], RC_MASK_XYZW);
44 callback(data, c->OutputColor[3], RC_MASK_XYZW);
45 callback(data, c->OutputDepth, RC_MASK_W);
46 }
47
48 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
49 {
50 struct rc_instruction *rci;
51
52 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
53 struct rc_sub_instruction * inst = &rci->U.I;
54
55 if (inst->DstReg.File != RC_FILE_OUTPUT || inst->DstReg.Index != c->OutputDepth)
56 continue;
57
58 if (inst->DstReg.WriteMask & RC_MASK_Z) {
59 inst->DstReg.WriteMask = RC_MASK_W;
60 } else {
61 inst->DstReg.WriteMask = 0;
62 continue;
63 }
64
65 switch (inst->Opcode) {
66 case RC_OPCODE_FRC:
67 case RC_OPCODE_MOV:
68 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
69 break;
70 case RC_OPCODE_ADD:
71 case RC_OPCODE_MAX:
72 case RC_OPCODE_MIN:
73 case RC_OPCODE_MUL:
74 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
75 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
76 break;
77 case RC_OPCODE_CMP:
78 case RC_OPCODE_MAD:
79 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
80 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
81 inst->SrcReg[2] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[2]);
82 break;
83 default:
84 // Scalar instructions needn't be reswizzled
85 break;
86 }
87 }
88 }
89
90 static void debug_program_log(struct r300_fragment_program_compiler* c, const char * where)
91 {
92 if (c->Base.Debug) {
93 fprintf(stderr, "Fragment Program: %s\n", where);
94 rc_print_program(&c->Base.Program);
95 }
96 }
97
98 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
99 {
100 rewrite_depth_out(c);
101
102 debug_program_log(c, "before compilation");
103
104 /* XXX Ideally this should be done only for r3xx, but since
105 * we don't have branching support for r5xx, we use the emulation
106 * on all chipsets. */
107
108 if (c->Base.is_r500) {
109 rc_emulate_loops(&c->Base, R500_PFS_MAX_INST);
110 } else {
111 rc_emulate_loops(&c->Base, R300_PFS_MAX_ALU_INST);
112 }
113 debug_program_log(c, "after emulate loops");
114
115 rc_emulate_branches(&c->Base);
116
117 debug_program_log(c, "after emulate branches");
118
119 if (c->Base.is_r500) {
120 struct radeon_program_transformation transformations[] = {
121 { &r500_transform_IF, 0 },
122 { &radeonTransformALU, 0 },
123 { &radeonTransformDeriv, 0 },
124 { &radeonTransformTrigScale, 0 }
125 };
126 radeonLocalTransform(&c->Base, 4, transformations);
127
128 debug_program_log(c, "after native rewrite part 1");
129
130 c->Base.SwizzleCaps = &r500_swizzle_caps;
131 } else {
132 struct radeon_program_transformation transformations[] = {
133 { &radeonTransformALU, 0 },
134 { &radeonTransformTrigSimple, 0 }
135 };
136 radeonLocalTransform(&c->Base, 2, transformations);
137
138 debug_program_log(c, "after native rewrite part 1");
139
140 c->Base.SwizzleCaps = &r300_swizzle_caps;
141 }
142
143 /* Run the common transformations too.
144 * Remember, lowering comes last! */
145 struct radeon_program_transformation common_transformations[] = {
146 { &radeonTransformTEX, c },
147 };
148 radeonLocalTransform(&c->Base, 1, common_transformations);
149
150 common_transformations[0].function = &radeonTransformALU;
151 radeonLocalTransform(&c->Base, 1, common_transformations);
152
153 if (c->Base.Error)
154 return;
155
156 debug_program_log(c, "after native rewrite part 2");
157
158 rc_dataflow_deadcode(&c->Base, &dataflow_outputs_mark_use, c);
159 if (c->Base.Error)
160 return;
161
162 debug_program_log(c, "after deadcode");
163
164 rc_optimize(&c->Base);
165
166 debug_program_log(c, "after dataflow optimize");
167
168 rc_dataflow_swizzles(&c->Base);
169 if (c->Base.Error)
170 return;
171
172 debug_program_log(c, "after dataflow passes");
173
174 rc_pair_translate(c);
175 if (c->Base.Error)
176 return;
177
178 debug_program_log(c, "after pair translate");
179
180 rc_pair_schedule(c);
181 if (c->Base.Error)
182 return;
183
184 debug_program_log(c, "after pair scheduling");
185
186 rc_pair_regalloc(c, c->Base.max_temp_regs);
187
188 if (c->Base.Error)
189 return;
190
191 debug_program_log(c, "after register allocation");
192
193 if (c->Base.is_r500) {
194 r500BuildFragmentProgramHwCode(c);
195 } else {
196 r300BuildFragmentProgramHwCode(c);
197 }
198
199 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
200
201 if (c->Base.Debug) {
202 if (c->Base.is_r500) {
203 r500FragmentProgramDump(c->code);
204 } else {
205 r300FragmentProgramDump(c->code);
206 }
207 }
208 }