i965: Share the KIL_NV implementation between glsl and non-glsl.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_glsl.c
1 #include "main/macros.h"
2 #include "program/prog_parameter.h"
3 #include "program/prog_print.h"
4 #include "program/prog_optimize.h"
5 #include "brw_context.h"
6 #include "brw_eu.h"
7 #include "brw_wm.h"
8
9 static struct brw_reg get_dst_reg(struct brw_wm_compile *c,
10 const struct prog_instruction *inst,
11 GLuint component);
12
13 /**
14 * Determine if the given fragment program uses GLSL features such
15 * as flow conditionals, loops, subroutines.
16 * Some GLSL shaders may use these features, others might not.
17 */
18 GLboolean brw_wm_is_glsl(const struct gl_fragment_program *fp)
19 {
20 int i;
21
22 if (INTEL_DEBUG & DEBUG_GLSL_FORCE)
23 return GL_TRUE;
24
25 for (i = 0; i < fp->Base.NumInstructions; i++) {
26 const struct prog_instruction *inst = &fp->Base.Instructions[i];
27 switch (inst->Opcode) {
28 case OPCODE_ARL:
29 case OPCODE_IF:
30 case OPCODE_ENDIF:
31 case OPCODE_CAL:
32 case OPCODE_BRK:
33 case OPCODE_RET:
34 case OPCODE_BGNLOOP:
35 return GL_TRUE;
36 default:
37 break;
38 }
39 }
40 return GL_FALSE;
41 }
42
43
44
45 static void
46 reclaim_temps(struct brw_wm_compile *c);
47
48
49 /** Mark GRF register as used. */
50 static void
51 prealloc_grf(struct brw_wm_compile *c, int r)
52 {
53 c->used_grf[r] = GL_TRUE;
54 }
55
56
57 /** Mark given GRF register as not in use. */
58 static void
59 release_grf(struct brw_wm_compile *c, int r)
60 {
61 /*assert(c->used_grf[r]);*/
62 c->used_grf[r] = GL_FALSE;
63 c->first_free_grf = MIN2(c->first_free_grf, r);
64 }
65
66
67 /** Return index of a free GRF, mark it as used. */
68 static int
69 alloc_grf(struct brw_wm_compile *c)
70 {
71 GLuint r;
72 for (r = c->first_free_grf; r < BRW_WM_MAX_GRF; r++) {
73 if (!c->used_grf[r]) {
74 c->used_grf[r] = GL_TRUE;
75 c->first_free_grf = r + 1; /* a guess */
76 return r;
77 }
78 }
79
80 /* no free temps, try to reclaim some */
81 reclaim_temps(c);
82 c->first_free_grf = 0;
83
84 /* try alloc again */
85 for (r = c->first_free_grf; r < BRW_WM_MAX_GRF; r++) {
86 if (!c->used_grf[r]) {
87 c->used_grf[r] = GL_TRUE;
88 c->first_free_grf = r + 1; /* a guess */
89 return r;
90 }
91 }
92
93 for (r = 0; r < BRW_WM_MAX_GRF; r++) {
94 assert(c->used_grf[r]);
95 }
96
97 /* really, no free GRF regs found */
98 if (!c->out_of_regs) {
99 /* print warning once per compilation */
100 _mesa_warning(NULL, "i965: ran out of registers for fragment program");
101 c->out_of_regs = GL_TRUE;
102 }
103
104 return -1;
105 }
106
107
108 /** Return number of GRF registers used */
109 static int
110 num_grf_used(const struct brw_wm_compile *c)
111 {
112 int r;
113 for (r = BRW_WM_MAX_GRF - 1; r >= 0; r--)
114 if (c->used_grf[r])
115 return r + 1;
116 return 0;
117 }
118
119
120
121 /**
122 * Record the mapping of a Mesa register to a hardware register.
123 */
124 static void set_reg(struct brw_wm_compile *c, int file, int index,
125 int component, struct brw_reg reg)
126 {
127 c->wm_regs[file][index][component].reg = reg;
128 c->wm_regs[file][index][component].inited = GL_TRUE;
129 }
130
131 static struct brw_reg alloc_tmp(struct brw_wm_compile *c)
132 {
133 struct brw_reg reg;
134
135 /* if we need to allocate another temp, grow the tmp_regs[] array */
136 if (c->tmp_index == c->tmp_max) {
137 int r = alloc_grf(c);
138 if (r < 0) {
139 /*printf("Out of temps in %s\n", __FUNCTION__);*/
140 r = 50; /* XXX random register! */
141 }
142 c->tmp_regs[ c->tmp_max++ ] = r;
143 }
144
145 /* form the GRF register */
146 reg = brw_vec8_grf(c->tmp_regs[ c->tmp_index++ ], 0);
147 /*printf("alloc_temp %d\n", reg.nr);*/
148 assert(reg.nr < BRW_WM_MAX_GRF);
149 return reg;
150
151 }
152
153 /**
154 * Save current temp register info.
155 * There must be a matching call to release_tmps().
156 */
157 static int mark_tmps(struct brw_wm_compile *c)
158 {
159 return c->tmp_index;
160 }
161
162 static void release_tmps(struct brw_wm_compile *c, int mark)
163 {
164 c->tmp_index = mark;
165 }
166
167 /**
168 * Convert Mesa src register to brw register.
169 *
170 * Since we're running in SOA mode each Mesa register corresponds to four
171 * hardware registers. We allocate the hardware registers as needed here.
172 *
173 * \param file register file, one of PROGRAM_x
174 * \param index register number
175 * \param component src component (X=0, Y=1, Z=2, W=3)
176 * \param nr not used?!?
177 * \param neg negate value?
178 * \param abs take absolute value?
179 */
180 static struct brw_reg
181 get_reg(struct brw_wm_compile *c, int file, int index, int component,
182 int nr, GLuint neg, GLuint abs)
183 {
184 struct brw_reg reg;
185 switch (file) {
186 case PROGRAM_STATE_VAR:
187 case PROGRAM_CONSTANT:
188 case PROGRAM_UNIFORM:
189 file = PROGRAM_STATE_VAR;
190 break;
191 case PROGRAM_UNDEFINED:
192 return brw_null_reg();
193 case PROGRAM_TEMPORARY:
194 case PROGRAM_INPUT:
195 case PROGRAM_OUTPUT:
196 case PROGRAM_PAYLOAD:
197 break;
198 default:
199 _mesa_problem(NULL, "Unexpected file in get_reg()");
200 return brw_null_reg();
201 }
202
203 assert(index < 256);
204 assert(component < 4);
205
206 /* see if we've already allocated a HW register for this Mesa register */
207 if (c->wm_regs[file][index][component].inited) {
208 /* yes, re-use */
209 reg = c->wm_regs[file][index][component].reg;
210 }
211 else {
212 /* no, allocate new register */
213 int grf = alloc_grf(c);
214 /*printf("alloc grf %d for reg %d:%d.%d\n", grf, file, index, component);*/
215 if (grf < 0) {
216 /* totally out of temps */
217 grf = 51; /* XXX random register! */
218 }
219
220 reg = brw_vec8_grf(grf, 0);
221 /*printf("Alloc new grf %d for %d.%d\n", reg.nr, index, component);*/
222
223 set_reg(c, file, index, component, reg);
224 }
225
226 if (neg & (1 << component)) {
227 reg = negate(reg);
228 }
229 if (abs)
230 reg = brw_abs(reg);
231 return reg;
232 }
233
234
235
236 /**
237 * This is called if we run out of GRF registers. Examine the live intervals
238 * of temp regs in the program and free those which won't be used again.
239 */
240 static void
241 reclaim_temps(struct brw_wm_compile *c)
242 {
243 GLint intBegin[MAX_PROGRAM_TEMPS];
244 GLint intEnd[MAX_PROGRAM_TEMPS];
245 int index;
246
247 /*printf("Reclaim temps:\n");*/
248
249 _mesa_find_temp_intervals(c->prog_instructions, c->nr_fp_insns,
250 intBegin, intEnd);
251
252 for (index = 0; index < MAX_PROGRAM_TEMPS; index++) {
253 if (intEnd[index] != -1 && intEnd[index] < c->cur_inst) {
254 /* program temp[i] can be freed */
255 int component;
256 /*printf(" temp[%d] is dead\n", index);*/
257 for (component = 0; component < 4; component++) {
258 if (c->wm_regs[PROGRAM_TEMPORARY][index][component].inited) {
259 int r = c->wm_regs[PROGRAM_TEMPORARY][index][component].reg.nr;
260 release_grf(c, r);
261 /*
262 printf(" Reclaim temp %d, reg %d at inst %d\n",
263 index, r, c->cur_inst);
264 */
265 c->wm_regs[PROGRAM_TEMPORARY][index][component].inited = GL_FALSE;
266 }
267 }
268 }
269 }
270 }
271
272
273
274
275 /**
276 * Preallocate registers. This sets up the Mesa to hardware register
277 * mapping for certain registers, such as constants (uniforms/state vars)
278 * and shader inputs.
279 */
280 static void prealloc_reg(struct brw_wm_compile *c)
281 {
282 struct intel_context *intel = &c->func.brw->intel;
283 int i, j;
284 struct brw_reg reg;
285 int urb_read_length = 0;
286 GLuint inputs = FRAG_BIT_WPOS | c->fp_interp_emitted;
287 GLuint reg_index = 0;
288
289 memset(c->used_grf, GL_FALSE, sizeof(c->used_grf));
290 c->first_free_grf = 0;
291
292 for (i = 0; i < 4; i++) {
293 if (i < (c->key.nr_payload_regs + 1) / 2)
294 reg = brw_vec8_grf(i * 2, 0);
295 else
296 reg = brw_vec8_grf(0, 0);
297 set_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH, i, reg);
298 }
299 reg_index += c->key.nr_payload_regs;
300
301 /* constants */
302 {
303 const GLuint nr_params = c->fp->program.Base.Parameters->NumParameters;
304 const GLuint nr_temps = c->fp->program.Base.NumTemporaries;
305
306 /* use a real constant buffer, or just use a section of the GRF? */
307 /* XXX this heuristic may need adjustment... */
308 if ((nr_params + nr_temps) * 4 + reg_index > 80)
309 c->fp->use_const_buffer = GL_TRUE;
310 else
311 c->fp->use_const_buffer = GL_FALSE;
312 /*printf("WM use_const_buffer = %d\n", c->fp->use_const_buffer);*/
313
314 if (c->fp->use_const_buffer) {
315 /* We'll use a real constant buffer and fetch constants from
316 * it with a dataport read message.
317 */
318
319 /* number of float constants in CURBE */
320 c->prog_data.nr_params = 0;
321 }
322 else {
323 const struct gl_program_parameter_list *plist =
324 c->fp->program.Base.Parameters;
325 int index = 0;
326
327 /* number of float constants in CURBE */
328 c->prog_data.nr_params = 4 * nr_params;
329
330 /* loop over program constants (float[4]) */
331 for (i = 0; i < nr_params; i++) {
332 /* loop over XYZW channels */
333 for (j = 0; j < 4; j++, index++) {
334 reg = brw_vec1_grf(reg_index + index / 8, index % 8);
335 /* Save pointer to parameter/constant value.
336 * Constants will be copied in prepare_constant_buffer()
337 */
338 c->prog_data.param[index] = &plist->ParameterValues[i][j];
339 set_reg(c, PROGRAM_STATE_VAR, i, j, reg);
340 }
341 }
342 /* number of constant regs used (each reg is float[8]) */
343 c->nr_creg = 2 * ((4 * nr_params + 15) / 16);
344 reg_index += c->nr_creg;
345 }
346 }
347
348 /* fragment shader inputs */
349 for (i = 0; i < VERT_RESULT_MAX; i++) {
350 int fp_input;
351
352 if (i >= VERT_RESULT_VAR0)
353 fp_input = i - VERT_RESULT_VAR0 + FRAG_ATTRIB_VAR0;
354 else if (i <= VERT_RESULT_TEX7)
355 fp_input = i;
356 else
357 fp_input = -1;
358
359 if (fp_input >= 0 && inputs & (1 << fp_input)) {
360 urb_read_length = reg_index;
361 reg = brw_vec8_grf(reg_index, 0);
362 for (j = 0; j < 4; j++)
363 set_reg(c, PROGRAM_PAYLOAD, fp_input, j, reg);
364 }
365 if (c->key.vp_outputs_written & BITFIELD64_BIT(i)) {
366 reg_index += 2;
367 }
368 }
369
370 c->prog_data.first_curbe_grf = c->key.nr_payload_regs;
371 c->prog_data.urb_read_length = urb_read_length;
372 c->prog_data.curb_read_length = c->nr_creg;
373 c->emit_mask_reg = brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, reg_index, 0);
374 reg_index++;
375 c->stack = brw_uw16_reg(BRW_GENERAL_REGISTER_FILE, reg_index, 0);
376 reg_index += 2;
377
378 /* mark GRF regs [0..reg_index-1] as in-use */
379 for (i = 0; i < reg_index; i++)
380 prealloc_grf(c, i);
381
382 /* Don't use GRF 126, 127. Using them seems to lead to GPU lock-ups */
383 prealloc_grf(c, 126);
384 prealloc_grf(c, 127);
385
386 for (i = 0; i < c->nr_fp_insns; i++) {
387 const struct prog_instruction *inst = &c->prog_instructions[i];
388 struct brw_reg dst[4];
389
390 switch (inst->Opcode) {
391 case OPCODE_TEX:
392 case OPCODE_TXB:
393 /* Allocate the channels of texture results contiguously,
394 * since they are written out that way by the sampler unit.
395 */
396 for (j = 0; j < 4; j++) {
397 dst[j] = get_dst_reg(c, inst, j);
398 if (j != 0)
399 assert(dst[j].nr == dst[j - 1].nr + 1);
400 }
401 break;
402 default:
403 break;
404 }
405 }
406
407 for (i = 0; i < c->nr_fp_insns; i++) {
408 const struct prog_instruction *inst = &c->prog_instructions[i];
409
410 switch (inst->Opcode) {
411 case WM_DELTAXY:
412 /* Allocate WM_DELTAXY destination on G45/GM45 to an
413 * even-numbered GRF if possible so that we can use the PLN
414 * instruction.
415 */
416 if (inst->DstReg.WriteMask == WRITEMASK_XY &&
417 !c->wm_regs[inst->DstReg.File][inst->DstReg.Index][0].inited &&
418 !c->wm_regs[inst->DstReg.File][inst->DstReg.Index][1].inited &&
419 (IS_G4X(intel->intelScreen->deviceID) || intel->gen == 5)) {
420 int grf;
421
422 for (grf = c->first_free_grf & ~1;
423 grf < BRW_WM_MAX_GRF;
424 grf += 2)
425 {
426 if (!c->used_grf[grf] && !c->used_grf[grf + 1]) {
427 c->used_grf[grf] = GL_TRUE;
428 c->used_grf[grf + 1] = GL_TRUE;
429 c->first_free_grf = grf + 2; /* a guess */
430
431 set_reg(c, inst->DstReg.File, inst->DstReg.Index, 0,
432 brw_vec8_grf(grf, 0));
433 set_reg(c, inst->DstReg.File, inst->DstReg.Index, 1,
434 brw_vec8_grf(grf + 1, 0));
435 break;
436 }
437 }
438 }
439 default:
440 break;
441 }
442 }
443
444 /* An instruction may reference up to three constants.
445 * They'll be found in these registers.
446 * XXX alloc these on demand!
447 */
448 if (c->fp->use_const_buffer) {
449 for (i = 0; i < 3; i++) {
450 c->current_const[i].index = -1;
451 c->current_const[i].reg = brw_vec8_grf(alloc_grf(c), 0);
452 }
453 }
454 #if 0
455 printf("USE CONST BUFFER? %d\n", c->fp->use_const_buffer);
456 printf("AFTER PRE_ALLOC, reg_index = %d\n", reg_index);
457 #endif
458 }
459
460
461 /**
462 * Check if any of the instruction's src registers are constants, uniforms,
463 * or statevars. If so, fetch any constants that we don't already have in
464 * the three GRF slots.
465 */
466 static void fetch_constants(struct brw_wm_compile *c,
467 const struct prog_instruction *inst)
468 {
469 struct brw_compile *p = &c->func;
470 GLuint i;
471
472 /* loop over instruction src regs */
473 for (i = 0; i < 3; i++) {
474 const struct prog_src_register *src = &inst->SrcReg[i];
475 if (src->File == PROGRAM_STATE_VAR ||
476 src->File == PROGRAM_CONSTANT ||
477 src->File == PROGRAM_UNIFORM) {
478 c->current_const[i].index = src->Index;
479
480 #if 0
481 printf(" fetch const[%d] for arg %d into reg %d\n",
482 src->Index, i, c->current_const[i].reg.nr);
483 #endif
484
485 /* need to fetch the constant now */
486 brw_dp_READ_4(p,
487 c->current_const[i].reg, /* writeback dest */
488 src->RelAddr, /* relative indexing? */
489 16 * src->Index, /* byte offset */
490 SURF_INDEX_FRAG_CONST_BUFFER/* binding table index */
491 );
492 }
493 }
494 }
495
496
497 /**
498 * Convert Mesa dst register to brw register.
499 */
500 static struct brw_reg get_dst_reg(struct brw_wm_compile *c,
501 const struct prog_instruction *inst,
502 GLuint component)
503 {
504 const int nr = 1;
505 return get_reg(c, inst->DstReg.File, inst->DstReg.Index, component, nr,
506 0, 0);
507 }
508
509
510 static struct brw_reg
511 get_src_reg_const(struct brw_wm_compile *c,
512 const struct prog_instruction *inst,
513 GLuint srcRegIndex, GLuint component)
514 {
515 /* We should have already fetched the constant from the constant
516 * buffer in fetch_constants(). Now we just have to return a
517 * register description that extracts the needed component and
518 * smears it across all eight vector components.
519 */
520 const struct prog_src_register *src = &inst->SrcReg[srcRegIndex];
521 struct brw_reg const_reg;
522
523 assert(component < 4);
524 assert(srcRegIndex < 3);
525 assert(c->current_const[srcRegIndex].index != -1);
526 const_reg = c->current_const[srcRegIndex].reg;
527
528 /* extract desired float from the const_reg, and smear */
529 const_reg = stride(const_reg, 0, 1, 0);
530 const_reg.subnr = component * 4;
531
532 if (src->Negate & (1 << component))
533 const_reg = negate(const_reg);
534 if (src->Abs)
535 const_reg = brw_abs(const_reg);
536
537 #if 0
538 printf(" form const[%d].%d for arg %d, reg %d\n",
539 c->current_const[srcRegIndex].index,
540 component,
541 srcRegIndex,
542 const_reg.nr);
543 #endif
544
545 return const_reg;
546 }
547
548
549 /**
550 * Convert Mesa src register to brw register.
551 */
552 static struct brw_reg get_src_reg(struct brw_wm_compile *c,
553 const struct prog_instruction *inst,
554 GLuint srcRegIndex, GLuint channel)
555 {
556 const struct prog_src_register *src = &inst->SrcReg[srcRegIndex];
557 const GLuint nr = 1;
558 const GLuint component = GET_SWZ(src->Swizzle, channel);
559
560 /* Only one immediate value can be used per native opcode, and it
561 * has be in the src1 slot, so not all Mesa instructions will get
562 * to take advantage of immediate constants.
563 */
564 if (brw_wm_arg_can_be_immediate(inst->Opcode, srcRegIndex)) {
565 const struct gl_program_parameter_list *params;
566
567 params = c->fp->program.Base.Parameters;
568
569 /* Extended swizzle terms */
570 if (component == SWIZZLE_ZERO) {
571 return brw_imm_f(0.0F);
572 } else if (component == SWIZZLE_ONE) {
573 if (src->Negate)
574 return brw_imm_f(-1.0F);
575 else
576 return brw_imm_f(1.0F);
577 }
578
579 if (src->File == PROGRAM_CONSTANT) {
580 float f = params->ParameterValues[src->Index][component];
581
582 if (src->Abs)
583 f = fabs(f);
584 if (src->Negate)
585 f = -f;
586
587 return brw_imm_f(f);
588 }
589 }
590
591 if (c->fp->use_const_buffer &&
592 (src->File == PROGRAM_STATE_VAR ||
593 src->File == PROGRAM_CONSTANT ||
594 src->File == PROGRAM_UNIFORM)) {
595 return get_src_reg_const(c, inst, srcRegIndex, component);
596 }
597 else {
598 /* other type of source register */
599 return get_reg(c, src->File, src->Index, component, nr,
600 src->Negate, src->Abs);
601 }
602 }
603
604 static void emit_arl(struct brw_wm_compile *c,
605 const struct prog_instruction *inst)
606 {
607 struct brw_compile *p = &c->func;
608 struct brw_reg src0, addr_reg;
609 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
610 addr_reg = brw_uw8_reg(BRW_ARCHITECTURE_REGISTER_FILE,
611 BRW_ARF_ADDRESS, 0);
612 src0 = get_src_reg(c, inst, 0, 0); /* channel 0 */
613 brw_MOV(p, addr_reg, src0);
614 brw_set_saturate(p, 0);
615 }
616
617 static INLINE struct brw_reg high_words( struct brw_reg reg )
618 {
619 return stride( suboffset( retype( reg, BRW_REGISTER_TYPE_W ), 1 ),
620 0, 8, 2 );
621 }
622
623 static INLINE struct brw_reg low_words( struct brw_reg reg )
624 {
625 return stride( retype( reg, BRW_REGISTER_TYPE_W ), 0, 8, 2 );
626 }
627
628 static INLINE struct brw_reg even_bytes( struct brw_reg reg )
629 {
630 return stride( retype( reg, BRW_REGISTER_TYPE_B ), 0, 16, 2 );
631 }
632
633 static INLINE struct brw_reg odd_bytes( struct brw_reg reg )
634 {
635 return stride( suboffset( retype( reg, BRW_REGISTER_TYPE_B ), 1 ),
636 0, 16, 2 );
637 }
638
639 /**
640 * Resolve subroutine calls after code emit is done.
641 */
642 static void post_wm_emit( struct brw_wm_compile *c )
643 {
644 brw_resolve_cals(&c->func);
645 }
646
647 static void
648 get_argument_regs(struct brw_wm_compile *c,
649 const struct prog_instruction *inst,
650 int index,
651 struct brw_reg *dst,
652 struct brw_reg *regs,
653 int mask)
654 {
655 struct brw_compile *p = &c->func;
656 int i, j;
657
658 for (i = 0; i < 4; i++) {
659 if (mask & (1 << i)) {
660 regs[i] = get_src_reg(c, inst, index, i);
661
662 /* Unalias destination registers from our sources. */
663 if (regs[i].file == BRW_GENERAL_REGISTER_FILE) {
664 for (j = 0; j < 4; j++) {
665 if (memcmp(&regs[i], &dst[j], sizeof(regs[0])) == 0) {
666 struct brw_reg tmp = alloc_tmp(c);
667 brw_MOV(p, tmp, regs[i]);
668 regs[i] = tmp;
669 break;
670 }
671 }
672 }
673 }
674 }
675 }
676
677 static void brw_wm_emit_glsl(struct brw_context *brw, struct brw_wm_compile *c)
678 {
679 struct intel_context *intel = &brw->intel;
680 #define MAX_IF_DEPTH 32
681 #define MAX_LOOP_DEPTH 32
682 struct brw_instruction *if_inst[MAX_IF_DEPTH], *loop_inst[MAX_LOOP_DEPTH];
683 int if_depth_in_loop[MAX_LOOP_DEPTH];
684 GLuint i, if_depth = 0, loop_depth = 0;
685 struct brw_compile *p = &c->func;
686 struct brw_indirect stack_index = brw_indirect(0, 0);
687
688 c->out_of_regs = GL_FALSE;
689
690 if_depth_in_loop[loop_depth] = 0;
691
692 prealloc_reg(c);
693 brw_set_compression_control(p, BRW_COMPRESSION_NONE);
694 brw_MOV(p, get_addr_reg(stack_index), brw_address(c->stack));
695
696 for (i = 0; i < c->nr_fp_insns; i++) {
697 const struct prog_instruction *inst = &c->prog_instructions[i];
698 int dst_flags;
699 struct brw_reg args[3][4], dst[4];
700 int j;
701 int mark = mark_tmps( c );
702
703 c->cur_inst = i;
704
705 #if 0
706 printf("Inst %d: ", i);
707 _mesa_print_instruction(inst);
708 #endif
709
710 /* fetch any constants that this instruction needs */
711 if (c->fp->use_const_buffer)
712 fetch_constants(c, inst);
713
714 if (inst->Opcode != OPCODE_ARL) {
715 for (j = 0; j < 4; j++) {
716 if (inst->DstReg.WriteMask & (1 << j))
717 dst[j] = get_dst_reg(c, inst, j);
718 else
719 dst[j] = brw_null_reg();
720 }
721 }
722 for (j = 0; j < brw_wm_nr_args(inst->Opcode); j++)
723 get_argument_regs(c, inst, j, dst, args[j], WRITEMASK_XYZW);
724
725 dst_flags = inst->DstReg.WriteMask;
726 if (inst->SaturateMode == SATURATE_ZERO_ONE)
727 dst_flags |= SATURATE;
728
729 if (inst->CondUpdate)
730 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
731 else
732 brw_set_conditionalmod(p, BRW_CONDITIONAL_NONE);
733
734 switch (inst->Opcode) {
735 case WM_PIXELXY:
736 emit_pixel_xy(c, dst, dst_flags);
737 break;
738 case WM_DELTAXY:
739 emit_delta_xy(p, dst, dst_flags, args[0]);
740 break;
741 case WM_PIXELW:
742 emit_pixel_w(c, dst, dst_flags, args[0], args[1]);
743 break;
744 case WM_LINTERP:
745 emit_linterp(p, dst, dst_flags, args[0], args[1]);
746 break;
747 case WM_PINTERP:
748 emit_pinterp(p, dst, dst_flags, args[0], args[1], args[2]);
749 break;
750 case WM_CINTERP:
751 emit_cinterp(p, dst, dst_flags, args[0]);
752 break;
753 case WM_WPOSXY:
754 emit_wpos_xy(c, dst, dst_flags, args[0]);
755 break;
756 case WM_FB_WRITE:
757 emit_fb_write(c, args[0], args[1], args[2],
758 INST_AUX_GET_TARGET(inst->Aux),
759 inst->Aux & INST_AUX_EOT);
760 break;
761 case WM_FRONTFACING:
762 emit_frontfacing(p, dst, dst_flags);
763 break;
764 case OPCODE_ADD:
765 emit_alu2(p, brw_ADD, dst, dst_flags, args[0], args[1]);
766 break;
767 case OPCODE_ARL:
768 emit_arl(c, inst);
769 break;
770 case OPCODE_FRC:
771 emit_alu1(p, brw_FRC, dst, dst_flags, args[0]);
772 break;
773 case OPCODE_FLR:
774 emit_alu1(p, brw_RNDD, dst, dst_flags, args[0]);
775 break;
776 case OPCODE_LRP:
777 emit_lrp(p, dst, dst_flags, args[0], args[1], args[2]);
778 break;
779 case OPCODE_TRUNC:
780 emit_alu1(p, brw_RNDZ, dst, dst_flags, args[0]);
781 break;
782 case OPCODE_MOV:
783 case OPCODE_SWZ:
784 emit_alu1(p, brw_MOV, dst, dst_flags, args[0]);
785 break;
786 case OPCODE_DP2:
787 emit_dp2(p, dst, dst_flags, args[0], args[1]);
788 break;
789 case OPCODE_DP3:
790 emit_dp3(p, dst, dst_flags, args[0], args[1]);
791 break;
792 case OPCODE_DP4:
793 emit_dp4(p, dst, dst_flags, args[0], args[1]);
794 break;
795 case OPCODE_XPD:
796 emit_xpd(p, dst, dst_flags, args[0], args[1]);
797 break;
798 case OPCODE_DPH:
799 emit_dph(p, dst, dst_flags, args[0], args[1]);
800 break;
801 case OPCODE_RCP:
802 emit_math1(c, BRW_MATH_FUNCTION_INV, dst, dst_flags, args[0]);
803 break;
804 case OPCODE_RSQ:
805 emit_math1(c, BRW_MATH_FUNCTION_RSQ, dst, dst_flags, args[0]);
806 break;
807 case OPCODE_SIN:
808 emit_math1(c, BRW_MATH_FUNCTION_SIN, dst, dst_flags, args[0]);
809 break;
810 case OPCODE_COS:
811 emit_math1(c, BRW_MATH_FUNCTION_COS, dst, dst_flags, args[0]);
812 break;
813 case OPCODE_EX2:
814 emit_math1(c, BRW_MATH_FUNCTION_EXP, dst, dst_flags, args[0]);
815 break;
816 case OPCODE_LG2:
817 emit_math1(c, BRW_MATH_FUNCTION_LOG, dst, dst_flags, args[0]);
818 break;
819 case OPCODE_CMP:
820 emit_cmp(p, dst, dst_flags, args[0], args[1], args[2]);
821 break;
822 case OPCODE_MIN:
823 emit_min(p, dst, dst_flags, args[0], args[1]);
824 break;
825 case OPCODE_MAX:
826 emit_max(p, dst, dst_flags, args[0], args[1]);
827 break;
828 case OPCODE_DDX:
829 case OPCODE_DDY:
830 emit_ddxy(p, dst, dst_flags, (inst->Opcode == OPCODE_DDX),
831 args[0]);
832 break;
833 case OPCODE_SLT:
834 emit_sop(p, dst, dst_flags,
835 BRW_CONDITIONAL_L, args[0], args[1]);
836 break;
837 case OPCODE_SLE:
838 emit_sop(p, dst, dst_flags,
839 BRW_CONDITIONAL_LE, args[0], args[1]);
840 break;
841 case OPCODE_SGT:
842 emit_sop(p, dst, dst_flags,
843 BRW_CONDITIONAL_G, args[0], args[1]);
844 break;
845 case OPCODE_SGE:
846 emit_sop(p, dst, dst_flags,
847 BRW_CONDITIONAL_GE, args[0], args[1]);
848 break;
849 case OPCODE_SEQ:
850 emit_sop(p, dst, dst_flags,
851 BRW_CONDITIONAL_EQ, args[0], args[1]);
852 break;
853 case OPCODE_SNE:
854 emit_sop(p, dst, dst_flags,
855 BRW_CONDITIONAL_NEQ, args[0], args[1]);
856 break;
857 case OPCODE_SSG:
858 emit_sign(p, dst, dst_flags, args[0]);
859 break;
860 case OPCODE_MUL:
861 emit_alu2(p, brw_MUL, dst, dst_flags, args[0], args[1]);
862 break;
863 case OPCODE_POW:
864 emit_math2(c, BRW_MATH_FUNCTION_POW,
865 dst, dst_flags, args[0], args[1]);
866 break;
867 case OPCODE_MAD:
868 emit_mad(p, dst, dst_flags, args[0], args[1], args[2]);
869 break;
870 case OPCODE_TEX:
871 emit_tex(c, dst, dst_flags, args[0],
872 get_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH,
873 0, 1, 0, 0),
874 inst->TexSrcTarget,
875 inst->TexSrcUnit,
876 (c->key.shadowtex_mask & (1 << inst->TexSrcUnit)) != 0);
877 break;
878 case OPCODE_TXB:
879 emit_txb(c, dst, dst_flags, args[0],
880 get_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH,
881 0, 1, 0, 0),
882 inst->TexSrcTarget,
883 c->fp->program.Base.SamplerUnits[inst->TexSrcUnit]);
884 break;
885 case OPCODE_KIL_NV:
886 emit_kil_nv(c);
887 break;
888 case OPCODE_IF:
889 assert(if_depth < MAX_IF_DEPTH);
890 if_inst[if_depth++] = brw_IF(p, BRW_EXECUTE_8);
891 if_depth_in_loop[loop_depth]++;
892 break;
893 case OPCODE_ELSE:
894 assert(if_depth > 0);
895 if_inst[if_depth-1] = brw_ELSE(p, if_inst[if_depth-1]);
896 break;
897 case OPCODE_ENDIF:
898 assert(if_depth > 0);
899 brw_ENDIF(p, if_inst[--if_depth]);
900 if_depth_in_loop[loop_depth]--;
901 break;
902 case OPCODE_BGNSUB:
903 brw_save_label(p, inst->Comment, p->nr_insn);
904 break;
905 case OPCODE_ENDSUB:
906 /* no-op */
907 break;
908 case OPCODE_CAL:
909 brw_push_insn_state(p);
910 brw_set_mask_control(p, BRW_MASK_DISABLE);
911 brw_set_access_mode(p, BRW_ALIGN_1);
912 brw_ADD(p, deref_1ud(stack_index, 0), brw_ip_reg(), brw_imm_d(3*16));
913 brw_set_access_mode(p, BRW_ALIGN_16);
914 brw_ADD(p, get_addr_reg(stack_index),
915 get_addr_reg(stack_index), brw_imm_d(4));
916 brw_save_call(&c->func, inst->Comment, p->nr_insn);
917 brw_ADD(p, brw_ip_reg(), brw_ip_reg(), brw_imm_d(1*16));
918 brw_pop_insn_state(p);
919 break;
920
921 case OPCODE_RET:
922 brw_push_insn_state(p);
923 brw_set_mask_control(p, BRW_MASK_DISABLE);
924 brw_ADD(p, get_addr_reg(stack_index),
925 get_addr_reg(stack_index), brw_imm_d(-4));
926 brw_set_access_mode(p, BRW_ALIGN_1);
927 brw_MOV(p, brw_ip_reg(), deref_1ud(stack_index, 0));
928 brw_set_access_mode(p, BRW_ALIGN_16);
929 brw_pop_insn_state(p);
930
931 break;
932 case OPCODE_BGNLOOP:
933 /* XXX may need to invalidate the current_constant regs */
934 loop_inst[loop_depth++] = brw_DO(p, BRW_EXECUTE_8);
935 if_depth_in_loop[loop_depth] = 0;
936 break;
937 case OPCODE_BRK:
938 brw_BREAK(p, if_depth_in_loop[loop_depth]);
939 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
940 break;
941 case OPCODE_CONT:
942 brw_CONT(p, if_depth_in_loop[loop_depth]);
943 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
944 break;
945 case OPCODE_ENDLOOP:
946 {
947 struct brw_instruction *inst0, *inst1;
948 GLuint br = 1;
949
950 if (intel->gen == 5)
951 br = 2;
952
953 assert(loop_depth > 0);
954 loop_depth--;
955 inst0 = inst1 = brw_WHILE(p, loop_inst[loop_depth]);
956 /* patch all the BREAK/CONT instructions from last BGNLOOP */
957 while (inst0 > loop_inst[loop_depth]) {
958 inst0--;
959 if (inst0->header.opcode == BRW_OPCODE_BREAK &&
960 inst0->bits3.if_else.jump_count == 0) {
961 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
962 }
963 else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
964 inst0->bits3.if_else.jump_count == 0) {
965 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
966 }
967 }
968 }
969 break;
970 default:
971 printf("unsupported opcode %d (%s) in fragment shader\n",
972 inst->Opcode, inst->Opcode < MAX_OPCODE ?
973 _mesa_opcode_string(inst->Opcode) : "unknown");
974 }
975
976 /* Release temporaries containing any unaliased source regs. */
977 release_tmps( c, mark );
978
979 if (inst->CondUpdate)
980 brw_set_predicate_control(p, BRW_PREDICATE_NORMAL);
981 else
982 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
983 }
984 post_wm_emit(c);
985
986 if (INTEL_DEBUG & DEBUG_WM) {
987 printf("wm-native:\n");
988 for (i = 0; i < p->nr_insn; i++)
989 brw_disasm(stdout, &p->store[i], intel->gen);
990 printf("\n");
991 }
992 }
993
994 /**
995 * Do GPU code generation for shaders that use GLSL features such as
996 * flow control. Other shaders will be compiled with the
997 */
998 void brw_wm_glsl_emit(struct brw_context *brw, struct brw_wm_compile *c)
999 {
1000 if (INTEL_DEBUG & DEBUG_WM) {
1001 printf("brw_wm_glsl_emit:\n");
1002 }
1003
1004 /* initial instruction translation/simplification */
1005 brw_wm_pass_fp(c);
1006
1007 /* actual code generation */
1008 brw_wm_emit_glsl(brw, c);
1009
1010 if (INTEL_DEBUG & DEBUG_WM) {
1011 brw_wm_print_program(c, "brw_wm_glsl_emit done");
1012 }
1013
1014 c->prog_data.total_grf = num_grf_used(c);
1015 c->prog_data.total_scratch = 0;
1016 }