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"
48 #include "glsl/glsl_types.h"
49 #include "glsl/ir_optimization.h"
52 fs_visitor::visit(ir_variable
*ir
)
56 if (variable_storage(ir
))
59 if (ir
->mode
== ir_var_shader_in
) {
60 if (!strcmp(ir
->name
, "gl_FragCoord")) {
61 reg
= emit_fragcoord_interpolation(ir
);
62 } else if (!strcmp(ir
->name
, "gl_FrontFacing")) {
63 reg
= emit_frontfacing_interpolation(ir
);
65 reg
= emit_general_interpolation(ir
);
68 hash_table_insert(this->variable_ht
, reg
, ir
);
70 } else if (ir
->mode
== ir_var_shader_out
) {
71 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
74 assert(ir
->location
== FRAG_RESULT_DATA0
);
75 assert(ir
->index
== 1);
76 this->dual_src_output
= *reg
;
77 } else if (ir
->location
== FRAG_RESULT_COLOR
) {
78 /* Writing gl_FragColor outputs to all color regions. */
79 for (unsigned int i
= 0; i
< MAX2(c
->key
.nr_color_regions
, 1); i
++) {
80 this->outputs
[i
] = *reg
;
81 this->output_components
[i
] = 4;
83 } else if (ir
->location
== FRAG_RESULT_DEPTH
) {
84 this->frag_depth
= *reg
;
86 /* gl_FragData or a user-defined FS output */
87 assert(ir
->location
>= FRAG_RESULT_DATA0
&&
88 ir
->location
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
);
91 ir
->type
->is_array() ? ir
->type
->fields
.array
->vector_elements
92 : ir
->type
->vector_elements
;
94 /* General color output. */
95 for (unsigned int i
= 0; i
< MAX2(1, ir
->type
->length
); i
++) {
96 int output
= ir
->location
- FRAG_RESULT_DATA0
+ i
;
97 this->outputs
[output
] = *reg
;
98 this->outputs
[output
].reg_offset
+= vector_elements
* i
;
99 this->output_components
[output
] = vector_elements
;
102 } else if (ir
->mode
== ir_var_uniform
) {
103 int param_index
= c
->prog_data
.nr_params
;
105 /* Thanks to the lower_ubo_reference pass, we will see only
106 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
107 * variables, so no need for them to be in variable_ht.
109 if (ir
->is_in_uniform_block())
112 if (dispatch_width
== 16) {
113 if (!variable_storage(ir
)) {
114 fail("Failed to find uniform '%s' in 16-wide\n", ir
->name
);
119 param_size
[param_index
] = type_size(ir
->type
);
120 if (!strncmp(ir
->name
, "gl_", 3)) {
121 setup_builtin_uniform_values(ir
);
123 setup_uniform_values(ir
);
126 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
127 reg
->type
= brw_type_for_base_type(ir
->type
);
131 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
133 hash_table_insert(this->variable_ht
, reg
, ir
);
137 fs_visitor::visit(ir_dereference_variable
*ir
)
139 fs_reg
*reg
= variable_storage(ir
->var
);
144 fs_visitor::visit(ir_dereference_record
*ir
)
146 const glsl_type
*struct_type
= ir
->record
->type
;
148 ir
->record
->accept(this);
150 unsigned int offset
= 0;
151 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
152 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
154 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
156 this->result
.reg_offset
+= offset
;
157 this->result
.type
= brw_type_for_base_type(ir
->type
);
161 fs_visitor::visit(ir_dereference_array
*ir
)
163 ir_constant
*constant_index
;
165 int element_size
= type_size(ir
->type
);
167 constant_index
= ir
->array_index
->as_constant();
169 ir
->array
->accept(this);
171 src
.type
= brw_type_for_base_type(ir
->type
);
173 if (constant_index
) {
174 assert(src
.file
== UNIFORM
|| src
.file
== GRF
);
175 src
.reg_offset
+= constant_index
->value
.i
[0] * element_size
;
177 /* Variable index array dereference. We attach the variable index
178 * component to the reg as a pointer to a register containing the
179 * offset. Currently only uniform arrays are supported in this patch,
180 * and that reladdr pointer is resolved by
181 * move_uniform_array_access_to_pull_constants(). All other array types
182 * are lowered by lower_variable_index_to_cond_assign().
184 ir
->array_index
->accept(this);
187 index_reg
= fs_reg(this, glsl_type::int_type
);
188 emit(BRW_OPCODE_MUL
, index_reg
, this->result
, fs_reg(element_size
));
191 emit(BRW_OPCODE_ADD
, index_reg
, *src
.reladdr
, index_reg
);
194 src
.reladdr
= ralloc(mem_ctx
, fs_reg
);
195 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
201 fs_visitor::emit_lrp(fs_reg dst
, fs_reg x
, fs_reg y
, fs_reg a
)
204 !x
.is_valid_3src() ||
205 !y
.is_valid_3src() ||
206 !a
.is_valid_3src()) {
207 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
208 fs_reg y_times_a
= fs_reg(this, glsl_type::float_type
);
209 fs_reg one_minus_a
= fs_reg(this, glsl_type::float_type
);
210 fs_reg x_times_one_minus_a
= fs_reg(this, glsl_type::float_type
);
212 emit(MUL(y_times_a
, y
, a
));
214 a
.negate
= !a
.negate
;
215 emit(ADD(one_minus_a
, a
, fs_reg(1.0f
)));
216 emit(MUL(x_times_one_minus_a
, x
, one_minus_a
));
218 emit(ADD(dst
, x_times_one_minus_a
, y_times_a
));
220 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
221 * we need to reorder the operands.
223 emit(LRP(dst
, a
, y
, x
));
228 fs_visitor::emit_minmax(uint32_t conditionalmod
, fs_reg dst
,
229 fs_reg src0
, fs_reg src1
)
234 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
235 inst
->conditional_mod
= conditionalmod
;
237 emit(CMP(reg_null_d
, src0
, src1
, conditionalmod
));
239 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
240 inst
->predicate
= BRW_PREDICATE_NORMAL
;
244 /* Instruction selection: Produce a MOV.sat instead of
245 * MIN(MAX(val, 0), 1) when possible.
248 fs_visitor::try_emit_saturate(ir_expression
*ir
)
250 ir_rvalue
*sat_val
= ir
->as_rvalue_to_saturate();
255 fs_inst
*pre_inst
= (fs_inst
*) this->instructions
.get_tail();
257 sat_val
->accept(this);
258 fs_reg src
= this->result
;
260 fs_inst
*last_inst
= (fs_inst
*) this->instructions
.get_tail();
262 /* If the last instruction from our accept() didn't generate our
263 * src, generate a saturated MOV
265 fs_inst
*modify
= get_instruction_generating_reg(pre_inst
, last_inst
, src
);
266 if (!modify
|| modify
->regs_written
!= 1) {
267 this->result
= fs_reg(this, ir
->type
);
268 fs_inst
*inst
= emit(MOV(this->result
, src
));
269 inst
->saturate
= true;
271 modify
->saturate
= true;
280 fs_visitor::try_emit_mad(ir_expression
*ir
, int mul_arg
)
282 /* 3-src instructions were introduced in gen6. */
286 /* MAD can only handle floating-point data. */
287 if (ir
->type
!= glsl_type::float_type
)
290 ir_rvalue
*nonmul
= ir
->operands
[1 - mul_arg
];
291 ir_expression
*mul
= ir
->operands
[mul_arg
]->as_expression();
293 if (!mul
|| mul
->operation
!= ir_binop_mul
)
296 if (nonmul
->as_constant() ||
297 mul
->operands
[0]->as_constant() ||
298 mul
->operands
[1]->as_constant())
301 nonmul
->accept(this);
302 fs_reg src0
= this->result
;
304 mul
->operands
[0]->accept(this);
305 fs_reg src1
= this->result
;
307 mul
->operands
[1]->accept(this);
308 fs_reg src2
= this->result
;
310 this->result
= fs_reg(this, ir
->type
);
311 emit(BRW_OPCODE_MAD
, this->result
, src0
, src1
, src2
);
317 fs_visitor::visit(ir_expression
*ir
)
319 unsigned int operand
;
323 assert(ir
->get_num_operands() <= 3);
325 if (try_emit_saturate(ir
))
327 if (ir
->operation
== ir_binop_add
) {
328 if (try_emit_mad(ir
, 0) || try_emit_mad(ir
, 1))
332 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
333 ir
->operands
[operand
]->accept(this);
334 if (this->result
.file
== BAD_FILE
) {
335 fail("Failed to get tree for expression operand:\n");
336 ir
->operands
[operand
]->print();
339 assert(this->result
.is_valid_3src());
340 op
[operand
] = this->result
;
342 /* Matrix expression operands should have been broken down to vector
343 * operations already.
345 assert(!ir
->operands
[operand
]->type
->is_matrix());
346 /* And then those vector operands should have been broken down to scalar.
348 assert(!ir
->operands
[operand
]->type
->is_vector());
351 /* Storage for our result. If our result goes into an assignment, it will
352 * just get copy-propagated out, so no worries.
354 this->result
= fs_reg(this, ir
->type
);
356 switch (ir
->operation
) {
357 case ir_unop_logic_not
:
358 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
359 * ones complement of the whole register, not just bit 0.
361 emit(XOR(this->result
, op
[0], fs_reg(1)));
364 op
[0].negate
= !op
[0].negate
;
365 emit(MOV(this->result
, op
[0]));
369 op
[0].negate
= false;
370 emit(MOV(this->result
, op
[0]));
373 temp
= fs_reg(this, ir
->type
);
375 emit(MOV(this->result
, fs_reg(0.0f
)));
377 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_G
));
378 inst
= emit(MOV(this->result
, fs_reg(1.0f
)));
379 inst
->predicate
= BRW_PREDICATE_NORMAL
;
381 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_L
));
382 inst
= emit(MOV(this->result
, fs_reg(-1.0f
)));
383 inst
->predicate
= BRW_PREDICATE_NORMAL
;
387 emit_math(SHADER_OPCODE_RCP
, this->result
, op
[0]);
391 emit_math(SHADER_OPCODE_EXP2
, this->result
, op
[0]);
394 emit_math(SHADER_OPCODE_LOG2
, this->result
, op
[0]);
398 assert(!"not reached: should be handled by ir_explog_to_explog2");
401 case ir_unop_sin_reduced
:
402 emit_math(SHADER_OPCODE_SIN
, this->result
, op
[0]);
405 case ir_unop_cos_reduced
:
406 emit_math(SHADER_OPCODE_COS
, this->result
, op
[0]);
410 emit(FS_OPCODE_DDX
, this->result
, op
[0]);
413 emit(FS_OPCODE_DDY
, this->result
, op
[0]);
417 emit(ADD(this->result
, op
[0], op
[1]));
420 assert(!"not reached: should be handled by ir_sub_to_add_neg");
424 if (ir
->type
->is_integer()) {
425 /* For integer multiplication, the MUL uses the low 16 bits
426 * of one of the operands (src0 on gen6, src1 on gen7). The
427 * MACH accumulates in the contribution of the upper 16 bits
430 * FINISHME: Emit just the MUL if we know an operand is small
433 if (brw
->gen
>= 7 && dispatch_width
== 16)
434 fail("16-wide explicit accumulator operands unsupported\n");
436 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_D
);
438 emit(MUL(acc
, op
[0], op
[1]));
439 emit(MACH(reg_null_d
, op
[0], op
[1]));
440 emit(MOV(this->result
, fs_reg(acc
)));
442 emit(MUL(this->result
, op
[0], op
[1]));
446 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
447 assert(ir
->type
->is_integer());
448 emit_math(SHADER_OPCODE_INT_QUOTIENT
, this->result
, op
[0], op
[1]);
451 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
452 assert(ir
->type
->is_integer());
453 emit_math(SHADER_OPCODE_INT_REMAINDER
, this->result
, op
[0], op
[1]);
457 case ir_binop_greater
:
458 case ir_binop_lequal
:
459 case ir_binop_gequal
:
461 case ir_binop_all_equal
:
462 case ir_binop_nequal
:
463 case ir_binop_any_nequal
:
464 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
465 resolve_bool_comparison(ir
->operands
[1], &op
[1]);
467 emit(CMP(this->result
, op
[0], op
[1],
468 brw_conditional_for_comparison(ir
->operation
)));
471 case ir_binop_logic_xor
:
472 emit(XOR(this->result
, op
[0], op
[1]));
475 case ir_binop_logic_or
:
476 emit(OR(this->result
, op
[0], op
[1]));
479 case ir_binop_logic_and
:
480 emit(AND(this->result
, op
[0], op
[1]));
485 assert(!"not reached: should be handled by brw_fs_channel_expressions");
489 assert(!"not reached: should be handled by lower_noise");
492 case ir_quadop_vector
:
493 assert(!"not reached: should be handled by lower_quadop_vector");
496 case ir_binop_vector_extract
:
497 assert(!"not reached: should be handled by lower_vec_index_to_cond_assign()");
500 case ir_triop_vector_insert
:
501 assert(!"not reached: should be handled by lower_vector_insert()");
505 assert(!"not reached: should be handled by ldexp_to_arith()");
509 emit_math(SHADER_OPCODE_SQRT
, this->result
, op
[0]);
513 emit_math(SHADER_OPCODE_RSQ
, this->result
, op
[0]);
516 case ir_unop_bitcast_i2f
:
517 case ir_unop_bitcast_u2f
:
518 op
[0].type
= BRW_REGISTER_TYPE_F
;
519 this->result
= op
[0];
522 case ir_unop_bitcast_f2u
:
523 op
[0].type
= BRW_REGISTER_TYPE_UD
;
524 this->result
= op
[0];
527 case ir_unop_bitcast_f2i
:
528 op
[0].type
= BRW_REGISTER_TYPE_D
;
529 this->result
= op
[0];
535 emit(MOV(this->result
, op
[0]));
539 emit(AND(this->result
, op
[0], fs_reg(1)));
542 temp
= fs_reg(this, glsl_type::int_type
);
543 emit(AND(temp
, op
[0], fs_reg(1)));
544 emit(MOV(this->result
, temp
));
548 emit(CMP(this->result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
551 emit(CMP(this->result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
555 emit(RNDZ(this->result
, op
[0]));
558 op
[0].negate
= !op
[0].negate
;
559 emit(RNDD(this->result
, op
[0]));
560 this->result
.negate
= true;
563 emit(RNDD(this->result
, op
[0]));
566 emit(FRC(this->result
, op
[0]));
568 case ir_unop_round_even
:
569 emit(RNDE(this->result
, op
[0]));
574 resolve_ud_negate(&op
[0]);
575 resolve_ud_negate(&op
[1]);
576 emit_minmax(ir
->operation
== ir_binop_min
?
577 BRW_CONDITIONAL_L
: BRW_CONDITIONAL_GE
,
578 this->result
, op
[0], op
[1]);
580 case ir_unop_pack_snorm_2x16
:
581 case ir_unop_pack_snorm_4x8
:
582 case ir_unop_pack_unorm_2x16
:
583 case ir_unop_pack_unorm_4x8
:
584 case ir_unop_unpack_snorm_2x16
:
585 case ir_unop_unpack_snorm_4x8
:
586 case ir_unop_unpack_unorm_2x16
:
587 case ir_unop_unpack_unorm_4x8
:
588 case ir_unop_unpack_half_2x16
:
589 case ir_unop_pack_half_2x16
:
590 assert(!"not reached: should be handled by lower_packing_builtins");
592 case ir_unop_unpack_half_2x16_split_x
:
593 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, this->result
, op
[0]);
595 case ir_unop_unpack_half_2x16_split_y
:
596 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, this->result
, op
[0]);
599 emit_math(SHADER_OPCODE_POW
, this->result
, op
[0], op
[1]);
602 case ir_unop_bitfield_reverse
:
603 emit(BFREV(this->result
, op
[0]));
605 case ir_unop_bit_count
:
606 emit(CBIT(this->result
, op
[0]));
608 case ir_unop_find_msb
:
609 temp
= fs_reg(this, glsl_type::uint_type
);
610 emit(FBH(temp
, op
[0]));
612 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
613 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
614 * subtract the result from 31 to convert the MSB count into an LSB count.
617 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
618 emit(MOV(this->result
, temp
));
619 emit(CMP(reg_null_d
, this->result
, fs_reg(-1), BRW_CONDITIONAL_NZ
));
622 inst
= emit(ADD(this->result
, temp
, fs_reg(31)));
623 inst
->predicate
= BRW_PREDICATE_NORMAL
;
625 case ir_unop_find_lsb
:
626 emit(FBL(this->result
, op
[0]));
628 case ir_triop_bitfield_extract
:
629 /* Note that the instruction's argument order is reversed from GLSL
632 emit(BFE(this->result
, op
[2], op
[1], op
[0]));
635 emit(BFI1(this->result
, op
[0], op
[1]));
638 emit(BFI2(this->result
, op
[0], op
[1], op
[2]));
640 case ir_quadop_bitfield_insert
:
641 assert(!"not reached: should be handled by "
642 "lower_instructions::bitfield_insert_to_bfm_bfi");
645 case ir_unop_bit_not
:
646 emit(NOT(this->result
, op
[0]));
648 case ir_binop_bit_and
:
649 emit(AND(this->result
, op
[0], op
[1]));
651 case ir_binop_bit_xor
:
652 emit(XOR(this->result
, op
[0], op
[1]));
654 case ir_binop_bit_or
:
655 emit(OR(this->result
, op
[0], op
[1]));
658 case ir_binop_lshift
:
659 emit(SHL(this->result
, op
[0], op
[1]));
662 case ir_binop_rshift
:
663 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
664 emit(ASR(this->result
, op
[0], op
[1]));
666 emit(SHR(this->result
, op
[0], op
[1]));
668 case ir_binop_pack_half_2x16_split
:
669 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, this->result
, op
[0], op
[1]);
671 case ir_binop_ubo_load
: {
672 /* This IR node takes a constant uniform block and a constant or
673 * variable byte offset within the block and loads a vector from that.
675 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
676 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
677 fs_reg surf_index
= fs_reg((unsigned)SURF_INDEX_WM_UBO(uniform_block
->value
.u
[0]));
679 fs_reg packed_consts
= fs_reg(this, glsl_type::float_type
);
680 packed_consts
.type
= result
.type
;
682 fs_reg const_offset_reg
= fs_reg(const_offset
->value
.u
[0] & ~15);
683 emit(fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
684 packed_consts
, surf_index
, const_offset_reg
));
686 packed_consts
.smear
= const_offset
->value
.u
[0] % 16 / 4;
687 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
688 /* UBO bools are any nonzero value. We consider bools to be
689 * values with the low bit set to 1. Convert them using CMP.
691 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
692 emit(CMP(result
, packed_consts
, fs_reg(0u), BRW_CONDITIONAL_NZ
));
694 emit(MOV(result
, packed_consts
));
697 packed_consts
.smear
++;
700 /* The std140 packing rules don't allow vectors to cross 16-byte
701 * boundaries, and a reg is 32 bytes.
703 assert(packed_consts
.smear
< 8);
706 /* Turn the byte offset into a dword offset. */
707 fs_reg base_offset
= fs_reg(this, glsl_type::int_type
);
708 emit(SHR(base_offset
, op
[1], fs_reg(2)));
710 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
711 emit(VARYING_PULL_CONSTANT_LOAD(result
, surf_index
,
714 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
715 emit(CMP(result
, result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
721 result
.reg_offset
= 0;
726 /* Note that the instruction's argument order is reversed from GLSL
729 emit(MAD(this->result
, op
[2], op
[1], op
[0]));
733 emit_lrp(this->result
, op
[0], op
[1], op
[2]);
737 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
738 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[1], op
[2]);
739 inst
->predicate
= BRW_PREDICATE_NORMAL
;
745 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
746 const glsl_type
*type
, bool predicated
)
748 switch (type
->base_type
) {
749 case GLSL_TYPE_FLOAT
:
753 for (unsigned int i
= 0; i
< type
->components(); i
++) {
754 l
.type
= brw_type_for_base_type(type
);
755 r
.type
= brw_type_for_base_type(type
);
757 if (predicated
|| !l
.equals(r
)) {
758 fs_inst
*inst
= emit(MOV(l
, r
));
759 inst
->predicate
= predicated
? BRW_PREDICATE_NORMAL
: BRW_PREDICATE_NONE
;
766 case GLSL_TYPE_ARRAY
:
767 for (unsigned int i
= 0; i
< type
->length
; i
++) {
768 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
772 case GLSL_TYPE_STRUCT
:
773 for (unsigned int i
= 0; i
< type
->length
; i
++) {
774 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
779 case GLSL_TYPE_SAMPLER
:
783 case GLSL_TYPE_ERROR
:
784 case GLSL_TYPE_INTERFACE
:
785 assert(!"not reached");
790 /* If the RHS processing resulted in an instruction generating a
791 * temporary value, and it would be easy to rewrite the instruction to
792 * generate its result right into the LHS instead, do so. This ends
793 * up reliably removing instructions where it can be tricky to do so
794 * later without real UD chain information.
797 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
800 fs_inst
*pre_rhs_inst
,
801 fs_inst
*last_rhs_inst
)
803 /* Only attempt if we're doing a direct assignment. */
805 !(ir
->lhs
->type
->is_scalar() ||
806 (ir
->lhs
->type
->is_vector() &&
807 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1)))
810 /* Make sure the last instruction generated our source reg. */
811 fs_inst
*modify
= get_instruction_generating_reg(pre_rhs_inst
,
817 /* If last_rhs_inst wrote a different number of components than our LHS,
818 * we can't safely rewrite it.
820 if (virtual_grf_sizes
[dst
.reg
] != modify
->regs_written
)
823 /* Success! Rewrite the instruction. */
830 fs_visitor::visit(ir_assignment
*ir
)
835 /* FINISHME: arrays on the lhs */
836 ir
->lhs
->accept(this);
839 fs_inst
*pre_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
841 ir
->rhs
->accept(this);
844 fs_inst
*last_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
846 assert(l
.file
!= BAD_FILE
);
847 assert(r
.file
!= BAD_FILE
);
849 if (try_rewrite_rhs_to_dst(ir
, l
, r
, pre_rhs_inst
, last_rhs_inst
))
853 emit_bool_to_cond_code(ir
->condition
);
856 if (ir
->lhs
->type
->is_scalar() ||
857 ir
->lhs
->type
->is_vector()) {
858 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
859 if (ir
->write_mask
& (1 << i
)) {
860 inst
= emit(MOV(l
, r
));
862 inst
->predicate
= BRW_PREDICATE_NORMAL
;
868 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
873 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
874 fs_reg shadow_c
, fs_reg lod
, fs_reg dPdy
)
884 if (ir
->shadow_comparitor
) {
885 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
886 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
887 coordinate
.reg_offset
++;
890 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
891 * the unused slots must be zeroed.
893 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
894 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
898 if (ir
->op
== ir_tex
) {
899 /* There's no plain shadow compare message, so we use shadow
900 * compare with a bias of 0.0.
902 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
)));
904 } else if (ir
->op
== ir_txb
|| ir
->op
== ir_txl
) {
905 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
908 assert(!"Should not get here.");
911 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
913 } else if (ir
->op
== ir_tex
) {
914 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
915 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
916 coordinate
.reg_offset
++;
918 /* zero the others. */
919 for (int i
= ir
->coordinate
->type
->vector_elements
; i
<3; i
++) {
920 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
922 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
924 } else if (ir
->op
== ir_txd
) {
927 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
928 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
929 coordinate
.reg_offset
++;
931 /* the slots for u and v are always present, but r is optional */
932 mlen
+= MAX2(ir
->coordinate
->type
->vector_elements
, 2);
935 * dPdx = dudx, dvdx, drdx
936 * dPdy = dudy, dvdy, drdy
938 * 1-arg: Does not exist.
940 * 2-arg: dudx dvdx dudy dvdy
941 * dPdx.x dPdx.y dPdy.x dPdy.y
944 * 3-arg: dudx dvdx drdx dudy dvdy drdy
945 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
948 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
949 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdx
));
952 mlen
+= MAX2(ir
->lod_info
.grad
.dPdx
->type
->vector_elements
, 2);
954 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdy
->type
->vector_elements
; i
++) {
955 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdy
));
958 mlen
+= MAX2(ir
->lod_info
.grad
.dPdy
->type
->vector_elements
, 2);
959 } else if (ir
->op
== ir_txs
) {
960 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
962 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
965 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
966 * instructions. We'll need to do SIMD16 here.
969 assert(ir
->op
== ir_txb
|| ir
->op
== ir_txl
|| ir
->op
== ir_txf
);
971 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
972 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2, coordinate
.type
),
974 coordinate
.reg_offset
++;
977 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
978 * be necessary for TXF (ld), but seems wise to do for all messages.
980 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
981 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2), fs_reg(0.0f
)));
984 /* lod/bias appears after u/v/r. */
987 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, lod
.type
), lod
));
990 /* The unused upper half. */
995 /* Now, since we're doing simd16, the return is 2 interleaved
996 * vec4s where the odd-indexed ones are junk. We'll need to move
997 * this weirdness around to the expected layout.
1000 dst
= fs_reg(GRF
, virtual_grf_alloc(8),
1002 brw_type_for_base_type(ir
->type
) :
1003 BRW_REGISTER_TYPE_F
));
1006 fs_inst
*inst
= NULL
;
1009 inst
= emit(SHADER_OPCODE_TEX
, dst
);
1012 inst
= emit(FS_OPCODE_TXB
, dst
);
1015 inst
= emit(SHADER_OPCODE_TXL
, dst
);
1018 inst
= emit(SHADER_OPCODE_TXD
, dst
);
1021 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1024 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1027 fail("unrecognized texture opcode");
1029 inst
->base_mrf
= base_mrf
;
1031 inst
->header_present
= true;
1032 inst
->regs_written
= simd16
? 8 : 4;
1035 for (int i
= 0; i
< 4; i
++) {
1036 emit(MOV(orig_dst
, dst
));
1037 orig_dst
.reg_offset
++;
1038 dst
.reg_offset
+= 2;
1045 /* gen5's sampler has slots for u, v, r, array index, then optional
1046 * parameters like shadow comparitor or LOD bias. If optional
1047 * parameters aren't present, those base slots are optional and don't
1048 * need to be included in the message.
1050 * We don't fill in the unnecessary slots regardless, which may look
1051 * surprising in the disassembly.
1054 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1055 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1056 fs_reg sample_index
)
1060 int reg_width
= dispatch_width
/ 8;
1061 bool header_present
= false;
1062 const int vector_elements
=
1063 ir
->coordinate
? ir
->coordinate
->type
->vector_elements
: 0;
1065 if (ir
->offset
!= NULL
&& ir
->op
== ir_txf
) {
1066 /* It appears that the ld instruction used for txf does its
1067 * address bounds check before adding in the offset. To work
1068 * around this, just add the integer offset to the integer texel
1069 * coordinate, and don't put the offset in the header.
1071 ir_constant
*offset
= ir
->offset
->as_constant();
1072 for (int i
= 0; i
< vector_elements
; i
++) {
1073 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1075 offset
->value
.i
[i
]));
1076 coordinate
.reg_offset
++;
1080 /* The offsets set up by the ir_texture visitor are in the
1081 * m1 header, so we can't go headerless.
1083 header_present
= true;
1088 for (int i
= 0; i
< vector_elements
; i
++) {
1089 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1091 coordinate
.reg_offset
++;
1094 mlen
+= vector_elements
* reg_width
;
1096 if (ir
->shadow_comparitor
) {
1097 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1099 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1103 fs_inst
*inst
= NULL
;
1106 inst
= emit(SHADER_OPCODE_TEX
, dst
);
1109 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1110 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1113 inst
= emit(FS_OPCODE_TXB
, dst
);
1116 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1117 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1120 inst
= emit(SHADER_OPCODE_TXL
, dst
);
1123 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
); /* skip over 'ai' */
1127 * dPdx = dudx, dvdx, drdx
1128 * dPdy = dudy, dvdy, drdy
1130 * Load up these values:
1131 * - dudx dudy dvdx dvdy drdx drdy
1132 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1134 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
1135 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1139 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod2
));
1144 inst
= emit(SHADER_OPCODE_TXD
, dst
);
1148 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1150 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1153 mlen
= header_present
+ 4 * reg_width
;
1154 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), lod
));
1155 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1158 mlen
= header_present
+ 4 * reg_width
;
1161 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), fs_reg(0)));
1163 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), sample_index
));
1165 inst
= emit(SHADER_OPCODE_TXF_MS
, dst
);
1168 inst
= emit(SHADER_OPCODE_LOD
, dst
);
1171 inst
->base_mrf
= base_mrf
;
1173 inst
->header_present
= header_present
;
1174 inst
->regs_written
= 4;
1177 fail("Message length >11 disallowed by hardware\n");
1184 fs_visitor::emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1185 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1186 fs_reg sample_index
)
1190 int reg_width
= dispatch_width
/ 8;
1191 bool header_present
= false;
1194 if (ir
->offset
&& ir
->op
!= ir_txf
) {
1195 /* The offsets set up by the ir_texture visitor are in the
1196 * m1 header, so we can't go headerless.
1198 header_present
= true;
1203 if (ir
->shadow_comparitor
) {
1204 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1208 /* Set up the LOD info */
1214 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1218 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1222 if (dispatch_width
== 16)
1223 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1225 /* Load dPdx and the coordinate together:
1226 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1228 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1229 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1230 coordinate
.reg_offset
++;
1233 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1234 * only derivatives for (u, v, r).
1236 if (i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
) {
1237 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1241 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod2
));
1249 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1253 /* It appears that the ld instruction used for txf does its
1254 * address bounds check before adding in the offset. To work
1255 * around this, just add the integer offset to the integer texel
1256 * coordinate, and don't put the offset in the header.
1259 ir_constant
*offset
= ir
->offset
->as_constant();
1260 offsets
[0] = offset
->value
.i
[0];
1261 offsets
[1] = offset
->value
.i
[1];
1262 offsets
[2] = offset
->value
.i
[2];
1264 memset(offsets
, 0, sizeof(offsets
));
1267 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1268 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1269 coordinate
, offsets
[0]));
1270 coordinate
.reg_offset
++;
1273 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
), lod
));
1276 for (int i
= 1; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1277 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1278 coordinate
, offsets
[i
]));
1279 coordinate
.reg_offset
++;
1284 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), sample_index
));
1287 /* constant zero MCS; we arrange to never actually have a compressed
1288 * multisample surface here for now. TODO: issue ld_mcs to get this first,
1289 * if we ever support texturing from compressed multisample surfaces
1291 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1294 /* there is no offsetting for this message; just copy in the integer
1295 * texture coordinates
1297 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1298 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1300 coordinate
.reg_offset
++;
1306 /* Set up the coordinate (except for cases where it was done above) */
1307 if (ir
->op
!= ir_txd
&& ir
->op
!= ir_txs
&& ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
) {
1308 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1309 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1310 coordinate
.reg_offset
++;
1315 /* Generate the SEND */
1316 fs_inst
*inst
= NULL
;
1318 case ir_tex
: inst
= emit(SHADER_OPCODE_TEX
, dst
); break;
1319 case ir_txb
: inst
= emit(FS_OPCODE_TXB
, dst
); break;
1320 case ir_txl
: inst
= emit(SHADER_OPCODE_TXL
, dst
); break;
1321 case ir_txd
: inst
= emit(SHADER_OPCODE_TXD
, dst
); break;
1322 case ir_txf
: inst
= emit(SHADER_OPCODE_TXF
, dst
); break;
1323 case ir_txf_ms
: inst
= emit(SHADER_OPCODE_TXF_MS
, dst
); break;
1324 case ir_txs
: inst
= emit(SHADER_OPCODE_TXS
, dst
); break;
1325 case ir_lod
: inst
= emit(SHADER_OPCODE_LOD
, dst
); break;
1327 inst
->base_mrf
= base_mrf
;
1329 inst
->header_present
= header_present
;
1330 inst
->regs_written
= 4;
1333 fail("Message length >11 disallowed by hardware\n");
1340 fs_visitor::rescale_texcoord(ir_texture
*ir
, fs_reg coordinate
,
1341 bool is_rect
, int sampler
, int texunit
)
1343 fs_inst
*inst
= NULL
;
1344 bool needs_gl_clamp
= true;
1345 fs_reg scale_x
, scale_y
;
1347 /* The 965 requires the EU to do the normalization of GL rectangle
1348 * texture coordinates. We use the program parameter state
1349 * tracking to get the scaling factor.
1353 (brw
->gen
>= 6 && (c
->key
.tex
.gl_clamp_mask
[0] & (1 << sampler
) ||
1354 c
->key
.tex
.gl_clamp_mask
[1] & (1 << sampler
))))) {
1355 struct gl_program_parameter_list
*params
= fp
->Base
.Parameters
;
1356 int tokens
[STATE_LENGTH
] = {
1358 STATE_TEXRECT_SCALE
,
1364 if (dispatch_width
== 16) {
1365 fail("rectangle scale uniform setup not supported on 16-wide\n");
1369 scale_x
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
);
1370 scale_y
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
+ 1);
1372 GLuint index
= _mesa_add_state_reference(params
,
1373 (gl_state_index
*)tokens
);
1374 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1375 &fp
->Base
.Parameters
->ParameterValues
[index
][0].f
;
1376 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1377 &fp
->Base
.Parameters
->ParameterValues
[index
][1].f
;
1380 /* The 965 requires the EU to do the normalization of GL rectangle
1381 * texture coordinates. We use the program parameter state
1382 * tracking to get the scaling factor.
1384 if (brw
->gen
< 6 && is_rect
) {
1385 fs_reg dst
= fs_reg(this, ir
->coordinate
->type
);
1386 fs_reg src
= coordinate
;
1389 emit(MUL(dst
, src
, scale_x
));
1392 emit(MUL(dst
, src
, scale_y
));
1393 } else if (is_rect
) {
1394 /* On gen6+, the sampler handles the rectangle coordinates
1395 * natively, without needing rescaling. But that means we have
1396 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
1397 * not [0, 1] like the default case below.
1399 needs_gl_clamp
= false;
1401 for (int i
= 0; i
< 2; i
++) {
1402 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1403 fs_reg chan
= coordinate
;
1404 chan
.reg_offset
+= i
;
1406 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, brw_imm_f(0.0));
1407 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1409 /* Our parameter comes in as 1.0/width or 1.0/height,
1410 * because that's what people normally want for doing
1411 * texture rectangle handling. We need width or height
1412 * for clamping, but we don't care enough to make a new
1413 * parameter type, so just invert back.
1415 fs_reg limit
= fs_reg(this, glsl_type::float_type
);
1416 emit(MOV(limit
, i
== 0 ? scale_x
: scale_y
));
1417 emit(SHADER_OPCODE_RCP
, limit
, limit
);
1419 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, limit
);
1420 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1425 if (ir
->coordinate
&& needs_gl_clamp
) {
1426 for (unsigned int i
= 0;
1427 i
< MIN2(ir
->coordinate
->type
->vector_elements
, 3); i
++) {
1428 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1429 fs_reg chan
= coordinate
;
1430 chan
.reg_offset
+= i
;
1432 fs_inst
*inst
= emit(MOV(chan
, chan
));
1433 inst
->saturate
= true;
1441 fs_visitor::visit(ir_texture
*ir
)
1443 fs_inst
*inst
= NULL
;
1446 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, &fp
->Base
);
1447 /* FINISHME: We're failing to recompile our programs when the sampler is
1448 * updated. This only matters for the texture rectangle scale parameters
1449 * (pre-gen6, or gen6+ with GL_CLAMP).
1451 int texunit
= fp
->Base
.SamplerUnits
[sampler
];
1453 /* Should be lowered by do_lower_texture_projection */
1454 assert(!ir
->projector
);
1456 /* Generate code to compute all the subexpression trees. This has to be
1457 * done before loading any values into MRFs for the sampler message since
1458 * generating these values may involve SEND messages that need the MRFs.
1461 if (ir
->coordinate
) {
1462 ir
->coordinate
->accept(this);
1464 coordinate
= rescale_texcoord(ir
, this->result
,
1465 ir
->sampler
->type
->sampler_dimensionality
==
1466 GLSL_SAMPLER_DIM_RECT
,
1470 fs_reg shadow_comparitor
;
1471 if (ir
->shadow_comparitor
) {
1472 ir
->shadow_comparitor
->accept(this);
1473 shadow_comparitor
= this->result
;
1476 fs_reg lod
, lod2
, sample_index
;
1482 ir
->lod_info
.bias
->accept(this);
1486 ir
->lod_info
.grad
.dPdx
->accept(this);
1489 ir
->lod_info
.grad
.dPdy
->accept(this);
1490 lod2
= this->result
;
1495 ir
->lod_info
.lod
->accept(this);
1499 ir
->lod_info
.sample_index
->accept(this);
1500 sample_index
= this->result
;
1504 /* Writemasking doesn't eliminate channels on SIMD8 texture
1505 * samples, so don't worry about them.
1507 fs_reg dst
= fs_reg(this, glsl_type::get_instance(ir
->type
->base_type
, 4, 1));
1509 if (brw
->gen
>= 7) {
1510 inst
= emit_texture_gen7(ir
, dst
, coordinate
, shadow_comparitor
,
1511 lod
, lod2
, sample_index
);
1512 } else if (brw
->gen
>= 5) {
1513 inst
= emit_texture_gen5(ir
, dst
, coordinate
, shadow_comparitor
,
1514 lod
, lod2
, sample_index
);
1516 inst
= emit_texture_gen4(ir
, dst
, coordinate
, shadow_comparitor
,
1520 /* The header is set up by generate_tex() when necessary. */
1521 inst
->src
[0] = reg_undef
;
1523 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
1524 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1526 inst
->sampler
= sampler
;
1528 if (ir
->shadow_comparitor
)
1529 inst
->shadow_compare
= true;
1531 /* fixup #layers for cube map arrays */
1532 if (ir
->op
== ir_txs
) {
1533 glsl_type
const *type
= ir
->sampler
->type
;
1534 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
1535 type
->sampler_array
) {
1537 depth
.reg_offset
= 2;
1538 emit_math(SHADER_OPCODE_INT_QUOTIENT
, depth
, depth
, fs_reg(6));
1542 swizzle_result(ir
, dst
, sampler
);
1546 * Swizzle the result of a texture result. This is necessary for
1547 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1550 fs_visitor::swizzle_result(ir_texture
*ir
, fs_reg orig_val
, int sampler
)
1552 this->result
= orig_val
;
1554 if (ir
->op
== ir_txs
|| ir
->op
== ir_lod
)
1557 if (ir
->type
== glsl_type::float_type
) {
1558 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1559 assert(ir
->sampler
->type
->sampler_shadow
);
1560 } else if (c
->key
.tex
.swizzles
[sampler
] != SWIZZLE_NOOP
) {
1561 fs_reg swizzled_result
= fs_reg(this, glsl_type::vec4_type
);
1563 for (int i
= 0; i
< 4; i
++) {
1564 int swiz
= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1565 fs_reg l
= swizzled_result
;
1568 if (swiz
== SWIZZLE_ZERO
) {
1569 emit(MOV(l
, fs_reg(0.0f
)));
1570 } else if (swiz
== SWIZZLE_ONE
) {
1571 emit(MOV(l
, fs_reg(1.0f
)));
1573 fs_reg r
= orig_val
;
1574 r
.reg_offset
+= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1578 this->result
= swizzled_result
;
1583 fs_visitor::visit(ir_swizzle
*ir
)
1585 ir
->val
->accept(this);
1586 fs_reg val
= this->result
;
1588 if (ir
->type
->vector_elements
== 1) {
1589 this->result
.reg_offset
+= ir
->mask
.x
;
1593 fs_reg result
= fs_reg(this, ir
->type
);
1594 this->result
= result
;
1596 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1597 fs_reg channel
= val
;
1615 channel
.reg_offset
+= swiz
;
1616 emit(MOV(result
, channel
));
1617 result
.reg_offset
++;
1622 fs_visitor::visit(ir_discard
*ir
)
1624 assert(ir
->condition
== NULL
); /* FINISHME */
1626 /* We track our discarded pixels in f0.1. By predicating on it, we can
1627 * update just the flag bits that aren't yet discarded. By emitting a
1628 * CMP of g0 != g0, all our currently executing channels will get turned
1631 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
1632 BRW_REGISTER_TYPE_UW
));
1633 fs_inst
*cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
1634 BRW_CONDITIONAL_NZ
));
1635 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
1636 cmp
->flag_subreg
= 1;
1638 if (brw
->gen
>= 6) {
1639 /* For performance, after a discard, jump to the end of the shader.
1640 * However, many people will do foliage by discarding based on a
1641 * texture's alpha mask, and then continue on to texture with the
1642 * remaining pixels. To avoid trashing the derivatives for those
1643 * texture samples, we'll only jump if all of the pixels in the subspan
1644 * have been discarded.
1646 fs_inst
*discard_jump
= emit(FS_OPCODE_DISCARD_JUMP
);
1647 discard_jump
->flag_subreg
= 1;
1648 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1649 discard_jump
->predicate_inverse
= true;
1654 fs_visitor::visit(ir_constant
*ir
)
1656 /* Set this->result to reg at the bottom of the function because some code
1657 * paths will cause this visitor to be applied to other fields. This will
1658 * cause the value stored in this->result to be modified.
1660 * Make reg constant so that it doesn't get accidentally modified along the
1661 * way. Yes, I actually had this problem. :(
1663 const fs_reg
reg(this, ir
->type
);
1664 fs_reg dst_reg
= reg
;
1666 if (ir
->type
->is_array()) {
1667 const unsigned size
= type_size(ir
->type
->fields
.array
);
1669 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
1670 ir
->array_elements
[i
]->accept(this);
1671 fs_reg src_reg
= this->result
;
1673 dst_reg
.type
= src_reg
.type
;
1674 for (unsigned j
= 0; j
< size
; j
++) {
1675 emit(MOV(dst_reg
, src_reg
));
1676 src_reg
.reg_offset
++;
1677 dst_reg
.reg_offset
++;
1680 } else if (ir
->type
->is_record()) {
1681 foreach_list(node
, &ir
->components
) {
1682 ir_constant
*const field
= (ir_constant
*) node
;
1683 const unsigned size
= type_size(field
->type
);
1685 field
->accept(this);
1686 fs_reg src_reg
= this->result
;
1688 dst_reg
.type
= src_reg
.type
;
1689 for (unsigned j
= 0; j
< size
; j
++) {
1690 emit(MOV(dst_reg
, src_reg
));
1691 src_reg
.reg_offset
++;
1692 dst_reg
.reg_offset
++;
1696 const unsigned size
= type_size(ir
->type
);
1698 for (unsigned i
= 0; i
< size
; i
++) {
1699 switch (ir
->type
->base_type
) {
1700 case GLSL_TYPE_FLOAT
:
1701 emit(MOV(dst_reg
, fs_reg(ir
->value
.f
[i
])));
1703 case GLSL_TYPE_UINT
:
1704 emit(MOV(dst_reg
, fs_reg(ir
->value
.u
[i
])));
1707 emit(MOV(dst_reg
, fs_reg(ir
->value
.i
[i
])));
1709 case GLSL_TYPE_BOOL
:
1710 emit(MOV(dst_reg
, fs_reg((int)ir
->value
.b
[i
])));
1713 assert(!"Non-float/uint/int/bool constant");
1715 dst_reg
.reg_offset
++;
1723 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
1725 ir_expression
*expr
= ir
->as_expression();
1728 expr
->operation
!= ir_binop_logic_and
&&
1729 expr
->operation
!= ir_binop_logic_or
&&
1730 expr
->operation
!= ir_binop_logic_xor
) {
1734 assert(expr
->get_num_operands() <= 2);
1735 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1736 assert(expr
->operands
[i
]->type
->is_scalar());
1738 expr
->operands
[i
]->accept(this);
1739 op
[i
] = this->result
;
1741 resolve_ud_negate(&op
[i
]);
1744 switch (expr
->operation
) {
1745 case ir_unop_logic_not
:
1746 inst
= emit(AND(reg_null_d
, op
[0], fs_reg(1)));
1747 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1751 if (brw
->gen
>= 6) {
1752 emit(CMP(reg_null_d
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1754 inst
= emit(MOV(reg_null_f
, op
[0]));
1755 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1760 if (brw
->gen
>= 6) {
1761 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1763 inst
= emit(MOV(reg_null_d
, op
[0]));
1764 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1768 case ir_binop_greater
:
1769 case ir_binop_gequal
:
1771 case ir_binop_lequal
:
1772 case ir_binop_equal
:
1773 case ir_binop_all_equal
:
1774 case ir_binop_nequal
:
1775 case ir_binop_any_nequal
:
1776 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1777 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1779 emit(CMP(reg_null_d
, op
[0], op
[1],
1780 brw_conditional_for_comparison(expr
->operation
)));
1784 assert(!"not reached");
1785 fail("bad cond code\n");
1793 fs_inst
*inst
= emit(AND(reg_null_d
, this->result
, fs_reg(1)));
1794 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1798 * Emit a gen6 IF statement with the comparison folded into the IF
1802 fs_visitor::emit_if_gen6(ir_if
*ir
)
1804 ir_expression
*expr
= ir
->condition
->as_expression();
1811 assert(expr
->get_num_operands() <= 2);
1812 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1813 assert(expr
->operands
[i
]->type
->is_scalar());
1815 expr
->operands
[i
]->accept(this);
1816 op
[i
] = this->result
;
1819 switch (expr
->operation
) {
1820 case ir_unop_logic_not
:
1821 case ir_binop_logic_xor
:
1822 case ir_binop_logic_or
:
1823 case ir_binop_logic_and
:
1824 /* For operations on bool arguments, only the low bit of the bool is
1825 * valid, and the others are undefined. Fall back to the condition
1831 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
1832 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1836 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1839 case ir_binop_greater
:
1840 case ir_binop_gequal
:
1842 case ir_binop_lequal
:
1843 case ir_binop_equal
:
1844 case ir_binop_all_equal
:
1845 case ir_binop_nequal
:
1846 case ir_binop_any_nequal
:
1847 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1848 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1850 emit(IF(op
[0], op
[1],
1851 brw_conditional_for_comparison(expr
->operation
)));
1854 assert(!"not reached");
1855 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1856 fail("bad condition\n");
1861 emit_bool_to_cond_code(ir
->condition
);
1862 fs_inst
*inst
= emit(BRW_OPCODE_IF
);
1863 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1867 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
1869 * Many GLSL shaders contain the following pattern:
1871 * x = condition ? foo : bar
1873 * The compiler emits an ir_if tree for this, since each subexpression might be
1874 * a complex tree that could have side-effects or short-circuit logic.
1876 * However, the common case is to simply select one of two constants or
1877 * variable values---which is exactly what SEL is for. In this case, the
1878 * assembly looks like:
1886 * which can be easily translated into:
1888 * (+f0) SEL dst src0 src1
1890 * If src0 is an immediate value, we promote it to a temporary GRF.
1893 fs_visitor::try_replace_with_sel()
1895 fs_inst
*endif_inst
= (fs_inst
*) instructions
.get_tail();
1896 assert(endif_inst
->opcode
== BRW_OPCODE_ENDIF
);
1898 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
1900 BRW_OPCODE_IF
, BRW_OPCODE_MOV
, BRW_OPCODE_ELSE
, BRW_OPCODE_MOV
,
1903 fs_inst
*match
= (fs_inst
*) endif_inst
->prev
;
1904 for (int i
= 0; i
< 4; i
++) {
1905 if (match
->is_head_sentinel() || match
->opcode
!= opcodes
[4-i
-1])
1907 match
= (fs_inst
*) match
->prev
;
1910 /* The opcodes match; it looks like the right sequence of instructions. */
1911 fs_inst
*else_mov
= (fs_inst
*) endif_inst
->prev
;
1912 fs_inst
*then_mov
= (fs_inst
*) else_mov
->prev
->prev
;
1913 fs_inst
*if_inst
= (fs_inst
*) then_mov
->prev
;
1915 /* Check that the MOVs are the right form. */
1916 if (then_mov
->dst
.equals(else_mov
->dst
) &&
1917 !then_mov
->is_partial_write() &&
1918 !else_mov
->is_partial_write()) {
1920 /* Remove the matched instructions; we'll emit a SEL to replace them. */
1921 while (!if_inst
->next
->is_tail_sentinel())
1922 if_inst
->next
->remove();
1925 /* Only the last source register can be a constant, so if the MOV in
1926 * the "then" clause uses a constant, we need to put it in a temporary.
1928 fs_reg
src0(then_mov
->src
[0]);
1929 if (src0
.file
== IMM
) {
1930 src0
= fs_reg(this, glsl_type::float_type
);
1931 src0
.type
= then_mov
->src
[0].type
;
1932 emit(MOV(src0
, then_mov
->src
[0]));
1936 if (if_inst
->conditional_mod
) {
1937 /* Sandybridge-specific IF with embedded comparison */
1938 emit(CMP(reg_null_d
, if_inst
->src
[0], if_inst
->src
[1],
1939 if_inst
->conditional_mod
));
1940 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
1941 sel
->predicate
= BRW_PREDICATE_NORMAL
;
1943 /* Separate CMP and IF instructions */
1944 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
1945 sel
->predicate
= if_inst
->predicate
;
1946 sel
->predicate_inverse
= if_inst
->predicate_inverse
;
1952 fs_visitor::visit(ir_if
*ir
)
1954 if (brw
->gen
< 6 && dispatch_width
== 16) {
1955 fail("Can't support (non-uniform) control flow on 16-wide\n");
1958 /* Don't point the annotation at the if statement, because then it plus
1959 * the then and else blocks get printed.
1961 this->base_ir
= ir
->condition
;
1963 if (brw
->gen
== 6) {
1966 emit_bool_to_cond_code(ir
->condition
);
1968 emit(IF(BRW_PREDICATE_NORMAL
));
1971 foreach_list(node
, &ir
->then_instructions
) {
1972 ir_instruction
*ir
= (ir_instruction
*)node
;
1978 if (!ir
->else_instructions
.is_empty()) {
1979 emit(BRW_OPCODE_ELSE
);
1981 foreach_list(node
, &ir
->else_instructions
) {
1982 ir_instruction
*ir
= (ir_instruction
*)node
;
1989 emit(BRW_OPCODE_ENDIF
);
1991 try_replace_with_sel();
1995 fs_visitor::visit(ir_loop
*ir
)
1997 fs_reg counter
= reg_undef
;
1999 if (brw
->gen
< 6 && dispatch_width
== 16) {
2000 fail("Can't support (non-uniform) control flow on 16-wide\n");
2004 this->base_ir
= ir
->counter
;
2005 ir
->counter
->accept(this);
2006 counter
= *(variable_storage(ir
->counter
));
2009 this->base_ir
= ir
->from
;
2010 ir
->from
->accept(this);
2012 emit(MOV(counter
, this->result
));
2016 this->base_ir
= NULL
;
2017 emit(BRW_OPCODE_DO
);
2020 this->base_ir
= ir
->to
;
2021 ir
->to
->accept(this);
2023 emit(CMP(reg_null_d
, counter
, this->result
,
2024 brw_conditional_for_comparison(ir
->cmp
)));
2026 fs_inst
*inst
= emit(BRW_OPCODE_BREAK
);
2027 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2030 foreach_list(node
, &ir
->body_instructions
) {
2031 ir_instruction
*ir
= (ir_instruction
*)node
;
2037 if (ir
->increment
) {
2038 this->base_ir
= ir
->increment
;
2039 ir
->increment
->accept(this);
2040 emit(ADD(counter
, counter
, this->result
));
2043 this->base_ir
= NULL
;
2044 emit(BRW_OPCODE_WHILE
);
2048 fs_visitor::visit(ir_loop_jump
*ir
)
2051 case ir_loop_jump::jump_break
:
2052 emit(BRW_OPCODE_BREAK
);
2054 case ir_loop_jump::jump_continue
:
2055 emit(BRW_OPCODE_CONTINUE
);
2061 fs_visitor::visit(ir_call
*ir
)
2063 assert(!"FINISHME");
2067 fs_visitor::visit(ir_return
*ir
)
2069 assert(!"FINISHME");
2073 fs_visitor::visit(ir_function
*ir
)
2075 /* Ignore function bodies other than main() -- we shouldn't see calls to
2076 * them since they should all be inlined before we get to ir_to_mesa.
2078 if (strcmp(ir
->name
, "main") == 0) {
2079 const ir_function_signature
*sig
;
2082 sig
= ir
->matching_signature(NULL
, &empty
);
2086 foreach_list(node
, &sig
->body
) {
2087 ir_instruction
*ir
= (ir_instruction
*)node
;
2096 fs_visitor::visit(ir_function_signature
*ir
)
2098 assert(!"not reached");
2103 fs_visitor::visit(ir_emit_vertex
*)
2105 assert(!"not reached");
2109 fs_visitor::visit(ir_end_primitive
*)
2111 assert(!"not reached");
2115 fs_visitor::emit(fs_inst inst
)
2117 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
2124 fs_visitor::emit(fs_inst
*inst
)
2126 if (force_uncompressed_stack
> 0)
2127 inst
->force_uncompressed
= true;
2128 else if (force_sechalf_stack
> 0)
2129 inst
->force_sechalf
= true;
2131 inst
->annotation
= this->current_annotation
;
2132 inst
->ir
= this->base_ir
;
2134 this->instructions
.push_tail(inst
);
2140 fs_visitor::emit(exec_list list
)
2142 foreach_list_safe(node
, &list
) {
2143 fs_inst
*inst
= (fs_inst
*)node
;
2149 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
2151 fs_visitor::emit_dummy_fs()
2153 int reg_width
= dispatch_width
/ 8;
2155 /* Everyone's favorite color. */
2156 emit(MOV(fs_reg(MRF
, 2 + 0 * reg_width
), fs_reg(1.0f
)));
2157 emit(MOV(fs_reg(MRF
, 2 + 1 * reg_width
), fs_reg(0.0f
)));
2158 emit(MOV(fs_reg(MRF
, 2 + 2 * reg_width
), fs_reg(1.0f
)));
2159 emit(MOV(fs_reg(MRF
, 2 + 3 * reg_width
), fs_reg(0.0f
)));
2162 write
= emit(FS_OPCODE_FB_WRITE
, fs_reg(0), fs_reg(0));
2163 write
->base_mrf
= 2;
2164 write
->mlen
= 4 * reg_width
;
2168 /* The register location here is relative to the start of the URB
2169 * data. It will get adjusted to be a real location before
2170 * generate_code() time.
2173 fs_visitor::interp_reg(int location
, int channel
)
2175 int regnr
= c
->prog_data
.urb_setup
[location
] * 2 + channel
/ 2;
2176 int stride
= (channel
& 1) * 4;
2178 assert(c
->prog_data
.urb_setup
[location
] != -1);
2180 return brw_vec1_grf(regnr
, stride
);
2183 /** Emits the interpolation for the varying inputs. */
2185 fs_visitor::emit_interpolation_setup_gen4()
2187 this->current_annotation
= "compute pixel centers";
2188 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
2189 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
2190 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2191 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2193 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
2194 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
2196 this->current_annotation
= "compute pixel deltas from v0";
2198 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2199 fs_reg(this, glsl_type::vec2_type
);
2200 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2201 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
];
2202 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
].reg_offset
++;
2204 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2205 fs_reg(this, glsl_type::float_type
);
2206 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2207 fs_reg(this, glsl_type::float_type
);
2209 emit(ADD(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2210 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0)))));
2211 emit(ADD(this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2212 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1)))));
2214 this->current_annotation
= "compute pos.w and 1/pos.w";
2215 /* Compute wpos.w. It's always in our setup, since it's needed to
2216 * interpolate the other attributes.
2218 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2219 emit(FS_OPCODE_LINTERP
, wpos_w
,
2220 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2221 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2222 interp_reg(VARYING_SLOT_POS
, 3));
2223 /* Compute the pixel 1/W value from wpos.w. */
2224 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
2225 emit_math(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
2226 this->current_annotation
= NULL
;
2229 /** Emits the interpolation for the varying inputs. */
2231 fs_visitor::emit_interpolation_setup_gen6()
2233 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
2235 /* If the pixel centers end up used, the setup is the same as for gen4. */
2236 this->current_annotation
= "compute pixel centers";
2237 fs_reg int_pixel_x
= fs_reg(this, glsl_type::uint_type
);
2238 fs_reg int_pixel_y
= fs_reg(this, glsl_type::uint_type
);
2239 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2240 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2241 emit(ADD(int_pixel_x
,
2242 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
2243 fs_reg(brw_imm_v(0x10101010))));
2244 emit(ADD(int_pixel_y
,
2245 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
2246 fs_reg(brw_imm_v(0x11001100))));
2248 /* As of gen6, we can no longer mix float and int sources. We have
2249 * to turn the integer pixel centers into floats for their actual
2252 this->pixel_x
= fs_reg(this, glsl_type::float_type
);
2253 this->pixel_y
= fs_reg(this, glsl_type::float_type
);
2254 emit(MOV(this->pixel_x
, int_pixel_x
));
2255 emit(MOV(this->pixel_y
, int_pixel_y
));
2257 this->current_annotation
= "compute pos.w";
2258 this->pixel_w
= fs_reg(brw_vec8_grf(c
->source_w_reg
, 0));
2259 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2260 emit_math(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
2262 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
2263 uint8_t reg
= c
->barycentric_coord_reg
[i
];
2264 this->delta_x
[i
] = fs_reg(brw_vec8_grf(reg
, 0));
2265 this->delta_y
[i
] = fs_reg(brw_vec8_grf(reg
+ 1, 0));
2268 this->current_annotation
= NULL
;
2272 fs_visitor::emit_color_write(int target
, int index
, int first_color_mrf
)
2274 int reg_width
= dispatch_width
/ 8;
2276 fs_reg color
= outputs
[target
];
2279 /* If there's no color data to be written, skip it. */
2280 if (color
.file
== BAD_FILE
)
2283 color
.reg_offset
+= index
;
2285 if (dispatch_width
== 8 || brw
->gen
>= 6) {
2286 /* SIMD8 write looks like:
2292 * gen6 SIMD16 DP write looks like:
2302 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
* reg_width
,
2305 inst
->saturate
= c
->key
.clamp_fragment_color
;
2307 /* pre-gen6 SIMD16 single source DP write looks like:
2317 if (brw
->has_compr4
) {
2318 /* By setting the high bit of the MRF register number, we
2319 * indicate that we want COMPR4 mode - instead of doing the
2320 * usual destination + 1 for the second half we get
2323 inst
= emit(MOV(fs_reg(MRF
, BRW_MRF_COMPR4
+ first_color_mrf
+ index
,
2326 inst
->saturate
= c
->key
.clamp_fragment_color
;
2328 push_force_uncompressed();
2329 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
, color
.type
),
2331 inst
->saturate
= c
->key
.clamp_fragment_color
;
2332 pop_force_uncompressed();
2334 push_force_sechalf();
2335 color
.sechalf
= true;
2336 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
+ 4, color
.type
),
2338 inst
->saturate
= c
->key
.clamp_fragment_color
;
2339 pop_force_sechalf();
2340 color
.sechalf
= false;
2346 fs_visitor::emit_fb_writes()
2348 this->current_annotation
= "FB write header";
2349 bool header_present
= true;
2350 /* We can potentially have a message length of up to 15, so we have to set
2351 * base_mrf to either 0 or 1 in order to fit in m0..m15.
2355 int reg_width
= dispatch_width
/ 8;
2356 bool do_dual_src
= this->dual_src_output
.file
!= BAD_FILE
;
2357 bool src0_alpha_to_render_target
= false;
2359 if (dispatch_width
== 16 && do_dual_src
) {
2360 fail("GL_ARB_blend_func_extended not yet supported in 16-wide.");
2361 do_dual_src
= false;
2364 /* From the Sandy Bridge PRM, volume 4, page 198:
2366 * "Dispatched Pixel Enables. One bit per pixel indicating
2367 * which pixels were originally enabled when the thread was
2368 * dispatched. This field is only required for the end-of-
2369 * thread message and on all dual-source messages."
2371 if (brw
->gen
>= 6 &&
2372 !this->fp
->UsesKill
&&
2374 c
->key
.nr_color_regions
== 1) {
2375 header_present
= false;
2378 if (header_present
) {
2379 src0_alpha_to_render_target
= brw
->gen
>= 6 &&
2381 c
->key
.replicate_alpha
;
2386 if (c
->aa_dest_stencil_reg
) {
2387 push_force_uncompressed();
2388 emit(MOV(fs_reg(MRF
, nr
++),
2389 fs_reg(brw_vec8_grf(c
->aa_dest_stencil_reg
, 0))));
2390 pop_force_uncompressed();
2393 /* Reserve space for color. It'll be filled in per MRT below. */
2395 nr
+= 4 * reg_width
;
2398 if (src0_alpha_to_render_target
)
2401 if (c
->source_depth_to_render_target
) {
2402 if (brw
->gen
== 6 && dispatch_width
== 16) {
2403 /* For outputting oDepth on gen6, SIMD8 writes have to be
2404 * used. This would require 8-wide moves of each half to
2405 * message regs, kind of like pre-gen5 SIMD16 FB writes.
2406 * Just bail on doing so for now.
2408 fail("Missing support for simd16 depth writes on gen6\n");
2411 if (fp
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
2412 /* Hand over gl_FragDepth. */
2413 assert(this->frag_depth
.file
!= BAD_FILE
);
2414 emit(MOV(fs_reg(MRF
, nr
), this->frag_depth
));
2416 /* Pass through the payload depth. */
2417 emit(MOV(fs_reg(MRF
, nr
),
2418 fs_reg(brw_vec8_grf(c
->source_depth_reg
, 0))));
2423 if (c
->dest_depth_reg
) {
2424 emit(MOV(fs_reg(MRF
, nr
),
2425 fs_reg(brw_vec8_grf(c
->dest_depth_reg
, 0))));
2430 fs_reg src0
= this->outputs
[0];
2431 fs_reg src1
= this->dual_src_output
;
2433 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2435 for (int i
= 0; i
< 4; i
++) {
2436 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ i
, src0
.type
), src0
));
2438 inst
->saturate
= c
->key
.clamp_fragment_color
;
2441 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2443 for (int i
= 0; i
< 4; i
++) {
2444 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ 4 + i
, src1
.type
),
2447 inst
->saturate
= c
->key
.clamp_fragment_color
;
2450 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2451 emit_shader_time_end();
2453 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2455 inst
->base_mrf
= base_mrf
;
2456 inst
->mlen
= nr
- base_mrf
;
2458 inst
->header_present
= header_present
;
2460 c
->prog_data
.dual_src_blend
= true;
2461 this->current_annotation
= NULL
;
2465 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
2466 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2467 "FB write target %d",
2469 /* If src0_alpha_to_render_target is true, include source zero alpha
2470 * data in RenderTargetWrite message for targets > 0.
2472 int write_color_mrf
= color_mrf
;
2473 if (src0_alpha_to_render_target
&& target
!= 0) {
2475 fs_reg color
= outputs
[0];
2476 color
.reg_offset
+= 3;
2478 inst
= emit(MOV(fs_reg(MRF
, write_color_mrf
, color
.type
),
2480 inst
->saturate
= c
->key
.clamp_fragment_color
;
2481 write_color_mrf
= color_mrf
+ reg_width
;
2484 for (unsigned i
= 0; i
< this->output_components
[target
]; i
++)
2485 emit_color_write(target
, i
, write_color_mrf
);
2488 if (target
== c
->key
.nr_color_regions
- 1) {
2491 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2492 emit_shader_time_end();
2495 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2496 inst
->target
= target
;
2497 inst
->base_mrf
= base_mrf
;
2498 if (src0_alpha_to_render_target
&& target
== 0)
2499 inst
->mlen
= nr
- base_mrf
- reg_width
;
2501 inst
->mlen
= nr
- base_mrf
;
2503 inst
->header_present
= header_present
;
2506 if (c
->key
.nr_color_regions
== 0) {
2507 /* Even if there's no color buffers enabled, we still need to send
2508 * alpha out the pipeline to our null renderbuffer to support
2509 * alpha-testing, alpha-to-coverage, and so on.
2511 emit_color_write(0, 3, color_mrf
);
2513 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2514 emit_shader_time_end();
2516 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2517 inst
->base_mrf
= base_mrf
;
2518 inst
->mlen
= nr
- base_mrf
;
2520 inst
->header_present
= header_present
;
2523 this->current_annotation
= NULL
;
2527 fs_visitor::resolve_ud_negate(fs_reg
*reg
)
2529 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2533 fs_reg temp
= fs_reg(this, glsl_type::uint_type
);
2534 emit(MOV(temp
, *reg
));
2539 fs_visitor::resolve_bool_comparison(ir_rvalue
*rvalue
, fs_reg
*reg
)
2541 if (rvalue
->type
!= glsl_type::bool_type
)
2544 fs_reg temp
= fs_reg(this, glsl_type::bool_type
);
2545 emit(AND(temp
, *reg
, fs_reg(1)));
2549 fs_visitor::fs_visitor(struct brw_context
*brw
,
2550 struct brw_wm_compile
*c
,
2551 struct gl_shader_program
*shader_prog
,
2552 struct gl_fragment_program
*fp
,
2553 unsigned dispatch_width
)
2554 : dispatch_width(dispatch_width
)
2559 this->shader_prog
= shader_prog
;
2560 this->ctx
= &brw
->ctx
;
2561 this->mem_ctx
= ralloc_context(NULL
);
2563 shader
= (struct brw_shader
*)
2564 shader_prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2567 this->failed
= false;
2568 this->variable_ht
= hash_table_ctor(0,
2569 hash_table_pointer_hash
,
2570 hash_table_pointer_compare
);
2572 memset(this->outputs
, 0, sizeof(this->outputs
));
2573 memset(this->output_components
, 0, sizeof(this->output_components
));
2574 this->first_non_payload_grf
= 0;
2575 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2577 this->current_annotation
= NULL
;
2578 this->base_ir
= NULL
;
2580 this->virtual_grf_sizes
= NULL
;
2581 this->virtual_grf_count
= 0;
2582 this->virtual_grf_array_size
= 0;
2583 this->virtual_grf_start
= NULL
;
2584 this->virtual_grf_end
= NULL
;
2585 this->live_intervals_valid
= false;
2587 this->params_remap
= NULL
;
2588 this->nr_params_remap
= 0;
2590 this->force_uncompressed_stack
= 0;
2591 this->force_sechalf_stack
= 0;
2593 memset(&this->param_size
, 0, sizeof(this->param_size
));
2596 fs_visitor::~fs_visitor()
2598 ralloc_free(this->mem_ctx
);
2599 hash_table_dtor(this->variable_ht
);