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"
25 #include "shader/prog_parameter.h"
26 #include "shader/prog_print.h"
27 #include "shader/prog_statevars.h"
29 #include "radeon_nqssadce.h"
30 #include "radeon_program_alu.h"
31 #include "r300_fragprog.h"
32 #include "r300_fragprog_swizzle.h"
33 #include "r500_fragprog.h"
36 static void nqssadce_init(struct nqssadce_state
* s
)
38 s
->Outputs
[FRAG_RESULT_COLOR
].Sourced
= WRITEMASK_XYZW
;
39 s
->Outputs
[FRAG_RESULT_DEPTH
].Sourced
= WRITEMASK_W
;
43 * Transform the program to support fragment.position.
45 * Introduce a small fragment at the start of the program that will be
46 * the only code that directly reads the FRAG_ATTRIB_WPOS input.
47 * All other code pieces that reference that input will be rewritten
48 * to read from a newly allocated temporary.
51 static void insert_WPOS_trailer(struct r300_fragment_program_compiler
*compiler
)
55 if (!(compiler
->Base
.Program
.InputsRead
& FRAG_BIT_WPOS
)) {
56 compiler
->code
->wpos_attr
= FRAG_ATTRIB_MAX
;
60 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
62 if (!(compiler
->Base
.Program
.InputsRead
& (1 << i
))) {
63 compiler
->code
->wpos_attr
= i
;
68 rc_transform_fragment_wpos(&compiler
->Base
, FRAG_ATTRIB_WPOS
, compiler
->code
->wpos_attr
);
72 * Rewrite fragment.fogcoord to use a texture coordinate slot.
73 * Note that fogcoord is forced into an X001 pattern, and this enforcement
76 * See also the counterpart rewriting for vertex programs.
78 static void rewriteFog(struct r300_fragment_program_compiler
*compiler
)
80 struct rX00_fragment_program_code
*code
= compiler
->code
;
81 struct prog_src_register src
;
84 if (!(compiler
->Base
.Program
.InputsRead
& FRAG_BIT_FOGC
)) {
85 code
->fog_attr
= FRAG_ATTRIB_MAX
;
89 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
91 if (!(compiler
->Base
.Program
.InputsRead
& (1 << i
))) {
98 src
.File
= PROGRAM_INPUT
;
99 src
.Index
= code
->fog_attr
;
100 src
.Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
101 rc_move_input(&compiler
->Base
, FRAG_ATTRIB_FOGC
, src
);
105 static void rewrite_depth_out(struct r300_fragment_program_compiler
* c
)
107 struct rc_instruction
*rci
;
109 for (rci
= c
->Base
.Program
.Instructions
.Next
; rci
!= &c
->Base
.Program
.Instructions
; rci
= rci
->Next
) {
110 struct prog_instruction
* inst
= &rci
->I
;
112 if (inst
->DstReg
.File
!= PROGRAM_OUTPUT
|| inst
->DstReg
.Index
!= FRAG_RESULT_DEPTH
)
115 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
) {
116 inst
->DstReg
.WriteMask
= WRITEMASK_W
;
118 inst
->DstReg
.WriteMask
= 0;
122 switch (inst
->Opcode
) {
125 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
131 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
132 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
136 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
137 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
138 inst
->SrcReg
[2] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[2]);
141 // Scalar instructions needn't be reswizzled
147 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler
* c
)
151 _mesa_printf("Fragment Program: Initial program:\n");
152 _mesa_print_program(c
->program
);
156 rc_mesa_to_rc_program(&c
->Base
, c
->program
);
158 insert_WPOS_trailer(c
);
162 rewrite_depth_out(c
);
165 struct radeon_program_transformation transformations
[] = {
166 { &r500_transform_TEX
, c
},
167 { &radeonTransformALU
, 0 },
168 { &radeonTransformDeriv
, 0 },
169 { &radeonTransformTrigScale
, 0 }
171 radeonLocalTransform(&c
->Base
, 4, transformations
);
173 struct radeon_program_transformation transformations
[] = {
174 { &r300_transform_TEX
, c
},
175 { &radeonTransformALU
, 0 },
176 { &radeonTransformTrigSimple
, 0 }
178 radeonLocalTransform(&c
->Base
, 3, transformations
);
182 _mesa_printf("Fragment Program: After native rewrite:\n");
183 rc_print_program(&c
->Base
.Program
);
188 struct radeon_nqssadce_descr nqssadce
= {
189 .Init
= &nqssadce_init
,
190 .IsNativeSwizzle
= &r500FPIsNativeSwizzle
,
191 .BuildSwizzle
= &r500FPBuildSwizzle
193 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
195 struct radeon_nqssadce_descr nqssadce
= {
196 .Init
= &nqssadce_init
,
197 .IsNativeSwizzle
= &r300FPIsNativeSwizzle
,
198 .BuildSwizzle
= &r300FPBuildSwizzle
200 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
204 _mesa_printf("Compiler: after NqSSA-DCE:\n");
205 rc_print_program(&c
->Base
.Program
);
210 r500BuildFragmentProgramHwCode(c
);
212 r300BuildFragmentProgramHwCode(c
);
215 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
219 r500FragmentProgramDump(c
->code
);
221 r300FragmentProgramDump(c
->code
);