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 "r300_fragprog.h"
33 #include "r300_fragprog_swizzle.h"
34 #include "r500_fragprog.h"
37 static void dataflow_outputs_mark_use(void * userdata
, void * data
,
38 void (*callback
)(void *, unsigned int, unsigned int))
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
);
48 static void rewrite_depth_out(struct r300_fragment_program_compiler
* c
)
50 struct rc_instruction
*rci
;
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
;
55 if (inst
->DstReg
.File
!= RC_FILE_OUTPUT
|| inst
->DstReg
.Index
!= c
->OutputDepth
)
58 if (inst
->DstReg
.WriteMask
& RC_MASK_Z
) {
59 inst
->DstReg
.WriteMask
= RC_MASK_W
;
61 inst
->DstReg
.WriteMask
= 0;
65 switch (inst
->Opcode
) {
68 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
74 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
75 inst
->SrcReg
[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
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]);
84 // Scalar instructions needn't be reswizzled
90 static void debug_program_log(struct r300_fragment_program_compiler
* c
, const char * where
)
93 fprintf(stderr
, "Fragment Program: %s\n", where
);
94 rc_print_program(&c
->Base
.Program
);
98 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler
* c
)
100 rewrite_depth_out(c
);
102 debug_program_log(c
, "before compilation");
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. */
108 if (c
->Base
.is_r500
) {
109 rc_emulate_loops(&c
->Base
, R500_PFS_MAX_INST
);
111 rc_emulate_loops(&c
->Base
, R300_PFS_MAX_ALU_INST
);
113 debug_program_log(c
, "after emulate loops");
115 rc_emulate_branches(&c
->Base
);
117 debug_program_log(c
, "after emulate branches");
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 }
126 radeonLocalTransform(&c
->Base
, 4, transformations
);
128 debug_program_log(c
, "after native rewrite part 1");
130 c
->Base
.SwizzleCaps
= &r500_swizzle_caps
;
132 struct radeon_program_transformation transformations
[] = {
133 { &radeonTransformALU
, 0 },
134 { &radeonTransformTrigSimple
, 0 }
136 radeonLocalTransform(&c
->Base
, 2, transformations
);
138 debug_program_log(c
, "after native rewrite part 1");
140 c
->Base
.SwizzleCaps
= &r300_swizzle_caps
;
143 /* Run the common transformations too.
144 * Remember, lowering comes last! */
145 struct radeon_program_transformation common_transformations
[] = {
146 { &radeonTransformTEX
, c
},
148 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
150 common_transformations
[0].function
= &radeonTransformALU
;
151 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
156 debug_program_log(c
, "after native rewrite part 2");
158 rc_dataflow_deadcode(&c
->Base
, &dataflow_outputs_mark_use
, c
);
162 debug_program_log(c
, "after deadcode");
164 rc_optimize(&c
->Base
);
166 debug_program_log(c
, "after dataflow optimize");
168 rc_dataflow_swizzles(&c
->Base
);
172 debug_program_log(c
, "after dataflow passes");
174 rc_pair_translate(c
);
178 debug_program_log(c
, "after pair translate");
184 debug_program_log(c
, "after pair scheduling");
186 rc_pair_regalloc(c
, c
->Base
.max_temp_regs
);
191 debug_program_log(c
, "after register allocation");
193 if (c
->Base
.is_r500
) {
194 r500BuildFragmentProgramHwCode(c
);
196 r300BuildFragmentProgramHwCode(c
);
199 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
202 if (c
->Base
.is_r500
) {
203 r500FragmentProgramDump(c
->code
);
205 r300FragmentProgramDump(c
->code
);