2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
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:
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
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. */
23 #include "radeon_compiler.h"
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"
39 static void dataflow_outputs_mark_use(void * userdata
, void * data
,
40 void (*callback
)(void *, unsigned int, unsigned int))
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
);
50 static void rewrite_depth_out(struct r300_fragment_program_compiler
* c
)
52 struct rc_instruction
*rci
;
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
;
57 if (inst
->DstReg
.File
!= RC_FILE_OUTPUT
|| inst
->DstReg
.Index
!= c
->OutputDepth
)
60 if (inst
->DstReg
.WriteMask
& RC_MASK_Z
) {
61 inst
->DstReg
.WriteMask
= RC_MASK_W
;
63 inst
->DstReg
.WriteMask
= 0;
67 switch (inst
->Opcode
) {
70 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
76 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
77 inst
->SrcReg
[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
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]);
86 // Scalar instructions needn't be reswizzled
92 static void debug_program_log(struct r300_fragment_program_compiler
* c
, const char * where
)
95 fprintf(stderr
, "Fragment Program: %s\n", where
);
96 rc_print_program(&c
->Base
.Program
);
100 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler
* c
)
102 rewrite_depth_out(c
);
104 /* This transformation needs to be done before any of the IF
105 * instructions are modified. */
106 radeonTransformKILP(&c
->Base
);
108 debug_program_log(c
, "before compilation");
110 if (c
->Base
.is_r500
) {
111 rc_unroll_loops(&c
->Base
);
112 debug_program_log(c
, "after unroll loops");
114 rc_transform_loops(&c
->Base
);
115 debug_program_log(c
, "after transform loops");
117 rc_emulate_branches(&c
->Base
);
118 debug_program_log(c
, "after emulate branches");
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 }
128 radeonLocalTransform(&c
->Base
, 4, transformations
);
130 debug_program_log(c
, "after native rewrite part 1");
132 c
->Base
.SwizzleCaps
= &r500_swizzle_caps
;
134 struct radeon_program_transformation transformations
[] = {
135 { &radeonTransformALU
, 0 },
136 { &radeonTransformTrigSimple
, 0 }
138 radeonLocalTransform(&c
->Base
, 2, transformations
);
140 debug_program_log(c
, "after native rewrite part 1");
142 c
->Base
.SwizzleCaps
= &r300_swizzle_caps
;
145 /* Run the common transformations too.
146 * Remember, lowering comes last! */
147 struct radeon_program_transformation common_transformations
[] = {
148 { &radeonTransformTEX
, c
},
150 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
152 common_transformations
[0].function
= &radeonTransformALU
;
153 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
158 debug_program_log(c
, "after native rewrite part 2");
160 rc_dataflow_deadcode(&c
->Base
, &dataflow_outputs_mark_use
, c
);
164 debug_program_log(c
, "after deadcode");
166 if (!c
->Base
.is_r500
) {
167 rc_emulate_loops(&c
->Base
);
168 debug_program_log(c
, "after emulate loops");
171 rc_optimize(&c
->Base
);
173 debug_program_log(c
, "after dataflow optimize");
175 rc_dataflow_swizzles(&c
->Base
);
179 debug_program_log(c
, "after dataflow passes");
181 if (c
->Base
.remove_unused_constants
) {
182 rc_remove_unused_constants(&c
->Base
,
183 &c
->code
->constants_remap_table
);
185 debug_program_log(c
, "after constants cleanup");
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
);
195 debug_program_log(c
, "after register rename");
198 rc_pair_translate(c
);
202 debug_program_log(c
, "after pair translate");
208 debug_program_log(c
, "after pair scheduling");
210 rc_pair_regalloc(c
, c
->Base
.max_temp_regs
);
215 debug_program_log(c
, "after register allocation");
217 if (c
->Base
.is_r500
) {
218 r500BuildFragmentProgramHwCode(c
);
220 r300BuildFragmentProgramHwCode(c
);
223 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
226 if (c
->Base
.is_r500
) {
227 r500FragmentProgramDump(c
->code
);
229 r300FragmentProgramDump(c
->code
);
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
;
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
);