2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 /** @file brw_fs_visitor.cpp
26 * This file supports generating the FS LIR from the GLSL IR. The LIR
27 * makes it easier to do backend-specific optimizations than doing so
28 * in the GLSL IR or in the native code.
32 #include <sys/types.h>
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "program/prog_optimize.h"
40 #include "program/register_allocate.h"
41 #include "program/sampler.h"
42 #include "program/hash_table.h"
43 #include "brw_context.h"
47 #include "brw_shader.h"
49 #include "../glsl/glsl_types.h"
50 #include "../glsl/ir_optimization.h"
51 #include "../glsl/ir_print_visitor.h"
54 fs_visitor::visit(ir_variable
*ir
)
58 if (variable_storage(ir
))
61 if (strcmp(ir
->name
, "gl_FragColor") == 0) {
62 this->frag_color
= ir
;
63 } else if (strcmp(ir
->name
, "gl_FragData") == 0) {
65 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
66 this->frag_depth
= ir
;
69 if (ir
->mode
== ir_var_in
) {
70 if (!strcmp(ir
->name
, "gl_FragCoord")) {
71 reg
= emit_fragcoord_interpolation(ir
);
72 } else if (!strcmp(ir
->name
, "gl_FrontFacing")) {
73 reg
= emit_frontfacing_interpolation(ir
);
75 reg
= emit_general_interpolation(ir
);
78 hash_table_insert(this->variable_ht
, reg
, ir
);
82 if (ir
->mode
== ir_var_uniform
) {
83 int param_index
= c
->prog_data
.nr_params
;
85 if (c
->dispatch_width
== 16) {
86 if (!variable_storage(ir
)) {
87 fail("Failed to find uniform '%s' in 16-wide\n", ir
->name
);
92 if (!strncmp(ir
->name
, "gl_", 3)) {
93 setup_builtin_uniform_values(ir
);
95 setup_uniform_values(ir
->location
, ir
->type
);
98 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
99 reg
->type
= brw_type_for_base_type(ir
->type
);
103 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
105 hash_table_insert(this->variable_ht
, reg
, ir
);
109 fs_visitor::visit(ir_dereference_variable
*ir
)
111 fs_reg
*reg
= variable_storage(ir
->var
);
116 fs_visitor::visit(ir_dereference_record
*ir
)
118 const glsl_type
*struct_type
= ir
->record
->type
;
120 ir
->record
->accept(this);
122 unsigned int offset
= 0;
123 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
124 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
126 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
128 this->result
.reg_offset
+= offset
;
129 this->result
.type
= brw_type_for_base_type(ir
->type
);
133 fs_visitor::visit(ir_dereference_array
*ir
)
138 ir
->array
->accept(this);
139 index
= ir
->array_index
->as_constant();
141 element_size
= type_size(ir
->type
);
142 this->result
.type
= brw_type_for_base_type(ir
->type
);
145 assert(this->result
.file
== UNIFORM
||
146 (this->result
.file
== GRF
&&
147 this->result
.reg
!= 0));
148 this->result
.reg_offset
+= index
->value
.i
[0] * element_size
;
150 assert(!"FINISHME: non-constant array element");
154 /* Instruction selection: Produce a MOV.sat instead of
155 * MIN(MAX(val, 0), 1) when possible.
158 fs_visitor::try_emit_saturate(ir_expression
*ir
)
160 ir_rvalue
*sat_val
= ir
->as_rvalue_to_saturate();
165 this->result
= reg_undef
;
166 sat_val
->accept(this);
167 fs_reg src
= this->result
;
169 this->result
= fs_reg(this, ir
->type
);
170 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, this->result
, src
);
171 inst
->saturate
= true;
177 fs_visitor::visit(ir_expression
*ir
)
179 unsigned int operand
;
183 assert(ir
->get_num_operands() <= 2);
185 if (try_emit_saturate(ir
))
188 /* This is where our caller would like us to put the result, if possible. */
189 fs_reg saved_result_storage
= this->result
;
191 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
192 this->result
= reg_undef
;
193 ir
->operands
[operand
]->accept(this);
194 if (this->result
.file
== BAD_FILE
) {
196 fail("Failed to get tree for expression operand:\n");
197 ir
->operands
[operand
]->accept(&v
);
199 op
[operand
] = this->result
;
201 /* Matrix expression operands should have been broken down to vector
202 * operations already.
204 assert(!ir
->operands
[operand
]->type
->is_matrix());
205 /* And then those vector operands should have been broken down to scalar.
207 assert(!ir
->operands
[operand
]->type
->is_vector());
210 /* Inherit storage from our parent if possible, and otherwise we
213 if (saved_result_storage
.file
== BAD_FILE
) {
214 this->result
= fs_reg(this, ir
->type
);
216 this->result
= saved_result_storage
;
219 switch (ir
->operation
) {
220 case ir_unop_logic_not
:
221 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
222 * ones complement of the whole register, not just bit 0.
224 emit(BRW_OPCODE_XOR
, this->result
, op
[0], fs_reg(1));
227 op
[0].negate
= !op
[0].negate
;
228 this->result
= op
[0];
232 op
[0].negate
= false;
233 this->result
= op
[0];
236 temp
= fs_reg(this, ir
->type
);
238 /* Unalias the destination. (imagine a = sign(a)) */
239 this->result
= fs_reg(this, ir
->type
);
241 emit(BRW_OPCODE_MOV
, this->result
, fs_reg(0.0f
));
243 inst
= emit(BRW_OPCODE_CMP
, reg_null_f
, op
[0], fs_reg(0.0f
));
244 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
245 inst
= emit(BRW_OPCODE_MOV
, this->result
, fs_reg(1.0f
));
246 inst
->predicated
= true;
248 inst
= emit(BRW_OPCODE_CMP
, reg_null_f
, op
[0], fs_reg(0.0f
));
249 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
250 inst
= emit(BRW_OPCODE_MOV
, this->result
, fs_reg(-1.0f
));
251 inst
->predicated
= true;
255 emit_math(FS_OPCODE_RCP
, this->result
, op
[0]);
259 emit_math(FS_OPCODE_EXP2
, this->result
, op
[0]);
262 emit_math(FS_OPCODE_LOG2
, this->result
, op
[0]);
266 assert(!"not reached: should be handled by ir_explog_to_explog2");
269 case ir_unop_sin_reduced
:
270 emit_math(FS_OPCODE_SIN
, this->result
, op
[0]);
273 case ir_unop_cos_reduced
:
274 emit_math(FS_OPCODE_COS
, this->result
, op
[0]);
278 emit(FS_OPCODE_DDX
, this->result
, op
[0]);
281 emit(FS_OPCODE_DDY
, this->result
, op
[0]);
285 emit(BRW_OPCODE_ADD
, this->result
, op
[0], op
[1]);
288 assert(!"not reached: should be handled by ir_sub_to_add_neg");
292 emit(BRW_OPCODE_MUL
, this->result
, op
[0], op
[1]);
295 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
298 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
302 case ir_binop_greater
:
303 case ir_binop_lequal
:
304 case ir_binop_gequal
:
306 case ir_binop_all_equal
:
307 case ir_binop_nequal
:
308 case ir_binop_any_nequal
:
310 /* original gen4 does implicit conversion before comparison. */
312 temp
.type
= op
[0].type
;
314 inst
= emit(BRW_OPCODE_CMP
, temp
, op
[0], op
[1]);
315 inst
->conditional_mod
= brw_conditional_for_comparison(ir
->operation
);
316 emit(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1));
319 case ir_binop_logic_xor
:
320 emit(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]);
323 case ir_binop_logic_or
:
324 emit(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]);
327 case ir_binop_logic_and
:
328 emit(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]);
333 assert(!"not reached: should be handled by brw_fs_channel_expressions");
337 assert(!"not reached: should be handled by lower_noise");
340 case ir_quadop_vector
:
341 assert(!"not reached: should be handled by lower_quadop_vector");
345 emit_math(FS_OPCODE_SQRT
, this->result
, op
[0]);
349 emit_math(FS_OPCODE_RSQ
, this->result
, op
[0]);
356 emit(BRW_OPCODE_MOV
, this->result
, op
[0]);
361 /* original gen4 does implicit conversion before comparison. */
363 temp
.type
= op
[0].type
;
365 inst
= emit(BRW_OPCODE_CMP
, temp
, op
[0], fs_reg(0.0f
));
366 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
367 inst
= emit(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(1));
371 emit(BRW_OPCODE_RNDZ
, this->result
, op
[0]);
374 op
[0].negate
= !op
[0].negate
;
375 inst
= emit(BRW_OPCODE_RNDD
, this->result
, op
[0]);
376 this->result
.negate
= true;
379 inst
= emit(BRW_OPCODE_RNDD
, this->result
, op
[0]);
382 inst
= emit(BRW_OPCODE_FRC
, this->result
, op
[0]);
384 case ir_unop_round_even
:
385 emit(BRW_OPCODE_RNDE
, this->result
, op
[0]);
389 if (intel
->gen
>= 6) {
390 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]);
391 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
393 /* Unalias the destination */
394 this->result
= fs_reg(this, ir
->type
);
396 inst
= emit(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]);
397 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
399 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]);
400 inst
->predicated
= true;
404 if (intel
->gen
>= 6) {
405 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]);
406 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
408 /* Unalias the destination */
409 this->result
= fs_reg(this, ir
->type
);
411 inst
= emit(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]);
412 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
414 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]);
415 inst
->predicated
= true;
420 emit_math(FS_OPCODE_POW
, this->result
, op
[0], op
[1]);
423 case ir_unop_bit_not
:
424 inst
= emit(BRW_OPCODE_NOT
, this->result
, op
[0]);
426 case ir_binop_bit_and
:
427 inst
= emit(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]);
429 case ir_binop_bit_xor
:
430 inst
= emit(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]);
432 case ir_binop_bit_or
:
433 inst
= emit(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]);
437 case ir_binop_lshift
:
438 case ir_binop_rshift
:
439 assert(!"GLSL 1.30 features unsupported");
445 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
446 const glsl_type
*type
, bool predicated
)
448 switch (type
->base_type
) {
449 case GLSL_TYPE_FLOAT
:
453 for (unsigned int i
= 0; i
< type
->components(); i
++) {
454 l
.type
= brw_type_for_base_type(type
);
455 r
.type
= brw_type_for_base_type(type
);
457 if (predicated
|| !l
.equals(&r
)) {
458 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, l
, r
);
459 inst
->predicated
= predicated
;
466 case GLSL_TYPE_ARRAY
:
467 for (unsigned int i
= 0; i
< type
->length
; i
++) {
468 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
472 case GLSL_TYPE_STRUCT
:
473 for (unsigned int i
= 0; i
< type
->length
; i
++) {
474 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
479 case GLSL_TYPE_SAMPLER
:
483 assert(!"not reached");
489 fs_visitor::visit(ir_assignment
*ir
)
494 /* FINISHME: arrays on the lhs */
495 this->result
= reg_undef
;
496 ir
->lhs
->accept(this);
499 /* If we're doing a direct assignment, an RHS expression could
500 * drop its result right into our destination. Otherwise, tell it
504 !(ir
->lhs
->type
->is_scalar() ||
505 (ir
->lhs
->type
->is_vector() &&
506 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1))) {
507 this->result
= reg_undef
;
510 ir
->rhs
->accept(this);
513 assert(l
.file
!= BAD_FILE
);
514 assert(r
.file
!= BAD_FILE
);
517 emit_bool_to_cond_code(ir
->condition
);
520 if (ir
->lhs
->type
->is_scalar() ||
521 ir
->lhs
->type
->is_vector()) {
522 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
523 if (ir
->write_mask
& (1 << i
)) {
525 inst
= emit(BRW_OPCODE_MOV
, l
, r
);
526 inst
->predicated
= true;
527 } else if (!l
.equals(&r
)) {
528 inst
= emit(BRW_OPCODE_MOV
, l
, r
);
536 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
541 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
552 if (ir
->shadow_comparitor
) {
553 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
554 fs_inst
*inst
= emit(BRW_OPCODE_MOV
,
555 fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
);
556 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
557 inst
->saturate
= true;
559 coordinate
.reg_offset
++;
561 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
564 if (ir
->op
== ir_tex
) {
565 /* There's no plain shadow compare message, so we use shadow
566 * compare with a bias of 0.0.
568 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
));
570 } else if (ir
->op
== ir_txb
) {
571 this->result
= reg_undef
;
572 ir
->lod_info
.bias
->accept(this);
573 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
576 assert(ir
->op
== ir_txl
);
577 this->result
= reg_undef
;
578 ir
->lod_info
.lod
->accept(this);
579 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
583 this->result
= reg_undef
;
584 ir
->shadow_comparitor
->accept(this);
585 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
587 } else if (ir
->op
== ir_tex
) {
588 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
589 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
+ i
),
591 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
592 inst
->saturate
= true;
593 coordinate
.reg_offset
++;
595 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
597 } else if (ir
->op
== ir_txd
) {
598 assert(!"TXD isn't supported on gen4 yet.");
600 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
601 * instructions. We'll need to do SIMD16 here.
603 assert(ir
->op
== ir_txb
|| ir
->op
== ir_txl
);
605 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
606 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, fs_reg(MRF
,
607 base_mrf
+ mlen
+ i
* 2),
609 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
610 inst
->saturate
= true;
611 coordinate
.reg_offset
++;
614 /* lod/bias appears after u/v/r. */
617 if (ir
->op
== ir_txb
) {
618 this->result
= reg_undef
;
619 ir
->lod_info
.bias
->accept(this);
620 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
623 this->result
= reg_undef
;
624 ir
->lod_info
.lod
->accept(this);
625 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
629 /* The unused upper half. */
632 /* Now, since we're doing simd16, the return is 2 interleaved
633 * vec4s where the odd-indexed ones are junk. We'll need to move
634 * this weirdness around to the expected layout.
638 dst
= fs_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type
,
640 dst
.type
= BRW_REGISTER_TYPE_F
;
643 fs_inst
*inst
= NULL
;
646 inst
= emit(FS_OPCODE_TEX
, dst
);
649 inst
= emit(FS_OPCODE_TXB
, dst
);
652 inst
= emit(FS_OPCODE_TXL
, dst
);
655 inst
= emit(FS_OPCODE_TXD
, dst
);
658 assert(!"GLSL 1.30 features unsupported");
661 inst
->base_mrf
= base_mrf
;
663 inst
->header_present
= true;
666 for (int i
= 0; i
< 4; i
++) {
667 emit(BRW_OPCODE_MOV
, orig_dst
, dst
);
668 orig_dst
.reg_offset
++;
676 /* gen5's sampler has slots for u, v, r, array index, then optional
677 * parameters like shadow comparitor or LOD bias. If optional
678 * parameters aren't present, those base slots are optional and don't
679 * need to be included in the message.
681 * We don't fill in the unnecessary slots regardless, which may look
682 * surprising in the disassembly.
685 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
690 int reg_width
= c
->dispatch_width
/ 8;
691 bool header_present
= false;
694 /* The offsets set up by the ir_texture visitor are in the
695 * m1 header, so we can't go headerless.
697 header_present
= true;
702 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
703 fs_inst
*inst
= emit(BRW_OPCODE_MOV
,
704 fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
),
706 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
707 inst
->saturate
= true;
708 coordinate
.reg_offset
++;
710 mlen
+= ir
->coordinate
->type
->vector_elements
* reg_width
;
712 if (ir
->shadow_comparitor
) {
713 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
715 this->result
= reg_undef
;
716 ir
->shadow_comparitor
->accept(this);
717 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
721 fs_inst
*inst
= NULL
;
724 inst
= emit(FS_OPCODE_TEX
, dst
);
727 this->result
= reg_undef
;
728 ir
->lod_info
.bias
->accept(this);
729 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
730 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
733 inst
= emit(FS_OPCODE_TXB
, dst
);
737 this->result
= reg_undef
;
738 ir
->lod_info
.lod
->accept(this);
739 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
740 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
743 inst
= emit(FS_OPCODE_TXL
, dst
);
746 ir
->lod_info
.grad
.dPdx
->accept(this);
747 fs_reg dPdx
= this->result
;
749 ir
->lod_info
.grad
.dPdy
->accept(this);
750 fs_reg dPdy
= this->result
;
752 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
); /* skip over 'ai' */
756 * dPdx = dudx, dvdx, drdx
757 * dPdy = dudy, dvdy, drdy
759 * Load up these values:
760 * - dudx dudy dvdx dvdy drdx drdy
761 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
763 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
764 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), dPdx
);
768 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), dPdy
);
773 inst
= emit(FS_OPCODE_TXD
, dst
);
777 assert(!"GLSL 1.30 features unsupported");
780 inst
->base_mrf
= base_mrf
;
782 inst
->header_present
= header_present
;
785 fail("Message length >11 disallowed by hardware\n");
792 fs_visitor::emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
797 int reg_width
= c
->dispatch_width
/ 8;
798 bool header_present
= false;
801 /* The offsets set up by the ir_texture visitor are in the
802 * m1 header, so we can't go headerless.
804 header_present
= true;
809 if (ir
->shadow_comparitor
) {
810 ir
->shadow_comparitor
->accept(this);
811 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
815 /* Set up the LOD info */
820 ir
->lod_info
.bias
->accept(this);
821 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
825 ir
->lod_info
.lod
->accept(this);
826 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
);
830 if (c
->dispatch_width
== 16)
831 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
833 ir
->lod_info
.grad
.dPdx
->accept(this);
834 fs_reg dPdx
= this->result
;
836 ir
->lod_info
.grad
.dPdy
->accept(this);
837 fs_reg dPdy
= this->result
;
839 /* Load dPdx and the coordinate together:
840 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
842 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
843 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
),
845 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
846 inst
->saturate
= true;
847 coordinate
.reg_offset
++;
850 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), dPdx
);
854 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), dPdy
);
861 assert(!"GLSL 1.30 features unsupported");
865 /* Set up the coordinate (except for TXD where it was done earlier) */
866 if (ir
->op
!= ir_txd
) {
867 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
868 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
),
870 if (i
< 3 && c
->key
.gl_clamp_mask
[i
] & (1 << sampler
))
871 inst
->saturate
= true;
872 coordinate
.reg_offset
++;
877 /* Generate the SEND */
878 fs_inst
*inst
= NULL
;
880 case ir_tex
: inst
= emit(FS_OPCODE_TEX
, dst
); break;
881 case ir_txb
: inst
= emit(FS_OPCODE_TXB
, dst
); break;
882 case ir_txl
: inst
= emit(FS_OPCODE_TXL
, dst
); break;
883 case ir_txd
: inst
= emit(FS_OPCODE_TXD
, dst
); break;
884 case ir_txf
: assert(!"TXF unsupported.");
886 inst
->base_mrf
= base_mrf
;
888 inst
->header_present
= header_present
;
891 fail("Message length >11 disallowed by hardware\n");
898 fs_visitor::visit(ir_texture
*ir
)
901 fs_inst
*inst
= NULL
;
903 this->result
= reg_undef
;
904 ir
->coordinate
->accept(this);
905 fs_reg coordinate
= this->result
;
907 if (ir
->offset
!= NULL
) {
908 ir_constant
*offset
= ir
->offset
->as_constant();
909 assert(offset
!= NULL
);
911 signed char offsets
[3];
912 for (unsigned i
= 0; i
< ir
->offset
->type
->vector_elements
; i
++)
913 offsets
[i
] = (signed char) offset
->value
.i
[i
];
915 /* Combine all three offsets into a single unsigned dword:
917 * bits 11:8 - U Offset (X component)
918 * bits 7:4 - V Offset (Y component)
919 * bits 3:0 - R Offset (Z component)
921 unsigned offset_bits
= 0;
922 for (unsigned i
= 0; i
< ir
->offset
->type
->vector_elements
; i
++) {
923 const unsigned shift
= 4 * (2 - i
);
924 offset_bits
|= (offsets
[i
] << shift
) & (0xF << shift
);
927 /* Explicitly set up the message header by copying g0 to msg reg m1. */
928 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, 1, BRW_REGISTER_TYPE_UD
),
929 fs_reg(GRF
, 0, BRW_REGISTER_TYPE_UD
));
931 /* Then set the offset bits in DWord 2 of the message header. */
933 fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE
, 1, 2),
934 BRW_REGISTER_TYPE_UD
)),
935 fs_reg(brw_imm_uw(offset_bits
)));
938 /* Should be lowered by do_lower_texture_projection */
939 assert(!ir
->projector
);
941 sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
944 sampler
= fp
->Base
.SamplerUnits
[sampler
];
946 /* The 965 requires the EU to do the normalization of GL rectangle
947 * texture coordinates. We use the program parameter state
948 * tracking to get the scaling factor.
950 if (ir
->sampler
->type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_RECT
) {
951 struct gl_program_parameter_list
*params
= c
->fp
->program
.Base
.Parameters
;
952 int tokens
[STATE_LENGTH
] = {
960 if (c
->dispatch_width
== 16) {
961 fail("rectangle scale uniform setup not supported on 16-wide\n");
962 this->result
= fs_reg(this, ir
->type
);
966 c
->prog_data
.param_convert
[c
->prog_data
.nr_params
] =
968 c
->prog_data
.param_convert
[c
->prog_data
.nr_params
+ 1] =
971 fs_reg scale_x
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
);
972 fs_reg scale_y
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
+ 1);
973 GLuint index
= _mesa_add_state_reference(params
,
974 (gl_state_index
*)tokens
);
976 this->param_index
[c
->prog_data
.nr_params
] = index
;
977 this->param_offset
[c
->prog_data
.nr_params
] = 0;
978 c
->prog_data
.nr_params
++;
979 this->param_index
[c
->prog_data
.nr_params
] = index
;
980 this->param_offset
[c
->prog_data
.nr_params
] = 1;
981 c
->prog_data
.nr_params
++;
983 fs_reg dst
= fs_reg(this, ir
->coordinate
->type
);
984 fs_reg src
= coordinate
;
987 emit(BRW_OPCODE_MUL
, dst
, src
, scale_x
);
990 emit(BRW_OPCODE_MUL
, dst
, src
, scale_y
);
993 /* Writemasking doesn't eliminate channels on SIMD8 texture
994 * samples, so don't worry about them.
996 fs_reg dst
= fs_reg(this, glsl_type::vec4_type
);
998 if (intel
->gen
>= 7) {
999 inst
= emit_texture_gen7(ir
, dst
, coordinate
, sampler
);
1000 } else if (intel
->gen
>= 5) {
1001 inst
= emit_texture_gen5(ir
, dst
, coordinate
, sampler
);
1003 inst
= emit_texture_gen4(ir
, dst
, coordinate
, sampler
);
1006 /* If there's an offset, we already set up m1. To avoid the implied move,
1007 * use the null register. Otherwise, we want an implied move from g0.
1009 if (ir
->offset
!= NULL
|| !inst
->header_present
)
1010 inst
->src
[0] = reg_undef
;
1012 inst
->src
[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
));
1014 inst
->sampler
= sampler
;
1018 if (ir
->shadow_comparitor
)
1019 inst
->shadow_compare
= true;
1021 if (ir
->type
== glsl_type::float_type
) {
1022 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1023 assert(ir
->sampler
->type
->sampler_shadow
);
1024 } else if (c
->key
.tex_swizzles
[inst
->sampler
] != SWIZZLE_NOOP
) {
1025 fs_reg swizzle_dst
= fs_reg(this, glsl_type::vec4_type
);
1027 for (int i
= 0; i
< 4; i
++) {
1028 int swiz
= GET_SWZ(c
->key
.tex_swizzles
[inst
->sampler
], i
);
1029 fs_reg l
= swizzle_dst
;
1032 if (swiz
== SWIZZLE_ZERO
) {
1033 emit(BRW_OPCODE_MOV
, l
, fs_reg(0.0f
));
1034 } else if (swiz
== SWIZZLE_ONE
) {
1035 emit(BRW_OPCODE_MOV
, l
, fs_reg(1.0f
));
1038 r
.reg_offset
+= GET_SWZ(c
->key
.tex_swizzles
[inst
->sampler
], i
);
1039 emit(BRW_OPCODE_MOV
, l
, r
);
1042 this->result
= swizzle_dst
;
1047 fs_visitor::visit(ir_swizzle
*ir
)
1049 this->result
= reg_undef
;
1050 ir
->val
->accept(this);
1051 fs_reg val
= this->result
;
1053 if (ir
->type
->vector_elements
== 1) {
1054 this->result
.reg_offset
+= ir
->mask
.x
;
1058 fs_reg result
= fs_reg(this, ir
->type
);
1059 this->result
= result
;
1061 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1062 fs_reg channel
= val
;
1080 channel
.reg_offset
+= swiz
;
1081 emit(BRW_OPCODE_MOV
, result
, channel
);
1082 result
.reg_offset
++;
1087 fs_visitor::visit(ir_discard
*ir
)
1089 assert(ir
->condition
== NULL
); /* FINISHME */
1091 emit(FS_OPCODE_DISCARD
);
1092 kill_emitted
= true;
1096 fs_visitor::visit(ir_constant
*ir
)
1098 /* Set this->result to reg at the bottom of the function because some code
1099 * paths will cause this visitor to be applied to other fields. This will
1100 * cause the value stored in this->result to be modified.
1102 * Make reg constant so that it doesn't get accidentally modified along the
1103 * way. Yes, I actually had this problem. :(
1105 const fs_reg
reg(this, ir
->type
);
1106 fs_reg dst_reg
= reg
;
1108 if (ir
->type
->is_array()) {
1109 const unsigned size
= type_size(ir
->type
->fields
.array
);
1111 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
1112 this->result
= reg_undef
;
1113 ir
->array_elements
[i
]->accept(this);
1114 fs_reg src_reg
= this->result
;
1116 dst_reg
.type
= src_reg
.type
;
1117 for (unsigned j
= 0; j
< size
; j
++) {
1118 emit(BRW_OPCODE_MOV
, dst_reg
, src_reg
);
1119 src_reg
.reg_offset
++;
1120 dst_reg
.reg_offset
++;
1123 } else if (ir
->type
->is_record()) {
1124 foreach_list(node
, &ir
->components
) {
1125 ir_instruction
*const field
= (ir_instruction
*) node
;
1126 const unsigned size
= type_size(field
->type
);
1128 this->result
= reg_undef
;
1129 field
->accept(this);
1130 fs_reg src_reg
= this->result
;
1132 dst_reg
.type
= src_reg
.type
;
1133 for (unsigned j
= 0; j
< size
; j
++) {
1134 emit(BRW_OPCODE_MOV
, dst_reg
, src_reg
);
1135 src_reg
.reg_offset
++;
1136 dst_reg
.reg_offset
++;
1140 const unsigned size
= type_size(ir
->type
);
1142 for (unsigned i
= 0; i
< size
; i
++) {
1143 switch (ir
->type
->base_type
) {
1144 case GLSL_TYPE_FLOAT
:
1145 emit(BRW_OPCODE_MOV
, dst_reg
, fs_reg(ir
->value
.f
[i
]));
1147 case GLSL_TYPE_UINT
:
1148 emit(BRW_OPCODE_MOV
, dst_reg
, fs_reg(ir
->value
.u
[i
]));
1151 emit(BRW_OPCODE_MOV
, dst_reg
, fs_reg(ir
->value
.i
[i
]));
1153 case GLSL_TYPE_BOOL
:
1154 emit(BRW_OPCODE_MOV
, dst_reg
, fs_reg((int)ir
->value
.b
[i
]));
1157 assert(!"Non-float/uint/int/bool constant");
1159 dst_reg
.reg_offset
++;
1167 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
1169 ir_expression
*expr
= ir
->as_expression();
1175 assert(expr
->get_num_operands() <= 2);
1176 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1177 assert(expr
->operands
[i
]->type
->is_scalar());
1179 this->result
= reg_undef
;
1180 expr
->operands
[i
]->accept(this);
1181 op
[i
] = this->result
;
1184 switch (expr
->operation
) {
1185 case ir_unop_logic_not
:
1186 inst
= emit(BRW_OPCODE_AND
, reg_null_d
, op
[0], fs_reg(1));
1187 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1190 case ir_binop_logic_xor
:
1191 inst
= emit(BRW_OPCODE_XOR
, reg_null_d
, op
[0], op
[1]);
1192 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1195 case ir_binop_logic_or
:
1196 inst
= emit(BRW_OPCODE_OR
, reg_null_d
, op
[0], op
[1]);
1197 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1200 case ir_binop_logic_and
:
1201 inst
= emit(BRW_OPCODE_AND
, reg_null_d
, op
[0], op
[1]);
1202 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1206 if (intel
->gen
>= 6) {
1207 inst
= emit(BRW_OPCODE_CMP
, reg_null_d
, op
[0], fs_reg(0.0f
));
1209 inst
= emit(BRW_OPCODE_MOV
, reg_null_f
, op
[0]);
1211 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1215 if (intel
->gen
>= 6) {
1216 inst
= emit(BRW_OPCODE_CMP
, reg_null_d
, op
[0], fs_reg(0));
1218 inst
= emit(BRW_OPCODE_MOV
, reg_null_d
, op
[0]);
1220 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1223 case ir_binop_greater
:
1224 case ir_binop_gequal
:
1226 case ir_binop_lequal
:
1227 case ir_binop_equal
:
1228 case ir_binop_all_equal
:
1229 case ir_binop_nequal
:
1230 case ir_binop_any_nequal
:
1231 inst
= emit(BRW_OPCODE_CMP
, reg_null_cmp
, op
[0], op
[1]);
1232 inst
->conditional_mod
=
1233 brw_conditional_for_comparison(expr
->operation
);
1237 assert(!"not reached");
1238 fail("bad cond code\n");
1244 this->result
= reg_undef
;
1247 if (intel
->gen
>= 6) {
1248 fs_inst
*inst
= emit(BRW_OPCODE_AND
, reg_null_d
, this->result
, fs_reg(1));
1249 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1251 fs_inst
*inst
= emit(BRW_OPCODE_MOV
, reg_null_d
, this->result
);
1252 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1257 * Emit a gen6 IF statement with the comparison folded into the IF
1261 fs_visitor::emit_if_gen6(ir_if
*ir
)
1263 ir_expression
*expr
= ir
->condition
->as_expression();
1270 assert(expr
->get_num_operands() <= 2);
1271 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1272 assert(expr
->operands
[i
]->type
->is_scalar());
1274 this->result
= reg_undef
;
1275 expr
->operands
[i
]->accept(this);
1276 op
[i
] = this->result
;
1279 switch (expr
->operation
) {
1280 case ir_unop_logic_not
:
1281 inst
= emit(BRW_OPCODE_IF
, temp
, op
[0], fs_reg(0));
1282 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1285 case ir_binop_logic_xor
:
1286 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, op
[0], op
[1]);
1287 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1290 case ir_binop_logic_or
:
1291 temp
= fs_reg(this, glsl_type::bool_type
);
1292 emit(BRW_OPCODE_OR
, temp
, op
[0], op
[1]);
1293 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, temp
, fs_reg(0));
1294 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1297 case ir_binop_logic_and
:
1298 temp
= fs_reg(this, glsl_type::bool_type
);
1299 emit(BRW_OPCODE_AND
, temp
, op
[0], op
[1]);
1300 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, temp
, fs_reg(0));
1301 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1305 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
1306 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1310 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, op
[0], fs_reg(0));
1311 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1314 case ir_binop_greater
:
1315 case ir_binop_gequal
:
1317 case ir_binop_lequal
:
1318 case ir_binop_equal
:
1319 case ir_binop_all_equal
:
1320 case ir_binop_nequal
:
1321 case ir_binop_any_nequal
:
1322 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, op
[0], op
[1]);
1323 inst
->conditional_mod
=
1324 brw_conditional_for_comparison(expr
->operation
);
1327 assert(!"not reached");
1328 inst
= emit(BRW_OPCODE_IF
, reg_null_d
, op
[0], fs_reg(0));
1329 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1330 fail("bad condition\n");
1336 this->result
= reg_undef
;
1337 ir
->condition
->accept(this);
1339 fs_inst
*inst
= emit(BRW_OPCODE_IF
, reg_null_d
, this->result
, fs_reg(0));
1340 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1344 fs_visitor::visit(ir_if
*ir
)
1348 if (intel
->gen
!= 6 && c
->dispatch_width
== 16) {
1349 fail("Can't support (non-uniform) control flow on 16-wide\n");
1352 /* Don't point the annotation at the if statement, because then it plus
1353 * the then and else blocks get printed.
1355 this->base_ir
= ir
->condition
;
1357 if (intel
->gen
== 6) {
1360 emit_bool_to_cond_code(ir
->condition
);
1362 inst
= emit(BRW_OPCODE_IF
);
1363 inst
->predicated
= true;
1366 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
1367 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1369 this->result
= reg_undef
;
1373 if (!ir
->else_instructions
.is_empty()) {
1374 emit(BRW_OPCODE_ELSE
);
1376 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
1377 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1379 this->result
= reg_undef
;
1384 emit(BRW_OPCODE_ENDIF
);
1388 fs_visitor::visit(ir_loop
*ir
)
1390 fs_reg counter
= reg_undef
;
1392 if (c
->dispatch_width
== 16) {
1393 fail("Can't support (non-uniform) control flow on 16-wide\n");
1397 this->base_ir
= ir
->counter
;
1398 ir
->counter
->accept(this);
1399 counter
= *(variable_storage(ir
->counter
));
1402 this->result
= counter
;
1404 this->base_ir
= ir
->from
;
1405 this->result
= counter
;
1406 ir
->from
->accept(this);
1408 if (!this->result
.equals(&counter
))
1409 emit(BRW_OPCODE_MOV
, counter
, this->result
);
1413 emit(BRW_OPCODE_DO
);
1416 this->base_ir
= ir
->to
;
1417 this->result
= reg_undef
;
1418 ir
->to
->accept(this);
1420 fs_inst
*inst
= emit(BRW_OPCODE_CMP
, reg_null_cmp
, counter
, this->result
);
1421 inst
->conditional_mod
= brw_conditional_for_comparison(ir
->cmp
);
1423 inst
= emit(BRW_OPCODE_BREAK
);
1424 inst
->predicated
= true;
1427 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
1428 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1431 this->result
= reg_undef
;
1435 if (ir
->increment
) {
1436 this->base_ir
= ir
->increment
;
1437 this->result
= reg_undef
;
1438 ir
->increment
->accept(this);
1439 emit(BRW_OPCODE_ADD
, counter
, counter
, this->result
);
1442 emit(BRW_OPCODE_WHILE
);
1446 fs_visitor::visit(ir_loop_jump
*ir
)
1449 case ir_loop_jump::jump_break
:
1450 emit(BRW_OPCODE_BREAK
);
1452 case ir_loop_jump::jump_continue
:
1453 emit(BRW_OPCODE_CONTINUE
);
1459 fs_visitor::visit(ir_call
*ir
)
1461 assert(!"FINISHME");
1465 fs_visitor::visit(ir_return
*ir
)
1467 assert(!"FINISHME");
1471 fs_visitor::visit(ir_function
*ir
)
1473 /* Ignore function bodies other than main() -- we shouldn't see calls to
1474 * them since they should all be inlined before we get to ir_to_mesa.
1476 if (strcmp(ir
->name
, "main") == 0) {
1477 const ir_function_signature
*sig
;
1480 sig
= ir
->matching_signature(&empty
);
1484 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1485 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1487 this->result
= reg_undef
;
1494 fs_visitor::visit(ir_function_signature
*ir
)
1496 assert(!"not reached");
1501 fs_visitor::emit(fs_inst inst
)
1503 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
1506 if (force_uncompressed_stack
> 0)
1507 list_inst
->force_uncompressed
= true;
1508 else if (force_sechalf_stack
> 0)
1509 list_inst
->force_sechalf
= true;
1511 list_inst
->annotation
= this->current_annotation
;
1512 list_inst
->ir
= this->base_ir
;
1514 this->instructions
.push_tail(list_inst
);
1519 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1521 fs_visitor::emit_dummy_fs()
1523 /* Everyone's favorite color. */
1524 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, 2), fs_reg(1.0f
));
1525 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, 3), fs_reg(0.0f
));
1526 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, 4), fs_reg(1.0f
));
1527 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, 5), fs_reg(0.0f
));
1530 write
= emit(FS_OPCODE_FB_WRITE
, fs_reg(0), fs_reg(0));
1531 write
->base_mrf
= 0;
1534 /* The register location here is relative to the start of the URB
1535 * data. It will get adjusted to be a real location before
1536 * generate_code() time.
1539 fs_visitor::interp_reg(int location
, int channel
)
1541 int regnr
= urb_setup
[location
] * 2 + channel
/ 2;
1542 int stride
= (channel
& 1) * 4;
1544 assert(urb_setup
[location
] != -1);
1546 return brw_vec1_grf(regnr
, stride
);
1549 /** Emits the interpolation for the varying inputs. */
1551 fs_visitor::emit_interpolation_setup_gen4()
1553 this->current_annotation
= "compute pixel centers";
1554 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
1555 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
1556 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
1557 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
1559 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
1560 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
1562 this->current_annotation
= "compute pixel deltas from v0";
1564 this->delta_x
= fs_reg(this, glsl_type::vec2_type
);
1565 this->delta_y
= this->delta_x
;
1566 this->delta_y
.reg_offset
++;
1568 this->delta_x
= fs_reg(this, glsl_type::float_type
);
1569 this->delta_y
= fs_reg(this, glsl_type::float_type
);
1571 emit(BRW_OPCODE_ADD
, this->delta_x
,
1572 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0))));
1573 emit(BRW_OPCODE_ADD
, this->delta_y
,
1574 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1))));
1576 this->current_annotation
= "compute pos.w and 1/pos.w";
1577 /* Compute wpos.w. It's always in our setup, since it's needed to
1578 * interpolate the other attributes.
1580 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
1581 emit(FS_OPCODE_LINTERP
, wpos_w
, this->delta_x
, this->delta_y
,
1582 interp_reg(FRAG_ATTRIB_WPOS
, 3));
1583 /* Compute the pixel 1/W value from wpos.w. */
1584 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
1585 emit_math(FS_OPCODE_RCP
, this->pixel_w
, wpos_w
);
1586 this->current_annotation
= NULL
;
1589 /** Emits the interpolation for the varying inputs. */
1591 fs_visitor::emit_interpolation_setup_gen6()
1593 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
1595 /* If the pixel centers end up used, the setup is the same as for gen4. */
1596 this->current_annotation
= "compute pixel centers";
1597 fs_reg int_pixel_x
= fs_reg(this, glsl_type::uint_type
);
1598 fs_reg int_pixel_y
= fs_reg(this, glsl_type::uint_type
);
1599 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
1600 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
1601 emit(BRW_OPCODE_ADD
,
1603 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
1604 fs_reg(brw_imm_v(0x10101010)));
1605 emit(BRW_OPCODE_ADD
,
1607 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
1608 fs_reg(brw_imm_v(0x11001100)));
1610 /* As of gen6, we can no longer mix float and int sources. We have
1611 * to turn the integer pixel centers into floats for their actual
1614 this->pixel_x
= fs_reg(this, glsl_type::float_type
);
1615 this->pixel_y
= fs_reg(this, glsl_type::float_type
);
1616 emit(BRW_OPCODE_MOV
, this->pixel_x
, int_pixel_x
);
1617 emit(BRW_OPCODE_MOV
, this->pixel_y
, int_pixel_y
);
1619 this->current_annotation
= "compute pos.w";
1620 this->pixel_w
= fs_reg(brw_vec8_grf(c
->source_w_reg
, 0));
1621 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
1622 emit_math(FS_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
1624 this->delta_x
= fs_reg(brw_vec8_grf(2, 0));
1625 this->delta_y
= fs_reg(brw_vec8_grf(3, 0));
1627 this->current_annotation
= NULL
;
1631 fs_visitor::emit_color_write(int index
, int first_color_mrf
, fs_reg color
)
1633 int reg_width
= c
->dispatch_width
/ 8;
1635 if (c
->dispatch_width
== 8 || intel
->gen
== 6) {
1636 /* SIMD8 write looks like:
1642 * gen6 SIMD16 DP write looks like:
1652 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, first_color_mrf
+ index
* reg_width
),
1655 /* pre-gen6 SIMD16 single source DP write looks like:
1665 if (brw
->has_compr4
) {
1666 /* By setting the high bit of the MRF register number, we
1667 * indicate that we want COMPR4 mode - instead of doing the
1668 * usual destination + 1 for the second half we get
1671 emit(BRW_OPCODE_MOV
,
1672 fs_reg(MRF
, BRW_MRF_COMPR4
+ first_color_mrf
+ index
), color
);
1674 push_force_uncompressed();
1675 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, first_color_mrf
+ index
), color
);
1676 pop_force_uncompressed();
1678 push_force_sechalf();
1679 color
.sechalf
= true;
1680 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, first_color_mrf
+ index
+ 4), color
);
1681 pop_force_sechalf();
1682 color
.sechalf
= false;
1688 fs_visitor::emit_fb_writes()
1690 this->current_annotation
= "FB write header";
1691 GLboolean header_present
= GL_TRUE
;
1693 int reg_width
= c
->dispatch_width
/ 8;
1695 if (intel
->gen
>= 6 &&
1696 !this->kill_emitted
&&
1697 c
->key
.nr_color_regions
== 1) {
1698 header_present
= false;
1701 if (header_present
) {
1706 if (c
->aa_dest_stencil_reg
) {
1707 push_force_uncompressed();
1708 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1709 fs_reg(brw_vec8_grf(c
->aa_dest_stencil_reg
, 0)));
1710 pop_force_uncompressed();
1713 /* Reserve space for color. It'll be filled in per MRT below. */
1715 nr
+= 4 * reg_width
;
1717 if (c
->source_depth_to_render_target
) {
1718 if (intel
->gen
== 6 && c
->dispatch_width
== 16) {
1719 /* For outputting oDepth on gen6, SIMD8 writes have to be
1720 * used. This would require 8-wide moves of each half to
1721 * message regs, kind of like pre-gen5 SIMD16 FB writes.
1722 * Just bail on doing so for now.
1724 fail("Missing support for simd16 depth writes on gen6\n");
1727 if (c
->computes_depth
) {
1728 /* Hand over gl_FragDepth. */
1729 assert(this->frag_depth
);
1730 fs_reg depth
= *(variable_storage(this->frag_depth
));
1732 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
), depth
);
1734 /* Pass through the payload depth. */
1735 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
),
1736 fs_reg(brw_vec8_grf(c
->source_depth_reg
, 0)));
1741 if (c
->dest_depth_reg
) {
1742 emit(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
),
1743 fs_reg(brw_vec8_grf(c
->dest_depth_reg
, 0)));
1747 fs_reg color
= reg_undef
;
1748 if (this->frag_color
)
1749 color
= *(variable_storage(this->frag_color
));
1750 else if (this->frag_data
) {
1751 color
= *(variable_storage(this->frag_data
));
1752 color
.type
= BRW_REGISTER_TYPE_F
;
1755 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
1756 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
1757 "FB write target %d",
1759 if (this->frag_color
|| this->frag_data
) {
1760 for (int i
= 0; i
< 4; i
++) {
1761 emit_color_write(i
, color_mrf
, color
);
1766 if (this->frag_color
)
1767 color
.reg_offset
-= 4;
1769 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
1770 inst
->target
= target
;
1773 if (target
== c
->key
.nr_color_regions
- 1)
1775 inst
->header_present
= header_present
;
1778 if (c
->key
.nr_color_regions
== 0) {
1779 if (c
->key
.alpha_test
&& (this->frag_color
|| this->frag_data
)) {
1780 /* If the alpha test is enabled but there's no color buffer,
1781 * we still need to send alpha out the pipeline to our null
1784 color
.reg_offset
+= 3;
1785 emit_color_write(3, color_mrf
, color
);
1788 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
1792 inst
->header_present
= header_present
;
1795 this->current_annotation
= NULL
;