Minor r200 vertex program cleanups. Remove disabled leftovers from r300 vertex progra...
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_pass1.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "brw_context.h"
34 #include "brw_wm.h"
35 #include "program.h"
36 #include "arbprogparse.h"
37 #include "program_instruction.h"
38
39
40 static GLuint get_tracked_mask(struct brw_wm_compile *c,
41 struct brw_wm_instruction *inst)
42 {
43 GLuint i;
44 for (i = 0; i < 4; i++) {
45 if (inst->writemask & (1<<i)) {
46 if (!inst->dst[i]->contributes_to_output) {
47 inst->writemask &= ~(1<<i);
48 inst->dst[i] = 0;
49 }
50 }
51 }
52
53 return inst->writemask;
54 }
55
56 /* Remove a reference from a value's usage chain.
57 */
58 static void unlink_ref(struct brw_wm_ref *ref)
59 {
60 struct brw_wm_value *value = ref->value;
61
62 if (ref == value->lastuse) {
63 value->lastuse = ref->prevuse;
64 } else {
65 struct brw_wm_ref *i = value->lastuse;
66 while (i->prevuse != ref) i = i->prevuse;
67 i->prevuse = ref->prevuse;
68 }
69 }
70
71 static void track_arg(struct brw_wm_compile *c,
72 struct brw_wm_instruction *inst,
73 GLuint arg,
74 GLuint readmask)
75 {
76 GLuint i;
77
78 for (i = 0; i < 4; i++) {
79 struct brw_wm_ref *ref = inst->src[arg][i];
80 if (ref) {
81 if (readmask & (1<<i))
82 ref->value->contributes_to_output = 1;
83 else {
84 unlink_ref(ref);
85 inst->src[arg][i] = NULL;
86 }
87 }
88 }
89 }
90
91 static GLuint get_texcoord_mask( GLuint tex_idx )
92 {
93 switch (tex_idx) {
94 case TEXTURE_1D_INDEX: return WRITEMASK_X;
95 case TEXTURE_2D_INDEX: return WRITEMASK_XY;
96 case TEXTURE_3D_INDEX: return WRITEMASK_XYZ;
97 case TEXTURE_CUBE_INDEX: return WRITEMASK_XYZ;
98 case TEXTURE_RECT_INDEX: return WRITEMASK_XY;
99 default: return 0;
100 }
101 }
102
103 /* Step two: Basically this is dead code elimination.
104 *
105 * Iterate backwards over instructions, noting which values
106 * contribute to the final result. Adjust writemasks to only
107 * calculate these values.
108 */
109 void brw_wm_pass1( struct brw_wm_compile *c )
110 {
111 GLint insn;
112
113 for (insn = c->nr_insns-1; insn >= 0; insn--) {
114 struct brw_wm_instruction *inst = &c->instruction[insn];
115 GLuint writemask;
116 GLuint read0, read1, read2;
117
118 if (inst->opcode == OPCODE_KIL) {
119 track_arg(c, inst, 0, WRITEMASK_XYZW); /* All args contribute to final */
120 continue;
121 }
122
123 if (inst->opcode == WM_FB_WRITE) {
124 track_arg(c, inst, 0, WRITEMASK_XYZW);
125 track_arg(c, inst, 1, WRITEMASK_XYZW);
126 if (c->key.source_depth_to_render_target &&
127 c->key.computes_depth)
128 track_arg(c, inst, 2, WRITEMASK_Z);
129 else
130 track_arg(c, inst, 2, 0);
131 continue;
132 }
133
134 /* Lookup all the registers which were written by this
135 * instruction and get a mask of those that contribute to the output:
136 */
137 writemask = get_tracked_mask(c, inst);
138 if (!writemask) {
139 GLuint arg;
140 for (arg = 0; arg < 3; arg++)
141 track_arg(c, inst, arg, 0);
142 continue;
143 }
144
145 read0 = 0;
146 read1 = 0;
147 read2 = 0;
148
149 /* Mark all inputs which contribute to the marked outputs:
150 */
151 switch (inst->opcode) {
152 case OPCODE_ABS:
153 case OPCODE_FLR:
154 case OPCODE_FRC:
155 case OPCODE_MOV:
156 read0 = writemask;
157 break;
158
159 case OPCODE_SUB:
160 case OPCODE_SLT:
161 case OPCODE_SGE:
162 case OPCODE_ADD:
163 case OPCODE_MAX:
164 case OPCODE_MIN:
165 case OPCODE_MUL:
166 read0 = writemask;
167 read1 = writemask;
168 break;
169
170 case OPCODE_MAD:
171 case OPCODE_CMP:
172 case OPCODE_LRP:
173 read0 = writemask;
174 read1 = writemask;
175 read2 = writemask;
176 break;
177
178 case OPCODE_XPD:
179 if (writemask & WRITEMASK_X) read0 |= WRITEMASK_YZ;
180 if (writemask & WRITEMASK_Y) read0 |= WRITEMASK_XZ;
181 if (writemask & WRITEMASK_Z) read0 |= WRITEMASK_XY;
182 read1 = read0;
183 break;
184
185 case OPCODE_COS:
186 case OPCODE_EX2:
187 case OPCODE_LG2:
188 case OPCODE_RCP:
189 case OPCODE_RSQ:
190 case OPCODE_SIN:
191 case OPCODE_SCS:
192 case WM_CINTERP:
193 case WM_PIXELXY:
194 read0 = WRITEMASK_X;
195 break;
196
197 case OPCODE_POW:
198 read0 = WRITEMASK_X;
199 read1 = WRITEMASK_X;
200 break;
201
202 case OPCODE_TEX:
203 read0 = get_texcoord_mask(inst->tex_idx);
204
205 if (c->key.shadowtex_mask & (1<<inst->tex_unit))
206 read0 |= WRITEMASK_Z;
207 break;
208
209 case OPCODE_TXB:
210 /* Shadow ignored for txb.
211 */
212 read0 = get_texcoord_mask(inst->tex_idx) | WRITEMASK_W;
213 break;
214
215 case WM_WPOSXY:
216 read0 = writemask & WRITEMASK_XY;
217 break;
218
219 case WM_DELTAXY:
220 read0 = writemask & WRITEMASK_XY;
221 read1 = WRITEMASK_X;
222 break;
223
224 case WM_PIXELW:
225 read0 = WRITEMASK_X;
226 read1 = WRITEMASK_XY;
227 break;
228
229 case WM_LINTERP:
230 read0 = WRITEMASK_X;
231 read1 = WRITEMASK_XY;
232 break;
233
234 case WM_PINTERP:
235 read0 = WRITEMASK_X; /* interpolant */
236 read1 = WRITEMASK_XY; /* deltas */
237 read2 = WRITEMASK_W; /* pixel w */
238 break;
239
240 case OPCODE_DP3:
241 read0 = WRITEMASK_XYZ;
242 read1 = WRITEMASK_XYZ;
243 break;
244
245 case OPCODE_DPH:
246 read0 = WRITEMASK_XYZ;
247 read1 = WRITEMASK_XYZW;
248 break;
249
250 case OPCODE_DP4:
251 read0 = WRITEMASK_XYZW;
252 read1 = WRITEMASK_XYZW;
253 break;
254
255 case OPCODE_LIT:
256 read0 = WRITEMASK_XYW;
257 break;
258
259 case OPCODE_SWZ:
260 case OPCODE_DST:
261 case OPCODE_TXP:
262 default:
263 assert(0);
264 break;
265 }
266
267 track_arg(c, inst, 0, read0);
268 track_arg(c, inst, 1, read1);
269 track_arg(c, inst, 2, read2);
270 }
271
272 if (INTEL_DEBUG & DEBUG_WM) {
273 brw_wm_print_program(c, "pass1");
274 }
275 }
276
277
278