6fca9ad220afbf444a5304b6bf70361fe23ed87b
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_pass2.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 /* Use these to force spilling so that that functionality can be
38 * tested with known-good examples rather than having to construct new
39 * tests.
40 */
41 #define TEST_PAYLOAD_SPILLS 0
42 #define TEST_DST_SPILLS 0
43
44 static void spill_value(struct brw_wm_compile *c,
45 struct brw_wm_value *value);
46
47 static void prealloc_reg(struct brw_wm_compile *c,
48 struct brw_wm_value *value,
49 GLuint reg)
50 {
51 if (value->lastuse) {
52 /* Set nextuse to zero, it will be corrected by
53 * update_register_usage().
54 */
55 c->pass2_grf[reg].value = value;
56 c->pass2_grf[reg].nextuse = 0;
57
58 value->resident = &c->pass2_grf[reg];
59 value->hw_reg = brw_vec8_grf(reg*2, 0);
60
61 if (TEST_PAYLOAD_SPILLS)
62 spill_value(c, value);
63 }
64 }
65
66
67 /* Initialize all the register values. Do the initial setup
68 * calculations for interpolants.
69 */
70 static void init_registers( struct brw_wm_compile *c )
71 {
72 struct brw_context *brw = c->func.brw;
73 GLuint inputs = (brw->vs.prog_data->outputs_written & DO_SETUP_BITS);
74 GLuint nr_interp_regs = 0;
75 GLuint i = 0;
76 GLuint j;
77
78 for (j = 0; j < c->grf_limit; j++)
79 c->pass2_grf[j].nextuse = BRW_WM_MAX_INSN;
80
81 for (j = 0; j < c->key.nr_depth_regs; j++)
82 prealloc_reg(c, &c->payload.depth[j], i++);
83
84 for (j = 0; j < c->nr_creg; j++)
85 prealloc_reg(c, &c->creg[j], i++);
86
87 for (j = 0; j < FRAG_ATTRIB_MAX; j++)
88 if (inputs & (1<<j)) {
89 /* index for vs output and ps input are not the same
90 in shader varying */
91 GLuint index;
92 if (j > FRAG_ATTRIB_VAR0)
93 index = j - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
94 else
95 index = j;
96 nr_interp_regs++;
97 prealloc_reg(c, &c->payload.input_interp[index], i++);
98 }
99
100 assert(nr_interp_regs >= 1);
101
102 c->prog_data.first_curbe_grf = c->key.nr_depth_regs * 2;
103 c->prog_data.urb_read_length = nr_interp_regs * 2;
104 c->prog_data.curb_read_length = c->nr_creg * 2;
105
106 c->max_wm_grf = i * 2;
107 }
108
109
110 /* Update the nextuse value for each register in our file.
111 */
112 static void update_register_usage(struct brw_wm_compile *c,
113 GLuint thisinsn)
114 {
115 GLuint i;
116
117 for (i = 1; i < c->grf_limit; i++) {
118 struct brw_wm_grf *grf = &c->pass2_grf[i];
119
120 /* Only search those which can change:
121 */
122 if (grf->nextuse < thisinsn) {
123 struct brw_wm_ref *ref = grf->value->lastuse;
124
125 /* Has last use of value been passed?
126 */
127 if (ref->insn < thisinsn) {
128 grf->value->resident = 0;
129 grf->value = 0;
130 grf->nextuse = BRW_WM_MAX_INSN;
131 }
132 else {
133 /* Else loop through chain to update:
134 */
135 while (ref->prevuse && ref->prevuse->insn >= thisinsn)
136 ref = ref->prevuse;
137
138 grf->nextuse = ref->insn;
139 }
140 }
141 }
142 }
143
144
145 static void spill_value(struct brw_wm_compile *c,
146 struct brw_wm_value *value)
147 {
148 /* Allocate a spill slot. Note that allocations start from 0x40 -
149 * the first slot is reserved to mean "undef" in brw_wm_emit.c
150 */
151 if (!value->spill_slot) {
152 c->last_scratch += 0x40;
153 value->spill_slot = c->last_scratch;
154 }
155
156 /* The spill will be done in brw_wm_emit.c immediately after the
157 * value is calculated, so we can just take this reg without any
158 * further work.
159 */
160 value->resident->value = NULL;
161 value->resident->nextuse = BRW_WM_MAX_INSN;
162 value->resident = NULL;
163 }
164
165
166
167 /* Search for contiguous region with the most distant nearest
168 * member. Free regs count as very distant.
169 *
170 * TODO: implement spill-to-reg so that we can rearrange discontigous
171 * free regs and then spill the oldest non-free regs in sequence.
172 * This would mean inserting instructions in this pass.
173 */
174 static GLuint search_contiguous_regs(struct brw_wm_compile *c,
175 GLuint nr,
176 GLuint thisinsn)
177 {
178 struct brw_wm_grf *grf = c->pass2_grf;
179 GLuint furthest = 0;
180 GLuint reg = 0;
181 GLuint i, j;
182
183 /* Start search at 1: r0 is special and can't be used or spilled.
184 */
185 for (i = 1; i < c->grf_limit && furthest < BRW_WM_MAX_INSN; i++) {
186 GLuint group_nextuse = BRW_WM_MAX_INSN;
187
188 for (j = 0; j < nr; j++) {
189 if (grf[i+j].nextuse < group_nextuse)
190 group_nextuse = grf[i+j].nextuse;
191 }
192
193 if (group_nextuse > furthest) {
194 furthest = group_nextuse;
195 reg = i;
196 }
197 }
198
199 assert(furthest != thisinsn);
200
201 /* Any non-empty regs will need to be spilled:
202 */
203 for (j = 0; j < nr; j++)
204 if (grf[reg+j].value)
205 spill_value(c, grf[reg+j].value);
206
207 return reg;
208 }
209
210
211 static void alloc_contiguous_dest(struct brw_wm_compile *c,
212 struct brw_wm_value *dst[],
213 GLuint nr,
214 GLuint thisinsn)
215 {
216 GLuint reg = search_contiguous_regs(c, nr, thisinsn);
217 GLuint i;
218
219 for (i = 0; i < nr; i++) {
220 if (!dst[i]) {
221 /* Need to grab a dummy value in TEX case. Don't introduce
222 * it into the tracking scheme.
223 */
224 dst[i] = &c->vreg[c->nr_vreg++];
225 }
226 else {
227 assert(!dst[i]->resident);
228 assert(c->pass2_grf[reg+i].nextuse != thisinsn);
229
230 c->pass2_grf[reg+i].value = dst[i];
231 c->pass2_grf[reg+i].nextuse = thisinsn;
232
233 dst[i]->resident = &c->pass2_grf[reg+i];
234 }
235
236 dst[i]->hw_reg = brw_vec8_grf((reg+i)*2, 0);
237 }
238
239 if ((reg+nr)*2 > c->max_wm_grf)
240 c->max_wm_grf = (reg+nr) * 2;
241 }
242
243
244 static void load_args(struct brw_wm_compile *c,
245 struct brw_wm_instruction *inst)
246 {
247 GLuint thisinsn = inst - c->instruction;
248 GLuint i,j;
249
250 for (i = 0; i < 3; i++) {
251 for (j = 0; j < 4; j++) {
252 struct brw_wm_ref *ref = inst->src[i][j];
253
254 if (ref) {
255 if (!ref->value->resident) {
256 /* Need to bring the value in from scratch space. The code for
257 * this will be done in brw_wm_emit.c, here we just do the
258 * register allocation and mark the ref as requiring a fill.
259 */
260 GLuint reg = search_contiguous_regs(c, 1, thisinsn);
261
262 c->pass2_grf[reg].value = ref->value;
263 c->pass2_grf[reg].nextuse = thisinsn;
264
265 ref->value->resident = &c->pass2_grf[reg];
266
267 /* Note that a fill is required:
268 */
269 ref->unspill_reg = reg*2;
270 }
271
272 /* Adjust the hw_reg to point at the value's current location:
273 */
274 assert(ref->value == ref->value->resident->value);
275 ref->hw_reg.nr += (ref->value->resident - c->pass2_grf) * 2;
276 }
277 }
278 }
279 }
280
281
282
283 /* Step 3: Work forwards once again. Perform register allocations,
284 * taking into account instructions like TEX which require contiguous
285 * result registers. Where necessary spill registers to scratch space
286 * and reload later.
287 */
288 void brw_wm_pass2( struct brw_wm_compile *c )
289 {
290 GLuint insn;
291 GLuint i;
292
293 init_registers(c);
294
295 for (insn = 0; insn < c->nr_insns; insn++) {
296 struct brw_wm_instruction *inst = &c->instruction[insn];
297
298 /* Update registers' nextuse values:
299 */
300 update_register_usage(c, insn);
301
302 /* May need to unspill some args.
303 */
304 load_args(c, inst);
305
306 /* Allocate registers to hold results:
307 */
308 switch (inst->opcode) {
309 case OPCODE_TEX:
310 case OPCODE_TXB:
311 case OPCODE_TXP:
312 alloc_contiguous_dest(c, inst->dst, 4, insn);
313 break;
314
315 default:
316 for (i = 0; i < 4; i++) {
317 if (inst->writemask & (1<<i)) {
318 assert(inst->dst[i]);
319 alloc_contiguous_dest(c, &inst->dst[i], 1, insn);
320 }
321 }
322 break;
323 }
324
325 if (TEST_DST_SPILLS && inst->opcode != WM_PIXELXY)
326 for (i = 0; i < 4; i++)
327 if (inst->dst[i])
328 spill_value(c, inst->dst[i]);
329
330 }
331
332 if (INTEL_DEBUG & DEBUG_WM) {
333 brw_wm_print_program(c, "pass2");
334 }
335
336 c->state = PASS2_DONE;
337
338 if (INTEL_DEBUG & DEBUG_WM) {
339 brw_wm_print_program(c, "pass2/done");
340 }
341 }
342
343
344