r300/fragprog: Finally get rid of the duplicate program copy
[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 insert_WPOS_trailer(c);
150
151 rewriteFog(c);
152
153 rewrite_depth_out(c);
154
155 if (c->is_r500) {
156 struct radeon_program_transformation transformations[] = {
157 { &r500_transform_TEX, c },
158 { &radeonTransformALU, 0 },
159 { &radeonTransformDeriv, 0 },
160 { &radeonTransformTrigScale, 0 }
161 };
162 radeonLocalTransform(&c->Base, 4, transformations);
163 } else {
164 struct radeon_program_transformation transformations[] = {
165 { &r300_transform_TEX, c },
166 { &radeonTransformALU, 0 },
167 { &radeonTransformTrigSimple, 0 }
168 };
169 radeonLocalTransform(&c->Base, 3, transformations);
170 }
171
172 if (c->Base.Debug) {
173 _mesa_printf("Fragment Program: After native rewrite:\n");
174 rc_print_program(&c->Base.Program);
175 fflush(stdout);
176 }
177
178 if (c->is_r500) {
179 struct radeon_nqssadce_descr nqssadce = {
180 .Init = &nqssadce_init,
181 .IsNativeSwizzle = &r500FPIsNativeSwizzle,
182 .BuildSwizzle = &r500FPBuildSwizzle
183 };
184 radeonNqssaDce(&c->Base, &nqssadce, 0);
185 } else {
186 struct radeon_nqssadce_descr nqssadce = {
187 .Init = &nqssadce_init,
188 .IsNativeSwizzle = &r300FPIsNativeSwizzle,
189 .BuildSwizzle = &r300FPBuildSwizzle
190 };
191 radeonNqssaDce(&c->Base, &nqssadce, 0);
192 }
193
194 if (c->Base.Debug) {
195 _mesa_printf("Compiler: after NqSSA-DCE:\n");
196 rc_print_program(&c->Base.Program);
197 fflush(stdout);
198 }
199
200 if (c->is_r500) {
201 r500BuildFragmentProgramHwCode(c);
202 } else {
203 r300BuildFragmentProgramHwCode(c);
204 }
205
206 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
207
208 if (c->Base.Debug) {
209 if (c->is_r500) {
210 r500FragmentProgramDump(c->code);
211 } else {
212 r300FragmentProgramDump(c->code);
213 }
214 }
215 }