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 struct emulate_loop_state loop_state
;
104 rewrite_depth_out(c
);
106 /* This transformation needs to be done before any of the IF
107 * instructions are modified. */
108 radeonTransformKILP(&c
->Base
);
110 debug_program_log(c
, "before compilation");
112 if (c
->Base
.is_r500
){
113 rc_unroll_loops(&c
->Base
, R500_PFS_MAX_INST
);
114 debug_program_log(c
, "after unroll loops");
117 rc_transform_loops(&c
->Base
, &loop_state
, -1);
118 debug_program_log(c
, "after transform loops");
120 rc_emulate_branches(&c
->Base
);
121 debug_program_log(c
, "after emulate branches");
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 }
131 radeonLocalTransform(&c
->Base
, 4, transformations
);
133 debug_program_log(c
, "after native rewrite part 1");
135 c
->Base
.SwizzleCaps
= &r500_swizzle_caps
;
137 struct radeon_program_transformation transformations
[] = {
138 { &radeonTransformALU
, 0 },
139 { &radeonTransformTrigSimple
, 0 }
141 radeonLocalTransform(&c
->Base
, 2, transformations
);
143 debug_program_log(c
, "after native rewrite part 1");
145 c
->Base
.SwizzleCaps
= &r300_swizzle_caps
;
148 /* Run the common transformations too.
149 * Remember, lowering comes last! */
150 struct radeon_program_transformation common_transformations
[] = {
151 { &radeonTransformTEX
, c
},
153 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
155 common_transformations
[0].function
= &radeonTransformALU
;
156 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
161 debug_program_log(c
, "after native rewrite part 2");
163 rc_dataflow_deadcode(&c
->Base
, &dataflow_outputs_mark_use
, c
);
167 debug_program_log(c
, "after deadcode");
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");
174 rc_optimize(&c
->Base
);
176 debug_program_log(c
, "after dataflow optimize");
178 rc_dataflow_swizzles(&c
->Base
);
182 debug_program_log(c
, "after dataflow passes");
184 if (c
->Base
.remove_unused_constants
) {
185 rc_remove_unused_constants(&c
->Base
,
186 &c
->code
->constants_remap_table
);
188 debug_program_log(c
, "after constants cleanup");
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
);
198 debug_program_log(c
, "after register rename");
201 rc_pair_translate(c
);
205 debug_program_log(c
, "after pair translate");
211 debug_program_log(c
, "after pair scheduling");
213 rc_pair_regalloc(c
, c
->Base
.max_temp_regs
);
218 debug_program_log(c
, "after register allocation");
220 if (c
->Base
.is_r500
) {
221 r500BuildFragmentProgramHwCode(c
);
223 r300BuildFragmentProgramHwCode(c
);
226 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
229 if (c
->Base
.is_r500
) {
230 r500FragmentProgramDump(c
->code
);
232 r300FragmentProgramDump(c
->code
);
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
;
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
);