i965: Remove BRW_WM_LOCK dirty bit, introduced to work around lack of relocs.
[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
36
37 static GLuint get_tracked_mask(struct brw_wm_compile *c,
38 struct brw_wm_instruction *inst)
39 {
40 GLuint i;
41 for (i = 0; i < 4; i++) {
42 if (inst->writemask & (1<<i)) {
43 if (!inst->dst[i]->contributes_to_output) {
44 inst->writemask &= ~(1<<i);
45 inst->dst[i] = 0;
46 }
47 }
48 }
49
50 return inst->writemask;
51 }
52
53 /* Remove a reference from a value's usage chain.
54 */
55 static void unlink_ref(struct brw_wm_ref *ref)
56 {
57 struct brw_wm_value *value = ref->value;
58
59 if (ref == value->lastuse) {
60 value->lastuse = ref->prevuse;
61 } else {
62 struct brw_wm_ref *i = value->lastuse;
63 while (i->prevuse != ref) i = i->prevuse;
64 i->prevuse = ref->prevuse;
65 }
66 }
67
68 static void track_arg(struct brw_wm_compile *c,
69 struct brw_wm_instruction *inst,
70 GLuint arg,
71 GLuint readmask)
72 {
73 GLuint i;
74
75 for (i = 0; i < 4; i++) {
76 struct brw_wm_ref *ref = inst->src[arg][i];
77 if (ref) {
78 if (readmask & (1<<i))
79 ref->value->contributes_to_output = 1;
80 else {
81 unlink_ref(ref);
82 inst->src[arg][i] = NULL;
83 }
84 }
85 }
86 }
87
88 static GLuint get_texcoord_mask( GLuint tex_idx )
89 {
90 switch (tex_idx) {
91 case TEXTURE_1D_INDEX: return WRITEMASK_X;
92 case TEXTURE_2D_INDEX: return WRITEMASK_XY;
93 case TEXTURE_3D_INDEX: return WRITEMASK_XYZ;
94 case TEXTURE_CUBE_INDEX: return WRITEMASK_XYZ;
95 case TEXTURE_RECT_INDEX: return WRITEMASK_XY;
96 default: return 0;
97 }
98 }
99
100 /* Step two: Basically this is dead code elimination.
101 *
102 * Iterate backwards over instructions, noting which values
103 * contribute to the final result. Adjust writemasks to only
104 * calculate these values.
105 */
106 void brw_wm_pass1( struct brw_wm_compile *c )
107 {
108 GLint insn;
109
110 for (insn = c->nr_insns-1; insn >= 0; insn--) {
111 struct brw_wm_instruction *inst = &c->instruction[insn];
112 GLuint writemask;
113 GLuint read0, read1, read2;
114
115 if (inst->opcode == OPCODE_KIL) {
116 track_arg(c, inst, 0, WRITEMASK_XYZW); /* All args contribute to final */
117 continue;
118 }
119
120 if (inst->opcode == WM_FB_WRITE) {
121 track_arg(c, inst, 0, WRITEMASK_XYZW);
122 track_arg(c, inst, 1, WRITEMASK_XYZW);
123 if (c->key.source_depth_to_render_target &&
124 c->key.computes_depth)
125 track_arg(c, inst, 2, WRITEMASK_Z);
126 else
127 track_arg(c, inst, 2, 0);
128 continue;
129 }
130
131 /* Lookup all the registers which were written by this
132 * instruction and get a mask of those that contribute to the output:
133 */
134 writemask = get_tracked_mask(c, inst);
135 if (!writemask) {
136 GLuint arg;
137 for (arg = 0; arg < 3; arg++)
138 track_arg(c, inst, arg, 0);
139 continue;
140 }
141
142 read0 = 0;
143 read1 = 0;
144 read2 = 0;
145
146 /* Mark all inputs which contribute to the marked outputs:
147 */
148 switch (inst->opcode) {
149 case OPCODE_ABS:
150 case OPCODE_FLR:
151 case OPCODE_FRC:
152 case OPCODE_MOV:
153 case OPCODE_SWZ:
154 read0 = writemask;
155 break;
156
157 case OPCODE_SUB:
158 case OPCODE_SLT:
159 case OPCODE_SLE:
160 case OPCODE_SGE:
161 case OPCODE_SGT:
162 case OPCODE_SEQ:
163 case OPCODE_SNE:
164 case OPCODE_ADD:
165 case OPCODE_MAX:
166 case OPCODE_MIN:
167 case OPCODE_MUL:
168 read0 = writemask;
169 read1 = writemask;
170 break;
171
172 case OPCODE_MAD:
173 case OPCODE_CMP:
174 case OPCODE_LRP:
175 read0 = writemask;
176 read1 = writemask;
177 read2 = writemask;
178 break;
179
180 case OPCODE_XPD:
181 if (writemask & WRITEMASK_X) read0 |= WRITEMASK_YZ;
182 if (writemask & WRITEMASK_Y) read0 |= WRITEMASK_XZ;
183 if (writemask & WRITEMASK_Z) read0 |= WRITEMASK_XY;
184 read1 = read0;
185 break;
186
187 case OPCODE_COS:
188 case OPCODE_EX2:
189 case OPCODE_LG2:
190 case OPCODE_RCP:
191 case OPCODE_RSQ:
192 case OPCODE_SIN:
193 case OPCODE_SCS:
194 case WM_CINTERP:
195 case WM_PIXELXY:
196 read0 = WRITEMASK_X;
197 break;
198
199 case OPCODE_POW:
200 read0 = WRITEMASK_X;
201 read1 = WRITEMASK_X;
202 break;
203
204 case OPCODE_TEX:
205 read0 = get_texcoord_mask(inst->tex_idx);
206
207 if (c->key.shadowtex_mask & (1<<inst->tex_unit))
208 read0 |= WRITEMASK_Z;
209 break;
210
211 case OPCODE_TXB:
212 /* Shadow ignored for txb.
213 */
214 read0 = get_texcoord_mask(inst->tex_idx) | WRITEMASK_W;
215 break;
216
217 case WM_WPOSXY:
218 read0 = writemask & WRITEMASK_XY;
219 break;
220
221 case WM_DELTAXY:
222 read0 = writemask & WRITEMASK_XY;
223 read1 = WRITEMASK_X;
224 break;
225
226 case WM_PIXELW:
227 read0 = WRITEMASK_X;
228 read1 = WRITEMASK_XY;
229 break;
230
231 case WM_LINTERP:
232 read0 = WRITEMASK_X;
233 read1 = WRITEMASK_XY;
234 break;
235
236 case WM_PINTERP:
237 read0 = WRITEMASK_X; /* interpolant */
238 read1 = WRITEMASK_XY; /* deltas */
239 read2 = WRITEMASK_W; /* pixel w */
240 break;
241
242 case OPCODE_DP3:
243 read0 = WRITEMASK_XYZ;
244 read1 = WRITEMASK_XYZ;
245 break;
246
247 case OPCODE_DPH:
248 read0 = WRITEMASK_XYZ;
249 read1 = WRITEMASK_XYZW;
250 break;
251
252 case OPCODE_DP4:
253 read0 = WRITEMASK_XYZW;
254 read1 = WRITEMASK_XYZW;
255 break;
256
257 case OPCODE_LIT:
258 read0 = WRITEMASK_XYW;
259 break;
260
261 case OPCODE_DST:
262 case OPCODE_TXP:
263 default:
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