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_program_alu.h"
30 #include "radeon_program_tex.h"
31 #include "r300_fragprog.h"
32 #include "r300_fragprog_swizzle.h"
33 #include "r500_fragprog.h"
36 static void dataflow_outputs_mark_use(void * userdata
, void * data
,
37 void (*callback
)(void *, unsigned int, unsigned int))
39 struct r300_fragment_program_compiler
* c
= userdata
;
40 callback(data
, c
->OutputColor
[0], RC_MASK_XYZW
);
41 callback(data
, c
->OutputColor
[1], RC_MASK_XYZW
);
42 callback(data
, c
->OutputColor
[2], RC_MASK_XYZW
);
43 callback(data
, c
->OutputColor
[3], RC_MASK_XYZW
);
44 callback(data
, c
->OutputDepth
, RC_MASK_W
);
47 static void rewrite_depth_out(struct r300_fragment_program_compiler
* c
)
49 struct rc_instruction
*rci
;
51 for (rci
= c
->Base
.Program
.Instructions
.Next
; rci
!= &c
->Base
.Program
.Instructions
; rci
= rci
->Next
) {
52 struct rc_sub_instruction
* inst
= &rci
->U
.I
;
54 if (inst
->DstReg
.File
!= RC_FILE_OUTPUT
|| inst
->DstReg
.Index
!= c
->OutputDepth
)
57 if (inst
->DstReg
.WriteMask
& RC_MASK_Z
) {
58 inst
->DstReg
.WriteMask
= RC_MASK_W
;
60 inst
->DstReg
.WriteMask
= 0;
64 switch (inst
->Opcode
) {
67 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
73 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
74 inst
->SrcReg
[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
78 inst
->SrcReg
[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
79 inst
->SrcReg
[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
80 inst
->SrcReg
[2] = lmul_swizzle(RC_SWIZZLE_ZZZZ
, inst
->SrcReg
[2]);
83 // Scalar instructions needn't be reswizzled
89 static void debug_program_log(struct r300_fragment_program_compiler
* c
, const char * where
)
92 fprintf(stderr
, "Fragment Program: %s\n", where
);
93 rc_print_program(&c
->Base
.Program
);
97 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler
* c
)
102 struct radeon_program_transformation transformations
[] = {
103 { &r500_transform_IF
, 0 },
104 { &radeonTransformALU
, 0 },
105 { &radeonTransformDeriv
, 0 },
106 { &radeonTransformTrigScale
, 0 }
108 radeonLocalTransform(&c
->Base
, 4, transformations
);
110 c
->Base
.SwizzleCaps
= &r500_swizzle_caps
;
112 struct radeon_program_transformation transformations
[] = {
113 { &radeonTransformALU
, 0 },
114 { &radeonTransformTrigSimple
, 0 }
116 radeonLocalTransform(&c
->Base
, 2, transformations
);
118 debug_program_log(c
, "before emulate branches");
120 rc_emulate_branches(&c
->Base
);
122 c
->Base
.SwizzleCaps
= &r300_swizzle_caps
;
125 /* Run the common transformations too.
126 * Remember, lowering comes last! */
127 struct radeon_program_transformation common_transformations
[] = {
128 { &radeonTransformTEX
, c
},
130 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
132 common_transformations
[0].function
= &radeonTransformALU
;
133 radeonLocalTransform(&c
->Base
, 1, common_transformations
);
138 debug_program_log(c
, "after native rewrite");
140 rc_dataflow_deadcode(&c
->Base
, &dataflow_outputs_mark_use
, c
);
144 debug_program_log(c
, "after deadcode");
146 rc_dataflow_swizzles(&c
->Base
);
150 debug_program_log(c
, "after dataflow passes");
152 rc_pair_translate(c
);
156 debug_program_log(c
, "after pair translate");
162 debug_program_log(c
, "after pair scheduling");
164 rc_pair_regalloc(c
, c
->max_temp_regs
);
169 debug_program_log(c
, "after register allocation");
172 r500BuildFragmentProgramHwCode(c
);
174 r300BuildFragmentProgramHwCode(c
);
177 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
181 r500FragmentProgramDump(c
->code
);
183 r300FragmentProgramDump(c
->code
);