1 #include "main/macros.h"
2 #include "shader/prog_parameter.h"
3 #include "brw_context.h"
8 SUB_NOISE1
, SUB_NOISE2
, SUB_NOISE3
, SUB_NOISE4
11 /* Only guess, need a flag in gl_fragment_program later */
12 GLboolean
brw_wm_is_glsl(const struct gl_fragment_program
*fp
)
15 for (i
= 0; i
< fp
->Base
.NumInstructions
; i
++) {
16 struct prog_instruction
*inst
= &fp
->Base
.Instructions
[i
];
17 switch (inst
->Opcode
) {
39 static void set_reg(struct brw_wm_compile
*c
, int file
, int index
,
40 int component
, struct brw_reg reg
)
42 c
->wm_regs
[file
][index
][component
].reg
= reg
;
43 c
->wm_regs
[file
][index
][component
].inited
= GL_TRUE
;
46 static int get_scalar_dst_index(struct prog_instruction
*inst
)
49 for (i
= 0; i
< 4; i
++)
50 if (inst
->DstReg
.WriteMask
& (1<<i
))
55 static struct brw_reg
alloc_tmp(struct brw_wm_compile
*c
)
58 if(c
->tmp_index
== c
->tmp_max
)
59 c
->tmp_regs
[ c
->tmp_max
++ ] = c
->reg_index
++;
61 reg
= brw_vec8_grf(c
->tmp_regs
[ c
->tmp_index
++ ], 0);
65 static int mark_tmps(struct brw_wm_compile
*c
)
70 static struct brw_reg
lookup_tmp( struct brw_wm_compile
*c
, int index
)
72 return brw_vec8_grf( c
->tmp_regs
[ index
], 0 );
75 static void release_tmps(struct brw_wm_compile
*c
, int mark
)
81 get_reg(struct brw_wm_compile
*c
, int file
, int index
, int component
, int nr
, GLuint neg
, GLuint abs
)
85 case PROGRAM_STATE_VAR
:
86 case PROGRAM_CONSTANT
:
88 file
= PROGRAM_STATE_VAR
;
90 case PROGRAM_UNDEFINED
:
91 return brw_null_reg();
96 if(c
->wm_regs
[file
][index
][component
].inited
)
97 reg
= c
->wm_regs
[file
][index
][component
].reg
;
99 reg
= brw_vec8_grf(c
->reg_index
, 0);
101 if(!c
->wm_regs
[file
][index
][component
].inited
) {
102 set_reg(c
, file
, index
, component
, reg
);
106 if (neg
& (1<< component
)) {
114 static void prealloc_reg(struct brw_wm_compile
*c
)
118 int nr_interp_regs
= 0;
119 GLuint inputs
= FRAG_BIT_WPOS
| c
->fp_interp_emitted
| c
->fp_deriv_emitted
;
121 for (i
= 0; i
< 4; i
++) {
122 reg
= (i
< c
->key
.nr_depth_regs
)
123 ? brw_vec8_grf(i
*2, 0) : brw_vec8_grf(0, 0);
124 set_reg(c
, PROGRAM_PAYLOAD
, PAYLOAD_DEPTH
, i
, reg
);
126 c
->reg_index
+= 2*c
->key
.nr_depth_regs
;
128 int nr_params
= c
->fp
->program
.Base
.Parameters
->NumParameters
;
129 struct gl_program_parameter_list
*plist
=
130 c
->fp
->program
.Base
.Parameters
;
132 c
->prog_data
.nr_params
= 4*nr_params
;
133 for (i
= 0; i
< nr_params
; i
++) {
134 for (j
= 0; j
< 4; j
++, index
++) {
135 reg
= brw_vec1_grf(c
->reg_index
+ index
/8,
137 c
->prog_data
.param
[index
] =
138 &plist
->ParameterValues
[i
][j
];
139 set_reg(c
, PROGRAM_STATE_VAR
, i
, j
, reg
);
142 c
->nr_creg
= 2*((4*nr_params
+15)/16);
143 c
->reg_index
+= c
->nr_creg
;
145 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
146 if (inputs
& (1<<i
)) {
148 reg
= brw_vec8_grf(c
->reg_index
, 0);
149 for (j
= 0; j
< 4; j
++)
150 set_reg(c
, PROGRAM_PAYLOAD
, i
, j
, reg
);
155 c
->prog_data
.first_curbe_grf
= c
->key
.nr_depth_regs
* 2;
156 c
->prog_data
.urb_read_length
= nr_interp_regs
* 2;
157 c
->prog_data
.curb_read_length
= c
->nr_creg
;
158 c
->emit_mask_reg
= brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, c
->reg_index
, 0);
160 c
->stack
= brw_uw16_reg(BRW_GENERAL_REGISTER_FILE
, c
->reg_index
, 0);
164 static struct brw_reg
get_dst_reg(struct brw_wm_compile
*c
,
165 struct prog_instruction
*inst
, int component
, int nr
)
167 return get_reg(c
, inst
->DstReg
.File
, inst
->DstReg
.Index
, component
, nr
,
171 static struct brw_reg
get_src_reg(struct brw_wm_compile
*c
,
172 struct prog_src_register
*src
, int index
, int nr
)
174 int component
= GET_SWZ(src
->Swizzle
, index
);
175 return get_reg(c
, src
->File
, src
->Index
, component
, nr
,
176 src
->NegateBase
, src
->Abs
);
179 /* Subroutines are minimal support for resusable instruction sequences.
180 They are implemented as simply as possible to minimise overhead: there
181 is no explicit support for communication between the caller and callee
182 other than saving the return address in a temporary register, nor is
183 there any automatic local storage. This implies that great care is
184 required before attempting reentrancy or any kind of nested
185 subroutine invocations. */
186 static void invoke_subroutine( struct brw_wm_compile
*c
,
187 enum _subroutine subroutine
,
188 void (*emit
)( struct brw_wm_compile
* ) )
190 struct brw_compile
*p
= &c
->func
;
192 assert( subroutine
< BRW_WM_MAX_SUBROUTINE
);
194 if( c
->subroutines
[ subroutine
] ) {
195 /* subroutine previously emitted: reuse existing instructions */
197 int mark
= mark_tmps( c
);
198 struct brw_reg return_address
= retype( alloc_tmp( c
),
199 BRW_REGISTER_TYPE_UD
);
200 int here
= p
->nr_insn
;
202 brw_push_insn_state(p
);
203 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
204 brw_ADD( p
, return_address
, brw_ip_reg(), brw_imm_ud( 2 << 4 ) );
206 brw_ADD( p
, brw_ip_reg(), brw_ip_reg(),
207 brw_imm_d( ( c
->subroutines
[ subroutine
] -
209 brw_pop_insn_state(p
);
211 release_tmps( c
, mark
);
213 /* previously unused subroutine: emit, and mark for later reuse */
215 int mark
= mark_tmps( c
);
216 struct brw_reg return_address
= retype( alloc_tmp( c
),
217 BRW_REGISTER_TYPE_UD
);
218 struct brw_instruction
*calc
;
219 int base
= p
->nr_insn
;
221 brw_push_insn_state(p
);
222 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
223 calc
= brw_ADD( p
, return_address
, brw_ip_reg(), brw_imm_ud( 0 ) );
224 brw_pop_insn_state(p
);
226 c
->subroutines
[ subroutine
] = p
->nr_insn
;
230 brw_push_insn_state(p
);
231 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
232 brw_MOV( p
, brw_ip_reg(), return_address
);
233 brw_pop_insn_state(p
);
235 brw_set_src1( calc
, brw_imm_ud( ( p
->nr_insn
- base
) << 4 ) );
237 release_tmps( c
, mark
);
241 static void emit_abs( struct brw_wm_compile
*c
,
242 struct prog_instruction
*inst
)
245 struct brw_compile
*p
= &c
->func
;
246 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
247 for (i
= 0; i
< 4; i
++) {
248 if (inst
->DstReg
.WriteMask
& (1<<i
)) {
249 struct brw_reg src
, dst
;
250 dst
= get_dst_reg(c
, inst
, i
, 1);
251 src
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
252 brw_MOV(p
, dst
, brw_abs(src
));
255 brw_set_saturate(p
, 0);
258 static void emit_trunc( struct brw_wm_compile
*c
,
259 struct prog_instruction
*inst
)
262 struct brw_compile
*p
= &c
->func
;
263 GLuint mask
= inst
->DstReg
.WriteMask
;
264 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
265 for (i
= 0; i
< 4; i
++) {
267 struct brw_reg src
, dst
;
268 dst
= get_dst_reg(c
, inst
, i
, 1) ;
269 src
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
270 brw_RNDZ(p
, dst
, src
);
273 brw_set_saturate(p
, 0);
276 static void emit_mov( struct brw_wm_compile
*c
,
277 struct prog_instruction
*inst
)
280 struct brw_compile
*p
= &c
->func
;
281 GLuint mask
= inst
->DstReg
.WriteMask
;
282 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
283 for (i
= 0; i
< 4; i
++) {
285 struct brw_reg src
, dst
;
286 dst
= get_dst_reg(c
, inst
, i
, 1);
287 src
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
288 brw_MOV(p
, dst
, src
);
291 brw_set_saturate(p
, 0);
294 static void emit_pixel_xy(struct brw_wm_compile
*c
,
295 struct prog_instruction
*inst
)
297 struct brw_reg r1
= brw_vec1_grf(1, 0);
298 struct brw_reg r1_uw
= retype(r1
, BRW_REGISTER_TYPE_UW
);
300 struct brw_reg dst0
, dst1
;
301 struct brw_compile
*p
= &c
->func
;
302 GLuint mask
= inst
->DstReg
.WriteMask
;
304 dst0
= get_dst_reg(c
, inst
, 0, 1);
305 dst1
= get_dst_reg(c
, inst
, 1, 1);
306 /* Calculate pixel centers by adding 1 or 0 to each of the
307 * micro-tile coordinates passed in r1.
309 if (mask
& WRITEMASK_X
) {
311 vec8(retype(dst0
, BRW_REGISTER_TYPE_UW
)),
312 stride(suboffset(r1_uw
, 4), 2, 4, 0),
313 brw_imm_v(0x10101010));
316 if (mask
& WRITEMASK_Y
) {
318 vec8(retype(dst1
, BRW_REGISTER_TYPE_UW
)),
319 stride(suboffset(r1_uw
, 5), 2, 4, 0),
320 brw_imm_v(0x11001100));
325 static void emit_delta_xy(struct brw_wm_compile
*c
,
326 struct prog_instruction
*inst
)
328 struct brw_reg r1
= brw_vec1_grf(1, 0);
329 struct brw_reg dst0
, dst1
, src0
, src1
;
330 struct brw_compile
*p
= &c
->func
;
331 GLuint mask
= inst
->DstReg
.WriteMask
;
333 dst0
= get_dst_reg(c
, inst
, 0, 1);
334 dst1
= get_dst_reg(c
, inst
, 1, 1);
335 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
336 src1
= get_src_reg(c
, &inst
->SrcReg
[0], 1, 1);
337 /* Calc delta X,Y by subtracting origin in r1 from the pixel
340 if (mask
& WRITEMASK_X
) {
343 retype(src0
, BRW_REGISTER_TYPE_UW
),
347 if (mask
& WRITEMASK_Y
) {
350 retype(src1
, BRW_REGISTER_TYPE_UW
),
351 negate(suboffset(r1
,1)));
358 static void fire_fb_write( struct brw_wm_compile
*c
,
364 struct brw_compile
*p
= &c
->func
;
365 /* Pass through control information:
367 /* mov (8) m1.0<1>:ud r1.0<8;8,1>:ud { Align1 NoMask } */
369 brw_push_insn_state(p
);
370 brw_set_mask_control(p
, BRW_MASK_DISABLE
); /* ? */
372 brw_message_reg(base_reg
+ 1),
374 brw_pop_insn_state(p
);
376 /* Send framebuffer write message: */
378 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW
),
380 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
387 static void emit_fb_write(struct brw_wm_compile
*c
,
388 struct prog_instruction
*inst
)
390 struct brw_compile
*p
= &c
->func
;
396 /* Reserve a space for AA - may not be needed:
398 if (c
->key
.aa_dest_stencil_reg
)
401 brw_push_insn_state(p
);
402 for (channel
= 0; channel
< 4; channel
++) {
403 src0
= get_src_reg(c
, &inst
->SrcReg
[0], channel
, 1);
404 /* mov (8) m2.0<1>:ud r28.0<8;8,1>:ud { Align1 } */
405 /* mov (8) m6.0<1>:ud r29.0<8;8,1>:ud { Align1 SecHalf } */
406 brw_MOV(p
, brw_message_reg(nr
+ channel
), src0
);
408 /* skip over the regs populated above: */
410 brw_pop_insn_state(p
);
413 if (c
->key
.source_depth_to_render_target
)
415 if (c
->key
.computes_depth
) {
416 src0
= get_src_reg(c
, &inst
->SrcReg
[2], 2, 1);
417 brw_MOV(p
, brw_message_reg(nr
), src0
);
419 src0
= get_src_reg(c
, &inst
->SrcReg
[1], 1, 1);
420 brw_MOV(p
, brw_message_reg(nr
), src0
);
425 target
= inst
->Sampler
>> 1;
426 eot
= inst
->Sampler
& 1;
427 fire_fb_write(c
, 0, nr
, target
, eot
);
430 static void emit_pixel_w( struct brw_wm_compile
*c
,
431 struct prog_instruction
*inst
)
433 struct brw_compile
*p
= &c
->func
;
434 GLuint mask
= inst
->DstReg
.WriteMask
;
435 if (mask
& WRITEMASK_W
) {
436 struct brw_reg dst
, src0
, delta0
, delta1
;
437 struct brw_reg interp3
;
439 dst
= get_dst_reg(c
, inst
, 3, 1);
440 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
441 delta0
= get_src_reg(c
, &inst
->SrcReg
[1], 0, 1);
442 delta1
= get_src_reg(c
, &inst
->SrcReg
[1], 1, 1);
444 interp3
= brw_vec1_grf(src0
.nr
+1, 4);
445 /* Calc 1/w - just linterp wpos[3] optimized by putting the
446 * result straight into a message reg.
448 brw_LINE(p
, brw_null_reg(), interp3
, delta0
);
449 brw_MAC(p
, brw_message_reg(2), suboffset(interp3
, 1), delta1
);
453 BRW_MATH_FUNCTION_INV
,
454 BRW_MATH_SATURATE_NONE
,
456 BRW_MATH_PRECISION_FULL
);
460 static void emit_linterp(struct brw_wm_compile
*c
,
461 struct prog_instruction
*inst
)
463 struct brw_compile
*p
= &c
->func
;
464 GLuint mask
= inst
->DstReg
.WriteMask
;
465 struct brw_reg interp
[4];
466 struct brw_reg dst
, delta0
, delta1
;
469 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
470 delta0
= get_src_reg(c
, &inst
->SrcReg
[1], 0, 1);
471 delta1
= get_src_reg(c
, &inst
->SrcReg
[1], 1, 1);
475 interp
[0] = brw_vec1_grf(nr
, 0);
476 interp
[1] = brw_vec1_grf(nr
, 4);
477 interp
[2] = brw_vec1_grf(nr
+1, 0);
478 interp
[3] = brw_vec1_grf(nr
+1, 4);
480 for(i
= 0; i
< 4; i
++ ) {
482 dst
= get_dst_reg(c
, inst
, i
, 1);
483 brw_LINE(p
, brw_null_reg(), interp
[i
], delta0
);
484 brw_MAC(p
, dst
, suboffset(interp
[i
],1), delta1
);
489 static void emit_cinterp(struct brw_wm_compile
*c
,
490 struct prog_instruction
*inst
)
492 struct brw_compile
*p
= &c
->func
;
493 GLuint mask
= inst
->DstReg
.WriteMask
;
495 struct brw_reg interp
[4];
496 struct brw_reg dst
, src0
;
498 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
502 interp
[0] = brw_vec1_grf(nr
, 0);
503 interp
[1] = brw_vec1_grf(nr
, 4);
504 interp
[2] = brw_vec1_grf(nr
+1, 0);
505 interp
[3] = brw_vec1_grf(nr
+1, 4);
507 for(i
= 0; i
< 4; i
++ ) {
509 dst
= get_dst_reg(c
, inst
, i
, 1);
510 brw_MOV(p
, dst
, suboffset(interp
[i
],3));
515 static void emit_pinterp(struct brw_wm_compile
*c
,
516 struct prog_instruction
*inst
)
518 struct brw_compile
*p
= &c
->func
;
519 GLuint mask
= inst
->DstReg
.WriteMask
;
521 struct brw_reg interp
[4];
522 struct brw_reg dst
, delta0
, delta1
;
523 struct brw_reg src0
, w
;
525 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
526 delta0
= get_src_reg(c
, &inst
->SrcReg
[1], 0, 1);
527 delta1
= get_src_reg(c
, &inst
->SrcReg
[1], 1, 1);
528 w
= get_src_reg(c
, &inst
->SrcReg
[2], 3, 1);
532 interp
[0] = brw_vec1_grf(nr
, 0);
533 interp
[1] = brw_vec1_grf(nr
, 4);
534 interp
[2] = brw_vec1_grf(nr
+1, 0);
535 interp
[3] = brw_vec1_grf(nr
+1, 4);
537 for(i
= 0; i
< 4; i
++ ) {
539 dst
= get_dst_reg(c
, inst
, i
, 1);
540 brw_LINE(p
, brw_null_reg(), interp
[i
], delta0
);
541 brw_MAC(p
, dst
, suboffset(interp
[i
],1),
543 brw_MUL(p
, dst
, dst
, w
);
548 static void emit_xpd(struct brw_wm_compile
*c
,
549 struct prog_instruction
*inst
)
552 struct brw_compile
*p
= &c
->func
;
553 GLuint mask
= inst
->DstReg
.WriteMask
;
554 for (i
= 0; i
< 4; i
++) {
558 struct brw_reg src0
, src1
, dst
;
559 dst
= get_dst_reg(c
, inst
, i
, 1);
560 src0
= negate(get_src_reg(c
, &inst
->SrcReg
[0], i2
, 1));
561 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i1
, 1);
562 brw_MUL(p
, brw_null_reg(), src0
, src1
);
563 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i1
, 1);
564 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i2
, 1);
565 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
566 brw_MAC(p
, dst
, src0
, src1
);
567 brw_set_saturate(p
, 0);
570 brw_set_saturate(p
, 0);
573 static void emit_dp3(struct brw_wm_compile
*c
,
574 struct prog_instruction
*inst
)
576 struct brw_reg src0
[3], src1
[3], dst
;
578 struct brw_compile
*p
= &c
->func
;
579 for (i
= 0; i
< 3; i
++) {
580 src0
[i
] = get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
581 src1
[i
] = get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
584 dst
= get_dst_reg(c
, inst
, get_scalar_dst_index(inst
), 1);
585 brw_MUL(p
, brw_null_reg(), src0
[0], src1
[0]);
586 brw_MAC(p
, brw_null_reg(), src0
[1], src1
[1]);
587 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
588 brw_MAC(p
, dst
, src0
[2], src1
[2]);
589 brw_set_saturate(p
, 0);
592 static void emit_dp4(struct brw_wm_compile
*c
,
593 struct prog_instruction
*inst
)
595 struct brw_reg src0
[4], src1
[4], dst
;
597 struct brw_compile
*p
= &c
->func
;
598 for (i
= 0; i
< 4; i
++) {
599 src0
[i
] = get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
600 src1
[i
] = get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
602 dst
= get_dst_reg(c
, inst
, get_scalar_dst_index(inst
), 1);
603 brw_MUL(p
, brw_null_reg(), src0
[0], src1
[0]);
604 brw_MAC(p
, brw_null_reg(), src0
[1], src1
[1]);
605 brw_MAC(p
, brw_null_reg(), src0
[2], src1
[2]);
606 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
607 brw_MAC(p
, dst
, src0
[3], src1
[3]);
608 brw_set_saturate(p
, 0);
611 static void emit_dph(struct brw_wm_compile
*c
,
612 struct prog_instruction
*inst
)
614 struct brw_reg src0
[4], src1
[4], dst
;
616 struct brw_compile
*p
= &c
->func
;
617 for (i
= 0; i
< 4; i
++) {
618 src0
[i
] = get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
619 src1
[i
] = get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
621 dst
= get_dst_reg(c
, inst
, get_scalar_dst_index(inst
), 1);
622 brw_MUL(p
, brw_null_reg(), src0
[0], src1
[0]);
623 brw_MAC(p
, brw_null_reg(), src0
[1], src1
[1]);
624 brw_MAC(p
, dst
, src0
[2], src1
[2]);
625 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
626 brw_ADD(p
, dst
, dst
, src1
[3]);
627 brw_set_saturate(p
, 0);
630 static void emit_math1(struct brw_wm_compile
*c
,
631 struct prog_instruction
*inst
, GLuint func
)
633 struct brw_compile
*p
= &c
->func
;
634 struct brw_reg src0
, dst
;
636 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
637 dst
= get_dst_reg(c
, inst
, get_scalar_dst_index(inst
), 1);
638 brw_MOV(p
, brw_message_reg(2), src0
);
642 (inst
->SaturateMode
!= SATURATE_OFF
) ? BRW_MATH_SATURATE_SATURATE
: BRW_MATH_SATURATE_NONE
,
645 BRW_MATH_DATA_VECTOR
,
646 BRW_MATH_PRECISION_FULL
);
649 static void emit_rcp(struct brw_wm_compile
*c
,
650 struct prog_instruction
*inst
)
652 emit_math1(c
, inst
, BRW_MATH_FUNCTION_INV
);
655 static void emit_rsq(struct brw_wm_compile
*c
,
656 struct prog_instruction
*inst
)
658 emit_math1(c
, inst
, BRW_MATH_FUNCTION_RSQ
);
661 static void emit_sin(struct brw_wm_compile
*c
,
662 struct prog_instruction
*inst
)
664 emit_math1(c
, inst
, BRW_MATH_FUNCTION_SIN
);
667 static void emit_cos(struct brw_wm_compile
*c
,
668 struct prog_instruction
*inst
)
670 emit_math1(c
, inst
, BRW_MATH_FUNCTION_COS
);
673 static void emit_ex2(struct brw_wm_compile
*c
,
674 struct prog_instruction
*inst
)
676 emit_math1(c
, inst
, BRW_MATH_FUNCTION_EXP
);
679 static void emit_lg2(struct brw_wm_compile
*c
,
680 struct prog_instruction
*inst
)
682 emit_math1(c
, inst
, BRW_MATH_FUNCTION_LOG
);
685 static void emit_add(struct brw_wm_compile
*c
,
686 struct prog_instruction
*inst
)
688 struct brw_compile
*p
= &c
->func
;
689 struct brw_reg src0
, src1
, dst
;
690 GLuint mask
= inst
->DstReg
.WriteMask
;
692 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
693 for (i
= 0 ; i
< 4; i
++) {
695 dst
= get_dst_reg(c
, inst
, i
, 1);
696 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
697 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
698 brw_ADD(p
, dst
, src0
, src1
);
701 brw_set_saturate(p
, 0);
704 static void emit_sub(struct brw_wm_compile
*c
,
705 struct prog_instruction
*inst
)
707 struct brw_compile
*p
= &c
->func
;
708 struct brw_reg src0
, src1
, dst
;
709 GLuint mask
= inst
->DstReg
.WriteMask
;
711 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
712 for (i
= 0 ; i
< 4; i
++) {
714 dst
= get_dst_reg(c
, inst
, i
, 1);
715 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
716 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
717 brw_ADD(p
, dst
, src0
, negate(src1
));
720 brw_set_saturate(p
, 0);
723 static void emit_mul(struct brw_wm_compile
*c
,
724 struct prog_instruction
*inst
)
726 struct brw_compile
*p
= &c
->func
;
727 struct brw_reg src0
, src1
, dst
;
728 GLuint mask
= inst
->DstReg
.WriteMask
;
730 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
731 for (i
= 0 ; i
< 4; i
++) {
733 dst
= get_dst_reg(c
, inst
, i
, 1);
734 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
735 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
736 brw_MUL(p
, dst
, src0
, src1
);
739 brw_set_saturate(p
, 0);
742 static void emit_frc(struct brw_wm_compile
*c
,
743 struct prog_instruction
*inst
)
745 struct brw_compile
*p
= &c
->func
;
746 struct brw_reg src0
, dst
;
747 GLuint mask
= inst
->DstReg
.WriteMask
;
749 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
750 for (i
= 0 ; i
< 4; i
++) {
752 dst
= get_dst_reg(c
, inst
, i
, 1);
753 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
754 brw_FRC(p
, dst
, src0
);
757 if (inst
->SaturateMode
!= SATURATE_OFF
)
758 brw_set_saturate(p
, 0);
761 static void emit_flr(struct brw_wm_compile
*c
,
762 struct prog_instruction
*inst
)
764 struct brw_compile
*p
= &c
->func
;
765 struct brw_reg src0
, dst
;
766 GLuint mask
= inst
->DstReg
.WriteMask
;
768 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
769 for (i
= 0 ; i
< 4; i
++) {
771 dst
= get_dst_reg(c
, inst
, i
, 1);
772 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
773 brw_RNDD(p
, dst
, src0
);
776 brw_set_saturate(p
, 0);
779 static void emit_max(struct brw_wm_compile
*c
,
780 struct prog_instruction
*inst
)
782 struct brw_compile
*p
= &c
->func
;
783 GLuint mask
= inst
->DstReg
.WriteMask
;
784 struct brw_reg src0
, src1
, dst
;
786 brw_push_insn_state(p
);
787 for (i
= 0; i
< 4; i
++) {
789 dst
= get_dst_reg(c
, inst
, i
, 1);
790 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
791 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
792 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
793 brw_MOV(p
, dst
, src0
);
794 brw_set_saturate(p
, 0);
796 brw_CMP(p
, brw_null_reg(), BRW_CONDITIONAL_L
, src0
, src1
);
797 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
798 brw_set_predicate_control(p
, BRW_PREDICATE_NORMAL
);
799 brw_MOV(p
, dst
, src1
);
800 brw_set_saturate(p
, 0);
801 brw_set_predicate_control_flag_value(p
, 0xff);
804 brw_pop_insn_state(p
);
807 static void emit_min(struct brw_wm_compile
*c
,
808 struct prog_instruction
*inst
)
810 struct brw_compile
*p
= &c
->func
;
811 GLuint mask
= inst
->DstReg
.WriteMask
;
812 struct brw_reg src0
, src1
, dst
;
814 brw_push_insn_state(p
);
815 for (i
= 0; i
< 4; i
++) {
817 dst
= get_dst_reg(c
, inst
, i
, 1);
818 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
819 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
820 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
821 brw_MOV(p
, dst
, src0
);
822 brw_set_saturate(p
, 0);
824 brw_CMP(p
, brw_null_reg(), BRW_CONDITIONAL_L
, src1
, src0
);
825 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
826 brw_set_predicate_control(p
, BRW_PREDICATE_NORMAL
);
827 brw_MOV(p
, dst
, src1
);
828 brw_set_saturate(p
, 0);
829 brw_set_predicate_control_flag_value(p
, 0xff);
832 brw_pop_insn_state(p
);
835 static void emit_pow(struct brw_wm_compile
*c
,
836 struct prog_instruction
*inst
)
838 struct brw_compile
*p
= &c
->func
;
839 struct brw_reg dst
, src0
, src1
;
840 dst
= get_dst_reg(c
, inst
, get_scalar_dst_index(inst
), 1);
841 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
842 src1
= get_src_reg(c
, &inst
->SrcReg
[1], 0, 1);
844 brw_MOV(p
, brw_message_reg(2), src0
);
845 brw_MOV(p
, brw_message_reg(3), src1
);
849 BRW_MATH_FUNCTION_POW
,
850 (inst
->SaturateMode
!= SATURATE_OFF
) ? BRW_MATH_SATURATE_SATURATE
: BRW_MATH_SATURATE_NONE
,
853 BRW_MATH_DATA_VECTOR
,
854 BRW_MATH_PRECISION_FULL
);
857 static void emit_lrp(struct brw_wm_compile
*c
,
858 struct prog_instruction
*inst
)
860 struct brw_compile
*p
= &c
->func
;
861 GLuint mask
= inst
->DstReg
.WriteMask
;
862 struct brw_reg dst
, tmp1
, tmp2
, src0
, src1
, src2
;
864 int mark
= mark_tmps(c
);
865 for (i
= 0; i
< 4; i
++) {
867 dst
= get_dst_reg(c
, inst
, i
, 1);
868 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
870 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
872 if (src1
.nr
== dst
.nr
) {
874 brw_MOV(p
, tmp1
, src1
);
878 src2
= get_src_reg(c
, &inst
->SrcReg
[2], i
, 1);
879 if (src2
.nr
== dst
.nr
) {
881 brw_MOV(p
, tmp2
, src2
);
885 brw_ADD(p
, dst
, negate(src0
), brw_imm_f(1.0));
886 brw_MUL(p
, brw_null_reg(), dst
, tmp2
);
887 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
888 brw_MAC(p
, dst
, src0
, tmp1
);
889 brw_set_saturate(p
, 0);
891 release_tmps(c
, mark
);
896 * For GLSL shaders, this KIL will be unconditional.
897 * It may be contained inside an IF/ENDIF structure of course.
899 static void emit_kil(struct brw_wm_compile
*c
)
901 struct brw_compile
*p
= &c
->func
;
902 struct brw_reg depth
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
903 brw_push_insn_state(p
);
904 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
905 brw_NOT(p
, c
->emit_mask_reg
, brw_mask_reg(1)); //IMASK
906 brw_AND(p
, depth
, c
->emit_mask_reg
, depth
);
907 brw_pop_insn_state(p
);
910 static void emit_mad(struct brw_wm_compile
*c
,
911 struct prog_instruction
*inst
)
913 struct brw_compile
*p
= &c
->func
;
914 GLuint mask
= inst
->DstReg
.WriteMask
;
915 struct brw_reg dst
, src0
, src1
, src2
;
918 for (i
= 0; i
< 4; i
++) {
920 dst
= get_dst_reg(c
, inst
, i
, 1);
921 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
922 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
923 src2
= get_src_reg(c
, &inst
->SrcReg
[2], i
, 1);
924 brw_MUL(p
, dst
, src0
, src1
);
926 brw_set_saturate(p
, (inst
->SaturateMode
!= SATURATE_OFF
) ? 1 : 0);
927 brw_ADD(p
, dst
, dst
, src2
);
928 brw_set_saturate(p
, 0);
933 static void emit_sop(struct brw_wm_compile
*c
,
934 struct prog_instruction
*inst
, GLuint cond
)
936 struct brw_compile
*p
= &c
->func
;
937 GLuint mask
= inst
->DstReg
.WriteMask
;
938 struct brw_reg dst
, src0
, src1
;
941 for (i
= 0; i
< 4; i
++) {
943 dst
= get_dst_reg(c
, inst
, i
, 1);
944 src0
= get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
945 src1
= get_src_reg(c
, &inst
->SrcReg
[1], i
, 1);
946 brw_push_insn_state(p
);
947 brw_CMP(p
, brw_null_reg(), cond
, src0
, src1
);
948 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
949 brw_MOV(p
, dst
, brw_imm_f(0.0));
950 brw_set_predicate_control(p
, BRW_PREDICATE_NORMAL
);
951 brw_MOV(p
, dst
, brw_imm_f(1.0));
952 brw_pop_insn_state(p
);
957 static void emit_slt(struct brw_wm_compile
*c
,
958 struct prog_instruction
*inst
)
960 emit_sop(c
, inst
, BRW_CONDITIONAL_L
);
963 static void emit_sle(struct brw_wm_compile
*c
,
964 struct prog_instruction
*inst
)
966 emit_sop(c
, inst
, BRW_CONDITIONAL_LE
);
969 static void emit_sgt(struct brw_wm_compile
*c
,
970 struct prog_instruction
*inst
)
972 emit_sop(c
, inst
, BRW_CONDITIONAL_G
);
975 static void emit_sge(struct brw_wm_compile
*c
,
976 struct prog_instruction
*inst
)
978 emit_sop(c
, inst
, BRW_CONDITIONAL_GE
);
981 static void emit_seq(struct brw_wm_compile
*c
,
982 struct prog_instruction
*inst
)
984 emit_sop(c
, inst
, BRW_CONDITIONAL_EQ
);
987 static void emit_sne(struct brw_wm_compile
*c
,
988 struct prog_instruction
*inst
)
990 emit_sop(c
, inst
, BRW_CONDITIONAL_NEQ
);
993 static void emit_ddx(struct brw_wm_compile
*c
,
994 struct prog_instruction
*inst
)
996 struct brw_compile
*p
= &c
->func
;
997 GLuint mask
= inst
->DstReg
.WriteMask
;
998 struct brw_reg interp
[4];
1000 struct brw_reg src0
, w
;
1002 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
1003 w
= get_src_reg(c
, &inst
->SrcReg
[1], 3, 1);
1005 interp
[0] = brw_vec1_grf(nr
, 0);
1006 interp
[1] = brw_vec1_grf(nr
, 4);
1007 interp
[2] = brw_vec1_grf(nr
+1, 0);
1008 interp
[3] = brw_vec1_grf(nr
+1, 4);
1009 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
1010 for(i
= 0; i
< 4; i
++ ) {
1011 if (mask
& (1<<i
)) {
1012 dst
= get_dst_reg(c
, inst
, i
, 1);
1013 brw_MOV(p
, dst
, interp
[i
]);
1014 brw_MUL(p
, dst
, dst
, w
);
1017 brw_set_saturate(p
, 0);
1020 static void emit_ddy(struct brw_wm_compile
*c
,
1021 struct prog_instruction
*inst
)
1023 struct brw_compile
*p
= &c
->func
;
1024 GLuint mask
= inst
->DstReg
.WriteMask
;
1025 struct brw_reg interp
[4];
1027 struct brw_reg src0
, w
;
1030 src0
= get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
1032 w
= get_src_reg(c
, &inst
->SrcReg
[1], 3, 1);
1033 interp
[0] = brw_vec1_grf(nr
, 0);
1034 interp
[1] = brw_vec1_grf(nr
, 4);
1035 interp
[2] = brw_vec1_grf(nr
+1, 0);
1036 interp
[3] = brw_vec1_grf(nr
+1, 4);
1037 brw_set_saturate(p
, inst
->SaturateMode
!= SATURATE_OFF
);
1038 for(i
= 0; i
< 4; i
++ ) {
1039 if (mask
& (1<<i
)) {
1040 dst
= get_dst_reg(c
, inst
, i
, 1);
1041 brw_MOV(p
, dst
, suboffset(interp
[i
], 1));
1042 brw_MUL(p
, dst
, dst
, w
);
1045 brw_set_saturate(p
, 0);
1048 static __inline
struct brw_reg
high_words( struct brw_reg reg
)
1050 return stride( suboffset( retype( reg
, BRW_REGISTER_TYPE_W
), 1 ),
1054 static __inline
struct brw_reg
low_words( struct brw_reg reg
)
1056 return stride( retype( reg
, BRW_REGISTER_TYPE_W
), 0, 8, 2 );
1059 static __inline
struct brw_reg
even_bytes( struct brw_reg reg
)
1061 return stride( retype( reg
, BRW_REGISTER_TYPE_B
), 0, 16, 2 );
1064 static __inline
struct brw_reg
odd_bytes( struct brw_reg reg
)
1066 return stride( suboffset( retype( reg
, BRW_REGISTER_TYPE_B
), 1 ),
1070 /* One-, two- and three-dimensional Perlin noise, similar to the description
1071 in _Improving Noise_, Ken Perlin, Computer Graphics vol. 35 no. 3. */
1072 static void noise1_sub( struct brw_wm_compile
*c
) {
1074 struct brw_compile
*p
= &c
->func
;
1075 struct brw_reg param
,
1076 x0
, x1
, /* gradients at each end */
1077 t
, tmp
[ 2 ], /* float temporaries */
1078 itmp
[ 5 ]; /* unsigned integer temporaries (aliases of floats above) */
1080 int mark
= mark_tmps( c
);
1082 x0
= alloc_tmp( c
);
1083 x1
= alloc_tmp( c
);
1085 tmp
[ 0 ] = alloc_tmp( c
);
1086 tmp
[ 1 ] = alloc_tmp( c
);
1087 itmp
[ 0 ] = retype( tmp
[ 0 ], BRW_REGISTER_TYPE_UD
);
1088 itmp
[ 1 ] = retype( tmp
[ 1 ], BRW_REGISTER_TYPE_UD
);
1089 itmp
[ 2 ] = retype( x0
, BRW_REGISTER_TYPE_UD
);
1090 itmp
[ 3 ] = retype( x1
, BRW_REGISTER_TYPE_UD
);
1091 itmp
[ 4 ] = retype( t
, BRW_REGISTER_TYPE_UD
);
1093 param
= lookup_tmp( c
, mark
- 2 );
1095 brw_set_access_mode( p
, BRW_ALIGN_1
);
1097 brw_MOV( p
, itmp
[ 2 ], brw_imm_ud( 0xBA97 ) ); /* constant used later */
1099 /* Arrange the two end coordinates into scalars (itmp0/itmp1) to
1100 be hashed. Also compute the remainder (offset within the unit
1101 length), interleaved to reduce register dependency penalties. */
1102 brw_RNDD( p
, retype( itmp
[ 0 ], BRW_REGISTER_TYPE_D
), param
);
1103 brw_FRC( p
, param
, param
);
1104 brw_ADD( p
, itmp
[ 1 ], itmp
[ 0 ], brw_imm_ud( 1 ) );
1105 brw_MOV( p
, itmp
[ 3 ], brw_imm_ud( 0x79D9 ) ); /* constant used later */
1106 brw_MOV( p
, itmp
[ 4 ], brw_imm_ud( 0xD5B1 ) ); /* constant used later */
1108 /* We're now ready to perform the hashing. The two hashes are
1109 interleaved for performance. The hash function used is
1110 designed to rapidly achieve avalanche and require only 32x16
1111 bit multiplication, and 16-bit swizzles (which we get for
1112 free). We can't use immediate operands in the multiplies,
1113 because immediates are permitted only in src1 and the 16-bit
1114 factor is permitted only in src0. */
1115 for( i
= 0; i
< 2; i
++ )
1116 brw_MUL( p
, itmp
[ i
], itmp
[ 2 ], itmp
[ i
] );
1117 for( i
= 0; i
< 2; i
++ )
1118 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1119 high_words( itmp
[ i
] ) );
1120 for( i
= 0; i
< 2; i
++ )
1121 brw_MUL( p
, itmp
[ i
], itmp
[ 3 ], itmp
[ i
] );
1122 for( i
= 0; i
< 2; i
++ )
1123 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1124 high_words( itmp
[ i
] ) );
1125 for( i
= 0; i
< 2; i
++ )
1126 brw_MUL( p
, itmp
[ i
], itmp
[ 4 ], itmp
[ i
] );
1127 for( i
= 0; i
< 2; i
++ )
1128 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1129 high_words( itmp
[ i
] ) );
1131 /* Now we want to initialise the two gradients based on the
1132 hashes. Format conversion from signed integer to float leaves
1133 everything scaled too high by a factor of pow( 2, 31 ), but
1134 we correct for that right at the end. */
1135 brw_ADD( p
, t
, param
, brw_imm_f( -1.0 ) );
1136 brw_MOV( p
, x0
, retype( tmp
[ 0 ], BRW_REGISTER_TYPE_D
) );
1137 brw_MOV( p
, x1
, retype( tmp
[ 1 ], BRW_REGISTER_TYPE_D
) );
1139 brw_MUL( p
, x0
, x0
, param
);
1140 brw_MUL( p
, x1
, x1
, t
);
1142 /* We interpolate between the gradients using the polynomial
1143 6t^5 - 15t^4 + 10t^3 (Perlin). */
1144 brw_MUL( p
, tmp
[ 0 ], param
, brw_imm_f( 6.0 ) );
1145 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], brw_imm_f( -15.0 ) );
1146 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param
);
1147 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], brw_imm_f( 10.0 ) );
1148 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param
);
1149 brw_ADD( p
, x1
, x1
, negate( x0
) ); /* unrelated work to fill the
1151 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param
);
1152 brw_MUL( p
, param
, tmp
[ 0 ], param
);
1153 brw_MUL( p
, x1
, x1
, param
);
1154 brw_ADD( p
, x0
, x0
, x1
);
1155 /* scale by pow( 2, -30 ), to compensate for the format conversion
1156 above and an extra factor of 2 so that a single gradient covers
1158 brw_MUL( p
, param
, x0
, brw_imm_f( 0.000000000931322574615478515625 ) );
1160 release_tmps( c
, mark
);
1163 static void emit_noise1( struct brw_wm_compile
*c
,
1164 struct prog_instruction
*inst
)
1166 struct brw_compile
*p
= &c
->func
;
1167 struct brw_reg src
, param
, dst
;
1168 GLuint mask
= inst
->DstReg
.WriteMask
;
1170 int mark
= mark_tmps( c
);
1172 assert( mark
== 0 );
1174 src
= get_src_reg( c
, inst
->SrcReg
, 0, 1 );
1176 param
= alloc_tmp( c
);
1178 brw_MOV( p
, param
, src
);
1180 invoke_subroutine( c
, SUB_NOISE1
, noise1_sub
);
1182 /* Fill in the result: */
1183 brw_set_saturate( p
, inst
->SaturateMode
== SATURATE_ZERO_ONE
);
1184 for (i
= 0 ; i
< 4; i
++) {
1185 if (mask
& (1<<i
)) {
1186 dst
= get_dst_reg(c
, inst
, i
, 1);
1187 brw_MOV( p
, dst
, param
);
1190 if( inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1191 brw_set_saturate( p
, 0 );
1193 release_tmps( c
, mark
);
1196 static void noise2_sub( struct brw_wm_compile
*c
) {
1198 struct brw_compile
*p
= &c
->func
;
1199 struct brw_reg param0
, param1
,
1200 x0y0
, x0y1
, x1y0
, x1y1
, /* gradients at each corner */
1201 t
, tmp
[ 4 ], /* float temporaries */
1202 itmp
[ 7 ]; /* unsigned integer temporaries (aliases of floats above) */
1204 int mark
= mark_tmps( c
);
1206 x0y0
= alloc_tmp( c
);
1207 x0y1
= alloc_tmp( c
);
1208 x1y0
= alloc_tmp( c
);
1209 x1y1
= alloc_tmp( c
);
1211 for( i
= 0; i
< 4; i
++ ) {
1212 tmp
[ i
] = alloc_tmp( c
);
1213 itmp
[ i
] = retype( tmp
[ i
], BRW_REGISTER_TYPE_UD
);
1215 itmp
[ 4 ] = retype( x0y0
, BRW_REGISTER_TYPE_UD
);
1216 itmp
[ 5 ] = retype( x0y1
, BRW_REGISTER_TYPE_UD
);
1217 itmp
[ 6 ] = retype( x1y0
, BRW_REGISTER_TYPE_UD
);
1219 param0
= lookup_tmp( c
, mark
- 3 );
1220 param1
= lookup_tmp( c
, mark
- 2 );
1222 brw_set_access_mode( p
, BRW_ALIGN_1
);
1224 /* Arrange the four corner coordinates into scalars (itmp0..itmp3) to
1225 be hashed. Also compute the remainders (offsets within the unit
1226 square), interleaved to reduce register dependency penalties. */
1227 brw_RNDD( p
, retype( itmp
[ 0 ], BRW_REGISTER_TYPE_D
), param0
);
1228 brw_RNDD( p
, retype( itmp
[ 1 ], BRW_REGISTER_TYPE_D
), param1
);
1229 brw_FRC( p
, param0
, param0
);
1230 brw_FRC( p
, param1
, param1
);
1231 brw_MOV( p
, itmp
[ 4 ], brw_imm_ud( 0xBA97 ) ); /* constant used later */
1232 brw_ADD( p
, high_words( itmp
[ 0 ] ), high_words( itmp
[ 0 ] ),
1233 low_words( itmp
[ 1 ] ) );
1234 brw_MOV( p
, itmp
[ 5 ], brw_imm_ud( 0x79D9 ) ); /* constant used later */
1235 brw_MOV( p
, itmp
[ 6 ], brw_imm_ud( 0xD5B1 ) ); /* constant used later */
1236 brw_ADD( p
, itmp
[ 1 ], itmp
[ 0 ], brw_imm_ud( 0x10000 ) );
1237 brw_ADD( p
, itmp
[ 2 ], itmp
[ 0 ], brw_imm_ud( 0x1 ) );
1238 brw_ADD( p
, itmp
[ 3 ], itmp
[ 0 ], brw_imm_ud( 0x10001 ) );
1240 /* We're now ready to perform the hashing. The four hashes are
1241 interleaved for performance. The hash function used is
1242 designed to rapidly achieve avalanche and require only 32x16
1243 bit multiplication, and 16-bit swizzles (which we get for
1244 free). We can't use immediate operands in the multiplies,
1245 because immediates are permitted only in src1 and the 16-bit
1246 factor is permitted only in src0. */
1247 for( i
= 0; i
< 4; i
++ )
1248 brw_MUL( p
, itmp
[ i
], itmp
[ 4 ], itmp
[ i
] );
1249 for( i
= 0; i
< 4; i
++ )
1250 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1251 high_words( itmp
[ i
] ) );
1252 for( i
= 0; i
< 4; i
++ )
1253 brw_MUL( p
, itmp
[ i
], itmp
[ 5 ], itmp
[ i
] );
1254 for( i
= 0; i
< 4; i
++ )
1255 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1256 high_words( itmp
[ i
] ) );
1257 for( i
= 0; i
< 4; i
++ )
1258 brw_MUL( p
, itmp
[ i
], itmp
[ 6 ], itmp
[ i
] );
1259 for( i
= 0; i
< 4; i
++ )
1260 brw_XOR( p
, low_words( itmp
[ i
] ), low_words( itmp
[ i
] ),
1261 high_words( itmp
[ i
] ) );
1263 /* Now we want to initialise the four gradients based on the
1264 hashes. Format conversion from signed integer to float leaves
1265 everything scaled too high by a factor of pow( 2, 15 ), but
1266 we correct for that right at the end. */
1267 brw_ADD( p
, t
, param0
, brw_imm_f( -1.0 ) );
1268 brw_MOV( p
, x0y0
, low_words( tmp
[ 0 ] ) );
1269 brw_MOV( p
, x0y1
, low_words( tmp
[ 1 ] ) );
1270 brw_MOV( p
, x1y0
, low_words( tmp
[ 2 ] ) );
1271 brw_MOV( p
, x1y1
, low_words( tmp
[ 3 ] ) );
1273 brw_MOV( p
, tmp
[ 0 ], high_words( tmp
[ 0 ] ) );
1274 brw_MOV( p
, tmp
[ 1 ], high_words( tmp
[ 1 ] ) );
1275 brw_MOV( p
, tmp
[ 2 ], high_words( tmp
[ 2 ] ) );
1276 brw_MOV( p
, tmp
[ 3 ], high_words( tmp
[ 3 ] ) );
1278 brw_MUL( p
, x1y0
, x1y0
, t
);
1279 brw_MUL( p
, x1y1
, x1y1
, t
);
1280 brw_ADD( p
, t
, param1
, brw_imm_f( -1.0 ) );
1281 brw_MUL( p
, x0y0
, x0y0
, param0
);
1282 brw_MUL( p
, x0y1
, x0y1
, param0
);
1284 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param1
);
1285 brw_MUL( p
, tmp
[ 2 ], tmp
[ 2 ], param1
);
1286 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], t
);
1287 brw_MUL( p
, tmp
[ 3 ], tmp
[ 3 ], t
);
1289 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 0 ] );
1290 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 2 ] );
1291 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 1 ] );
1292 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 3 ] );
1294 /* We interpolate between the gradients using the polynomial
1295 6t^5 - 15t^4 + 10t^3 (Perlin). */
1296 brw_MUL( p
, tmp
[ 0 ], param0
, brw_imm_f( 6.0 ) );
1297 brw_MUL( p
, tmp
[ 1 ], param1
, brw_imm_f( 6.0 ) );
1298 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], brw_imm_f( -15.0 ) );
1299 brw_ADD( p
, tmp
[ 1 ], tmp
[ 1 ], brw_imm_f( -15.0 ) );
1300 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param0
);
1301 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], param1
);
1302 brw_ADD( p
, x0y1
, x0y1
, negate( x0y0
) ); /* unrelated work to fill the
1304 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], brw_imm_f( 10.0 ) );
1305 brw_ADD( p
, tmp
[ 1 ], tmp
[ 1 ], brw_imm_f( 10.0 ) );
1306 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param0
);
1307 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], param1
);
1308 brw_ADD( p
, x1y1
, x1y1
, negate( x1y0
) ); /* unrelated work to fill the
1310 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], param0
);
1311 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], param1
);
1312 brw_MUL( p
, param0
, tmp
[ 0 ], param0
);
1313 brw_MUL( p
, param1
, tmp
[ 1 ], param1
);
1315 /* Here we interpolate in the y dimension... */
1316 brw_MUL( p
, x0y1
, x0y1
, param1
);
1317 brw_MUL( p
, x1y1
, x1y1
, param1
);
1318 brw_ADD( p
, x0y0
, x0y0
, x0y1
);
1319 brw_ADD( p
, x1y0
, x1y0
, x1y1
);
1321 /* And now in x. There are horrible register dependencies here,
1322 but we have nothing else to do. */
1323 brw_ADD( p
, x1y0
, x1y0
, negate( x0y0
) );
1324 brw_MUL( p
, x1y0
, x1y0
, param0
);
1325 brw_ADD( p
, x0y0
, x0y0
, x1y0
);
1327 /* scale by pow( 2, -15 ), as described above */
1328 brw_MUL( p
, param0
, x0y0
, brw_imm_f( 0.000030517578125 ) );
1330 release_tmps( c
, mark
);
1333 static void emit_noise2( struct brw_wm_compile
*c
,
1334 struct prog_instruction
*inst
)
1336 struct brw_compile
*p
= &c
->func
;
1337 struct brw_reg src0
, src1
, param0
, param1
, dst
;
1338 GLuint mask
= inst
->DstReg
.WriteMask
;
1340 int mark
= mark_tmps( c
);
1342 assert( mark
== 0 );
1344 src0
= get_src_reg( c
, inst
->SrcReg
, 0, 1 );
1345 src1
= get_src_reg( c
, inst
->SrcReg
, 1, 1 );
1347 param0
= alloc_tmp( c
);
1348 param1
= alloc_tmp( c
);
1350 brw_MOV( p
, param0
, src0
);
1351 brw_MOV( p
, param1
, src1
);
1353 invoke_subroutine( c
, SUB_NOISE2
, noise2_sub
);
1355 /* Fill in the result: */
1356 brw_set_saturate( p
, inst
->SaturateMode
== SATURATE_ZERO_ONE
);
1357 for (i
= 0 ; i
< 4; i
++) {
1358 if (mask
& (1<<i
)) {
1359 dst
= get_dst_reg(c
, inst
, i
, 1);
1360 brw_MOV( p
, dst
, param0
);
1363 if( inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1364 brw_set_saturate( p
, 0 );
1366 release_tmps( c
, mark
);
1369 /* The three-dimensional case is much like the one- and two- versions above,
1370 but since the number of corners is rapidly growing we now pack 16 16-bit
1371 hashes into each register to extract more parallelism from the EUs. */
1372 static void noise3_sub( struct brw_wm_compile
*c
) {
1374 struct brw_compile
*p
= &c
->func
;
1375 struct brw_reg param0
, param1
, param2
,
1376 x0y0
, x0y1
, x1y0
, x1y1
, /* gradients at four of the corners */
1377 xi
, yi
, zi
, /* interpolation coefficients */
1378 t
, tmp
[ 8 ], /* float temporaries */
1379 itmp
[ 8 ], /* unsigned integer temporaries (aliases of floats above) */
1380 wtmp
[ 8 ]; /* 16-way unsigned word temporaries (aliases of above) */
1382 int mark
= mark_tmps( c
);
1384 x0y0
= alloc_tmp( c
);
1385 x0y1
= alloc_tmp( c
);
1386 x1y0
= alloc_tmp( c
);
1387 x1y1
= alloc_tmp( c
);
1388 xi
= alloc_tmp( c
);
1389 yi
= alloc_tmp( c
);
1390 zi
= alloc_tmp( c
);
1392 for( i
= 0; i
< 8; i
++ ) {
1393 tmp
[ i
] = alloc_tmp( c
);
1394 itmp
[ i
] = retype( tmp
[ i
], BRW_REGISTER_TYPE_UD
);
1395 wtmp
[ i
] = brw_uw16_grf( tmp
[ i
].nr
, 0 );
1398 param0
= lookup_tmp( c
, mark
- 4 );
1399 param1
= lookup_tmp( c
, mark
- 3 );
1400 param2
= lookup_tmp( c
, mark
- 2 );
1402 brw_set_access_mode( p
, BRW_ALIGN_1
);
1404 /* Arrange the eight corner coordinates into scalars (itmp0..itmp3) to
1405 be hashed. Also compute the remainders (offsets within the unit
1406 cube), interleaved to reduce register dependency penalties. */
1407 brw_RNDD( p
, retype( itmp
[ 0 ], BRW_REGISTER_TYPE_D
), param0
);
1408 brw_RNDD( p
, retype( itmp
[ 1 ], BRW_REGISTER_TYPE_D
), param1
);
1409 brw_RNDD( p
, retype( itmp
[ 2 ], BRW_REGISTER_TYPE_D
), param2
);
1410 brw_FRC( p
, param0
, param0
);
1411 brw_FRC( p
, param1
, param1
);
1412 brw_FRC( p
, param2
, param2
);
1413 /* Since we now have only 16 bits of precision in the hash, we must
1414 be more careful about thorough mixing to maintain entropy as we
1415 squash the input vector into a small scalar. */
1416 brw_MUL( p
, brw_null_reg(), low_words( itmp
[ 0 ] ), brw_imm_uw( 0xBC8F ) );
1417 brw_MAC( p
, brw_null_reg(), low_words( itmp
[ 1 ] ), brw_imm_uw( 0xD0BD ) );
1418 brw_MAC( p
, low_words( itmp
[ 0 ] ), low_words( itmp
[ 2 ] ),
1419 brw_imm_uw( 0x9B93 ) );
1420 brw_ADD( p
, high_words( itmp
[ 0 ] ), low_words( itmp
[ 0 ] ),
1421 brw_imm_uw( 0xBC8F ) );
1423 /* Temporarily disable the execution mask while we work with ExecSize=16
1424 channels (the mask is set for ExecSize=8 and is probably incorrect).
1425 Although this might cause execution of unwanted channels, the code
1426 writes only to temporary registers and has no side effects, so
1427 disabling the mask is harmless. */
1428 brw_push_insn_state( p
);
1429 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1430 brw_ADD( p
, wtmp
[ 1 ], wtmp
[ 0 ], brw_imm_uw( 0xD0BD ) );
1431 brw_ADD( p
, wtmp
[ 2 ], wtmp
[ 0 ], brw_imm_uw( 0x9B93 ) );
1432 brw_ADD( p
, wtmp
[ 3 ], wtmp
[ 1 ], brw_imm_uw( 0x9B93 ) );
1434 /* We're now ready to perform the hashing. The eight hashes are
1435 interleaved for performance. The hash function used is
1436 designed to rapidly achieve avalanche and require only 16x16
1437 bit multiplication, and 8-bit swizzles (which we get for
1439 for( i
= 0; i
< 4; i
++ )
1440 brw_MUL( p
, wtmp
[ i
], wtmp
[ i
], brw_imm_uw( 0x28D9 ) );
1441 for( i
= 0; i
< 4; i
++ )
1442 brw_XOR( p
, even_bytes( wtmp
[ i
] ), even_bytes( wtmp
[ i
] ),
1443 odd_bytes( wtmp
[ i
] ) );
1444 for( i
= 0; i
< 4; i
++ )
1445 brw_MUL( p
, wtmp
[ i
], wtmp
[ i
], brw_imm_uw( 0xC6D5 ) );
1446 for( i
= 0; i
< 4; i
++ )
1447 brw_XOR( p
, even_bytes( wtmp
[ i
] ), even_bytes( wtmp
[ i
] ),
1448 odd_bytes( wtmp
[ i
] ) );
1449 brw_pop_insn_state( p
);
1451 /* Now we want to initialise the four rear gradients based on the
1452 hashes. Format conversion from signed integer to float leaves
1453 everything scaled too high by a factor of pow( 2, 15 ), but
1454 we correct for that right at the end. */
1456 brw_ADD( p
, t
, param0
, brw_imm_f( -1.0 ) );
1457 brw_MOV( p
, x0y0
, low_words( tmp
[ 0 ] ) );
1458 brw_MOV( p
, x0y1
, low_words( tmp
[ 1 ] ) );
1459 brw_MOV( p
, x1y0
, high_words( tmp
[ 0 ] ) );
1460 brw_MOV( p
, x1y1
, high_words( tmp
[ 1 ] ) );
1462 brw_push_insn_state( p
);
1463 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1464 brw_SHL( p
, wtmp
[ 0 ], wtmp
[ 0 ], brw_imm_uw( 5 ) );
1465 brw_SHL( p
, wtmp
[ 1 ], wtmp
[ 1 ], brw_imm_uw( 5 ) );
1466 brw_pop_insn_state( p
);
1468 brw_MUL( p
, x1y0
, x1y0
, t
);
1469 brw_MUL( p
, x1y1
, x1y1
, t
);
1470 brw_ADD( p
, t
, param1
, brw_imm_f( -1.0 ) );
1471 brw_MUL( p
, x0y0
, x0y0
, param0
);
1472 brw_MUL( p
, x0y1
, x0y1
, param0
);
1475 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 1 ] ) );
1476 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 1 ] ) );
1477 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 0 ] ) );
1478 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 0 ] ) );
1480 brw_push_insn_state( p
);
1481 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1482 brw_SHL( p
, wtmp
[ 0 ], wtmp
[ 0 ], brw_imm_uw( 5 ) );
1483 brw_SHL( p
, wtmp
[ 1 ], wtmp
[ 1 ], brw_imm_uw( 5 ) );
1484 brw_pop_insn_state( p
);
1486 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1487 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1488 brw_ADD( p
, t
, param0
, brw_imm_f( -1.0 ) );
1489 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param1
);
1490 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param1
);
1492 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1493 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1494 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1495 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1498 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 0 ] ) );
1499 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 1 ] ) );
1500 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 0 ] ) );
1501 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 1 ] ) );
1503 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param2
);
1504 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], param2
);
1505 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param2
);
1506 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], param2
);
1508 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1509 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1510 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1511 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1513 /* We interpolate between the gradients using the polynomial
1514 6t^5 - 15t^4 + 10t^3 (Perlin). */
1515 brw_MUL( p
, xi
, param0
, brw_imm_f( 6.0 ) );
1516 brw_MUL( p
, yi
, param1
, brw_imm_f( 6.0 ) );
1517 brw_MUL( p
, zi
, param2
, brw_imm_f( 6.0 ) );
1518 brw_ADD( p
, xi
, xi
, brw_imm_f( -15.0 ) );
1519 brw_ADD( p
, yi
, yi
, brw_imm_f( -15.0 ) );
1520 brw_ADD( p
, zi
, zi
, brw_imm_f( -15.0 ) );
1521 brw_MUL( p
, xi
, xi
, param0
);
1522 brw_MUL( p
, yi
, yi
, param1
);
1523 brw_MUL( p
, zi
, zi
, param2
);
1524 brw_ADD( p
, xi
, xi
, brw_imm_f( 10.0 ) );
1525 brw_ADD( p
, yi
, yi
, brw_imm_f( 10.0 ) );
1526 brw_ADD( p
, zi
, zi
, brw_imm_f( 10.0 ) );
1527 brw_ADD( p
, x0y1
, x0y1
, negate( x0y0
) ); /* unrelated work */
1528 brw_ADD( p
, x1y1
, x1y1
, negate( x1y0
) ); /* unrelated work */
1529 brw_MUL( p
, xi
, xi
, param0
);
1530 brw_MUL( p
, yi
, yi
, param1
);
1531 brw_MUL( p
, zi
, zi
, param2
);
1532 brw_MUL( p
, xi
, xi
, param0
);
1533 brw_MUL( p
, yi
, yi
, param1
);
1534 brw_MUL( p
, zi
, zi
, param2
);
1535 brw_MUL( p
, xi
, xi
, param0
);
1536 brw_MUL( p
, yi
, yi
, param1
);
1537 brw_MUL( p
, zi
, zi
, param2
);
1539 /* Here we interpolate in the y dimension... */
1540 brw_MUL( p
, x0y1
, x0y1
, yi
);
1541 brw_MUL( p
, x1y1
, x1y1
, yi
);
1542 brw_ADD( p
, x0y0
, x0y0
, x0y1
);
1543 brw_ADD( p
, x1y0
, x1y0
, x1y1
);
1545 /* And now in x. Leave the result in tmp[ 0 ] (see below)... */
1546 brw_ADD( p
, x1y0
, x1y0
, negate( x0y0
) );
1547 brw_MUL( p
, x1y0
, x1y0
, xi
);
1548 brw_ADD( p
, tmp
[ 0 ], x0y0
, x1y0
);
1550 /* Now do the same thing for the front four gradients... */
1552 brw_MOV( p
, x0y0
, low_words( tmp
[ 2 ] ) );
1553 brw_MOV( p
, x0y1
, low_words( tmp
[ 3 ] ) );
1554 brw_MOV( p
, x1y0
, high_words( tmp
[ 2 ] ) );
1555 brw_MOV( p
, x1y1
, high_words( tmp
[ 3 ] ) );
1557 brw_push_insn_state( p
);
1558 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1559 brw_SHL( p
, wtmp
[ 2 ], wtmp
[ 2 ], brw_imm_uw( 5 ) );
1560 brw_SHL( p
, wtmp
[ 3 ], wtmp
[ 3 ], brw_imm_uw( 5 ) );
1561 brw_pop_insn_state( p
);
1563 brw_MUL( p
, x1y0
, x1y0
, t
);
1564 brw_MUL( p
, x1y1
, x1y1
, t
);
1565 brw_ADD( p
, t
, param1
, brw_imm_f( -1.0 ) );
1566 brw_MUL( p
, x0y0
, x0y0
, param0
);
1567 brw_MUL( p
, x0y1
, x0y1
, param0
);
1570 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 3 ] ) );
1571 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 3 ] ) );
1572 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 2 ] ) );
1573 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 2 ] ) );
1575 brw_push_insn_state( p
);
1576 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1577 brw_SHL( p
, wtmp
[ 2 ], wtmp
[ 2 ], brw_imm_uw( 5 ) );
1578 brw_SHL( p
, wtmp
[ 3 ], wtmp
[ 3 ], brw_imm_uw( 5 ) );
1579 brw_pop_insn_state( p
);
1581 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1582 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1583 brw_ADD( p
, t
, param2
, brw_imm_f( -1.0 ) );
1584 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param1
);
1585 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param1
);
1587 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1588 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1589 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1590 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1593 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 2 ] ) );
1594 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 3 ] ) );
1595 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 2 ] ) );
1596 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 3 ] ) );
1598 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], t
);
1599 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1600 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], t
);
1601 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1603 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1604 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1605 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1606 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1608 /* The interpolation coefficients are still around from last time, so
1609 again interpolate in the y dimension... */
1610 brw_ADD( p
, x0y1
, x0y1
, negate( x0y0
) );
1611 brw_ADD( p
, x1y1
, x1y1
, negate( x1y0
) );
1612 brw_MUL( p
, x0y1
, x0y1
, yi
);
1613 brw_MUL( p
, x1y1
, x1y1
, yi
);
1614 brw_ADD( p
, x0y0
, x0y0
, x0y1
);
1615 brw_ADD( p
, x1y0
, x1y0
, x1y1
);
1617 /* And now in x. The rear face is in tmp[ 0 ] (see above), so this
1618 time put the front face in tmp[ 1 ] and we're nearly there... */
1619 brw_ADD( p
, x1y0
, x1y0
, negate( x0y0
) );
1620 brw_MUL( p
, x1y0
, x1y0
, xi
);
1621 brw_ADD( p
, tmp
[ 1 ], x0y0
, x1y0
);
1623 /* The final interpolation, in the z dimension: */
1624 brw_ADD( p
, tmp
[ 1 ], tmp
[ 1 ], negate( tmp
[ 0 ] ) );
1625 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], zi
);
1626 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], tmp
[ 1 ] );
1628 /* scale by pow( 2, -15 ), as described above */
1629 brw_MUL( p
, param0
, tmp
[ 0 ], brw_imm_f( 0.000030517578125 ) );
1631 release_tmps( c
, mark
);
1634 static void emit_noise3( struct brw_wm_compile
*c
,
1635 struct prog_instruction
*inst
)
1637 struct brw_compile
*p
= &c
->func
;
1638 struct brw_reg src0
, src1
, src2
, param0
, param1
, param2
, dst
;
1639 GLuint mask
= inst
->DstReg
.WriteMask
;
1641 int mark
= mark_tmps( c
);
1643 assert( mark
== 0 );
1645 src0
= get_src_reg( c
, inst
->SrcReg
, 0, 1 );
1646 src1
= get_src_reg( c
, inst
->SrcReg
, 1, 1 );
1647 src2
= get_src_reg( c
, inst
->SrcReg
, 2, 1 );
1649 param0
= alloc_tmp( c
);
1650 param1
= alloc_tmp( c
);
1651 param2
= alloc_tmp( c
);
1653 brw_MOV( p
, param0
, src0
);
1654 brw_MOV( p
, param1
, src1
);
1655 brw_MOV( p
, param2
, src2
);
1657 invoke_subroutine( c
, SUB_NOISE3
, noise3_sub
);
1659 /* Fill in the result: */
1660 brw_set_saturate( p
, inst
->SaturateMode
== SATURATE_ZERO_ONE
);
1661 for (i
= 0 ; i
< 4; i
++) {
1662 if (mask
& (1<<i
)) {
1663 dst
= get_dst_reg(c
, inst
, i
, 1);
1664 brw_MOV( p
, dst
, param0
);
1667 if( inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1668 brw_set_saturate( p
, 0 );
1670 release_tmps( c
, mark
);
1673 /* For the four-dimensional case, the little micro-optimisation benefits
1674 we obtain by unrolling all the loops aren't worth the massive bloat it
1675 now causes. Instead, we loop twice around performing a similar operation
1676 to noise3, once for the w=0 cube and once for the w=1, with a bit more
1677 code to glue it all together. */
1678 static void noise4_sub( struct brw_wm_compile
*c
) {
1680 struct brw_compile
*p
= &c
->func
;
1681 struct brw_reg param
[ 4 ],
1682 x0y0
, x0y1
, x1y0
, x1y1
, /* gradients at four of the corners */
1683 w0
, /* noise for the w=0 cube */
1684 floors
[ 2 ], /* integer coordinates of base corner of hypercube */
1685 interp
[ 4 ], /* interpolation coefficients */
1686 t
, tmp
[ 8 ], /* float temporaries */
1687 itmp
[ 8 ], /* unsigned integer temporaries (aliases of floats above) */
1688 wtmp
[ 8 ]; /* 16-way unsigned word temporaries (aliases of above) */
1690 int mark
= mark_tmps( c
);
1691 GLuint loop
, origin
;
1693 x0y0
= alloc_tmp( c
);
1694 x0y1
= alloc_tmp( c
);
1695 x1y0
= alloc_tmp( c
);
1696 x1y1
= alloc_tmp( c
);
1698 w0
= alloc_tmp( c
);
1699 floors
[ 0 ] = retype( alloc_tmp( c
), BRW_REGISTER_TYPE_UD
);
1700 floors
[ 1 ] = retype( alloc_tmp( c
), BRW_REGISTER_TYPE_UD
);
1702 for( i
= 0; i
< 4; i
++ ) {
1703 param
[ i
] = lookup_tmp( c
, mark
- 5 + i
);
1704 interp
[ i
] = alloc_tmp( c
);
1707 for( i
= 0; i
< 8; i
++ ) {
1708 tmp
[ i
] = alloc_tmp( c
);
1709 itmp
[ i
] = retype( tmp
[ i
], BRW_REGISTER_TYPE_UD
);
1710 wtmp
[ i
] = brw_uw16_grf( tmp
[ i
].nr
, 0 );
1713 brw_set_access_mode( p
, BRW_ALIGN_1
);
1715 /* We only want 16 bits of precision from the integral part of each
1716 co-ordinate, but unfortunately the RNDD semantics would saturate
1717 at 16 bits if we performed the operation directly to a 16-bit
1718 destination. Therefore, we round to 32-bit temporaries where
1719 appropriate, and then store only the lower 16 bits. */
1720 brw_RNDD( p
, retype( floors
[ 0 ], BRW_REGISTER_TYPE_D
), param
[ 0 ] );
1721 brw_RNDD( p
, retype( itmp
[ 0 ], BRW_REGISTER_TYPE_D
), param
[ 1 ] );
1722 brw_RNDD( p
, retype( floors
[ 1 ], BRW_REGISTER_TYPE_D
), param
[ 2 ] );
1723 brw_RNDD( p
, retype( itmp
[ 1 ], BRW_REGISTER_TYPE_D
), param
[ 3 ] );
1724 brw_MOV( p
, high_words( floors
[ 0 ] ), low_words( itmp
[ 0 ] ) );
1725 brw_MOV( p
, high_words( floors
[ 1 ] ), low_words( itmp
[ 1 ] ) );
1727 /* Modify the flag register here, because the side effect is useful
1728 later (see below). We know for certain that all flags will be
1729 cleared, since the FRC instruction cannot possibly generate
1730 negative results. Even for exceptional inputs (infinities, denormals,
1731 NaNs), the architecture guarantees that the L conditional is false. */
1732 brw_set_conditionalmod( p
, BRW_CONDITIONAL_L
);
1733 brw_FRC( p
, param
[ 0 ], param
[ 0 ] );
1734 brw_set_predicate_control( p
, BRW_PREDICATE_NONE
);
1735 for( i
= 1; i
< 4; i
++ )
1736 brw_FRC( p
, param
[ i
], param
[ i
] );
1738 /* Calculate the interpolation coefficients (6t^5 - 15t^4 + 10t^3) first
1740 for( i
= 0; i
< 4; i
++ )
1741 brw_MUL( p
, interp
[ i
], param
[ i
], brw_imm_f( 6.0 ) );
1742 for( i
= 0; i
< 4; i
++ )
1743 brw_ADD( p
, interp
[ i
], interp
[ i
], brw_imm_f( -15.0 ) );
1744 for( i
= 0; i
< 4; i
++ )
1745 brw_MUL( p
, interp
[ i
], interp
[ i
], param
[ i
] );
1746 for( i
= 0; i
< 4; i
++ )
1747 brw_ADD( p
, interp
[ i
], interp
[ i
], brw_imm_f( 10.0 ) );
1748 for( j
= 0; j
< 3; j
++ )
1749 for( i
= 0; i
< 4; i
++ )
1750 brw_MUL( p
, interp
[ i
], interp
[ i
], param
[ i
] );
1752 /* Mark the current address, as it will be a jump destination. The
1753 following code will be executed twice: first, with the flag
1754 register clear indicating the w=0 case, and second with flags
1758 /* Arrange the eight corner coordinates into scalars (itmp0..itmp3) to
1759 be hashed. Since we have only 16 bits of precision in the hash, we
1760 must be careful about thorough mixing to maintain entropy as we
1761 squash the input vector into a small scalar. */
1762 brw_MUL( p
, brw_null_reg(), low_words( floors
[ 0 ] ),
1763 brw_imm_uw( 0xBC8F ) );
1764 brw_MAC( p
, brw_null_reg(), high_words( floors
[ 0 ] ),
1765 brw_imm_uw( 0xD0BD ) );
1766 brw_MAC( p
, brw_null_reg(), low_words( floors
[ 1 ] ),
1767 brw_imm_uw( 0x9B93 ) );
1768 brw_MAC( p
, low_words( itmp
[ 0 ] ), high_words( floors
[ 1 ] ),
1769 brw_imm_uw( 0xA359 ) );
1770 brw_ADD( p
, high_words( itmp
[ 0 ] ), low_words( itmp
[ 0 ] ),
1771 brw_imm_uw( 0xBC8F ) );
1773 /* Temporarily disable the execution mask while we work with ExecSize=16
1774 channels (the mask is set for ExecSize=8 and is probably incorrect).
1775 Although this might cause execution of unwanted channels, the code
1776 writes only to temporary registers and has no side effects, so
1777 disabling the mask is harmless. */
1778 brw_push_insn_state( p
);
1779 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1780 brw_ADD( p
, wtmp
[ 1 ], wtmp
[ 0 ], brw_imm_uw( 0xD0BD ) );
1781 brw_ADD( p
, wtmp
[ 2 ], wtmp
[ 0 ], brw_imm_uw( 0x9B93 ) );
1782 brw_ADD( p
, wtmp
[ 3 ], wtmp
[ 1 ], brw_imm_uw( 0x9B93 ) );
1784 /* We're now ready to perform the hashing. The eight hashes are
1785 interleaved for performance. The hash function used is
1786 designed to rapidly achieve avalanche and require only 16x16
1787 bit multiplication, and 8-bit swizzles (which we get for
1789 for( i
= 0; i
< 4; i
++ )
1790 brw_MUL( p
, wtmp
[ i
], wtmp
[ i
], brw_imm_uw( 0x28D9 ) );
1791 for( i
= 0; i
< 4; i
++ )
1792 brw_XOR( p
, even_bytes( wtmp
[ i
] ), even_bytes( wtmp
[ i
] ),
1793 odd_bytes( wtmp
[ i
] ) );
1794 for( i
= 0; i
< 4; i
++ )
1795 brw_MUL( p
, wtmp
[ i
], wtmp
[ i
], brw_imm_uw( 0xC6D5 ) );
1796 for( i
= 0; i
< 4; i
++ )
1797 brw_XOR( p
, even_bytes( wtmp
[ i
] ), even_bytes( wtmp
[ i
] ),
1798 odd_bytes( wtmp
[ i
] ) );
1799 brw_pop_insn_state( p
);
1801 /* Now we want to initialise the four rear gradients based on the
1802 hashes. Format conversion from signed integer to float leaves
1803 everything scaled too high by a factor of pow( 2, 15 ), but
1804 we correct for that right at the end. */
1806 brw_ADD( p
, t
, param
[ 0 ], brw_imm_f( -1.0 ) );
1807 brw_MOV( p
, x0y0
, low_words( tmp
[ 0 ] ) );
1808 brw_MOV( p
, x0y1
, low_words( tmp
[ 1 ] ) );
1809 brw_MOV( p
, x1y0
, high_words( tmp
[ 0 ] ) );
1810 brw_MOV( p
, x1y1
, high_words( tmp
[ 1 ] ) );
1812 brw_push_insn_state( p
);
1813 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1814 brw_SHL( p
, wtmp
[ 0 ], wtmp
[ 0 ], brw_imm_uw( 4 ) );
1815 brw_SHL( p
, wtmp
[ 1 ], wtmp
[ 1 ], brw_imm_uw( 4 ) );
1816 brw_pop_insn_state( p
);
1818 brw_MUL( p
, x1y0
, x1y0
, t
);
1819 brw_MUL( p
, x1y1
, x1y1
, t
);
1820 brw_ADD( p
, t
, param
[ 1 ], brw_imm_f( -1.0 ) );
1821 brw_MUL( p
, x0y0
, x0y0
, param
[ 0 ] );
1822 brw_MUL( p
, x0y1
, x0y1
, param
[ 0 ] );
1825 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 1 ] ) );
1826 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 1 ] ) );
1827 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 0 ] ) );
1828 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 0 ] ) );
1830 brw_push_insn_state( p
);
1831 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1832 brw_SHL( p
, wtmp
[ 0 ], wtmp
[ 0 ], brw_imm_uw( 4 ) );
1833 brw_SHL( p
, wtmp
[ 1 ], wtmp
[ 1 ], brw_imm_uw( 4 ) );
1834 brw_pop_insn_state( p
);
1836 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1837 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1838 /* prepare t for the w component (used below): w the first time through
1839 the loop; w - 1 the second time) */
1840 brw_set_predicate_control( p
, BRW_PREDICATE_NORMAL
);
1841 brw_ADD( p
, t
, param
[ 3 ], brw_imm_f( -1.0 ) );
1842 p
->current
->header
.predicate_inverse
= 1;
1843 brw_MOV( p
, t
, param
[ 3 ] );
1844 p
->current
->header
.predicate_inverse
= 0;
1845 brw_set_predicate_control( p
, BRW_PREDICATE_NONE
);
1846 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param
[ 1 ] );
1847 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param
[ 1 ] );
1849 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1850 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1851 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1852 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1855 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 0 ] ) );
1856 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 1 ] ) );
1857 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 0 ] ) );
1858 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 1 ] ) );
1860 brw_push_insn_state( p
);
1861 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1862 brw_SHL( p
, wtmp
[ 0 ], wtmp
[ 0 ], brw_imm_uw( 4 ) );
1863 brw_SHL( p
, wtmp
[ 1 ], wtmp
[ 1 ], brw_imm_uw( 4 ) );
1864 brw_pop_insn_state( p
);
1866 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param
[ 2 ] );
1867 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], param
[ 2 ] );
1868 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param
[ 2 ] );
1869 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], param
[ 2 ] );
1871 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1872 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1873 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1874 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1877 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 0 ] ) );
1878 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 1 ] ) );
1879 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 0 ] ) );
1880 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 1 ] ) );
1882 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], t
);
1883 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1884 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], t
);
1885 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1886 brw_ADD( p
, t
, param
[ 0 ], brw_imm_f( -1.0 ) );
1888 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1889 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1890 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1891 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1893 /* Here we interpolate in the y dimension... */
1894 brw_ADD( p
, x0y1
, x0y1
, negate( x0y0
) );
1895 brw_ADD( p
, x1y1
, x1y1
, negate( x1y0
) );
1896 brw_MUL( p
, x0y1
, x0y1
, interp
[ 1 ] );
1897 brw_MUL( p
, x1y1
, x1y1
, interp
[ 1 ] );
1898 brw_ADD( p
, x0y0
, x0y0
, x0y1
);
1899 brw_ADD( p
, x1y0
, x1y0
, x1y1
);
1901 /* And now in x. Leave the result in tmp[ 0 ] (see below)... */
1902 brw_ADD( p
, x1y0
, x1y0
, negate( x0y0
) );
1903 brw_MUL( p
, x1y0
, x1y0
, interp
[ 0 ] );
1904 brw_ADD( p
, tmp
[ 0 ], x0y0
, x1y0
);
1906 /* Now do the same thing for the front four gradients... */
1908 brw_MOV( p
, x0y0
, low_words( tmp
[ 2 ] ) );
1909 brw_MOV( p
, x0y1
, low_words( tmp
[ 3 ] ) );
1910 brw_MOV( p
, x1y0
, high_words( tmp
[ 2 ] ) );
1911 brw_MOV( p
, x1y1
, high_words( tmp
[ 3 ] ) );
1913 brw_push_insn_state( p
);
1914 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1915 brw_SHL( p
, wtmp
[ 2 ], wtmp
[ 2 ], brw_imm_uw( 4 ) );
1916 brw_SHL( p
, wtmp
[ 3 ], wtmp
[ 3 ], brw_imm_uw( 4 ) );
1917 brw_pop_insn_state( p
);
1919 brw_MUL( p
, x1y0
, x1y0
, t
);
1920 brw_MUL( p
, x1y1
, x1y1
, t
);
1921 brw_ADD( p
, t
, param
[ 1 ], brw_imm_f( -1.0 ) );
1922 brw_MUL( p
, x0y0
, x0y0
, param
[ 0 ] );
1923 brw_MUL( p
, x0y1
, x0y1
, param
[ 0 ] );
1926 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 3 ] ) );
1927 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 3 ] ) );
1928 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 2 ] ) );
1929 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 2 ] ) );
1931 brw_push_insn_state( p
);
1932 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1933 brw_SHL( p
, wtmp
[ 2 ], wtmp
[ 2 ], brw_imm_uw( 4 ) );
1934 brw_SHL( p
, wtmp
[ 3 ], wtmp
[ 3 ], brw_imm_uw( 4 ) );
1935 brw_pop_insn_state( p
);
1937 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1938 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1939 brw_ADD( p
, t
, param
[ 2 ], brw_imm_f( -1.0 ) );
1940 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], param
[ 1 ] );
1941 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], param
[ 1 ] );
1943 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1944 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1945 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1946 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1949 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 2 ] ) );
1950 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 3 ] ) );
1951 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 2 ] ) );
1952 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 3 ] ) );
1954 brw_push_insn_state( p
);
1955 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
1956 brw_SHL( p
, wtmp
[ 2 ], wtmp
[ 2 ], brw_imm_uw( 4 ) );
1957 brw_SHL( p
, wtmp
[ 3 ], wtmp
[ 3 ], brw_imm_uw( 4 ) );
1958 brw_pop_insn_state( p
);
1960 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], t
);
1961 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1962 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], t
);
1963 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1964 /* prepare t for the w component (used below): w the first time through
1965 the loop; w - 1 the second time) */
1966 brw_set_predicate_control( p
, BRW_PREDICATE_NORMAL
);
1967 brw_ADD( p
, t
, param
[ 3 ], brw_imm_f( -1.0 ) );
1968 p
->current
->header
.predicate_inverse
= 1;
1969 brw_MOV( p
, t
, param
[ 3 ] );
1970 p
->current
->header
.predicate_inverse
= 0;
1971 brw_set_predicate_control( p
, BRW_PREDICATE_NONE
);
1973 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1974 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1975 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1976 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1979 brw_MOV( p
, tmp
[ 4 ], low_words( tmp
[ 2 ] ) );
1980 brw_MOV( p
, tmp
[ 5 ], low_words( tmp
[ 3 ] ) );
1981 brw_MOV( p
, tmp
[ 6 ], high_words( tmp
[ 2 ] ) );
1982 brw_MOV( p
, tmp
[ 7 ], high_words( tmp
[ 3 ] ) );
1984 brw_MUL( p
, tmp
[ 4 ], tmp
[ 4 ], t
);
1985 brw_MUL( p
, tmp
[ 5 ], tmp
[ 5 ], t
);
1986 brw_MUL( p
, tmp
[ 6 ], tmp
[ 6 ], t
);
1987 brw_MUL( p
, tmp
[ 7 ], tmp
[ 7 ], t
);
1989 brw_ADD( p
, x0y0
, x0y0
, tmp
[ 4 ] );
1990 brw_ADD( p
, x0y1
, x0y1
, tmp
[ 5 ] );
1991 brw_ADD( p
, x1y0
, x1y0
, tmp
[ 6 ] );
1992 brw_ADD( p
, x1y1
, x1y1
, tmp
[ 7 ] );
1994 /* Interpolate in the y dimension: */
1995 brw_ADD( p
, x0y1
, x0y1
, negate( x0y0
) );
1996 brw_ADD( p
, x1y1
, x1y1
, negate( x1y0
) );
1997 brw_MUL( p
, x0y1
, x0y1
, interp
[ 1 ] );
1998 brw_MUL( p
, x1y1
, x1y1
, interp
[ 1 ] );
1999 brw_ADD( p
, x0y0
, x0y0
, x0y1
);
2000 brw_ADD( p
, x1y0
, x1y0
, x1y1
);
2002 /* And now in x. The rear face is in tmp[ 0 ] (see above), so this
2003 time put the front face in tmp[ 1 ] and we're nearly there... */
2004 brw_ADD( p
, x1y0
, x1y0
, negate( x0y0
) );
2005 brw_MUL( p
, x1y0
, x1y0
, interp
[ 0 ] );
2006 brw_ADD( p
, tmp
[ 1 ], x0y0
, x1y0
);
2008 /* Another interpolation, in the z dimension: */
2009 brw_ADD( p
, tmp
[ 1 ], tmp
[ 1 ], negate( tmp
[ 0 ] ) );
2010 brw_MUL( p
, tmp
[ 1 ], tmp
[ 1 ], interp
[ 2 ] );
2011 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], tmp
[ 1 ] );
2013 /* Exit the loop if we've computed both cubes... */
2014 origin
= p
->nr_insn
;
2015 brw_push_insn_state( p
);
2016 brw_set_predicate_control( p
, BRW_PREDICATE_NORMAL
);
2017 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
2018 brw_ADD( p
, brw_ip_reg(), brw_ip_reg(), brw_imm_d( 0 ) );
2019 brw_pop_insn_state( p
);
2021 /* Save the result for the w=0 case, and increment the w coordinate: */
2022 brw_MOV( p
, w0
, tmp
[ 0 ] );
2023 brw_ADD( p
, high_words( floors
[ 1 ] ), high_words( floors
[ 1 ] ),
2026 /* Loop around for the other cube. Explicitly set the flag register
2027 (unfortunately we must spend an extra instruction to do this: we
2028 can't rely on a side effect of the previous MOV or ADD because
2029 conditional modifiers which are normally true might be false in
2030 exceptional circumstances, e.g. given a NaN input; the add to
2031 brw_ip_reg() is not suitable because the IP is not an 8-vector). */
2032 brw_push_insn_state( p
);
2033 brw_set_mask_control( p
, BRW_MASK_DISABLE
);
2034 brw_MOV( p
, brw_flag_reg(), brw_imm_uw( 0xFF ) );
2035 brw_ADD( p
, brw_ip_reg(), brw_ip_reg(),
2036 brw_imm_d( ( loop
- p
->nr_insn
) << 4 ) );
2037 brw_pop_insn_state( p
);
2039 /* Patch the previous conditional branch now that we know the
2040 destination address. */
2041 brw_set_src1( p
->store
+ origin
,
2042 brw_imm_d( ( p
->nr_insn
- origin
) << 4 ) );
2044 /* The very last interpolation. */
2045 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], negate( w0
) );
2046 brw_MUL( p
, tmp
[ 0 ], tmp
[ 0 ], interp
[ 3 ] );
2047 brw_ADD( p
, tmp
[ 0 ], tmp
[ 0 ], w0
);
2049 /* scale by pow( 2, -15 ), as described above */
2050 brw_MUL( p
, param
[ 0 ], tmp
[ 0 ], brw_imm_f( 0.000030517578125 ) );
2052 release_tmps( c
, mark
);
2055 static void emit_noise4( struct brw_wm_compile
*c
,
2056 struct prog_instruction
*inst
)
2058 struct brw_compile
*p
= &c
->func
;
2059 struct brw_reg src0
, src1
, src2
, src3
, param0
, param1
, param2
, param3
, dst
;
2060 GLuint mask
= inst
->DstReg
.WriteMask
;
2062 int mark
= mark_tmps( c
);
2064 assert( mark
== 0 );
2066 src0
= get_src_reg( c
, inst
->SrcReg
, 0, 1 );
2067 src1
= get_src_reg( c
, inst
->SrcReg
, 1, 1 );
2068 src2
= get_src_reg( c
, inst
->SrcReg
, 2, 1 );
2069 src3
= get_src_reg( c
, inst
->SrcReg
, 3, 1 );
2071 param0
= alloc_tmp( c
);
2072 param1
= alloc_tmp( c
);
2073 param2
= alloc_tmp( c
);
2074 param3
= alloc_tmp( c
);
2076 brw_MOV( p
, param0
, src0
);
2077 brw_MOV( p
, param1
, src1
);
2078 brw_MOV( p
, param2
, src2
);
2079 brw_MOV( p
, param3
, src3
);
2081 invoke_subroutine( c
, SUB_NOISE4
, noise4_sub
);
2083 /* Fill in the result: */
2084 brw_set_saturate( p
, inst
->SaturateMode
== SATURATE_ZERO_ONE
);
2085 for (i
= 0 ; i
< 4; i
++) {
2086 if (mask
& (1<<i
)) {
2087 dst
= get_dst_reg(c
, inst
, i
, 1);
2088 brw_MOV( p
, dst
, param0
);
2091 if( inst
->SaturateMode
== SATURATE_ZERO_ONE
)
2092 brw_set_saturate( p
, 0 );
2094 release_tmps( c
, mark
);
2097 static void emit_wpos_xy(struct brw_wm_compile
*c
,
2098 struct prog_instruction
*inst
)
2100 struct brw_compile
*p
= &c
->func
;
2101 GLuint mask
= inst
->DstReg
.WriteMask
;
2102 struct brw_reg src0
[2], dst
[2];
2104 dst
[0] = get_dst_reg(c
, inst
, 0, 1);
2105 dst
[1] = get_dst_reg(c
, inst
, 1, 1);
2107 src0
[0] = get_src_reg(c
, &inst
->SrcReg
[0], 0, 1);
2108 src0
[1] = get_src_reg(c
, &inst
->SrcReg
[0], 1, 1);
2110 /* Calculate the pixel offset from window bottom left into destination
2113 if (mask
& WRITEMASK_X
) {
2114 /* X' = X - origin_x */
2117 retype(src0
[0], BRW_REGISTER_TYPE_W
),
2118 brw_imm_d(0 - c
->key
.origin_x
));
2121 if (mask
& WRITEMASK_Y
) {
2122 /* Y' = height - (Y - origin_y) = height + origin_y - Y */
2125 negate(retype(src0
[1], BRW_REGISTER_TYPE_W
)),
2126 brw_imm_d(c
->key
.origin_y
+ c
->key
.drawable_height
- 1));
2131 BIAS on SIMD8 not workind yet...
2133 static void emit_txb(struct brw_wm_compile
*c
,
2134 struct prog_instruction
*inst
)
2136 struct brw_compile
*p
= &c
->func
;
2137 struct brw_reg dst
[4], src
[4], payload_reg
;
2138 GLuint unit
= c
->fp
->program
.Base
.SamplerUnits
[inst
->TexSrcUnit
];
2141 payload_reg
= get_reg(c
, PROGRAM_PAYLOAD
, PAYLOAD_DEPTH
, 0, 1, 0, 0);
2142 for (i
= 0; i
< 4; i
++)
2143 dst
[i
] = get_dst_reg(c
, inst
, i
, 1);
2144 for (i
= 0; i
< 4; i
++)
2145 src
[i
] = get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
2147 switch (inst
->TexSrcTarget
) {
2148 case TEXTURE_1D_INDEX
:
2149 brw_MOV(p
, brw_message_reg(2), src
[0]);
2150 brw_MOV(p
, brw_message_reg(3), brw_imm_f(0));
2151 brw_MOV(p
, brw_message_reg(4), brw_imm_f(0));
2153 case TEXTURE_2D_INDEX
:
2154 case TEXTURE_RECT_INDEX
:
2155 brw_MOV(p
, brw_message_reg(2), src
[0]);
2156 brw_MOV(p
, brw_message_reg(3), src
[1]);
2157 brw_MOV(p
, brw_message_reg(4), brw_imm_f(0));
2160 brw_MOV(p
, brw_message_reg(2), src
[0]);
2161 brw_MOV(p
, brw_message_reg(3), src
[1]);
2162 brw_MOV(p
, brw_message_reg(4), src
[2]);
2165 brw_MOV(p
, brw_message_reg(5), src
[3]);
2166 brw_MOV(p
, brw_message_reg(6), brw_imm_f(0));
2168 retype(vec8(dst
[0]), BRW_REGISTER_TYPE_UW
),
2170 retype(payload_reg
, BRW_REGISTER_TYPE_UW
),
2171 unit
+ MAX_DRAW_BUFFERS
, /* surface */
2173 inst
->DstReg
.WriteMask
,
2174 BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
,
2180 static void emit_tex(struct brw_wm_compile
*c
,
2181 struct prog_instruction
*inst
)
2183 struct brw_compile
*p
= &c
->func
;
2184 struct brw_reg dst
[4], src
[4], payload_reg
;
2185 GLuint unit
= c
->fp
->program
.Base
.SamplerUnits
[inst
->TexSrcUnit
];
2190 GLboolean shadow
= (c
->key
.shadowtex_mask
& (1<<unit
)) ? 1 : 0;
2192 payload_reg
= get_reg(c
, PROGRAM_PAYLOAD
, PAYLOAD_DEPTH
, 0, 1, 0, 0);
2194 for (i
= 0; i
< 4; i
++)
2195 dst
[i
] = get_dst_reg(c
, inst
, i
, 1);
2196 for (i
= 0; i
< 4; i
++)
2197 src
[i
] = get_src_reg(c
, &inst
->SrcReg
[0], i
, 1);
2200 switch (inst
->TexSrcTarget
) {
2201 case TEXTURE_1D_INDEX
:
2205 case TEXTURE_2D_INDEX
:
2206 case TEXTURE_RECT_INDEX
:
2207 emit
= WRITEMASK_XY
;
2211 emit
= WRITEMASK_XYZ
;
2217 for (i
= 0; i
< nr
; i
++) {
2218 static const GLuint swz
[4] = {0,1,2,2};
2220 brw_MOV(p
, brw_message_reg(msg_len
+1), src
[swz
[i
]]);
2222 brw_MOV(p
, brw_message_reg(msg_len
+1), brw_imm_f(0));
2227 brw_MOV(p
, brw_message_reg(5), brw_imm_f(0));
2228 brw_MOV(p
, brw_message_reg(6), src
[2]);
2232 retype(vec8(dst
[0]), BRW_REGISTER_TYPE_UW
),
2234 retype(payload_reg
, BRW_REGISTER_TYPE_UW
),
2235 unit
+ MAX_DRAW_BUFFERS
, /* surface */
2237 inst
->DstReg
.WriteMask
,
2238 BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE
,
2244 brw_MOV(p
, dst
[3], brw_imm_f(1.0));
2247 static void post_wm_emit( struct brw_wm_compile
*c
)
2249 GLuint nr_insns
= c
->fp
->program
.Base
.NumInstructions
;
2250 GLuint insn
, target_insn
;
2251 struct prog_instruction
*inst1
, *inst2
;
2252 struct brw_instruction
*brw_inst1
, *brw_inst2
;
2254 for (insn
= 0; insn
< nr_insns
; insn
++) {
2255 inst1
= &c
->fp
->program
.Base
.Instructions
[insn
];
2256 brw_inst1
= inst1
->Data
;
2257 switch (inst1
->Opcode
) {
2259 target_insn
= inst1
->BranchTarget
;
2260 inst2
= &c
->fp
->program
.Base
.Instructions
[target_insn
];
2261 brw_inst2
= inst2
->Data
;
2262 offset
= brw_inst2
- brw_inst1
;
2263 brw_set_src1(brw_inst1
, brw_imm_d(offset
*16));
2271 static void brw_wm_emit_glsl(struct brw_context
*brw
, struct brw_wm_compile
*c
)
2274 #define MAX_LOOP_DEPTH 32
2275 struct brw_instruction
*if_inst
[MAX_IFSN
], *loop_inst
[MAX_LOOP_DEPTH
];
2276 struct brw_instruction
*inst0
, *inst1
;
2277 int i
, if_insn
= 0, loop_insn
= 0;
2278 struct brw_compile
*p
= &c
->func
;
2279 struct brw_indirect stack_index
= brw_indirect(0, 0);
2283 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
2284 brw_MOV(p
, get_addr_reg(stack_index
), brw_address(c
->stack
));
2286 for (i
= 0; i
< c
->nr_fp_insns
; i
++) {
2287 struct prog_instruction
*inst
= &c
->prog_instructions
[i
];
2288 struct prog_instruction
*orig_inst
;
2290 if ((orig_inst
= inst
->Data
) != 0)
2291 orig_inst
->Data
= current_insn(p
);
2293 if (inst
->CondUpdate
)
2294 brw_set_conditionalmod(p
, BRW_CONDITIONAL_NZ
);
2296 brw_set_conditionalmod(p
, BRW_CONDITIONAL_NONE
);
2298 switch (inst
->Opcode
) {
2300 emit_pixel_xy(c
, inst
);
2303 emit_delta_xy(c
, inst
);
2306 emit_pixel_w(c
, inst
);
2309 emit_linterp(c
, inst
);
2312 emit_pinterp(c
, inst
);
2315 emit_cinterp(c
, inst
);
2318 emit_wpos_xy(c
, inst
);
2321 emit_fb_write(c
, inst
);
2342 emit_trunc(c
, inst
);
2417 emit_noise1(c
, inst
);
2420 emit_noise2(c
, inst
);
2423 emit_noise3(c
, inst
);
2426 emit_noise4(c
, inst
);
2438 assert(if_insn
< MAX_IFSN
);
2439 if_inst
[if_insn
++] = brw_IF(p
, BRW_EXECUTE_8
);
2442 if_inst
[if_insn
-1] = brw_ELSE(p
, if_inst
[if_insn
-1]);
2445 assert(if_insn
> 0);
2446 brw_ENDIF(p
, if_inst
[--if_insn
]);
2452 brw_push_insn_state(p
);
2453 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
2454 brw_set_access_mode(p
, BRW_ALIGN_1
);
2455 brw_ADD(p
, deref_1ud(stack_index
, 0), brw_ip_reg(), brw_imm_d(3*16));
2456 brw_set_access_mode(p
, BRW_ALIGN_16
);
2457 brw_ADD(p
, get_addr_reg(stack_index
),
2458 get_addr_reg(stack_index
), brw_imm_d(4));
2459 orig_inst
= inst
->Data
;
2460 orig_inst
->Data
= &p
->store
[p
->nr_insn
];
2461 brw_ADD(p
, brw_ip_reg(), brw_ip_reg(), brw_imm_d(1*16));
2462 brw_pop_insn_state(p
);
2466 brw_push_insn_state(p
);
2467 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
2468 brw_ADD(p
, get_addr_reg(stack_index
),
2469 get_addr_reg(stack_index
), brw_imm_d(-4));
2470 brw_set_access_mode(p
, BRW_ALIGN_1
);
2471 brw_MOV(p
, brw_ip_reg(), deref_1ud(stack_index
, 0));
2472 brw_set_access_mode(p
, BRW_ALIGN_16
);
2473 brw_pop_insn_state(p
);
2476 case OPCODE_BGNLOOP
:
2477 loop_inst
[loop_insn
++] = brw_DO(p
, BRW_EXECUTE_8
);
2481 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2485 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2487 case OPCODE_ENDLOOP
:
2489 inst0
= inst1
= brw_WHILE(p
, loop_inst
[loop_insn
]);
2490 /* patch all the BREAK instructions from
2492 while (inst0
> loop_inst
[loop_insn
]) {
2494 if (inst0
->header
.opcode
== BRW_OPCODE_BREAK
) {
2495 inst0
->bits3
.if_else
.jump_count
= inst1
- inst0
+ 1;
2496 inst0
->bits3
.if_else
.pop_count
= 0;
2497 } else if (inst0
->header
.opcode
== BRW_OPCODE_CONTINUE
) {
2498 inst0
->bits3
.if_else
.jump_count
= inst1
- inst0
;
2499 inst0
->bits3
.if_else
.pop_count
= 0;
2504 _mesa_printf("unsupported IR in fragment shader %d\n",
2507 if (inst
->CondUpdate
)
2508 brw_set_predicate_control(p
, BRW_PREDICATE_NORMAL
);
2510 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2513 for (i
= 0; i
< c
->fp
->program
.Base
.NumInstructions
; i
++)
2514 c
->fp
->program
.Base
.Instructions
[i
].Data
= NULL
;
2517 void brw_wm_glsl_emit(struct brw_context
*brw
, struct brw_wm_compile
*c
)
2520 brw_wm_emit_glsl(brw
, c
);
2521 c
->prog_data
.total_grf
= c
->reg_index
;
2522 c
->prog_data
.total_scratch
= 0;