Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[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_program_alu.h"
29 #include "r300_fragprog.h"
30 #include "r300_fragprog_swizzle.h"
31 #include "r500_fragprog.h"
32
33
34 static void dataflow_outputs_mark_use(void * userdata, void * data,
35 void (*callback)(void *, unsigned int, unsigned int))
36 {
37 struct r300_fragment_program_compiler * c = userdata;
38 callback(data, c->OutputColor, RC_MASK_XYZW);
39 callback(data, c->OutputDepth, RC_MASK_W);
40 }
41
42 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
43 {
44 struct rc_instruction *rci;
45
46 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
47 struct rc_sub_instruction * inst = &rci->U.I;
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 switch (inst->Opcode) {
60 case RC_OPCODE_FRC:
61 case RC_OPCODE_MOV:
62 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
63 break;
64 case RC_OPCODE_ADD:
65 case RC_OPCODE_MAX:
66 case RC_OPCODE_MIN:
67 case RC_OPCODE_MUL:
68 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
69 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
70 break;
71 case RC_OPCODE_CMP:
72 case RC_OPCODE_MAD:
73 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
74 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
75 inst->SrcReg[2] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[2]);
76 break;
77 default:
78 // Scalar instructions needn't be reswizzled
79 break;
80 }
81 }
82 }
83
84 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
85 {
86 rewrite_depth_out(c);
87
88 if (c->is_r500) {
89 struct radeon_program_transformation transformations[] = {
90 { &r500_transform_TEX, c },
91 { &r500_transform_IF, 0 },
92 { &radeonTransformALU, 0 },
93 { &radeonTransformDeriv, 0 },
94 { &radeonTransformTrigScale, 0 }
95 };
96 radeonLocalTransform(&c->Base, 5, transformations);
97
98 c->Base.SwizzleCaps = &r500_swizzle_caps;
99 } else {
100 struct radeon_program_transformation transformations[] = {
101 { &r300_transform_TEX, c },
102 { &radeonTransformALU, 0 },
103 { &radeonTransformTrigSimple, 0 }
104 };
105 radeonLocalTransform(&c->Base, 3, transformations);
106
107 c->Base.SwizzleCaps = &r300_swizzle_caps;
108 }
109
110 if (c->Base.Debug) {
111 fprintf(stderr, "Fragment Program: After native rewrite:\n");
112 rc_print_program(&c->Base.Program);
113 fflush(stderr);
114 }
115
116 rc_dataflow_deadcode(&c->Base, &dataflow_outputs_mark_use, c);
117 if (c->Base.Error)
118 return;
119
120 if (c->Base.Debug) {
121 fprintf(stderr, "Fragment Program: After deadcode:\n");
122 rc_print_program(&c->Base.Program);
123 fflush(stderr);
124 }
125
126 rc_dataflow_swizzles(&c->Base);
127 if (c->Base.Error)
128 return;
129
130 if (c->Base.Debug) {
131 fprintf(stderr, "Compiler: after dataflow passes:\n");
132 rc_print_program(&c->Base.Program);
133 fflush(stderr);
134 }
135
136 rc_pair_translate(c);
137 if (c->Base.Error)
138 return;
139
140 if (c->Base.Debug) {
141 fprintf(stderr, "Compiler: after pair translate:\n");
142 rc_print_program(&c->Base.Program);
143 fflush(stderr);
144 }
145
146 rc_pair_schedule(c);
147 if (c->Base.Error)
148 return;
149
150 if (c->Base.Debug) {
151 fprintf(stderr, "Compiler: after pair scheduling:\n");
152 rc_print_program(&c->Base.Program);
153 fflush(stderr);
154 }
155
156 if (c->is_r500)
157 rc_pair_regalloc(c, 128);
158 else
159 rc_pair_regalloc(c, R300_PFS_NUM_TEMP_REGS);
160
161 if (c->Base.Error)
162 return;
163
164 if (c->Base.Debug) {
165 fprintf(stderr, "Compiler: after pair register allocation:\n");
166 rc_print_program(&c->Base.Program);
167 fflush(stderr);
168 }
169
170 if (c->is_r500) {
171 r500BuildFragmentProgramHwCode(c);
172 } else {
173 r300BuildFragmentProgramHwCode(c);
174 }
175
176 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
177
178 if (c->Base.Debug) {
179 if (c->is_r500) {
180 r500FragmentProgramDump(c->code);
181 } else {
182 r300FragmentProgramDump(c->code);
183 }
184 }
185 }