2 #include "shader/prog_parameter.h"
3 #include "brw_context.h"
7 /* Only guess, need a flag in gl_fragment_program later */
8 GLboolean brw_wm_is_glsl(const struct gl_fragment_program *fp)
11 for (i = 0; i < fp->Base.NumInstructions; i++) {
12 struct prog_instruction *inst = &fp->Base.Instructions[i];
13 switch (inst->Opcode) {
31 static void set_reg(struct brw_wm_compile *c, int file, int index,
32 int component, struct brw_reg reg)
34 c->wm_regs[file][index][component].reg = reg;
35 c->wm_regs[file][index][component].inited = GL_TRUE;
38 static int get_scalar_dst_index(struct prog_instruction *inst)
41 for (i = 0; i < 4; i++)
42 if (inst->DstReg.WriteMask & (1<<i))
47 static struct brw_reg alloc_tmp(struct brw_wm_compile *c)
50 reg = brw_vec8_grf(c->tmp_index--, 0);
54 static void release_tmps(struct brw_wm_compile *c)
60 get_reg(struct brw_wm_compile *c, int file, int index, int component, int nr, GLuint neg, GLuint abs)
64 case PROGRAM_STATE_VAR:
65 case PROGRAM_CONSTANT:
67 file = PROGRAM_STATE_VAR;
69 case PROGRAM_UNDEFINED:
70 return brw_null_reg();
75 if(c->wm_regs[file][index][component].inited)
76 reg = c->wm_regs[file][index][component].reg;
78 reg = brw_vec8_grf(c->reg_index, 0);
80 if(!c->wm_regs[file][index][component].inited) {
81 set_reg(c, file, index, component, reg);
85 if (neg & (1<< component)) {
93 static void prealloc_reg(struct brw_wm_compile *c)
97 int nr_interp_regs = 0;
98 GLuint inputs = FRAG_BIT_WPOS | c->fp_interp_emitted | c->fp_deriv_emitted;
100 for (i = 0; i < 4; i++) {
101 reg = (i < c->key.nr_depth_regs)
102 ? brw_vec8_grf(i*2, 0) : brw_vec8_grf(0, 0);
103 set_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH, i, reg);
105 c->reg_index += 2*c->key.nr_depth_regs;
107 int nr_params = c->fp->program.Base.Parameters->NumParameters;
108 struct gl_program_parameter_list *plist =
109 c->fp->program.Base.Parameters;
111 c->prog_data.nr_params = 4*nr_params;
112 for (i = 0; i < nr_params; i++) {
113 for (j = 0; j < 4; j++, index++) {
114 reg = brw_vec1_grf(c->reg_index + index/8,
116 c->prog_data.param[index] =
117 &plist->ParameterValues[i][j];
118 set_reg(c, PROGRAM_STATE_VAR, i, j, reg);
121 c->nr_creg = 2*((4*nr_params+15)/16);
122 c->reg_index += c->nr_creg;
124 for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
125 if (inputs & (1<<i)) {
127 reg = brw_vec8_grf(c->reg_index, 0);
128 for (j = 0; j < 4; j++)
129 set_reg(c, PROGRAM_PAYLOAD, i, j, reg);
134 c->prog_data.first_curbe_grf = c->key.nr_depth_regs * 2;
135 c->prog_data.urb_read_length = nr_interp_regs * 2;
136 c->prog_data.curb_read_length = c->nr_creg;
137 c->emit_mask_reg = brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, c->reg_index, 0);
139 c->stack = brw_uw16_reg(BRW_GENERAL_REGISTER_FILE, c->reg_index, 0);
143 static struct brw_reg get_dst_reg(struct brw_wm_compile *c,
144 struct prog_instruction *inst, int component, int nr)
146 return get_reg(c, inst->DstReg.File, inst->DstReg.Index, component, nr,
150 static struct brw_reg get_src_reg(struct brw_wm_compile *c,
151 struct prog_src_register *src, int index, int nr)
153 int component = GET_SWZ(src->Swizzle, index);
154 return get_reg(c, src->File, src->Index, component, nr,
155 src->NegateBase, src->Abs);
158 static void emit_abs( struct brw_wm_compile *c,
159 struct prog_instruction *inst)
162 struct brw_compile *p = &c->func;
163 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
164 for (i = 0; i < 4; i++) {
165 if (inst->DstReg.WriteMask & (1<<i)) {
166 struct brw_reg src, dst;
167 dst = get_dst_reg(c, inst, i, 1);
168 src = get_src_reg(c, &inst->SrcReg[0], i, 1);
169 brw_MOV(p, dst, brw_abs(src));
172 brw_set_saturate(p, 0);
175 static void emit_int( struct brw_wm_compile *c,
176 struct prog_instruction *inst)
179 struct brw_compile *p = &c->func;
180 GLuint mask = inst->DstReg.WriteMask;
181 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
182 for (i = 0; i < 4; i++) {
184 struct brw_reg src, dst;
185 dst = get_dst_reg(c, inst, i, 1) ;
186 src = get_src_reg(c, &inst->SrcReg[0], i, 1);
187 brw_RNDD(p, dst, src);
190 brw_set_saturate(p, 0);
193 static void emit_mov( struct brw_wm_compile *c,
194 struct prog_instruction *inst)
197 struct brw_compile *p = &c->func;
198 GLuint mask = inst->DstReg.WriteMask;
199 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
200 for (i = 0; i < 4; i++) {
202 struct brw_reg src, dst;
203 dst = get_dst_reg(c, inst, i, 1);
204 src = get_src_reg(c, &inst->SrcReg[0], i, 1);
205 brw_MOV(p, dst, src);
208 brw_set_saturate(p, 0);
211 static void emit_pixel_xy(struct brw_wm_compile *c,
212 struct prog_instruction *inst)
214 struct brw_reg r1 = brw_vec1_grf(1, 0);
215 struct brw_reg r1_uw = retype(r1, BRW_REGISTER_TYPE_UW);
217 struct brw_reg dst0, dst1;
218 struct brw_compile *p = &c->func;
219 GLuint mask = inst->DstReg.WriteMask;
221 dst0 = get_dst_reg(c, inst, 0, 1);
222 dst1 = get_dst_reg(c, inst, 1, 1);
223 /* Calculate pixel centers by adding 1 or 0 to each of the
224 * micro-tile coordinates passed in r1.
226 if (mask & WRITEMASK_X) {
228 vec8(retype(dst0, BRW_REGISTER_TYPE_UW)),
229 stride(suboffset(r1_uw, 4), 2, 4, 0),
230 brw_imm_v(0x10101010));
233 if (mask & WRITEMASK_Y) {
235 vec8(retype(dst1, BRW_REGISTER_TYPE_UW)),
236 stride(suboffset(r1_uw, 5), 2, 4, 0),
237 brw_imm_v(0x11001100));
242 static void emit_delta_xy(struct brw_wm_compile *c,
243 struct prog_instruction *inst)
245 struct brw_reg r1 = brw_vec1_grf(1, 0);
246 struct brw_reg dst0, dst1, src0, src1;
247 struct brw_compile *p = &c->func;
248 GLuint mask = inst->DstReg.WriteMask;
250 dst0 = get_dst_reg(c, inst, 0, 1);
251 dst1 = get_dst_reg(c, inst, 1, 1);
252 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
253 src1 = get_src_reg(c, &inst->SrcReg[0], 1, 1);
254 /* Calc delta X,Y by subtracting origin in r1 from the pixel
257 if (mask & WRITEMASK_X) {
260 retype(src0, BRW_REGISTER_TYPE_UW),
264 if (mask & WRITEMASK_Y) {
267 retype(src1, BRW_REGISTER_TYPE_UW),
268 negate(suboffset(r1,1)));
275 static void fire_fb_write( struct brw_wm_compile *c,
279 struct brw_compile *p = &c->func;
281 /* Pass through control information:
283 /* mov (8) m1.0<1>:ud r1.0<8;8,1>:ud { Align1 NoMask } */
285 brw_push_insn_state(p);
286 brw_set_mask_control(p, BRW_MASK_DISABLE); /* ? */
288 brw_message_reg(base_reg + 1),
290 brw_pop_insn_state(p);
292 /* Send framebuffer write message: */
294 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW),
296 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW),
297 0, /* render surface always 0 */
303 static void emit_fb_write(struct brw_wm_compile *c,
304 struct prog_instruction *inst)
306 struct brw_compile *p = &c->func;
309 struct brw_reg src0;//, src1, src2, dst;
311 /* Reserve a space for AA - may not be needed:
313 if (c->key.aa_dest_stencil_reg)
316 brw_push_insn_state(p);
317 for (channel = 0; channel < 4; channel++) {
318 src0 = get_src_reg(c, &inst->SrcReg[0], channel, 1);
319 /* mov (8) m2.0<1>:ud r28.0<8;8,1>:ud { Align1 } */
320 /* mov (8) m6.0<1>:ud r29.0<8;8,1>:ud { Align1 SecHalf } */
321 brw_MOV(p, brw_message_reg(nr + channel), src0);
323 /* skip over the regs populated above: */
325 brw_pop_insn_state(p);
328 if (c->key.source_depth_to_render_target)
330 if (c->key.computes_depth) {
331 src0 = get_src_reg(c, &inst->SrcReg[2], 2, 1);
332 brw_MOV(p, brw_message_reg(nr), src0);
334 src0 = get_src_reg(c, &inst->SrcReg[1], 1, 1);
335 brw_MOV(p, brw_message_reg(nr), src0);
341 fire_fb_write(c, 0, nr);
344 static void emit_pixel_w( struct brw_wm_compile *c,
345 struct prog_instruction *inst)
347 struct brw_compile *p = &c->func;
348 GLuint mask = inst->DstReg.WriteMask;
349 if (mask & WRITEMASK_W) {
350 struct brw_reg dst, src0, delta0, delta1;
351 struct brw_reg interp3;
353 dst = get_dst_reg(c, inst, 3, 1);
354 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
355 delta0 = get_src_reg(c, &inst->SrcReg[1], 0, 1);
356 delta1 = get_src_reg(c, &inst->SrcReg[1], 1, 1);
358 interp3 = brw_vec1_grf(src0.nr+1, 4);
359 /* Calc 1/w - just linterp wpos[3] optimized by putting the
360 * result straight into a message reg.
362 brw_LINE(p, brw_null_reg(), interp3, delta0);
363 brw_MAC(p, brw_message_reg(2), suboffset(interp3, 1), delta1);
367 BRW_MATH_FUNCTION_INV,
368 BRW_MATH_SATURATE_NONE,
370 BRW_MATH_PRECISION_FULL);
374 static void emit_linterp(struct brw_wm_compile *c,
375 struct prog_instruction *inst)
377 struct brw_compile *p = &c->func;
378 GLuint mask = inst->DstReg.WriteMask;
379 struct brw_reg interp[4];
380 struct brw_reg dst, delta0, delta1;
383 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
384 delta0 = get_src_reg(c, &inst->SrcReg[1], 0, 1);
385 delta1 = get_src_reg(c, &inst->SrcReg[1], 1, 1);
389 interp[0] = brw_vec1_grf(nr, 0);
390 interp[1] = brw_vec1_grf(nr, 4);
391 interp[2] = brw_vec1_grf(nr+1, 0);
392 interp[3] = brw_vec1_grf(nr+1, 4);
394 for(i = 0; i < 4; i++ ) {
396 dst = get_dst_reg(c, inst, i, 1);
397 brw_LINE(p, brw_null_reg(), interp[i], delta0);
398 brw_MAC(p, dst, suboffset(interp[i],1), delta1);
403 static void emit_cinterp(struct brw_wm_compile *c,
404 struct prog_instruction *inst)
406 struct brw_compile *p = &c->func;
407 GLuint mask = inst->DstReg.WriteMask;
409 struct brw_reg interp[4];
410 struct brw_reg dst, src0;
412 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
416 interp[0] = brw_vec1_grf(nr, 0);
417 interp[1] = brw_vec1_grf(nr, 4);
418 interp[2] = brw_vec1_grf(nr+1, 0);
419 interp[3] = brw_vec1_grf(nr+1, 4);
421 for(i = 0; i < 4; i++ ) {
423 dst = get_dst_reg(c, inst, i, 1);
424 brw_MOV(p, dst, suboffset(interp[i],3));
429 static void emit_pinterp(struct brw_wm_compile *c,
430 struct prog_instruction *inst)
432 struct brw_compile *p = &c->func;
433 GLuint mask = inst->DstReg.WriteMask;
435 struct brw_reg interp[4];
436 struct brw_reg dst, delta0, delta1;
437 struct brw_reg src0, w;
439 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
440 delta0 = get_src_reg(c, &inst->SrcReg[1], 0, 1);
441 delta1 = get_src_reg(c, &inst->SrcReg[1], 1, 1);
442 w = get_src_reg(c, &inst->SrcReg[2], 3, 1);
446 interp[0] = brw_vec1_grf(nr, 0);
447 interp[1] = brw_vec1_grf(nr, 4);
448 interp[2] = brw_vec1_grf(nr+1, 0);
449 interp[3] = brw_vec1_grf(nr+1, 4);
451 for(i = 0; i < 4; i++ ) {
453 dst = get_dst_reg(c, inst, i, 1);
454 brw_LINE(p, brw_null_reg(), interp[i], delta0);
455 brw_MAC(p, dst, suboffset(interp[i],1),
457 brw_MUL(p, dst, dst, w);
462 static void emit_xpd(struct brw_wm_compile *c,
463 struct prog_instruction *inst)
466 struct brw_compile *p = &c->func;
467 GLuint mask = inst->DstReg.WriteMask;
468 for (i = 0; i < 4; i++) {
472 struct brw_reg src0, src1, dst;
473 dst = get_dst_reg(c, inst, i, 1);
474 src0 = negate(get_src_reg(c, &inst->SrcReg[0], i2, 1));
475 src1 = get_src_reg(c, &inst->SrcReg[1], i1, 1);
476 brw_MUL(p, brw_null_reg(), src0, src1);
477 src0 = get_src_reg(c, &inst->SrcReg[0], i1, 1);
478 src1 = get_src_reg(c, &inst->SrcReg[1], i2, 1);
479 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
480 brw_MAC(p, dst, src0, src1);
481 brw_set_saturate(p, 0);
484 brw_set_saturate(p, 0);
487 static void emit_dp3(struct brw_wm_compile *c,
488 struct prog_instruction *inst)
490 struct brw_reg src0[3], src1[3], dst;
492 struct brw_compile *p = &c->func;
493 for (i = 0; i < 3; i++) {
494 src0[i] = get_src_reg(c, &inst->SrcReg[0], i, 1);
495 src1[i] = get_src_reg(c, &inst->SrcReg[1], i, 1);
498 dst = get_dst_reg(c, inst, get_scalar_dst_index(inst), 1);
499 brw_MUL(p, brw_null_reg(), src0[0], src1[0]);
500 brw_MAC(p, brw_null_reg(), src0[1], src1[1]);
501 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
502 brw_MAC(p, dst, src0[2], src1[2]);
503 brw_set_saturate(p, 0);
506 static void emit_dp4(struct brw_wm_compile *c,
507 struct prog_instruction *inst)
509 struct brw_reg src0[4], src1[4], dst;
511 struct brw_compile *p = &c->func;
512 for (i = 0; i < 4; i++) {
513 src0[i] = get_src_reg(c, &inst->SrcReg[0], i, 1);
514 src1[i] = get_src_reg(c, &inst->SrcReg[1], i, 1);
516 dst = get_dst_reg(c, inst, get_scalar_dst_index(inst), 1);
517 brw_MUL(p, brw_null_reg(), src0[0], src1[0]);
518 brw_MAC(p, brw_null_reg(), src0[1], src1[1]);
519 brw_MAC(p, brw_null_reg(), src0[2], src1[2]);
520 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
521 brw_MAC(p, dst, src0[3], src1[3]);
522 brw_set_saturate(p, 0);
525 static void emit_dph(struct brw_wm_compile *c,
526 struct prog_instruction *inst)
528 struct brw_reg src0[4], src1[4], dst;
530 struct brw_compile *p = &c->func;
531 for (i = 0; i < 4; i++) {
532 src0[i] = get_src_reg(c, &inst->SrcReg[0], i, 1);
533 src1[i] = get_src_reg(c, &inst->SrcReg[1], i, 1);
535 dst = get_dst_reg(c, inst, get_scalar_dst_index(inst), 1);
536 brw_MUL(p, brw_null_reg(), src0[0], src1[0]);
537 brw_MAC(p, brw_null_reg(), src0[1], src1[1]);
538 brw_MAC(p, dst, src0[2], src1[2]);
539 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
540 brw_ADD(p, dst, src0[3], src1[3]);
541 brw_set_saturate(p, 0);
544 static void emit_math1(struct brw_wm_compile *c,
545 struct prog_instruction *inst, GLuint func)
547 struct brw_compile *p = &c->func;
548 struct brw_reg src0, dst;
550 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
551 dst = get_dst_reg(c, inst, get_scalar_dst_index(inst), 1);
552 brw_MOV(p, brw_message_reg(2), src0);
556 (inst->SaturateMode != SATURATE_OFF) ? BRW_MATH_SATURATE_SATURATE : BRW_MATH_SATURATE_NONE,
559 BRW_MATH_DATA_VECTOR,
560 BRW_MATH_PRECISION_FULL);
563 static void emit_rcp(struct brw_wm_compile *c,
564 struct prog_instruction *inst)
566 emit_math1(c, inst, BRW_MATH_FUNCTION_INV);
569 static void emit_rsq(struct brw_wm_compile *c,
570 struct prog_instruction *inst)
572 emit_math1(c, inst, BRW_MATH_FUNCTION_RSQ);
575 static void emit_sin(struct brw_wm_compile *c,
576 struct prog_instruction *inst)
578 emit_math1(c, inst, BRW_MATH_FUNCTION_SIN);
581 static void emit_cos(struct brw_wm_compile *c,
582 struct prog_instruction *inst)
584 emit_math1(c, inst, BRW_MATH_FUNCTION_COS);
587 static void emit_ex2(struct brw_wm_compile *c,
588 struct prog_instruction *inst)
590 emit_math1(c, inst, BRW_MATH_FUNCTION_EXP);
593 static void emit_lg2(struct brw_wm_compile *c,
594 struct prog_instruction *inst)
596 emit_math1(c, inst, BRW_MATH_FUNCTION_LOG);
599 static void emit_add(struct brw_wm_compile *c,
600 struct prog_instruction *inst)
602 struct brw_compile *p = &c->func;
603 struct brw_reg src0, src1, dst;
604 GLuint mask = inst->DstReg.WriteMask;
606 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
607 for (i = 0 ; i < 4; i++) {
609 dst = get_dst_reg(c, inst, i, 1);
610 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
611 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
612 brw_ADD(p, dst, src0, src1);
615 brw_set_saturate(p, 0);
618 static void emit_sub(struct brw_wm_compile *c,
619 struct prog_instruction *inst)
621 struct brw_compile *p = &c->func;
622 struct brw_reg src0, src1, dst;
623 GLuint mask = inst->DstReg.WriteMask;
625 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
626 for (i = 0 ; i < 4; i++) {
628 dst = get_dst_reg(c, inst, i, 1);
629 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
630 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
631 brw_ADD(p, dst, src0, negate(src1));
634 brw_set_saturate(p, 0);
637 static void emit_mul(struct brw_wm_compile *c,
638 struct prog_instruction *inst)
640 struct brw_compile *p = &c->func;
641 struct brw_reg src0, src1, dst;
642 GLuint mask = inst->DstReg.WriteMask;
644 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
645 for (i = 0 ; i < 4; i++) {
647 dst = get_dst_reg(c, inst, i, 1);
648 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
649 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
650 brw_MUL(p, dst, src0, src1);
653 brw_set_saturate(p, 0);
656 static void emit_frc(struct brw_wm_compile *c,
657 struct prog_instruction *inst)
659 struct brw_compile *p = &c->func;
660 struct brw_reg src0, dst;
661 GLuint mask = inst->DstReg.WriteMask;
663 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
664 for (i = 0 ; i < 4; i++) {
666 dst = get_dst_reg(c, inst, i, 1);
667 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
668 brw_FRC(p, dst, src0);
671 if (inst->SaturateMode != SATURATE_OFF)
672 brw_set_saturate(p, 0);
675 static void emit_flr(struct brw_wm_compile *c,
676 struct prog_instruction *inst)
678 struct brw_compile *p = &c->func;
679 struct brw_reg src0, dst;
680 GLuint mask = inst->DstReg.WriteMask;
682 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
683 for (i = 0 ; i < 4; i++) {
685 dst = get_dst_reg(c, inst, i, 1);
686 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
687 brw_RNDD(p, dst, src0);
690 brw_set_saturate(p, 0);
693 static void emit_max(struct brw_wm_compile *c,
694 struct prog_instruction *inst)
696 struct brw_compile *p = &c->func;
697 GLuint mask = inst->DstReg.WriteMask;
698 struct brw_reg src0, src1, dst;
700 brw_push_insn_state(p);
701 for (i = 0; i < 4; i++) {
703 dst = get_dst_reg(c, inst, i, 1);
704 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
705 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
706 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
707 brw_MOV(p, dst, src0);
708 brw_set_saturate(p, 0);
710 brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_L, src0, src1);
711 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
712 brw_set_predicate_control(p, BRW_PREDICATE_NORMAL);
713 brw_MOV(p, dst, src1);
714 brw_set_saturate(p, 0);
715 brw_set_predicate_control_flag_value(p, 0xff);
718 brw_pop_insn_state(p);
721 static void emit_min(struct brw_wm_compile *c,
722 struct prog_instruction *inst)
724 struct brw_compile *p = &c->func;
725 GLuint mask = inst->DstReg.WriteMask;
726 struct brw_reg src0, src1, dst;
728 brw_push_insn_state(p);
729 for (i = 0; i < 4; i++) {
731 dst = get_dst_reg(c, inst, i, 1);
732 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
733 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
734 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
735 brw_MOV(p, dst, src0);
736 brw_set_saturate(p, 0);
738 brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_L, src1, src0);
739 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
740 brw_set_predicate_control(p, BRW_PREDICATE_NORMAL);
741 brw_MOV(p, dst, src1);
742 brw_set_saturate(p, 0);
743 brw_set_predicate_control_flag_value(p, 0xff);
746 brw_pop_insn_state(p);
749 static void emit_pow(struct brw_wm_compile *c,
750 struct prog_instruction *inst)
752 struct brw_compile *p = &c->func;
753 struct brw_reg dst, src0, src1;
754 dst = get_dst_reg(c, inst, get_scalar_dst_index(inst), 1);
755 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
756 src1 = get_src_reg(c, &inst->SrcReg[1], 0, 1);
758 brw_MOV(p, brw_message_reg(2), src0);
759 brw_MOV(p, brw_message_reg(3), src1);
763 BRW_MATH_FUNCTION_POW,
764 (inst->SaturateMode != SATURATE_OFF) ? BRW_MATH_SATURATE_SATURATE : BRW_MATH_SATURATE_NONE,
767 BRW_MATH_DATA_VECTOR,
768 BRW_MATH_PRECISION_FULL);
771 static void emit_lrp(struct brw_wm_compile *c,
772 struct prog_instruction *inst)
774 struct brw_compile *p = &c->func;
775 GLuint mask = inst->DstReg.WriteMask;
776 struct brw_reg dst, tmp1, tmp2, src0, src1, src2;
778 for (i = 0; i < 4; i++) {
780 dst = get_dst_reg(c, inst, i, 1);
781 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
783 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
785 if (src1.nr == dst.nr) {
787 brw_MOV(p, tmp1, src1);
791 src2 = get_src_reg(c, &inst->SrcReg[2], i, 1);
792 if (src2.nr == dst.nr) {
794 brw_MOV(p, tmp2, src2);
798 brw_ADD(p, dst, negate(src0), brw_imm_f(1.0));
799 brw_MUL(p, brw_null_reg(), dst, tmp2);
800 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
801 brw_MAC(p, dst, src0, tmp1);
802 brw_set_saturate(p, 0);
808 static void emit_kil(struct brw_wm_compile *c)
810 struct brw_compile *p = &c->func;
811 struct brw_reg depth = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
812 brw_push_insn_state(p);
813 brw_set_mask_control(p, BRW_MASK_DISABLE);
814 brw_NOT(p, c->emit_mask_reg, brw_mask_reg(1)); //IMASK
815 brw_AND(p, depth, c->emit_mask_reg, depth);
816 brw_pop_insn_state(p);
819 static void emit_mad(struct brw_wm_compile *c,
820 struct prog_instruction *inst)
822 struct brw_compile *p = &c->func;
823 GLuint mask = inst->DstReg.WriteMask;
824 struct brw_reg dst, src0, src1, src2;
827 for (i = 0; i < 4; i++) {
829 dst = get_dst_reg(c, inst, i, 1);
830 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
831 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
832 src2 = get_src_reg(c, &inst->SrcReg[2], i, 1);
833 brw_MUL(p, dst, src0, src1);
835 brw_set_saturate(p, (inst->SaturateMode != SATURATE_OFF) ? 1 : 0);
836 brw_ADD(p, dst, dst, src2);
837 brw_set_saturate(p, 0);
842 static void emit_sop(struct brw_wm_compile *c,
843 struct prog_instruction *inst, GLuint cond)
845 struct brw_compile *p = &c->func;
846 GLuint mask = inst->DstReg.WriteMask;
847 struct brw_reg dst, src0, src1;
850 brw_push_insn_state(p);
851 for (i = 0; i < 4; i++) {
853 dst = get_dst_reg(c, inst, i, 1);
854 src0 = get_src_reg(c, &inst->SrcReg[0], i, 1);
855 src1 = get_src_reg(c, &inst->SrcReg[1], i, 1);
856 brw_CMP(p, brw_null_reg(), cond, src0, src1);
857 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
858 brw_MOV(p, dst, brw_imm_f(0.0));
859 brw_set_predicate_control(p, BRW_PREDICATE_NORMAL);
860 brw_MOV(p, dst, brw_imm_f(1.0));
863 brw_pop_insn_state(p);
866 static void emit_slt(struct brw_wm_compile *c,
867 struct prog_instruction *inst)
869 emit_sop(c, inst, BRW_CONDITIONAL_L);
872 static void emit_sle(struct brw_wm_compile *c,
873 struct prog_instruction *inst)
875 emit_sop(c, inst, BRW_CONDITIONAL_LE);
878 static void emit_sgt(struct brw_wm_compile *c,
879 struct prog_instruction *inst)
881 emit_sop(c, inst, BRW_CONDITIONAL_G);
884 static void emit_sge(struct brw_wm_compile *c,
885 struct prog_instruction *inst)
887 emit_sop(c, inst, BRW_CONDITIONAL_GE);
890 static void emit_seq(struct brw_wm_compile *c,
891 struct prog_instruction *inst)
893 emit_sop(c, inst, BRW_CONDITIONAL_EQ);
896 static void emit_sne(struct brw_wm_compile *c,
897 struct prog_instruction *inst)
899 emit_sop(c, inst, BRW_CONDITIONAL_NEQ);
902 static void emit_ddx(struct brw_wm_compile *c,
903 struct prog_instruction *inst)
905 struct brw_compile *p = &c->func;
906 GLuint mask = inst->DstReg.WriteMask;
907 struct brw_reg interp[4];
909 struct brw_reg src0, w;
911 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
912 w = get_src_reg(c, &inst->SrcReg[1], 3, 1);
914 interp[0] = brw_vec1_grf(nr, 0);
915 interp[1] = brw_vec1_grf(nr, 4);
916 interp[2] = brw_vec1_grf(nr+1, 0);
917 interp[3] = brw_vec1_grf(nr+1, 4);
918 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
919 for(i = 0; i < 4; i++ ) {
921 dst = get_dst_reg(c, inst, i, 1);
922 brw_MOV(p, dst, interp[i]);
923 brw_MUL(p, dst, dst, w);
926 brw_set_saturate(p, 0);
929 static void emit_ddy(struct brw_wm_compile *c,
930 struct prog_instruction *inst)
932 struct brw_compile *p = &c->func;
933 GLuint mask = inst->DstReg.WriteMask;
934 struct brw_reg interp[4];
936 struct brw_reg src0, w;
939 src0 = get_src_reg(c, &inst->SrcReg[0], 0, 1);
941 w = get_src_reg(c, &inst->SrcReg[1], 3, 1);
942 interp[0] = brw_vec1_grf(nr, 0);
943 interp[1] = brw_vec1_grf(nr, 4);
944 interp[2] = brw_vec1_grf(nr+1, 0);
945 interp[3] = brw_vec1_grf(nr+1, 4);
946 brw_set_saturate(p, inst->SaturateMode != SATURATE_OFF);
947 for(i = 0; i < 4; i++ ) {
949 dst = get_dst_reg(c, inst, i, 1);
950 brw_MOV(p, dst, suboffset(interp[i], 1));
951 brw_MUL(p, dst, dst, w);
954 brw_set_saturate(p, 0);
957 static void emit_wpos_xy(struct brw_wm_compile *c,
958 struct prog_instruction *inst)
960 struct brw_compile *p = &c->func;
961 GLuint mask = inst->DstReg.WriteMask;
962 struct brw_reg src0[2], dst[2];
964 dst[0] = get_dst_reg(c, inst, 0, 1);
965 dst[1] = get_dst_reg(c, inst, 1, 1);
967 src0[0] = get_src_reg(c, &inst->SrcReg[0], 0, 1);
968 src0[1] = get_src_reg(c, &inst->SrcReg[0], 1, 1);
970 /* Calc delta X,Y by subtracting origin in r1 from the pixel
973 if (mask & WRITEMASK_X) {
976 retype(src0[0], BRW_REGISTER_TYPE_UW));
979 if (mask & WRITEMASK_Y) {
980 /* TODO -- window_height - Y */
983 retype(src0[1], BRW_REGISTER_TYPE_UW));
989 BIAS on SIMD8 not workind yet...
991 static void emit_txb(struct brw_wm_compile *c,
992 struct prog_instruction *inst)
994 struct brw_compile *p = &c->func;
995 struct brw_reg dst[4], src[4], payload_reg;
997 payload_reg = get_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH, 0, 1, 0, 0);
998 for (i = 0; i < 4; i++)
999 dst[i] = get_dst_reg(c, inst, i, 1);
1000 for (i = 0; i < 4; i++)
1001 src[i] = get_src_reg(c, &inst->SrcReg[0], i, 1);
1003 switch (inst->TexSrcTarget) {
1004 case TEXTURE_1D_INDEX:
1005 brw_MOV(p, brw_message_reg(2), src[0]);
1006 brw_MOV(p, brw_message_reg(3), brw_imm_f(0));
1007 brw_MOV(p, brw_message_reg(4), brw_imm_f(0));
1009 case TEXTURE_2D_INDEX:
1010 case TEXTURE_RECT_INDEX:
1011 brw_MOV(p, brw_message_reg(2), src[0]);
1012 brw_MOV(p, brw_message_reg(3), src[1]);
1013 brw_MOV(p, brw_message_reg(4), brw_imm_f(0));
1016 brw_MOV(p, brw_message_reg(2), src[0]);
1017 brw_MOV(p, brw_message_reg(3), src[1]);
1018 brw_MOV(p, brw_message_reg(4), src[2]);
1021 brw_MOV(p, brw_message_reg(5), src[3]);
1022 brw_MOV(p, brw_message_reg(6), brw_imm_f(0));
1024 retype(vec8(dst[0]), BRW_REGISTER_TYPE_UW),
1026 retype(payload_reg, BRW_REGISTER_TYPE_UW),
1027 inst->TexSrcUnit + 1, /* surface */
1028 inst->TexSrcUnit, /* sampler */
1029 inst->DstReg.WriteMask,
1030 BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS,
1036 static void emit_tex(struct brw_wm_compile *c,
1037 struct prog_instruction *inst)
1039 struct brw_compile *p = &c->func;
1040 struct brw_reg dst[4], src[4], payload_reg;
1044 GLboolean shadow = (c->key.shadowtex_mask & (1<<inst->TexSrcUnit)) ? 1 : 0;
1046 payload_reg = get_reg(c, PROGRAM_PAYLOAD, PAYLOAD_DEPTH, 0, 1, 0, 0);
1048 for (i = 0; i < 4; i++)
1049 dst[i] = get_dst_reg(c, inst, i, 1);
1050 for (i = 0; i < 4; i++)
1051 src[i] = get_src_reg(c, &inst->SrcReg[0], i, 1);
1054 switch (inst->TexSrcTarget) {
1055 case TEXTURE_1D_INDEX:
1059 case TEXTURE_2D_INDEX:
1060 case TEXTURE_RECT_INDEX:
1061 emit = WRITEMASK_XY;
1065 emit = WRITEMASK_XYZ;
1071 for (i = 0; i < nr; i++) {
1072 static const GLuint swz[4] = {0,1,2,2};
1074 brw_MOV(p, brw_message_reg(msg_len+1), src[swz[i]]);
1076 brw_MOV(p, brw_message_reg(msg_len+1), brw_imm_f(0));
1081 brw_MOV(p, brw_message_reg(5), brw_imm_f(0));
1082 brw_MOV(p, brw_message_reg(6), src[2]);
1086 retype(vec8(dst[0]), BRW_REGISTER_TYPE_UW),
1088 retype(payload_reg, BRW_REGISTER_TYPE_UW),
1089 inst->TexSrcUnit + 1, /* surface */
1090 inst->TexSrcUnit, /* sampler */
1091 inst->DstReg.WriteMask,
1092 BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE,
1098 brw_MOV(p, dst[3], brw_imm_f(1.0));
1101 static void post_wm_emit( struct brw_wm_compile *c )
1103 GLuint nr_insns = c->fp->program.Base.NumInstructions;
1104 GLuint insn, target_insn;
1105 struct prog_instruction *inst1, *inst2;
1106 struct brw_instruction *brw_inst1, *brw_inst2;
1108 for (insn = 0; insn < nr_insns; insn++) {
1109 inst1 = &c->fp->program.Base.Instructions[insn];
1110 brw_inst1 = inst1->Data;
1111 switch (inst1->Opcode) {
1113 target_insn = inst1->BranchTarget;
1114 inst2 = &c->fp->program.Base.Instructions[target_insn];
1115 brw_inst2 = inst2->Data;
1116 offset = brw_inst2 - brw_inst1;
1117 brw_set_src1(brw_inst1, brw_imm_d(offset*16));
1125 static void brw_wm_emit_glsl(struct brw_wm_compile *c)
1129 #define MAX_LOOP_DEPTH 32
1130 struct brw_instruction *if_inst[MAX_IFSN], *loop_inst[MAX_LOOP_DEPTH];
1131 struct brw_instruction *inst0, *inst1;
1132 int i, if_insn = 0, loop_insn = 0;
1133 struct brw_compile *p = &c->func;
1134 struct brw_indirect stack_index = brw_indirect(0, 0);
1136 brw_init_compile(&c->func);
1139 brw_set_compression_control(p, BRW_COMPRESSION_NONE);
1140 brw_MOV(p, get_addr_reg(stack_index), brw_address(c->stack));
1142 for (i = 0; i < c->nr_fp_insns; i++) {
1143 struct prog_instruction *inst = &c->prog_instructions[i];
1144 struct prog_instruction *orig_inst;
1146 if ((orig_inst = inst->Data) != 0)
1147 orig_inst->Data = current_insn(p);
1149 if (inst->CondUpdate)
1150 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
1152 brw_set_conditionalmod(p, BRW_CONDITIONAL_NONE);
1154 switch (inst->Opcode) {
1156 emit_pixel_xy(c, inst);
1159 emit_delta_xy(c, inst);
1162 emit_pixel_w(c, inst);
1165 emit_linterp(c, inst);
1168 emit_pinterp(c, inst);
1171 emit_cinterp(c, inst);
1174 emit_wpos_xy(c, inst);
1177 emit_fb_write(c, inst);
1282 assert(if_insn < MAX_IFSN);
1283 if_inst[if_insn++] = brw_IF(p, BRW_EXECUTE_8);
1286 if_inst[if_insn-1] = brw_ELSE(p, if_inst[if_insn-1]);
1289 assert(if_insn > 0);
1290 brw_ENDIF(p, if_inst[--if_insn]);
1296 brw_push_insn_state(p);
1297 brw_set_mask_control(p, BRW_MASK_DISABLE);
1298 brw_set_access_mode(p, BRW_ALIGN_1);
1299 brw_ADD(p, deref_1ud(stack_index, 0), brw_ip_reg(), brw_imm_d(3*16));
1300 brw_set_access_mode(p, BRW_ALIGN_16);
1301 brw_ADD(p, get_addr_reg(stack_index),
1302 get_addr_reg(stack_index), brw_imm_d(4));
1303 orig_inst = inst->Data;
1304 orig_inst->Data = &p->store[p->nr_insn];
1305 brw_ADD(p, brw_ip_reg(), brw_ip_reg(), brw_imm_d(1*16));
1306 brw_pop_insn_state(p);
1310 brw_push_insn_state(p);
1311 brw_set_mask_control(p, BRW_MASK_DISABLE);
1312 brw_ADD(p, get_addr_reg(stack_index),
1313 get_addr_reg(stack_index), brw_imm_d(-4));
1314 brw_set_access_mode(p, BRW_ALIGN_1);
1315 brw_MOV(p, brw_ip_reg(), deref_1ud(stack_index, 0));
1316 brw_set_access_mode(p, BRW_ALIGN_16);
1317 brw_pop_insn_state(p);
1320 case OPCODE_BGNLOOP:
1321 loop_inst[loop_insn++] = brw_DO(p, BRW_EXECUTE_8);
1325 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
1329 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
1331 case OPCODE_ENDLOOP:
1333 inst0 = inst1 = brw_WHILE(p, loop_inst[loop_insn]);
1334 /* patch all the BREAK instructions from
1336 while (inst0 > loop_inst[loop_insn]) {
1338 if (inst0->header.opcode == BRW_OPCODE_BREAK) {
1339 inst0->bits3.if_else.jump_count = inst1 - inst0 + 1;
1340 inst0->bits3.if_else.pop_count = 0;
1341 } else if (inst0->header.opcode == BRW_OPCODE_CONTINUE) {
1342 inst0->bits3.if_else.jump_count = inst1 - inst0;
1343 inst0->bits3.if_else.pop_count = 0;
1348 _mesa_printf("unsupported IR in fragment shader %d\n",
1351 if (inst->CondUpdate)
1352 brw_set_predicate_control(p, BRW_PREDICATE_NORMAL);
1354 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
1357 for (i = 0; i < c->fp->program.Base.NumInstructions; i++)
1358 c->fp->program.Base.Instructions[i].Data = NULL;
1361 void brw_wm_glsl_emit(struct brw_wm_compile *c)
1365 brw_wm_emit_glsl(c);
1366 c->prog_data.total_grf = c->reg_index;
1367 c->prog_data.total_scratch = 0;