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
)
149 insert_WPOS_trailer(c
);
153 rewrite_depth_out(c
);
156 struct radeon_program_transformation transformations
[] = {
157 { &r500_transform_TEX
, c
},
158 { &radeonTransformALU
, 0 },
159 { &radeonTransformDeriv
, 0 },
160 { &radeonTransformTrigScale
, 0 }
162 radeonLocalTransform(&c
->Base
, 4, transformations
);
164 struct radeon_program_transformation transformations
[] = {
165 { &r300_transform_TEX
, c
},
166 { &radeonTransformALU
, 0 },
167 { &radeonTransformTrigSimple
, 0 }
169 radeonLocalTransform(&c
->Base
, 3, transformations
);
173 _mesa_printf("Fragment Program: After native rewrite:\n");
174 rc_print_program(&c
->Base
.Program
);
179 struct radeon_nqssadce_descr nqssadce
= {
180 .Init
= &nqssadce_init
,
181 .IsNativeSwizzle
= &r500FPIsNativeSwizzle
,
182 .BuildSwizzle
= &r500FPBuildSwizzle
184 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
186 struct radeon_nqssadce_descr nqssadce
= {
187 .Init
= &nqssadce_init
,
188 .IsNativeSwizzle
= &r300FPIsNativeSwizzle
,
189 .BuildSwizzle
= &r300FPBuildSwizzle
191 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
195 _mesa_printf("Compiler: after NqSSA-DCE:\n");
196 rc_print_program(&c
->Base
.Program
);
201 r500BuildFragmentProgramHwCode(c
);
203 r300BuildFragmentProgramHwCode(c
);
206 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
210 r500FragmentProgramDump(c
->code
);
212 r300FragmentProgramDump(c
->code
);