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 "program/prog_parameter.h"
37 #include "program/prog_print.h"
38 #include "program/prog_optimize.h"
39 #include "program/register_allocate.h"
40 #include "program/sampler.h"
41 #include "program/hash_table.h"
42 #include "brw_context.h"
47 #include "main/uniforms.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(), this->result
.type
);
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]));
445 case ir_binop_imul_high
: {
446 if (brw
->gen
>= 7 && dispatch_width
== 16)
447 fail("16-wide explicit accumulator operands unsupported\n");
449 struct brw_reg acc
= retype(brw_acc_reg(), this->result
.type
);
451 emit(MUL(acc
, op
[0], op
[1]));
452 emit(MACH(this->result
, op
[0], op
[1]));
456 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
457 assert(ir
->type
->is_integer());
458 emit_math(SHADER_OPCODE_INT_QUOTIENT
, this->result
, op
[0], op
[1]);
460 case ir_binop_carry
: {
461 if (brw
->gen
>= 7 && dispatch_width
== 16)
462 fail("16-wide explicit accumulator operands unsupported\n");
464 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD
);
466 emit(ADDC(reg_null_ud
, op
[0], op
[1]));
467 emit(MOV(this->result
, fs_reg(acc
)));
470 case ir_binop_borrow
: {
471 if (brw
->gen
>= 7 && dispatch_width
== 16)
472 fail("16-wide explicit accumulator operands unsupported\n");
474 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD
);
476 emit(SUBB(reg_null_ud
, op
[0], op
[1]));
477 emit(MOV(this->result
, fs_reg(acc
)));
481 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
482 assert(ir
->type
->is_integer());
483 emit_math(SHADER_OPCODE_INT_REMAINDER
, this->result
, op
[0], op
[1]);
487 case ir_binop_greater
:
488 case ir_binop_lequal
:
489 case ir_binop_gequal
:
491 case ir_binop_all_equal
:
492 case ir_binop_nequal
:
493 case ir_binop_any_nequal
:
494 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
495 resolve_bool_comparison(ir
->operands
[1], &op
[1]);
497 emit(CMP(this->result
, op
[0], op
[1],
498 brw_conditional_for_comparison(ir
->operation
)));
501 case ir_binop_logic_xor
:
502 emit(XOR(this->result
, op
[0], op
[1]));
505 case ir_binop_logic_or
:
506 emit(OR(this->result
, op
[0], op
[1]));
509 case ir_binop_logic_and
:
510 emit(AND(this->result
, op
[0], op
[1]));
515 assert(!"not reached: should be handled by brw_fs_channel_expressions");
519 assert(!"not reached: should be handled by lower_noise");
522 case ir_quadop_vector
:
523 assert(!"not reached: should be handled by lower_quadop_vector");
526 case ir_binop_vector_extract
:
527 assert(!"not reached: should be handled by lower_vec_index_to_cond_assign()");
530 case ir_triop_vector_insert
:
531 assert(!"not reached: should be handled by lower_vector_insert()");
535 assert(!"not reached: should be handled by ldexp_to_arith()");
539 emit_math(SHADER_OPCODE_SQRT
, this->result
, op
[0]);
543 emit_math(SHADER_OPCODE_RSQ
, this->result
, op
[0]);
546 case ir_unop_bitcast_i2f
:
547 case ir_unop_bitcast_u2f
:
548 op
[0].type
= BRW_REGISTER_TYPE_F
;
549 this->result
= op
[0];
552 case ir_unop_bitcast_f2u
:
553 op
[0].type
= BRW_REGISTER_TYPE_UD
;
554 this->result
= op
[0];
557 case ir_unop_bitcast_f2i
:
558 op
[0].type
= BRW_REGISTER_TYPE_D
;
559 this->result
= op
[0];
565 emit(MOV(this->result
, op
[0]));
569 emit(AND(this->result
, op
[0], fs_reg(1)));
572 temp
= fs_reg(this, glsl_type::int_type
);
573 emit(AND(temp
, op
[0], fs_reg(1)));
574 emit(MOV(this->result
, temp
));
578 emit(CMP(this->result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
581 emit(CMP(this->result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
585 emit(RNDZ(this->result
, op
[0]));
588 op
[0].negate
= !op
[0].negate
;
589 emit(RNDD(this->result
, op
[0]));
590 this->result
.negate
= true;
593 emit(RNDD(this->result
, op
[0]));
596 emit(FRC(this->result
, op
[0]));
598 case ir_unop_round_even
:
599 emit(RNDE(this->result
, op
[0]));
604 resolve_ud_negate(&op
[0]);
605 resolve_ud_negate(&op
[1]);
606 emit_minmax(ir
->operation
== ir_binop_min
?
607 BRW_CONDITIONAL_L
: BRW_CONDITIONAL_GE
,
608 this->result
, op
[0], op
[1]);
610 case ir_unop_pack_snorm_2x16
:
611 case ir_unop_pack_snorm_4x8
:
612 case ir_unop_pack_unorm_2x16
:
613 case ir_unop_pack_unorm_4x8
:
614 case ir_unop_unpack_snorm_2x16
:
615 case ir_unop_unpack_snorm_4x8
:
616 case ir_unop_unpack_unorm_2x16
:
617 case ir_unop_unpack_unorm_4x8
:
618 case ir_unop_unpack_half_2x16
:
619 case ir_unop_pack_half_2x16
:
620 assert(!"not reached: should be handled by lower_packing_builtins");
622 case ir_unop_unpack_half_2x16_split_x
:
623 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, this->result
, op
[0]);
625 case ir_unop_unpack_half_2x16_split_y
:
626 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, this->result
, op
[0]);
629 emit_math(SHADER_OPCODE_POW
, this->result
, op
[0], op
[1]);
632 case ir_unop_bitfield_reverse
:
633 emit(BFREV(this->result
, op
[0]));
635 case ir_unop_bit_count
:
636 emit(CBIT(this->result
, op
[0]));
638 case ir_unop_find_msb
:
639 temp
= fs_reg(this, glsl_type::uint_type
);
640 emit(FBH(temp
, op
[0]));
642 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
643 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
644 * subtract the result from 31 to convert the MSB count into an LSB count.
647 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
648 emit(MOV(this->result
, temp
));
649 emit(CMP(reg_null_d
, this->result
, fs_reg(-1), BRW_CONDITIONAL_NZ
));
652 inst
= emit(ADD(this->result
, temp
, fs_reg(31)));
653 inst
->predicate
= BRW_PREDICATE_NORMAL
;
655 case ir_unop_find_lsb
:
656 emit(FBL(this->result
, op
[0]));
658 case ir_triop_bitfield_extract
:
659 /* Note that the instruction's argument order is reversed from GLSL
662 emit(BFE(this->result
, op
[2], op
[1], op
[0]));
665 emit(BFI1(this->result
, op
[0], op
[1]));
668 emit(BFI2(this->result
, op
[0], op
[1], op
[2]));
670 case ir_quadop_bitfield_insert
:
671 assert(!"not reached: should be handled by "
672 "lower_instructions::bitfield_insert_to_bfm_bfi");
675 case ir_unop_bit_not
:
676 emit(NOT(this->result
, op
[0]));
678 case ir_binop_bit_and
:
679 emit(AND(this->result
, op
[0], op
[1]));
681 case ir_binop_bit_xor
:
682 emit(XOR(this->result
, op
[0], op
[1]));
684 case ir_binop_bit_or
:
685 emit(OR(this->result
, op
[0], op
[1]));
688 case ir_binop_lshift
:
689 emit(SHL(this->result
, op
[0], op
[1]));
692 case ir_binop_rshift
:
693 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
694 emit(ASR(this->result
, op
[0], op
[1]));
696 emit(SHR(this->result
, op
[0], op
[1]));
698 case ir_binop_pack_half_2x16_split
:
699 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, this->result
, op
[0], op
[1]);
701 case ir_binop_ubo_load
: {
702 /* This IR node takes a constant uniform block and a constant or
703 * variable byte offset within the block and loads a vector from that.
705 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
706 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
707 fs_reg surf_index
= fs_reg(c
->prog_data
.base
.binding_table
.ubo_start
+
708 uniform_block
->value
.u
[0]);
710 fs_reg packed_consts
= fs_reg(this, glsl_type::float_type
);
711 packed_consts
.type
= result
.type
;
713 fs_reg const_offset_reg
= fs_reg(const_offset
->value
.u
[0] & ~15);
714 emit(fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
715 packed_consts
, surf_index
, const_offset_reg
));
717 packed_consts
.smear
= const_offset
->value
.u
[0] % 16 / 4;
718 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
719 /* UBO bools are any nonzero value. We consider bools to be
720 * values with the low bit set to 1. Convert them using CMP.
722 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
723 emit(CMP(result
, packed_consts
, fs_reg(0u), BRW_CONDITIONAL_NZ
));
725 emit(MOV(result
, packed_consts
));
728 packed_consts
.smear
++;
731 /* The std140 packing rules don't allow vectors to cross 16-byte
732 * boundaries, and a reg is 32 bytes.
734 assert(packed_consts
.smear
< 8);
737 /* Turn the byte offset into a dword offset. */
738 fs_reg base_offset
= fs_reg(this, glsl_type::int_type
);
739 emit(SHR(base_offset
, op
[1], fs_reg(2)));
741 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
742 emit(VARYING_PULL_CONSTANT_LOAD(result
, surf_index
,
745 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
746 emit(CMP(result
, result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
752 result
.reg_offset
= 0;
757 /* Note that the instruction's argument order is reversed from GLSL
760 emit(MAD(this->result
, op
[2], op
[1], op
[0]));
764 emit_lrp(this->result
, op
[0], op
[1], op
[2]);
768 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
769 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[1], op
[2]);
770 inst
->predicate
= BRW_PREDICATE_NORMAL
;
776 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
777 const glsl_type
*type
, bool predicated
)
779 switch (type
->base_type
) {
780 case GLSL_TYPE_FLOAT
:
784 for (unsigned int i
= 0; i
< type
->components(); i
++) {
785 l
.type
= brw_type_for_base_type(type
);
786 r
.type
= brw_type_for_base_type(type
);
788 if (predicated
|| !l
.equals(r
)) {
789 fs_inst
*inst
= emit(MOV(l
, r
));
790 inst
->predicate
= predicated
? BRW_PREDICATE_NORMAL
: BRW_PREDICATE_NONE
;
797 case GLSL_TYPE_ARRAY
:
798 for (unsigned int i
= 0; i
< type
->length
; i
++) {
799 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
803 case GLSL_TYPE_STRUCT
:
804 for (unsigned int i
= 0; i
< type
->length
; i
++) {
805 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
810 case GLSL_TYPE_SAMPLER
:
814 case GLSL_TYPE_ERROR
:
815 case GLSL_TYPE_INTERFACE
:
816 assert(!"not reached");
821 /* If the RHS processing resulted in an instruction generating a
822 * temporary value, and it would be easy to rewrite the instruction to
823 * generate its result right into the LHS instead, do so. This ends
824 * up reliably removing instructions where it can be tricky to do so
825 * later without real UD chain information.
828 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
831 fs_inst
*pre_rhs_inst
,
832 fs_inst
*last_rhs_inst
)
834 /* Only attempt if we're doing a direct assignment. */
836 !(ir
->lhs
->type
->is_scalar() ||
837 (ir
->lhs
->type
->is_vector() &&
838 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1)))
841 /* Make sure the last instruction generated our source reg. */
842 fs_inst
*modify
= get_instruction_generating_reg(pre_rhs_inst
,
848 /* If last_rhs_inst wrote a different number of components than our LHS,
849 * we can't safely rewrite it.
851 if (virtual_grf_sizes
[dst
.reg
] != modify
->regs_written
)
854 /* Success! Rewrite the instruction. */
861 fs_visitor::visit(ir_assignment
*ir
)
866 /* FINISHME: arrays on the lhs */
867 ir
->lhs
->accept(this);
870 fs_inst
*pre_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
872 ir
->rhs
->accept(this);
875 fs_inst
*last_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
877 assert(l
.file
!= BAD_FILE
);
878 assert(r
.file
!= BAD_FILE
);
880 if (try_rewrite_rhs_to_dst(ir
, l
, r
, pre_rhs_inst
, last_rhs_inst
))
884 emit_bool_to_cond_code(ir
->condition
);
887 if (ir
->lhs
->type
->is_scalar() ||
888 ir
->lhs
->type
->is_vector()) {
889 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
890 if (ir
->write_mask
& (1 << i
)) {
891 inst
= emit(MOV(l
, r
));
893 inst
->predicate
= BRW_PREDICATE_NORMAL
;
899 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
904 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
905 fs_reg shadow_c
, fs_reg lod
, fs_reg dPdy
)
915 if (ir
->shadow_comparitor
) {
916 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
917 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
918 coordinate
.reg_offset
++;
921 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
922 * the unused slots must be zeroed.
924 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
925 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
929 if (ir
->op
== ir_tex
) {
930 /* There's no plain shadow compare message, so we use shadow
931 * compare with a bias of 0.0.
933 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
)));
935 } else if (ir
->op
== ir_txb
|| ir
->op
== ir_txl
) {
936 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
939 assert(!"Should not get here.");
942 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
944 } else if (ir
->op
== ir_tex
) {
945 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
946 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
947 coordinate
.reg_offset
++;
949 /* zero the others. */
950 for (int i
= ir
->coordinate
->type
->vector_elements
; i
<3; i
++) {
951 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
953 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
955 } else if (ir
->op
== ir_txd
) {
958 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
959 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
960 coordinate
.reg_offset
++;
962 /* the slots for u and v are always present, but r is optional */
963 mlen
+= MAX2(ir
->coordinate
->type
->vector_elements
, 2);
966 * dPdx = dudx, dvdx, drdx
967 * dPdy = dudy, dvdy, drdy
969 * 1-arg: Does not exist.
971 * 2-arg: dudx dvdx dudy dvdy
972 * dPdx.x dPdx.y dPdy.x dPdy.y
975 * 3-arg: dudx dvdx drdx dudy dvdy drdy
976 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
979 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
980 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdx
));
983 mlen
+= MAX2(ir
->lod_info
.grad
.dPdx
->type
->vector_elements
, 2);
985 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdy
->type
->vector_elements
; i
++) {
986 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdy
));
989 mlen
+= MAX2(ir
->lod_info
.grad
.dPdy
->type
->vector_elements
, 2);
990 } else if (ir
->op
== ir_txs
) {
991 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
993 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
996 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
997 * instructions. We'll need to do SIMD16 here.
1000 assert(ir
->op
== ir_txb
|| ir
->op
== ir_txl
|| ir
->op
== ir_txf
);
1002 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1003 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2, coordinate
.type
),
1005 coordinate
.reg_offset
++;
1008 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
1009 * be necessary for TXF (ld), but seems wise to do for all messages.
1011 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
1012 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2), fs_reg(0.0f
)));
1015 /* lod/bias appears after u/v/r. */
1018 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, lod
.type
), lod
));
1021 /* The unused upper half. */
1026 /* Now, since we're doing simd16, the return is 2 interleaved
1027 * vec4s where the odd-indexed ones are junk. We'll need to move
1028 * this weirdness around to the expected layout.
1031 dst
= fs_reg(GRF
, virtual_grf_alloc(8),
1033 brw_type_for_base_type(ir
->type
) :
1034 BRW_REGISTER_TYPE_F
));
1037 fs_inst
*inst
= NULL
;
1040 inst
= emit(SHADER_OPCODE_TEX
, dst
);
1043 inst
= emit(FS_OPCODE_TXB
, dst
);
1046 inst
= emit(SHADER_OPCODE_TXL
, dst
);
1049 inst
= emit(SHADER_OPCODE_TXD
, dst
);
1052 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1055 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1058 fail("unrecognized texture opcode");
1060 inst
->base_mrf
= base_mrf
;
1062 inst
->header_present
= true;
1063 inst
->regs_written
= simd16
? 8 : 4;
1066 for (int i
= 0; i
< 4; i
++) {
1067 emit(MOV(orig_dst
, dst
));
1068 orig_dst
.reg_offset
++;
1069 dst
.reg_offset
+= 2;
1076 /* gen5's sampler has slots for u, v, r, array index, then optional
1077 * parameters like shadow comparitor or LOD bias. If optional
1078 * parameters aren't present, those base slots are optional and don't
1079 * need to be included in the message.
1081 * We don't fill in the unnecessary slots regardless, which may look
1082 * surprising in the disassembly.
1085 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1086 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1087 fs_reg sample_index
)
1091 int reg_width
= dispatch_width
/ 8;
1092 bool header_present
= false;
1093 const int vector_elements
=
1094 ir
->coordinate
? ir
->coordinate
->type
->vector_elements
: 0;
1096 if (ir
->offset
!= NULL
&& ir
->op
== ir_txf
) {
1097 /* It appears that the ld instruction used for txf does its
1098 * address bounds check before adding in the offset. To work
1099 * around this, just add the integer offset to the integer texel
1100 * coordinate, and don't put the offset in the header.
1102 ir_constant
*offset
= ir
->offset
->as_constant();
1103 for (int i
= 0; i
< vector_elements
; i
++) {
1104 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1106 offset
->value
.i
[i
]));
1107 coordinate
.reg_offset
++;
1111 /* The offsets set up by the ir_texture visitor are in the
1112 * m1 header, so we can't go headerless.
1114 header_present
= true;
1119 for (int i
= 0; i
< vector_elements
; i
++) {
1120 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1122 coordinate
.reg_offset
++;
1125 mlen
+= vector_elements
* reg_width
;
1127 if (ir
->shadow_comparitor
) {
1128 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1130 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1134 fs_inst
*inst
= NULL
;
1137 inst
= emit(SHADER_OPCODE_TEX
, dst
);
1140 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1141 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1144 inst
= emit(FS_OPCODE_TXB
, dst
);
1147 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1148 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1151 inst
= emit(SHADER_OPCODE_TXL
, dst
);
1154 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
); /* skip over 'ai' */
1158 * dPdx = dudx, dvdx, drdx
1159 * dPdy = dudy, dvdy, drdy
1161 * Load up these values:
1162 * - dudx dudy dvdx dvdy drdx drdy
1163 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1165 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
1166 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1170 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod2
));
1175 inst
= emit(SHADER_OPCODE_TXD
, dst
);
1179 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1181 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1183 case ir_query_levels
:
1184 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1186 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1189 mlen
= header_present
+ 4 * reg_width
;
1190 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), lod
));
1191 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1194 mlen
= header_present
+ 4 * reg_width
;
1197 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), fs_reg(0)));
1199 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), sample_index
));
1201 inst
= emit(SHADER_OPCODE_TXF_MS
, dst
);
1204 inst
= emit(SHADER_OPCODE_LOD
, dst
);
1207 inst
= emit(SHADER_OPCODE_TG4
, dst
);
1210 fail("unrecognized texture opcode");
1213 inst
->base_mrf
= base_mrf
;
1215 inst
->header_present
= header_present
;
1216 inst
->regs_written
= 4;
1219 fail("Message length >11 disallowed by hardware\n");
1226 fs_visitor::emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1227 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1228 fs_reg sample_index
)
1230 int reg_width
= dispatch_width
/ 8;
1231 bool header_present
= false;
1234 fs_reg payload
= fs_reg(this, glsl_type::float_type
);
1235 fs_reg next
= payload
;
1237 if (ir
->op
== ir_tg4
|| (ir
->offset
&& ir
->op
!= ir_txf
)) {
1238 /* For general texture offsets (no txf workaround), we need a header to
1239 * put them in. Note that for 16-wide we're making space for two actual
1240 * hardware registers here, so the emit will have to fix up for this.
1242 * * ir4_tg4 needs to place its channel select in the header,
1243 * for interaction with ARB_texture_swizzle
1245 header_present
= true;
1249 if (ir
->shadow_comparitor
) {
1250 emit(MOV(next
, shadow_c
));
1254 /* Set up the LOD info */
1261 emit(MOV(next
, lod
));
1265 emit(MOV(next
, lod
));
1269 if (dispatch_width
== 16)
1270 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1272 /* Load dPdx and the coordinate together:
1273 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1275 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1276 emit(MOV(next
, coordinate
));
1277 coordinate
.reg_offset
++;
1280 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1281 * only derivatives for (u, v, r).
1283 if (i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
) {
1284 emit(MOV(next
, lod
));
1288 emit(MOV(next
, lod2
));
1296 emit(MOV(next
.retype(BRW_REGISTER_TYPE_UD
), lod
));
1299 case ir_query_levels
:
1300 emit(MOV(next
.retype(BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1304 /* It appears that the ld instruction used for txf does its
1305 * address bounds check before adding in the offset. To work
1306 * around this, just add the integer offset to the integer texel
1307 * coordinate, and don't put the offset in the header.
1310 ir_constant
*offset
= ir
->offset
->as_constant();
1311 offsets
[0] = offset
->value
.i
[0];
1312 offsets
[1] = offset
->value
.i
[1];
1313 offsets
[2] = offset
->value
.i
[2];
1315 memset(offsets
, 0, sizeof(offsets
));
1318 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1319 emit(ADD(next
.retype(BRW_REGISTER_TYPE_D
), coordinate
, offsets
[0]));
1320 coordinate
.reg_offset
++;
1323 emit(MOV(next
.retype(BRW_REGISTER_TYPE_D
), lod
));
1326 for (int i
= 1; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1327 emit(ADD(next
.retype(BRW_REGISTER_TYPE_D
), coordinate
, offsets
[i
]));
1328 coordinate
.reg_offset
++;
1333 emit(MOV(next
.retype(BRW_REGISTER_TYPE_UD
), sample_index
));
1336 /* constant zero MCS; we arrange to never actually have a compressed
1337 * multisample surface here for now. TODO: issue ld_mcs to get this first,
1338 * if we ever support texturing from compressed multisample surfaces
1340 emit(MOV(next
.retype(BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1343 /* there is no offsetting for this message; just copy in the integer
1344 * texture coordinates
1346 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1347 emit(MOV(next
.retype(BRW_REGISTER_TYPE_D
), coordinate
));
1348 coordinate
.reg_offset
++;
1354 /* Set up the coordinate (except for cases where it was done above) */
1355 if (ir
->op
!= ir_txd
&& ir
->op
!= ir_txs
&& ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&& ir
->op
!= ir_query_levels
) {
1356 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1357 emit(MOV(next
, coordinate
));
1358 coordinate
.reg_offset
++;
1363 /* Generate the SEND */
1364 fs_inst
*inst
= NULL
;
1366 case ir_tex
: inst
= emit(SHADER_OPCODE_TEX
, dst
, payload
); break;
1367 case ir_txb
: inst
= emit(FS_OPCODE_TXB
, dst
, payload
); break;
1368 case ir_txl
: inst
= emit(SHADER_OPCODE_TXL
, dst
, payload
); break;
1369 case ir_txd
: inst
= emit(SHADER_OPCODE_TXD
, dst
, payload
); break;
1370 case ir_txf
: inst
= emit(SHADER_OPCODE_TXF
, dst
, payload
); break;
1371 case ir_txf_ms
: inst
= emit(SHADER_OPCODE_TXF_MS
, dst
, payload
); break;
1372 case ir_txs
: inst
= emit(SHADER_OPCODE_TXS
, dst
, payload
); break;
1373 case ir_query_levels
: inst
= emit(SHADER_OPCODE_TXS
, dst
, payload
); break;
1374 case ir_lod
: inst
= emit(SHADER_OPCODE_LOD
, dst
, payload
); break;
1375 case ir_tg4
: inst
= emit(SHADER_OPCODE_TG4
, dst
, payload
); break;
1377 inst
->base_mrf
= -1;
1379 inst
->mlen
= next
.reg_offset
* reg_width
- header_present
;
1381 inst
->mlen
= next
.reg_offset
* reg_width
;
1383 inst
->header_present
= header_present
;
1384 inst
->regs_written
= 4;
1386 virtual_grf_sizes
[payload
.reg
] = next
.reg_offset
;
1387 if (inst
->mlen
> 11) {
1388 fail("Message length >11 disallowed by hardware\n");
1395 fs_visitor::rescale_texcoord(ir_texture
*ir
, fs_reg coordinate
,
1396 bool is_rect
, int sampler
, int texunit
)
1398 fs_inst
*inst
= NULL
;
1399 bool needs_gl_clamp
= true;
1400 fs_reg scale_x
, scale_y
;
1402 /* The 965 requires the EU to do the normalization of GL rectangle
1403 * texture coordinates. We use the program parameter state
1404 * tracking to get the scaling factor.
1408 (brw
->gen
>= 6 && (c
->key
.tex
.gl_clamp_mask
[0] & (1 << sampler
) ||
1409 c
->key
.tex
.gl_clamp_mask
[1] & (1 << sampler
))))) {
1410 struct gl_program_parameter_list
*params
= prog
->Parameters
;
1411 int tokens
[STATE_LENGTH
] = {
1413 STATE_TEXRECT_SCALE
,
1419 if (dispatch_width
== 16) {
1420 fail("rectangle scale uniform setup not supported on 16-wide\n");
1424 scale_x
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
);
1425 scale_y
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
+ 1);
1427 GLuint index
= _mesa_add_state_reference(params
,
1428 (gl_state_index
*)tokens
);
1429 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1430 &prog
->Parameters
->ParameterValues
[index
][0].f
;
1431 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1432 &prog
->Parameters
->ParameterValues
[index
][1].f
;
1435 /* The 965 requires the EU to do the normalization of GL rectangle
1436 * texture coordinates. We use the program parameter state
1437 * tracking to get the scaling factor.
1439 if (brw
->gen
< 6 && is_rect
) {
1440 fs_reg dst
= fs_reg(this, ir
->coordinate
->type
);
1441 fs_reg src
= coordinate
;
1444 emit(MUL(dst
, src
, scale_x
));
1447 emit(MUL(dst
, src
, scale_y
));
1448 } else if (is_rect
) {
1449 /* On gen6+, the sampler handles the rectangle coordinates
1450 * natively, without needing rescaling. But that means we have
1451 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
1452 * not [0, 1] like the default case below.
1454 needs_gl_clamp
= false;
1456 for (int i
= 0; i
< 2; i
++) {
1457 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1458 fs_reg chan
= coordinate
;
1459 chan
.reg_offset
+= i
;
1461 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, brw_imm_f(0.0));
1462 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1464 /* Our parameter comes in as 1.0/width or 1.0/height,
1465 * because that's what people normally want for doing
1466 * texture rectangle handling. We need width or height
1467 * for clamping, but we don't care enough to make a new
1468 * parameter type, so just invert back.
1470 fs_reg limit
= fs_reg(this, glsl_type::float_type
);
1471 emit(MOV(limit
, i
== 0 ? scale_x
: scale_y
));
1472 emit(SHADER_OPCODE_RCP
, limit
, limit
);
1474 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, limit
);
1475 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1480 if (ir
->coordinate
&& needs_gl_clamp
) {
1481 for (unsigned int i
= 0;
1482 i
< MIN2(ir
->coordinate
->type
->vector_elements
, 3); i
++) {
1483 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1484 fs_reg chan
= coordinate
;
1485 chan
.reg_offset
+= i
;
1487 fs_inst
*inst
= emit(MOV(chan
, chan
));
1488 inst
->saturate
= true;
1496 fs_visitor::visit(ir_texture
*ir
)
1498 fs_inst
*inst
= NULL
;
1501 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, prog
);
1502 /* FINISHME: We're failing to recompile our programs when the sampler is
1503 * updated. This only matters for the texture rectangle scale parameters
1504 * (pre-gen6, or gen6+ with GL_CLAMP).
1506 int texunit
= prog
->SamplerUnits
[sampler
];
1508 if (ir
->op
== ir_tg4
) {
1509 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
1510 * emitting anything other than setting up the constant result.
1512 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
1513 int swiz
= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], chan
->value
.i
[0]);
1514 if (swiz
== SWIZZLE_ZERO
|| swiz
== SWIZZLE_ONE
) {
1516 fs_reg res
= fs_reg(this, glsl_type::vec4_type
);
1519 for (int i
=0; i
<4; i
++) {
1520 emit(MOV(res
, fs_reg(swiz
== SWIZZLE_ZERO
? 0.0f
: 1.0f
)));
1527 /* Should be lowered by do_lower_texture_projection */
1528 assert(!ir
->projector
);
1530 /* Generate code to compute all the subexpression trees. This has to be
1531 * done before loading any values into MRFs for the sampler message since
1532 * generating these values may involve SEND messages that need the MRFs.
1535 if (ir
->coordinate
) {
1536 ir
->coordinate
->accept(this);
1538 coordinate
= rescale_texcoord(ir
, this->result
,
1539 ir
->sampler
->type
->sampler_dimensionality
==
1540 GLSL_SAMPLER_DIM_RECT
,
1544 fs_reg shadow_comparitor
;
1545 if (ir
->shadow_comparitor
) {
1546 ir
->shadow_comparitor
->accept(this);
1547 shadow_comparitor
= this->result
;
1550 fs_reg lod
, lod2
, sample_index
;
1555 case ir_query_levels
:
1558 ir
->lod_info
.bias
->accept(this);
1562 ir
->lod_info
.grad
.dPdx
->accept(this);
1565 ir
->lod_info
.grad
.dPdy
->accept(this);
1566 lod2
= this->result
;
1571 ir
->lod_info
.lod
->accept(this);
1575 ir
->lod_info
.sample_index
->accept(this);
1576 sample_index
= this->result
;
1579 assert(!"Unrecognized texture opcode");
1582 /* Writemasking doesn't eliminate channels on SIMD8 texture
1583 * samples, so don't worry about them.
1585 fs_reg dst
= fs_reg(this, glsl_type::get_instance(ir
->type
->base_type
, 4, 1));
1587 if (brw
->gen
>= 7) {
1588 inst
= emit_texture_gen7(ir
, dst
, coordinate
, shadow_comparitor
,
1589 lod
, lod2
, sample_index
);
1590 } else if (brw
->gen
>= 5) {
1591 inst
= emit_texture_gen5(ir
, dst
, coordinate
, shadow_comparitor
,
1592 lod
, lod2
, sample_index
);
1594 inst
= emit_texture_gen4(ir
, dst
, coordinate
, shadow_comparitor
,
1598 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
1599 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1601 if (ir
->op
== ir_tg4
)
1602 inst
->texture_offset
|= gather_channel(ir
, sampler
) << 16; // M0.2:16-17
1604 inst
->sampler
= sampler
;
1606 if (ir
->shadow_comparitor
)
1607 inst
->shadow_compare
= true;
1609 /* fixup #layers for cube map arrays */
1610 if (ir
->op
== ir_txs
) {
1611 glsl_type
const *type
= ir
->sampler
->type
;
1612 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
1613 type
->sampler_array
) {
1615 depth
.reg_offset
= 2;
1616 emit_math(SHADER_OPCODE_INT_QUOTIENT
, depth
, depth
, fs_reg(6));
1620 swizzle_result(ir
, dst
, sampler
);
1624 * Set up the gather channel based on the swizzle, for gather4.
1627 fs_visitor::gather_channel(ir_texture
*ir
, int sampler
)
1629 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
1630 int swiz
= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], chan
->value
.i
[0]);
1632 case SWIZZLE_X
: return 0;
1634 /* gather4 sampler is broken for green channel on RG32F --
1635 * we must ask for blue instead.
1637 if (c
->key
.tex
.gather_channel_quirk_mask
& (1<<sampler
))
1640 case SWIZZLE_Z
: return 2;
1641 case SWIZZLE_W
: return 3;
1643 assert(!"Not reached"); /* zero, one swizzles handled already */
1649 * Swizzle the result of a texture result. This is necessary for
1650 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1653 fs_visitor::swizzle_result(ir_texture
*ir
, fs_reg orig_val
, int sampler
)
1655 if (ir
->op
== ir_query_levels
) {
1656 /* # levels is in .w */
1657 orig_val
.reg_offset
+= 3;
1658 this->result
= orig_val
;
1662 this->result
= orig_val
;
1664 /* txs,lod don't actually sample the texture, so swizzling the result
1667 if (ir
->op
== ir_txs
|| ir
->op
== ir_lod
|| ir
->op
== ir_tg4
)
1670 if (ir
->type
== glsl_type::float_type
) {
1671 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1672 assert(ir
->sampler
->type
->sampler_shadow
);
1673 } else if (c
->key
.tex
.swizzles
[sampler
] != SWIZZLE_NOOP
) {
1674 fs_reg swizzled_result
= fs_reg(this, glsl_type::vec4_type
);
1676 for (int i
= 0; i
< 4; i
++) {
1677 int swiz
= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1678 fs_reg l
= swizzled_result
;
1681 if (swiz
== SWIZZLE_ZERO
) {
1682 emit(MOV(l
, fs_reg(0.0f
)));
1683 } else if (swiz
== SWIZZLE_ONE
) {
1684 emit(MOV(l
, fs_reg(1.0f
)));
1686 fs_reg r
= orig_val
;
1687 r
.reg_offset
+= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1691 this->result
= swizzled_result
;
1696 fs_visitor::visit(ir_swizzle
*ir
)
1698 ir
->val
->accept(this);
1699 fs_reg val
= this->result
;
1701 if (ir
->type
->vector_elements
== 1) {
1702 this->result
.reg_offset
+= ir
->mask
.x
;
1706 fs_reg result
= fs_reg(this, ir
->type
);
1707 this->result
= result
;
1709 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1710 fs_reg channel
= val
;
1728 channel
.reg_offset
+= swiz
;
1729 emit(MOV(result
, channel
));
1730 result
.reg_offset
++;
1735 fs_visitor::visit(ir_discard
*ir
)
1737 assert(ir
->condition
== NULL
); /* FINISHME */
1739 /* We track our discarded pixels in f0.1. By predicating on it, we can
1740 * update just the flag bits that aren't yet discarded. By emitting a
1741 * CMP of g0 != g0, all our currently executing channels will get turned
1744 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
1745 BRW_REGISTER_TYPE_UW
));
1746 fs_inst
*cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
1747 BRW_CONDITIONAL_NZ
));
1748 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
1749 cmp
->flag_subreg
= 1;
1751 if (brw
->gen
>= 6) {
1752 /* For performance, after a discard, jump to the end of the shader.
1753 * However, many people will do foliage by discarding based on a
1754 * texture's alpha mask, and then continue on to texture with the
1755 * remaining pixels. To avoid trashing the derivatives for those
1756 * texture samples, we'll only jump if all of the pixels in the subspan
1757 * have been discarded.
1759 fs_inst
*discard_jump
= emit(FS_OPCODE_DISCARD_JUMP
);
1760 discard_jump
->flag_subreg
= 1;
1761 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1762 discard_jump
->predicate_inverse
= true;
1767 fs_visitor::visit(ir_constant
*ir
)
1769 /* Set this->result to reg at the bottom of the function because some code
1770 * paths will cause this visitor to be applied to other fields. This will
1771 * cause the value stored in this->result to be modified.
1773 * Make reg constant so that it doesn't get accidentally modified along the
1774 * way. Yes, I actually had this problem. :(
1776 const fs_reg
reg(this, ir
->type
);
1777 fs_reg dst_reg
= reg
;
1779 if (ir
->type
->is_array()) {
1780 const unsigned size
= type_size(ir
->type
->fields
.array
);
1782 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
1783 ir
->array_elements
[i
]->accept(this);
1784 fs_reg src_reg
= this->result
;
1786 dst_reg
.type
= src_reg
.type
;
1787 for (unsigned j
= 0; j
< size
; j
++) {
1788 emit(MOV(dst_reg
, src_reg
));
1789 src_reg
.reg_offset
++;
1790 dst_reg
.reg_offset
++;
1793 } else if (ir
->type
->is_record()) {
1794 foreach_list(node
, &ir
->components
) {
1795 ir_constant
*const field
= (ir_constant
*) node
;
1796 const unsigned size
= type_size(field
->type
);
1798 field
->accept(this);
1799 fs_reg src_reg
= this->result
;
1801 dst_reg
.type
= src_reg
.type
;
1802 for (unsigned j
= 0; j
< size
; j
++) {
1803 emit(MOV(dst_reg
, src_reg
));
1804 src_reg
.reg_offset
++;
1805 dst_reg
.reg_offset
++;
1809 const unsigned size
= type_size(ir
->type
);
1811 for (unsigned i
= 0; i
< size
; i
++) {
1812 switch (ir
->type
->base_type
) {
1813 case GLSL_TYPE_FLOAT
:
1814 emit(MOV(dst_reg
, fs_reg(ir
->value
.f
[i
])));
1816 case GLSL_TYPE_UINT
:
1817 emit(MOV(dst_reg
, fs_reg(ir
->value
.u
[i
])));
1820 emit(MOV(dst_reg
, fs_reg(ir
->value
.i
[i
])));
1822 case GLSL_TYPE_BOOL
:
1823 emit(MOV(dst_reg
, fs_reg((int)ir
->value
.b
[i
])));
1826 assert(!"Non-float/uint/int/bool constant");
1828 dst_reg
.reg_offset
++;
1836 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
1838 ir_expression
*expr
= ir
->as_expression();
1841 expr
->operation
!= ir_binop_logic_and
&&
1842 expr
->operation
!= ir_binop_logic_or
&&
1843 expr
->operation
!= ir_binop_logic_xor
) {
1847 assert(expr
->get_num_operands() <= 2);
1848 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1849 assert(expr
->operands
[i
]->type
->is_scalar());
1851 expr
->operands
[i
]->accept(this);
1852 op
[i
] = this->result
;
1854 resolve_ud_negate(&op
[i
]);
1857 switch (expr
->operation
) {
1858 case ir_unop_logic_not
:
1859 inst
= emit(AND(reg_null_d
, op
[0], fs_reg(1)));
1860 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1864 if (brw
->gen
>= 6) {
1865 emit(CMP(reg_null_d
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1867 inst
= emit(MOV(reg_null_f
, op
[0]));
1868 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1873 if (brw
->gen
>= 6) {
1874 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1876 inst
= emit(MOV(reg_null_d
, op
[0]));
1877 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1881 case ir_binop_greater
:
1882 case ir_binop_gequal
:
1884 case ir_binop_lequal
:
1885 case ir_binop_equal
:
1886 case ir_binop_all_equal
:
1887 case ir_binop_nequal
:
1888 case ir_binop_any_nequal
:
1889 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1890 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1892 emit(CMP(reg_null_d
, op
[0], op
[1],
1893 brw_conditional_for_comparison(expr
->operation
)));
1897 assert(!"not reached");
1898 fail("bad cond code\n");
1906 fs_inst
*inst
= emit(AND(reg_null_d
, this->result
, fs_reg(1)));
1907 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1911 * Emit a gen6 IF statement with the comparison folded into the IF
1915 fs_visitor::emit_if_gen6(ir_if
*ir
)
1917 ir_expression
*expr
= ir
->condition
->as_expression();
1924 assert(expr
->get_num_operands() <= 2);
1925 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1926 assert(expr
->operands
[i
]->type
->is_scalar());
1928 expr
->operands
[i
]->accept(this);
1929 op
[i
] = this->result
;
1932 switch (expr
->operation
) {
1933 case ir_unop_logic_not
:
1934 case ir_binop_logic_xor
:
1935 case ir_binop_logic_or
:
1936 case ir_binop_logic_and
:
1937 /* For operations on bool arguments, only the low bit of the bool is
1938 * valid, and the others are undefined. Fall back to the condition
1944 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
1945 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1949 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1952 case ir_binop_greater
:
1953 case ir_binop_gequal
:
1955 case ir_binop_lequal
:
1956 case ir_binop_equal
:
1957 case ir_binop_all_equal
:
1958 case ir_binop_nequal
:
1959 case ir_binop_any_nequal
:
1960 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1961 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1963 emit(IF(op
[0], op
[1],
1964 brw_conditional_for_comparison(expr
->operation
)));
1967 assert(!"not reached");
1968 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1969 fail("bad condition\n");
1974 emit_bool_to_cond_code(ir
->condition
);
1975 fs_inst
*inst
= emit(BRW_OPCODE_IF
);
1976 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1980 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
1982 * Many GLSL shaders contain the following pattern:
1984 * x = condition ? foo : bar
1986 * The compiler emits an ir_if tree for this, since each subexpression might be
1987 * a complex tree that could have side-effects or short-circuit logic.
1989 * However, the common case is to simply select one of two constants or
1990 * variable values---which is exactly what SEL is for. In this case, the
1991 * assembly looks like:
1999 * which can be easily translated into:
2001 * (+f0) SEL dst src0 src1
2003 * If src0 is an immediate value, we promote it to a temporary GRF.
2006 fs_visitor::try_replace_with_sel()
2008 fs_inst
*endif_inst
= (fs_inst
*) instructions
.get_tail();
2009 assert(endif_inst
->opcode
== BRW_OPCODE_ENDIF
);
2011 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
2013 BRW_OPCODE_IF
, BRW_OPCODE_MOV
, BRW_OPCODE_ELSE
, BRW_OPCODE_MOV
,
2016 fs_inst
*match
= (fs_inst
*) endif_inst
->prev
;
2017 for (int i
= 0; i
< 4; i
++) {
2018 if (match
->is_head_sentinel() || match
->opcode
!= opcodes
[4-i
-1])
2020 match
= (fs_inst
*) match
->prev
;
2023 /* The opcodes match; it looks like the right sequence of instructions. */
2024 fs_inst
*else_mov
= (fs_inst
*) endif_inst
->prev
;
2025 fs_inst
*then_mov
= (fs_inst
*) else_mov
->prev
->prev
;
2026 fs_inst
*if_inst
= (fs_inst
*) then_mov
->prev
;
2028 /* Check that the MOVs are the right form. */
2029 if (then_mov
->dst
.equals(else_mov
->dst
) &&
2030 !then_mov
->is_partial_write() &&
2031 !else_mov
->is_partial_write()) {
2033 /* Remove the matched instructions; we'll emit a SEL to replace them. */
2034 while (!if_inst
->next
->is_tail_sentinel())
2035 if_inst
->next
->remove();
2038 /* Only the last source register can be a constant, so if the MOV in
2039 * the "then" clause uses a constant, we need to put it in a temporary.
2041 fs_reg
src0(then_mov
->src
[0]);
2042 if (src0
.file
== IMM
) {
2043 src0
= fs_reg(this, glsl_type::float_type
);
2044 src0
.type
= then_mov
->src
[0].type
;
2045 emit(MOV(src0
, then_mov
->src
[0]));
2049 if (if_inst
->conditional_mod
) {
2050 /* Sandybridge-specific IF with embedded comparison */
2051 emit(CMP(reg_null_d
, if_inst
->src
[0], if_inst
->src
[1],
2052 if_inst
->conditional_mod
));
2053 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
2054 sel
->predicate
= BRW_PREDICATE_NORMAL
;
2056 /* Separate CMP and IF instructions */
2057 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
2058 sel
->predicate
= if_inst
->predicate
;
2059 sel
->predicate_inverse
= if_inst
->predicate_inverse
;
2065 fs_visitor::visit(ir_if
*ir
)
2067 if (brw
->gen
< 6 && dispatch_width
== 16) {
2068 fail("Can't support (non-uniform) control flow on 16-wide\n");
2071 /* Don't point the annotation at the if statement, because then it plus
2072 * the then and else blocks get printed.
2074 this->base_ir
= ir
->condition
;
2076 if (brw
->gen
== 6) {
2079 emit_bool_to_cond_code(ir
->condition
);
2081 emit(IF(BRW_PREDICATE_NORMAL
));
2084 foreach_list(node
, &ir
->then_instructions
) {
2085 ir_instruction
*ir
= (ir_instruction
*)node
;
2091 if (!ir
->else_instructions
.is_empty()) {
2092 emit(BRW_OPCODE_ELSE
);
2094 foreach_list(node
, &ir
->else_instructions
) {
2095 ir_instruction
*ir
= (ir_instruction
*)node
;
2102 emit(BRW_OPCODE_ENDIF
);
2104 try_replace_with_sel();
2108 fs_visitor::visit(ir_loop
*ir
)
2110 fs_reg counter
= reg_undef
;
2112 if (brw
->gen
< 6 && dispatch_width
== 16) {
2113 fail("Can't support (non-uniform) control flow on 16-wide\n");
2117 this->base_ir
= ir
->counter
;
2118 ir
->counter
->accept(this);
2119 counter
= *(variable_storage(ir
->counter
));
2122 this->base_ir
= ir
->from
;
2123 ir
->from
->accept(this);
2125 emit(MOV(counter
, this->result
));
2129 this->base_ir
= NULL
;
2130 emit(BRW_OPCODE_DO
);
2133 this->base_ir
= ir
->to
;
2134 ir
->to
->accept(this);
2136 emit(CMP(reg_null_d
, counter
, this->result
,
2137 brw_conditional_for_comparison(ir
->cmp
)));
2139 fs_inst
*inst
= emit(BRW_OPCODE_BREAK
);
2140 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2143 foreach_list(node
, &ir
->body_instructions
) {
2144 ir_instruction
*ir
= (ir_instruction
*)node
;
2150 if (ir
->increment
) {
2151 this->base_ir
= ir
->increment
;
2152 ir
->increment
->accept(this);
2153 emit(ADD(counter
, counter
, this->result
));
2156 this->base_ir
= NULL
;
2157 emit(BRW_OPCODE_WHILE
);
2161 fs_visitor::visit(ir_loop_jump
*ir
)
2164 case ir_loop_jump::jump_break
:
2165 emit(BRW_OPCODE_BREAK
);
2167 case ir_loop_jump::jump_continue
:
2168 emit(BRW_OPCODE_CONTINUE
);
2174 fs_visitor::visit(ir_call
*ir
)
2176 assert(!"FINISHME");
2180 fs_visitor::visit(ir_return
*ir
)
2182 assert(!"FINISHME");
2186 fs_visitor::visit(ir_function
*ir
)
2188 /* Ignore function bodies other than main() -- we shouldn't see calls to
2189 * them since they should all be inlined before we get to ir_to_mesa.
2191 if (strcmp(ir
->name
, "main") == 0) {
2192 const ir_function_signature
*sig
;
2195 sig
= ir
->matching_signature(NULL
, &empty
);
2199 foreach_list(node
, &sig
->body
) {
2200 ir_instruction
*ir
= (ir_instruction
*)node
;
2209 fs_visitor::visit(ir_function_signature
*ir
)
2211 assert(!"not reached");
2216 fs_visitor::visit(ir_emit_vertex
*)
2218 assert(!"not reached");
2222 fs_visitor::visit(ir_end_primitive
*)
2224 assert(!"not reached");
2228 fs_visitor::emit(fs_inst inst
)
2230 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
2237 fs_visitor::emit(fs_inst
*inst
)
2239 if (force_uncompressed_stack
> 0)
2240 inst
->force_uncompressed
= true;
2241 else if (force_sechalf_stack
> 0)
2242 inst
->force_sechalf
= true;
2244 inst
->annotation
= this->current_annotation
;
2245 inst
->ir
= this->base_ir
;
2247 this->instructions
.push_tail(inst
);
2253 fs_visitor::emit(exec_list list
)
2255 foreach_list_safe(node
, &list
) {
2256 fs_inst
*inst
= (fs_inst
*)node
;
2262 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
2264 fs_visitor::emit_dummy_fs()
2266 int reg_width
= dispatch_width
/ 8;
2268 /* Everyone's favorite color. */
2269 emit(MOV(fs_reg(MRF
, 2 + 0 * reg_width
), fs_reg(1.0f
)));
2270 emit(MOV(fs_reg(MRF
, 2 + 1 * reg_width
), fs_reg(0.0f
)));
2271 emit(MOV(fs_reg(MRF
, 2 + 2 * reg_width
), fs_reg(1.0f
)));
2272 emit(MOV(fs_reg(MRF
, 2 + 3 * reg_width
), fs_reg(0.0f
)));
2275 write
= emit(FS_OPCODE_FB_WRITE
, fs_reg(0), fs_reg(0));
2276 write
->base_mrf
= 2;
2277 write
->mlen
= 4 * reg_width
;
2281 /* The register location here is relative to the start of the URB
2282 * data. It will get adjusted to be a real location before
2283 * generate_code() time.
2286 fs_visitor::interp_reg(int location
, int channel
)
2288 int regnr
= c
->prog_data
.urb_setup
[location
] * 2 + channel
/ 2;
2289 int stride
= (channel
& 1) * 4;
2291 assert(c
->prog_data
.urb_setup
[location
] != -1);
2293 return brw_vec1_grf(regnr
, stride
);
2296 /** Emits the interpolation for the varying inputs. */
2298 fs_visitor::emit_interpolation_setup_gen4()
2300 this->current_annotation
= "compute pixel centers";
2301 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
2302 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
2303 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2304 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2306 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
2307 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
2309 this->current_annotation
= "compute pixel deltas from v0";
2311 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2312 fs_reg(this, glsl_type::vec2_type
);
2313 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2314 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
];
2315 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
].reg_offset
++;
2317 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2318 fs_reg(this, glsl_type::float_type
);
2319 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2320 fs_reg(this, glsl_type::float_type
);
2322 emit(ADD(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2323 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0)))));
2324 emit(ADD(this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2325 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1)))));
2327 this->current_annotation
= "compute pos.w and 1/pos.w";
2328 /* Compute wpos.w. It's always in our setup, since it's needed to
2329 * interpolate the other attributes.
2331 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2332 emit(FS_OPCODE_LINTERP
, wpos_w
,
2333 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2334 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2335 interp_reg(VARYING_SLOT_POS
, 3));
2336 /* Compute the pixel 1/W value from wpos.w. */
2337 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
2338 emit_math(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
2339 this->current_annotation
= NULL
;
2342 /** Emits the interpolation for the varying inputs. */
2344 fs_visitor::emit_interpolation_setup_gen6()
2346 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
2348 /* If the pixel centers end up used, the setup is the same as for gen4. */
2349 this->current_annotation
= "compute pixel centers";
2350 fs_reg int_pixel_x
= fs_reg(this, glsl_type::uint_type
);
2351 fs_reg int_pixel_y
= fs_reg(this, glsl_type::uint_type
);
2352 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2353 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2354 emit(ADD(int_pixel_x
,
2355 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
2356 fs_reg(brw_imm_v(0x10101010))));
2357 emit(ADD(int_pixel_y
,
2358 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
2359 fs_reg(brw_imm_v(0x11001100))));
2361 /* As of gen6, we can no longer mix float and int sources. We have
2362 * to turn the integer pixel centers into floats for their actual
2365 this->pixel_x
= fs_reg(this, glsl_type::float_type
);
2366 this->pixel_y
= fs_reg(this, glsl_type::float_type
);
2367 emit(MOV(this->pixel_x
, int_pixel_x
));
2368 emit(MOV(this->pixel_y
, int_pixel_y
));
2370 this->current_annotation
= "compute pos.w";
2371 this->pixel_w
= fs_reg(brw_vec8_grf(c
->source_w_reg
, 0));
2372 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2373 emit_math(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
2375 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
2376 uint8_t reg
= c
->barycentric_coord_reg
[i
];
2377 this->delta_x
[i
] = fs_reg(brw_vec8_grf(reg
, 0));
2378 this->delta_y
[i
] = fs_reg(brw_vec8_grf(reg
+ 1, 0));
2381 this->current_annotation
= NULL
;
2385 fs_visitor::emit_color_write(int target
, int index
, int first_color_mrf
)
2387 int reg_width
= dispatch_width
/ 8;
2389 fs_reg color
= outputs
[target
];
2392 /* If there's no color data to be written, skip it. */
2393 if (color
.file
== BAD_FILE
)
2396 color
.reg_offset
+= index
;
2398 if (dispatch_width
== 8 || brw
->gen
>= 6) {
2399 /* SIMD8 write looks like:
2405 * gen6 SIMD16 DP write looks like:
2415 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
* reg_width
,
2418 inst
->saturate
= c
->key
.clamp_fragment_color
;
2420 /* pre-gen6 SIMD16 single source DP write looks like:
2430 if (brw
->has_compr4
) {
2431 /* By setting the high bit of the MRF register number, we
2432 * indicate that we want COMPR4 mode - instead of doing the
2433 * usual destination + 1 for the second half we get
2436 inst
= emit(MOV(fs_reg(MRF
, BRW_MRF_COMPR4
+ first_color_mrf
+ index
,
2439 inst
->saturate
= c
->key
.clamp_fragment_color
;
2441 push_force_uncompressed();
2442 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
, color
.type
),
2444 inst
->saturate
= c
->key
.clamp_fragment_color
;
2445 pop_force_uncompressed();
2447 push_force_sechalf();
2448 color
.sechalf
= true;
2449 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
+ 4, color
.type
),
2451 inst
->saturate
= c
->key
.clamp_fragment_color
;
2452 pop_force_sechalf();
2453 color
.sechalf
= false;
2459 fs_visitor::emit_fb_writes()
2461 this->current_annotation
= "FB write header";
2462 bool header_present
= true;
2463 /* We can potentially have a message length of up to 15, so we have to set
2464 * base_mrf to either 0 or 1 in order to fit in m0..m15.
2468 int reg_width
= dispatch_width
/ 8;
2469 bool do_dual_src
= this->dual_src_output
.file
!= BAD_FILE
;
2470 bool src0_alpha_to_render_target
= false;
2472 if (dispatch_width
== 16 && do_dual_src
) {
2473 fail("GL_ARB_blend_func_extended not yet supported in 16-wide.");
2474 do_dual_src
= false;
2477 /* From the Sandy Bridge PRM, volume 4, page 198:
2479 * "Dispatched Pixel Enables. One bit per pixel indicating
2480 * which pixels were originally enabled when the thread was
2481 * dispatched. This field is only required for the end-of-
2482 * thread message and on all dual-source messages."
2484 if (brw
->gen
>= 6 &&
2485 !this->fp
->UsesKill
&&
2487 c
->key
.nr_color_regions
== 1) {
2488 header_present
= false;
2491 if (header_present
) {
2492 src0_alpha_to_render_target
= brw
->gen
>= 6 &&
2494 c
->key
.replicate_alpha
;
2499 if (c
->aa_dest_stencil_reg
) {
2500 push_force_uncompressed();
2501 emit(MOV(fs_reg(MRF
, nr
++),
2502 fs_reg(brw_vec8_grf(c
->aa_dest_stencil_reg
, 0))));
2503 pop_force_uncompressed();
2506 /* Reserve space for color. It'll be filled in per MRT below. */
2508 nr
+= 4 * reg_width
;
2511 if (src0_alpha_to_render_target
)
2514 if (c
->source_depth_to_render_target
) {
2515 if (brw
->gen
== 6 && dispatch_width
== 16) {
2516 /* For outputting oDepth on gen6, SIMD8 writes have to be
2517 * used. This would require 8-wide moves of each half to
2518 * message regs, kind of like pre-gen5 SIMD16 FB writes.
2519 * Just bail on doing so for now.
2521 fail("Missing support for simd16 depth writes on gen6\n");
2524 if (prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
2525 /* Hand over gl_FragDepth. */
2526 assert(this->frag_depth
.file
!= BAD_FILE
);
2527 emit(MOV(fs_reg(MRF
, nr
), this->frag_depth
));
2529 /* Pass through the payload depth. */
2530 emit(MOV(fs_reg(MRF
, nr
),
2531 fs_reg(brw_vec8_grf(c
->source_depth_reg
, 0))));
2536 if (c
->dest_depth_reg
) {
2537 emit(MOV(fs_reg(MRF
, nr
),
2538 fs_reg(brw_vec8_grf(c
->dest_depth_reg
, 0))));
2543 fs_reg src0
= this->outputs
[0];
2544 fs_reg src1
= this->dual_src_output
;
2546 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2548 for (int i
= 0; i
< 4; i
++) {
2549 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ i
, src0
.type
), src0
));
2551 inst
->saturate
= c
->key
.clamp_fragment_color
;
2554 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2556 for (int i
= 0; i
< 4; i
++) {
2557 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ 4 + i
, src1
.type
),
2560 inst
->saturate
= c
->key
.clamp_fragment_color
;
2563 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2564 emit_shader_time_end();
2566 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2568 inst
->base_mrf
= base_mrf
;
2569 inst
->mlen
= nr
- base_mrf
;
2571 inst
->header_present
= header_present
;
2573 c
->prog_data
.dual_src_blend
= true;
2574 this->current_annotation
= NULL
;
2578 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
2579 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2580 "FB write target %d",
2582 /* If src0_alpha_to_render_target is true, include source zero alpha
2583 * data in RenderTargetWrite message for targets > 0.
2585 int write_color_mrf
= color_mrf
;
2586 if (src0_alpha_to_render_target
&& target
!= 0) {
2588 fs_reg color
= outputs
[0];
2589 color
.reg_offset
+= 3;
2591 inst
= emit(MOV(fs_reg(MRF
, write_color_mrf
, color
.type
),
2593 inst
->saturate
= c
->key
.clamp_fragment_color
;
2594 write_color_mrf
= color_mrf
+ reg_width
;
2597 for (unsigned i
= 0; i
< this->output_components
[target
]; i
++)
2598 emit_color_write(target
, i
, write_color_mrf
);
2601 if (target
== c
->key
.nr_color_regions
- 1) {
2604 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2605 emit_shader_time_end();
2608 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2609 inst
->target
= target
;
2610 inst
->base_mrf
= base_mrf
;
2611 if (src0_alpha_to_render_target
&& target
== 0)
2612 inst
->mlen
= nr
- base_mrf
- reg_width
;
2614 inst
->mlen
= nr
- base_mrf
;
2616 inst
->header_present
= header_present
;
2619 if (c
->key
.nr_color_regions
== 0) {
2620 /* Even if there's no color buffers enabled, we still need to send
2621 * alpha out the pipeline to our null renderbuffer to support
2622 * alpha-testing, alpha-to-coverage, and so on.
2624 emit_color_write(0, 3, color_mrf
);
2626 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2627 emit_shader_time_end();
2629 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2630 inst
->base_mrf
= base_mrf
;
2631 inst
->mlen
= nr
- base_mrf
;
2633 inst
->header_present
= header_present
;
2636 this->current_annotation
= NULL
;
2640 fs_visitor::resolve_ud_negate(fs_reg
*reg
)
2642 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2646 fs_reg temp
= fs_reg(this, glsl_type::uint_type
);
2647 emit(MOV(temp
, *reg
));
2652 fs_visitor::resolve_bool_comparison(ir_rvalue
*rvalue
, fs_reg
*reg
)
2654 if (rvalue
->type
!= glsl_type::bool_type
)
2657 fs_reg temp
= fs_reg(this, glsl_type::bool_type
);
2658 emit(AND(temp
, *reg
, fs_reg(1)));
2662 fs_visitor::fs_visitor(struct brw_context
*brw
,
2663 struct brw_wm_compile
*c
,
2664 struct gl_shader_program
*shader_prog
,
2665 struct gl_fragment_program
*fp
,
2666 unsigned dispatch_width
)
2667 : dispatch_width(dispatch_width
)
2672 this->prog
= &fp
->Base
;
2673 this->shader_prog
= shader_prog
;
2674 this->prog
= &fp
->Base
;
2675 this->stage_prog_data
= &c
->prog_data
.base
;
2676 this->ctx
= &brw
->ctx
;
2677 this->mem_ctx
= ralloc_context(NULL
);
2679 shader
= (struct brw_shader
*)
2680 shader_prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2683 this->failed
= false;
2684 this->variable_ht
= hash_table_ctor(0,
2685 hash_table_pointer_hash
,
2686 hash_table_pointer_compare
);
2688 memset(this->outputs
, 0, sizeof(this->outputs
));
2689 memset(this->output_components
, 0, sizeof(this->output_components
));
2690 this->first_non_payload_grf
= 0;
2691 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2693 this->current_annotation
= NULL
;
2694 this->base_ir
= NULL
;
2696 this->virtual_grf_sizes
= NULL
;
2697 this->virtual_grf_count
= 0;
2698 this->virtual_grf_array_size
= 0;
2699 this->virtual_grf_start
= NULL
;
2700 this->virtual_grf_end
= NULL
;
2701 this->live_intervals
= NULL
;
2703 this->params_remap
= NULL
;
2704 this->nr_params_remap
= 0;
2706 this->force_uncompressed_stack
= 0;
2707 this->force_sechalf_stack
= 0;
2709 memset(&this->param_size
, 0, sizeof(this->param_size
));
2712 fs_visitor::~fs_visitor()
2714 ralloc_free(this->mem_ctx
);
2715 hash_table_dtor(this->variable_ht
);