r300/fragprog: Refactor wpos rewrite to use rc_program
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / r3xx_fragprog.c
1 /*
2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "radeon_compiler.h"
24
25 #include "shader/prog_parameter.h"
26 #include "shader/prog_print.h"
27 #include "shader/prog_statevars.h"
28
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"
34
35
36 static void nqssadce_init(struct nqssadce_state* s)
37 {
38 s->Outputs[FRAG_RESULT_COLOR].Sourced = WRITEMASK_XYZW;
39 s->Outputs[FRAG_RESULT_DEPTH].Sourced = WRITEMASK_W;
40 }
41
42 /**
43 * Transform the program to support fragment.position.
44 *
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.
49 *
50 */
51 static void insert_WPOS_trailer(struct r300_fragment_program_compiler *compiler)
52 {
53 int i;
54
55 if (!(compiler->Base.Program.InputsRead & FRAG_BIT_WPOS)) {
56 compiler->code->wpos_attr = FRAG_ATTRIB_MAX;
57 return;
58 }
59
60 for (i = FRAG_ATTRIB_TEX0; i <= FRAG_ATTRIB_TEX7; ++i)
61 {
62 if (!(compiler->Base.Program.InputsRead & (1 << i))) {
63 compiler->code->wpos_attr = i;
64 break;
65 }
66 }
67
68 rc_transform_fragment_wpos(&compiler->Base, FRAG_ATTRIB_WPOS, compiler->code->wpos_attr);
69 }
70
71 /**
72 * Rewrite fragment.fogcoord to use a texture coordinate slot.
73 * Note that fogcoord is forced into an X001 pattern, and this enforcement
74 * is done here.
75 *
76 * See also the counterpart rewriting for vertex programs.
77 */
78 static void rewriteFog(struct r300_fragment_program_compiler *compiler)
79 {
80 struct rX00_fragment_program_code *code = compiler->code;
81 struct prog_src_register src;
82 int i;
83
84 if (!(compiler->Base.Program.InputsRead & FRAG_BIT_FOGC)) {
85 code->fog_attr = FRAG_ATTRIB_MAX;
86 return;
87 }
88
89 for (i = FRAG_ATTRIB_TEX0; i <= FRAG_ATTRIB_TEX7; ++i)
90 {
91 if (!(compiler->Base.Program.InputsRead & (1 << i))) {
92 code->fog_attr = i;
93 break;
94 }
95 }
96
97 reset_srcreg(&src);
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);
102 }
103
104
105 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
106 {
107 struct rc_instruction *rci;
108
109 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
110 struct prog_instruction * inst = &rci->I;
111
112 if (inst->DstReg.File != PROGRAM_OUTPUT || inst->DstReg.Index != FRAG_RESULT_DEPTH)
113 continue;
114
115 if (inst->DstReg.WriteMask & WRITEMASK_Z) {
116 inst->DstReg.WriteMask = WRITEMASK_W;
117 } else {
118 inst->DstReg.WriteMask = 0;
119 continue;
120 }
121
122 switch (inst->Opcode) {
123 case OPCODE_FRC:
124 case OPCODE_MOV:
125 inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
126 break;
127 case OPCODE_ADD:
128 case OPCODE_MAX:
129 case OPCODE_MIN:
130 case OPCODE_MUL:
131 inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
132 inst->SrcReg[1] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[1]);
133 break;
134 case OPCODE_CMP:
135 case OPCODE_MAD:
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]);
139 break;
140 default:
141 // Scalar instructions needn't be reswizzled
142 break;
143 }
144 }
145 }
146
147 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
148 {
149 if (c->Base.Debug) {
150 fflush(stdout);
151 _mesa_printf("Fragment Program: Initial program:\n");
152 _mesa_print_program(c->program);
153 fflush(stdout);
154 }
155
156 rc_mesa_to_rc_program(&c->Base, c->program);
157
158 insert_WPOS_trailer(c);
159
160 rewriteFog(c);
161
162 rewrite_depth_out(c);
163
164 if (c->is_r500) {
165 struct radeon_program_transformation transformations[] = {
166 { &r500_transform_TEX, c },
167 { &radeonTransformALU, 0 },
168 { &radeonTransformDeriv, 0 },
169 { &radeonTransformTrigScale, 0 }
170 };
171 radeonLocalTransform(&c->Base, 4, transformations);
172 } else {
173 struct radeon_program_transformation transformations[] = {
174 { &r300_transform_TEX, c },
175 { &radeonTransformALU, 0 },
176 { &radeonTransformTrigSimple, 0 }
177 };
178 radeonLocalTransform(&c->Base, 3, transformations);
179 }
180
181 if (c->Base.Debug) {
182 _mesa_printf("Fragment Program: After native rewrite:\n");
183 rc_print_program(&c->Base.Program);
184 fflush(stdout);
185 }
186
187 if (c->is_r500) {
188 struct radeon_nqssadce_descr nqssadce = {
189 .Init = &nqssadce_init,
190 .IsNativeSwizzle = &r500FPIsNativeSwizzle,
191 .BuildSwizzle = &r500FPBuildSwizzle
192 };
193 radeonNqssaDce(&c->Base, &nqssadce, 0);
194 } else {
195 struct radeon_nqssadce_descr nqssadce = {
196 .Init = &nqssadce_init,
197 .IsNativeSwizzle = &r300FPIsNativeSwizzle,
198 .BuildSwizzle = &r300FPBuildSwizzle
199 };
200 radeonNqssaDce(&c->Base, &nqssadce, 0);
201 }
202
203 if (c->Base.Debug) {
204 _mesa_printf("Compiler: after NqSSA-DCE:\n");
205 rc_print_program(&c->Base.Program);
206 fflush(stdout);
207 }
208
209 if (c->is_r500) {
210 r500BuildFragmentProgramHwCode(c);
211 } else {
212 r300BuildFragmentProgramHwCode(c);
213 }
214
215 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
216
217 if (c->Base.Debug) {
218 if (c->is_r500) {
219 r500FragmentProgramDump(c->code);
220 } else {
221 r300FragmentProgramDump(c->code);
222 }
223 }
224 }