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
)
53 GLuint InputsRead
= compiler
->program
->InputsRead
;
55 if (!(InputsRead
& FRAG_BIT_WPOS
)) {
56 compiler
->code
->wpos_attr
= FRAG_ATTRIB_MAX
;
60 static gl_state_index tokens
[STATE_LENGTH
] = {
61 STATE_INTERNAL
, STATE_R300_WINDOW_DIMENSION
, 0, 0, 0
63 struct prog_instruction
*fpi
;
67 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
69 if (!(InputsRead
& (1 << i
))) {
70 InputsRead
&= ~(1 << FRAG_ATTRIB_WPOS
);
72 compiler
->program
->InputsRead
= InputsRead
;
73 compiler
->code
->wpos_attr
= i
;
78 GLuint tempregi
= _mesa_find_free_register(compiler
->program
, PROGRAM_TEMPORARY
);
80 _mesa_insert_instructions(compiler
->program
, 0, 3);
81 fpi
= compiler
->program
->Instructions
;
84 /* perspective divide */
85 fpi
[i
].Opcode
= OPCODE_RCP
;
87 fpi
[i
].DstReg
.File
= PROGRAM_TEMPORARY
;
88 fpi
[i
].DstReg
.Index
= tempregi
;
89 fpi
[i
].DstReg
.WriteMask
= WRITEMASK_W
;
90 fpi
[i
].DstReg
.CondMask
= COND_TR
;
92 fpi
[i
].SrcReg
[0].File
= PROGRAM_INPUT
;
93 fpi
[i
].SrcReg
[0].Index
= compiler
->code
->wpos_attr
;
94 fpi
[i
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
97 fpi
[i
].Opcode
= OPCODE_MUL
;
99 fpi
[i
].DstReg
.File
= PROGRAM_TEMPORARY
;
100 fpi
[i
].DstReg
.Index
= tempregi
;
101 fpi
[i
].DstReg
.WriteMask
= WRITEMASK_XYZ
;
102 fpi
[i
].DstReg
.CondMask
= COND_TR
;
104 fpi
[i
].SrcReg
[0].File
= PROGRAM_INPUT
;
105 fpi
[i
].SrcReg
[0].Index
= compiler
->code
->wpos_attr
;
106 fpi
[i
].SrcReg
[0].Swizzle
= SWIZZLE_XYZW
;
108 fpi
[i
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
109 fpi
[i
].SrcReg
[1].Index
= tempregi
;
110 fpi
[i
].SrcReg
[1].Swizzle
= SWIZZLE_WWWW
;
113 /* viewport transformation */
114 window_index
= _mesa_add_state_reference(compiler
->program
->Parameters
, tokens
);
116 fpi
[i
].Opcode
= OPCODE_MAD
;
118 fpi
[i
].DstReg
.File
= PROGRAM_TEMPORARY
;
119 fpi
[i
].DstReg
.Index
= tempregi
;
120 fpi
[i
].DstReg
.WriteMask
= WRITEMASK_XYZ
;
121 fpi
[i
].DstReg
.CondMask
= COND_TR
;
123 fpi
[i
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
124 fpi
[i
].SrcReg
[0].Index
= tempregi
;
125 fpi
[i
].SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ZERO
);
127 fpi
[i
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
128 fpi
[i
].SrcReg
[1].Index
= window_index
;
129 fpi
[i
].SrcReg
[1].Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ZERO
);
131 fpi
[i
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
132 fpi
[i
].SrcReg
[2].Index
= window_index
;
133 fpi
[i
].SrcReg
[2].Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ZERO
);
136 for (; i
< compiler
->program
->NumInstructions
; ++i
) {
138 for (reg
= 0; reg
< 3; reg
++) {
139 if (fpi
[i
].SrcReg
[reg
].File
== PROGRAM_INPUT
&&
140 fpi
[i
].SrcReg
[reg
].Index
== FRAG_ATTRIB_WPOS
) {
141 fpi
[i
].SrcReg
[reg
].File
= PROGRAM_TEMPORARY
;
142 fpi
[i
].SrcReg
[reg
].Index
= tempregi
;
150 * Rewrite fragment.fogcoord to use a texture coordinate slot.
151 * Note that fogcoord is forced into an X001 pattern, and this enforcement
154 * See also the counterpart rewriting for vertex programs.
156 static void rewriteFog(struct r300_fragment_program_compiler
*compiler
)
158 struct rX00_fragment_program_code
*code
= compiler
->code
;
159 GLuint InputsRead
= compiler
->program
->InputsRead
;
162 if (!(InputsRead
& FRAG_BIT_FOGC
)) {
163 code
->fog_attr
= FRAG_ATTRIB_MAX
;
167 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
169 if (!(InputsRead
& (1 << i
))) {
170 InputsRead
&= ~(1 << FRAG_ATTRIB_FOGC
);
171 InputsRead
|= 1 << i
;
172 compiler
->program
->InputsRead
= InputsRead
;
179 struct prog_instruction
*inst
;
181 inst
= compiler
->program
->Instructions
;
182 while (inst
->Opcode
!= OPCODE_END
) {
183 const int src_regs
= _mesa_num_inst_src_regs(inst
->Opcode
);
184 for (i
= 0; i
< src_regs
; ++i
) {
185 if (inst
->SrcReg
[i
].File
== PROGRAM_INPUT
&& inst
->SrcReg
[i
].Index
== FRAG_ATTRIB_FOGC
) {
186 inst
->SrcReg
[i
].Index
= code
->fog_attr
;
187 inst
->SrcReg
[i
].Swizzle
= combine_swizzles(
188 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
, SWIZZLE_ZERO
, SWIZZLE_ONE
),
189 inst
->SrcReg
[i
].Swizzle
);
198 static void rewrite_depth_out(struct gl_program
*prog
)
200 struct prog_instruction
*inst
;
202 for (inst
= prog
->Instructions
; inst
->Opcode
!= OPCODE_END
; ++inst
) {
203 if (inst
->DstReg
.File
!= PROGRAM_OUTPUT
|| inst
->DstReg
.Index
!= FRAG_RESULT_DEPTH
)
206 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
) {
207 inst
->DstReg
.WriteMask
= WRITEMASK_W
;
209 inst
->DstReg
.WriteMask
= 0;
213 switch (inst
->Opcode
) {
216 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
222 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
223 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
227 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
228 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
229 inst
->SrcReg
[2] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[2]);
232 // Scalar instructions needn't be reswizzled
238 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler
* c
)
242 _mesa_printf("Fragment Program: Initial program:\n");
243 _mesa_print_program(c
->program
);
247 insert_WPOS_trailer(c
);
251 rewrite_depth_out(c
->program
);
253 rc_mesa_to_rc_program(&c
->Base
, c
->program
);
256 struct radeon_program_transformation transformations
[] = {
257 { &r500_transform_TEX
, c
},
258 { &radeonTransformALU
, 0 },
259 { &radeonTransformDeriv
, 0 },
260 { &radeonTransformTrigScale
, 0 }
262 radeonLocalTransform(&c
->Base
, 4, transformations
);
264 struct radeon_program_transformation transformations
[] = {
265 { &r300_transform_TEX
, c
},
266 { &radeonTransformALU
, 0 },
267 { &radeonTransformTrigSimple
, 0 }
269 radeonLocalTransform(&c
->Base
, 3, transformations
);
273 _mesa_printf("Fragment Program: After native rewrite:\n");
274 rc_print_program(&c
->Base
.Program
);
279 struct radeon_nqssadce_descr nqssadce
= {
280 .Init
= &nqssadce_init
,
281 .IsNativeSwizzle
= &r500FPIsNativeSwizzle
,
282 .BuildSwizzle
= &r500FPBuildSwizzle
284 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
286 struct radeon_nqssadce_descr nqssadce
= {
287 .Init
= &nqssadce_init
,
288 .IsNativeSwizzle
= &r300FPIsNativeSwizzle
,
289 .BuildSwizzle
= &r300FPBuildSwizzle
291 radeonNqssaDce(&c
->Base
, &nqssadce
, 0);
295 _mesa_printf("Compiler: after NqSSA-DCE:\n");
296 rc_print_program(&c
->Base
.Program
);
301 r500BuildFragmentProgramHwCode(c
);
303 r300BuildFragmentProgramHwCode(c
);
306 rc_constants_copy(&c
->code
->constants
, &c
->Base
.Program
.Constants
);
310 r500FragmentProgramDump(c
->code
);
312 r300FragmentProgramDump(c
->code
);