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 op
[operand
] = this->result
;
341 /* Matrix expression operands should have been broken down to vector
342 * operations already.
344 assert(!ir
->operands
[operand
]->type
->is_matrix());
345 /* And then those vector operands should have been broken down to scalar.
347 assert(!ir
->operands
[operand
]->type
->is_vector());
350 /* Storage for our result. If our result goes into an assignment, it will
351 * just get copy-propagated out, so no worries.
353 this->result
= fs_reg(this, ir
->type
);
355 switch (ir
->operation
) {
356 case ir_unop_logic_not
:
357 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
358 * ones complement of the whole register, not just bit 0.
360 emit(XOR(this->result
, op
[0], fs_reg(1)));
363 op
[0].negate
= !op
[0].negate
;
364 this->result
= op
[0];
368 op
[0].negate
= false;
369 this->result
= op
[0];
372 temp
= fs_reg(this, ir
->type
);
374 emit(MOV(this->result
, fs_reg(0.0f
)));
376 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_G
));
377 inst
= emit(MOV(this->result
, fs_reg(1.0f
)));
378 inst
->predicate
= BRW_PREDICATE_NORMAL
;
380 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_L
));
381 inst
= emit(MOV(this->result
, fs_reg(-1.0f
)));
382 inst
->predicate
= BRW_PREDICATE_NORMAL
;
386 emit_math(SHADER_OPCODE_RCP
, this->result
, op
[0]);
390 emit_math(SHADER_OPCODE_EXP2
, this->result
, op
[0]);
393 emit_math(SHADER_OPCODE_LOG2
, this->result
, op
[0]);
397 assert(!"not reached: should be handled by ir_explog_to_explog2");
400 case ir_unop_sin_reduced
:
401 emit_math(SHADER_OPCODE_SIN
, this->result
, op
[0]);
404 case ir_unop_cos_reduced
:
405 emit_math(SHADER_OPCODE_COS
, this->result
, op
[0]);
409 emit(FS_OPCODE_DDX
, this->result
, op
[0]);
412 emit(FS_OPCODE_DDY
, this->result
, op
[0]);
416 emit(ADD(this->result
, op
[0], op
[1]));
419 assert(!"not reached: should be handled by ir_sub_to_add_neg");
423 if (ir
->type
->is_integer()) {
424 /* For integer multiplication, the MUL uses the low 16 bits
425 * of one of the operands (src0 on gen6, src1 on gen7). The
426 * MACH accumulates in the contribution of the upper 16 bits
429 * FINISHME: Emit just the MUL if we know an operand is small
432 if (brw
->gen
>= 7 && dispatch_width
== 16)
433 fail("16-wide explicit accumulator operands unsupported\n");
435 struct brw_reg acc
= retype(brw_acc_reg(), BRW_REGISTER_TYPE_D
);
437 emit(MUL(acc
, op
[0], op
[1]));
438 emit(MACH(reg_null_d
, op
[0], op
[1]));
439 emit(MOV(this->result
, fs_reg(acc
)));
441 emit(MUL(this->result
, op
[0], op
[1]));
445 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
446 assert(ir
->type
->is_integer());
447 emit_math(SHADER_OPCODE_INT_QUOTIENT
, this->result
, op
[0], op
[1]);
450 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
451 assert(ir
->type
->is_integer());
452 emit_math(SHADER_OPCODE_INT_REMAINDER
, this->result
, op
[0], op
[1]);
456 case ir_binop_greater
:
457 case ir_binop_lequal
:
458 case ir_binop_gequal
:
460 case ir_binop_all_equal
:
461 case ir_binop_nequal
:
462 case ir_binop_any_nequal
:
463 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
464 resolve_bool_comparison(ir
->operands
[1], &op
[1]);
466 emit(CMP(this->result
, op
[0], op
[1],
467 brw_conditional_for_comparison(ir
->operation
)));
470 case ir_binop_logic_xor
:
471 emit(XOR(this->result
, op
[0], op
[1]));
474 case ir_binop_logic_or
:
475 emit(OR(this->result
, op
[0], op
[1]));
478 case ir_binop_logic_and
:
479 emit(AND(this->result
, op
[0], op
[1]));
484 assert(!"not reached: should be handled by brw_fs_channel_expressions");
488 assert(!"not reached: should be handled by lower_noise");
491 case ir_quadop_vector
:
492 assert(!"not reached: should be handled by lower_quadop_vector");
495 case ir_binop_vector_extract
:
496 assert(!"not reached: should be handled by lower_vec_index_to_cond_assign()");
499 case ir_triop_vector_insert
:
500 assert(!"not reached: should be handled by lower_vector_insert()");
504 emit_math(SHADER_OPCODE_SQRT
, this->result
, op
[0]);
508 emit_math(SHADER_OPCODE_RSQ
, this->result
, op
[0]);
511 case ir_unop_bitcast_i2f
:
512 case ir_unop_bitcast_u2f
:
513 op
[0].type
= BRW_REGISTER_TYPE_F
;
514 this->result
= op
[0];
517 case ir_unop_bitcast_f2u
:
518 op
[0].type
= BRW_REGISTER_TYPE_UD
;
519 this->result
= op
[0];
522 case ir_unop_bitcast_f2i
:
523 op
[0].type
= BRW_REGISTER_TYPE_D
;
524 this->result
= op
[0];
530 emit(MOV(this->result
, op
[0]));
534 emit(AND(this->result
, op
[0], fs_reg(1)));
537 temp
= fs_reg(this, glsl_type::int_type
);
538 emit(AND(temp
, op
[0], fs_reg(1)));
539 emit(MOV(this->result
, temp
));
543 emit(CMP(this->result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
546 emit(CMP(this->result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
550 emit(RNDZ(this->result
, op
[0]));
553 op
[0].negate
= !op
[0].negate
;
554 emit(RNDD(this->result
, op
[0]));
555 this->result
.negate
= true;
558 emit(RNDD(this->result
, op
[0]));
561 emit(FRC(this->result
, op
[0]));
563 case ir_unop_round_even
:
564 emit(RNDE(this->result
, op
[0]));
569 resolve_ud_negate(&op
[0]);
570 resolve_ud_negate(&op
[1]);
571 emit_minmax(ir
->operation
== ir_binop_min
?
572 BRW_CONDITIONAL_L
: BRW_CONDITIONAL_GE
,
573 this->result
, op
[0], op
[1]);
575 case ir_unop_pack_snorm_2x16
:
576 case ir_unop_pack_snorm_4x8
:
577 case ir_unop_pack_unorm_2x16
:
578 case ir_unop_pack_unorm_4x8
:
579 case ir_unop_unpack_snorm_2x16
:
580 case ir_unop_unpack_snorm_4x8
:
581 case ir_unop_unpack_unorm_2x16
:
582 case ir_unop_unpack_unorm_4x8
:
583 case ir_unop_unpack_half_2x16
:
584 case ir_unop_pack_half_2x16
:
585 assert(!"not reached: should be handled by lower_packing_builtins");
587 case ir_unop_unpack_half_2x16_split_x
:
588 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, this->result
, op
[0]);
590 case ir_unop_unpack_half_2x16_split_y
:
591 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, this->result
, op
[0]);
594 emit_math(SHADER_OPCODE_POW
, this->result
, op
[0], op
[1]);
597 case ir_unop_bitfield_reverse
:
598 emit(BFREV(this->result
, op
[0]));
600 case ir_unop_bit_count
:
601 emit(CBIT(this->result
, op
[0]));
603 case ir_unop_find_msb
:
604 temp
= fs_reg(this, glsl_type::uint_type
);
605 emit(FBH(temp
, op
[0]));
607 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
608 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
609 * subtract the result from 31 to convert the MSB count into an LSB count.
612 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
613 emit(MOV(this->result
, temp
));
614 emit(CMP(reg_null_d
, this->result
, fs_reg(-1), BRW_CONDITIONAL_NZ
));
617 inst
= emit(ADD(this->result
, temp
, fs_reg(31)));
618 inst
->predicate
= BRW_PREDICATE_NORMAL
;
620 case ir_unop_find_lsb
:
621 emit(FBL(this->result
, op
[0]));
623 case ir_triop_bitfield_extract
:
624 /* Note that the instruction's argument order is reversed from GLSL
627 emit(BFE(this->result
, op
[2], op
[1], op
[0]));
630 emit(BFI1(this->result
, op
[0], op
[1]));
633 emit(BFI2(this->result
, op
[0], op
[1], op
[2]));
635 case ir_quadop_bitfield_insert
:
636 assert(!"not reached: should be handled by "
637 "lower_instructions::bitfield_insert_to_bfm_bfi");
640 case ir_unop_bit_not
:
641 emit(NOT(this->result
, op
[0]));
643 case ir_binop_bit_and
:
644 emit(AND(this->result
, op
[0], op
[1]));
646 case ir_binop_bit_xor
:
647 emit(XOR(this->result
, op
[0], op
[1]));
649 case ir_binop_bit_or
:
650 emit(OR(this->result
, op
[0], op
[1]));
653 case ir_binop_lshift
:
654 emit(SHL(this->result
, op
[0], op
[1]));
657 case ir_binop_rshift
:
658 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
659 emit(ASR(this->result
, op
[0], op
[1]));
661 emit(SHR(this->result
, op
[0], op
[1]));
663 case ir_binop_pack_half_2x16_split
:
664 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, this->result
, op
[0], op
[1]);
666 case ir_binop_ubo_load
: {
667 /* This IR node takes a constant uniform block and a constant or
668 * variable byte offset within the block and loads a vector from that.
670 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
671 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
672 fs_reg surf_index
= fs_reg((unsigned)SURF_INDEX_WM_UBO(uniform_block
->value
.u
[0]));
674 fs_reg packed_consts
= fs_reg(this, glsl_type::float_type
);
675 packed_consts
.type
= result
.type
;
677 fs_reg const_offset_reg
= fs_reg(const_offset
->value
.u
[0] & ~15);
678 emit(fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
679 packed_consts
, surf_index
, const_offset_reg
));
681 packed_consts
.smear
= const_offset
->value
.u
[0] % 16 / 4;
682 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
683 /* UBO bools are any nonzero value. We consider bools to be
684 * values with the low bit set to 1. Convert them using CMP.
686 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
687 emit(CMP(result
, packed_consts
, fs_reg(0u), BRW_CONDITIONAL_NZ
));
689 emit(MOV(result
, packed_consts
));
692 packed_consts
.smear
++;
695 /* The std140 packing rules don't allow vectors to cross 16-byte
696 * boundaries, and a reg is 32 bytes.
698 assert(packed_consts
.smear
< 8);
701 /* Turn the byte offset into a dword offset. */
702 fs_reg base_offset
= fs_reg(this, glsl_type::int_type
);
703 emit(SHR(base_offset
, op
[1], fs_reg(2)));
705 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
706 emit(VARYING_PULL_CONSTANT_LOAD(result
, surf_index
,
709 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
710 emit(CMP(result
, result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
716 result
.reg_offset
= 0;
721 emit_lrp(this->result
, op
[0], op
[1], op
[2]);
727 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
728 const glsl_type
*type
, bool predicated
)
730 switch (type
->base_type
) {
731 case GLSL_TYPE_FLOAT
:
735 for (unsigned int i
= 0; i
< type
->components(); i
++) {
736 l
.type
= brw_type_for_base_type(type
);
737 r
.type
= brw_type_for_base_type(type
);
739 if (predicated
|| !l
.equals(r
)) {
740 fs_inst
*inst
= emit(MOV(l
, r
));
741 inst
->predicate
= predicated
? BRW_PREDICATE_NORMAL
: BRW_PREDICATE_NONE
;
748 case GLSL_TYPE_ARRAY
:
749 for (unsigned int i
= 0; i
< type
->length
; i
++) {
750 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
754 case GLSL_TYPE_STRUCT
:
755 for (unsigned int i
= 0; i
< type
->length
; i
++) {
756 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
761 case GLSL_TYPE_SAMPLER
:
765 case GLSL_TYPE_ERROR
:
766 case GLSL_TYPE_INTERFACE
:
767 assert(!"not reached");
772 /* If the RHS processing resulted in an instruction generating a
773 * temporary value, and it would be easy to rewrite the instruction to
774 * generate its result right into the LHS instead, do so. This ends
775 * up reliably removing instructions where it can be tricky to do so
776 * later without real UD chain information.
779 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
782 fs_inst
*pre_rhs_inst
,
783 fs_inst
*last_rhs_inst
)
785 /* Only attempt if we're doing a direct assignment. */
787 !(ir
->lhs
->type
->is_scalar() ||
788 (ir
->lhs
->type
->is_vector() &&
789 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1)))
792 /* Make sure the last instruction generated our source reg. */
793 fs_inst
*modify
= get_instruction_generating_reg(pre_rhs_inst
,
799 /* If last_rhs_inst wrote a different number of components than our LHS,
800 * we can't safely rewrite it.
802 if (virtual_grf_sizes
[dst
.reg
] != modify
->regs_written
)
805 /* Success! Rewrite the instruction. */
812 fs_visitor::visit(ir_assignment
*ir
)
817 /* FINISHME: arrays on the lhs */
818 ir
->lhs
->accept(this);
821 fs_inst
*pre_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
823 ir
->rhs
->accept(this);
826 fs_inst
*last_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
828 assert(l
.file
!= BAD_FILE
);
829 assert(r
.file
!= BAD_FILE
);
831 if (try_rewrite_rhs_to_dst(ir
, l
, r
, pre_rhs_inst
, last_rhs_inst
))
835 emit_bool_to_cond_code(ir
->condition
);
838 if (ir
->lhs
->type
->is_scalar() ||
839 ir
->lhs
->type
->is_vector()) {
840 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
841 if (ir
->write_mask
& (1 << i
)) {
842 inst
= emit(MOV(l
, r
));
844 inst
->predicate
= BRW_PREDICATE_NORMAL
;
850 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
855 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
856 fs_reg shadow_c
, fs_reg lod
, fs_reg dPdy
)
866 if (ir
->shadow_comparitor
) {
867 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
868 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
869 coordinate
.reg_offset
++;
871 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
874 if (ir
->op
== ir_tex
) {
875 /* There's no plain shadow compare message, so we use shadow
876 * compare with a bias of 0.0.
878 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
)));
880 } else if (ir
->op
== ir_txb
|| ir
->op
== ir_txl
) {
881 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
884 assert(!"Should not get here.");
887 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
889 } else if (ir
->op
== ir_tex
) {
890 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
891 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
892 coordinate
.reg_offset
++;
894 /* zero the others. */
895 for (int i
= ir
->coordinate
->type
->vector_elements
; i
<3; i
++) {
896 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
898 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
900 } else if (ir
->op
== ir_txd
) {
903 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
904 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
905 coordinate
.reg_offset
++;
907 /* the slots for u and v are always present, but r is optional */
908 mlen
+= MAX2(ir
->coordinate
->type
->vector_elements
, 2);
911 * dPdx = dudx, dvdx, drdx
912 * dPdy = dudy, dvdy, drdy
914 * 1-arg: Does not exist.
916 * 2-arg: dudx dvdx dudy dvdy
917 * dPdx.x dPdx.y dPdy.x dPdy.y
920 * 3-arg: dudx dvdx drdx dudy dvdy drdy
921 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
924 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
925 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdx
));
928 mlen
+= MAX2(ir
->lod_info
.grad
.dPdx
->type
->vector_elements
, 2);
930 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdy
->type
->vector_elements
; i
++) {
931 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdy
));
934 mlen
+= MAX2(ir
->lod_info
.grad
.dPdy
->type
->vector_elements
, 2);
935 } else if (ir
->op
== ir_txs
) {
936 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
938 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
941 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
942 * instructions. We'll need to do SIMD16 here.
945 assert(ir
->op
== ir_txb
|| ir
->op
== ir_txl
|| ir
->op
== ir_txf
);
947 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
948 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2, coordinate
.type
),
950 coordinate
.reg_offset
++;
953 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
954 * be necessary for TXF (ld), but seems wise to do for all messages.
956 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
957 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2), fs_reg(0.0f
)));
960 /* lod/bias appears after u/v/r. */
963 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, lod
.type
), lod
));
966 /* The unused upper half. */
971 /* Now, since we're doing simd16, the return is 2 interleaved
972 * vec4s where the odd-indexed ones are junk. We'll need to move
973 * this weirdness around to the expected layout.
976 dst
= fs_reg(GRF
, virtual_grf_alloc(8),
978 brw_type_for_base_type(ir
->type
) :
979 BRW_REGISTER_TYPE_F
));
982 fs_inst
*inst
= NULL
;
985 inst
= emit(SHADER_OPCODE_TEX
, dst
);
988 inst
= emit(FS_OPCODE_TXB
, dst
);
991 inst
= emit(SHADER_OPCODE_TXL
, dst
);
994 inst
= emit(SHADER_OPCODE_TXD
, dst
);
997 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1000 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1003 fail("unrecognized texture opcode");
1005 inst
->base_mrf
= base_mrf
;
1007 inst
->header_present
= true;
1008 inst
->regs_written
= simd16
? 8 : 4;
1011 for (int i
= 0; i
< 4; i
++) {
1012 emit(MOV(orig_dst
, dst
));
1013 orig_dst
.reg_offset
++;
1014 dst
.reg_offset
+= 2;
1021 /* gen5's sampler has slots for u, v, r, array index, then optional
1022 * parameters like shadow comparitor or LOD bias. If optional
1023 * parameters aren't present, those base slots are optional and don't
1024 * need to be included in the message.
1026 * We don't fill in the unnecessary slots regardless, which may look
1027 * surprising in the disassembly.
1030 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1031 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1032 fs_reg sample_index
)
1036 int reg_width
= dispatch_width
/ 8;
1037 bool header_present
= false;
1038 const int vector_elements
=
1039 ir
->coordinate
? ir
->coordinate
->type
->vector_elements
: 0;
1041 if (ir
->offset
!= NULL
&& ir
->op
== ir_txf
) {
1042 /* It appears that the ld instruction used for txf does its
1043 * address bounds check before adding in the offset. To work
1044 * around this, just add the integer offset to the integer texel
1045 * coordinate, and don't put the offset in the header.
1047 ir_constant
*offset
= ir
->offset
->as_constant();
1048 for (int i
= 0; i
< vector_elements
; i
++) {
1049 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1051 offset
->value
.i
[i
]));
1052 coordinate
.reg_offset
++;
1056 /* The offsets set up by the ir_texture visitor are in the
1057 * m1 header, so we can't go headerless.
1059 header_present
= true;
1064 for (int i
= 0; i
< vector_elements
; i
++) {
1065 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* reg_width
, coordinate
.type
),
1067 coordinate
.reg_offset
++;
1070 mlen
+= vector_elements
* reg_width
;
1072 if (ir
->shadow_comparitor
) {
1073 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1075 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1079 fs_inst
*inst
= NULL
;
1082 inst
= emit(SHADER_OPCODE_TEX
, dst
);
1085 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1086 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1089 inst
= emit(FS_OPCODE_TXB
, dst
);
1092 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
);
1093 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1096 inst
= emit(SHADER_OPCODE_TXL
, dst
);
1099 mlen
= MAX2(mlen
, header_present
+ 4 * reg_width
); /* skip over 'ai' */
1103 * dPdx = dudx, dvdx, drdx
1104 * dPdy = dudy, dvdy, drdy
1106 * Load up these values:
1107 * - dudx dudy dvdx dvdy drdx drdy
1108 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1110 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
1111 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1115 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod2
));
1120 inst
= emit(SHADER_OPCODE_TXD
, dst
);
1124 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1126 inst
= emit(SHADER_OPCODE_TXS
, dst
);
1129 mlen
= header_present
+ 4 * reg_width
;
1130 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), lod
));
1131 inst
= emit(SHADER_OPCODE_TXF
, dst
);
1134 mlen
= header_present
+ 4 * reg_width
;
1137 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
- reg_width
, BRW_REGISTER_TYPE_UD
), fs_reg(0)));
1139 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), sample_index
));
1141 inst
= emit(SHADER_OPCODE_TXF_MS
, dst
);
1144 inst
= emit(SHADER_OPCODE_LOD
, dst
);
1147 inst
->base_mrf
= base_mrf
;
1149 inst
->header_present
= header_present
;
1150 inst
->regs_written
= 4;
1153 fail("Message length >11 disallowed by hardware\n");
1160 fs_visitor::emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1161 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1162 fs_reg sample_index
)
1166 int reg_width
= dispatch_width
/ 8;
1167 bool header_present
= false;
1170 if (ir
->offset
&& ir
->op
!= ir_txf
) {
1171 /* The offsets set up by the ir_texture visitor are in the
1172 * m1 header, so we can't go headerless.
1174 header_present
= true;
1179 if (ir
->shadow_comparitor
) {
1180 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1184 /* Set up the LOD info */
1190 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1194 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1198 if (dispatch_width
== 16)
1199 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1201 /* Load dPdx and the coordinate together:
1202 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1204 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1205 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1206 coordinate
.reg_offset
++;
1209 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1210 * only derivatives for (u, v, r).
1212 if (i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
) {
1213 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1217 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod2
));
1225 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1229 /* It appears that the ld instruction used for txf does its
1230 * address bounds check before adding in the offset. To work
1231 * around this, just add the integer offset to the integer texel
1232 * coordinate, and don't put the offset in the header.
1235 ir_constant
*offset
= ir
->offset
->as_constant();
1236 offsets
[0] = offset
->value
.i
[0];
1237 offsets
[1] = offset
->value
.i
[1];
1238 offsets
[2] = offset
->value
.i
[2];
1240 memset(offsets
, 0, sizeof(offsets
));
1243 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1244 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1245 coordinate
, offsets
[0]));
1246 coordinate
.reg_offset
++;
1249 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
), lod
));
1252 for (int i
= 1; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1253 emit(ADD(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1254 coordinate
, offsets
[i
]));
1255 coordinate
.reg_offset
++;
1260 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), sample_index
));
1263 /* constant zero MCS; we arrange to never actually have a compressed
1264 * multisample surface here for now. TODO: issue ld_mcs to get this first,
1265 * if we ever support texturing from compressed multisample surfaces
1267 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1270 /* there is no offsetting for this message; just copy in the integer
1271 * texture coordinates
1273 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1274 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_D
),
1276 coordinate
.reg_offset
++;
1282 /* Set up the coordinate (except for cases where it was done above) */
1283 if (ir
->op
!= ir_txd
&& ir
->op
!= ir_txs
&& ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
) {
1284 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1285 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1286 coordinate
.reg_offset
++;
1291 /* Generate the SEND */
1292 fs_inst
*inst
= NULL
;
1294 case ir_tex
: inst
= emit(SHADER_OPCODE_TEX
, dst
); break;
1295 case ir_txb
: inst
= emit(FS_OPCODE_TXB
, dst
); break;
1296 case ir_txl
: inst
= emit(SHADER_OPCODE_TXL
, dst
); break;
1297 case ir_txd
: inst
= emit(SHADER_OPCODE_TXD
, dst
); break;
1298 case ir_txf
: inst
= emit(SHADER_OPCODE_TXF
, dst
); break;
1299 case ir_txf_ms
: inst
= emit(SHADER_OPCODE_TXF_MS
, dst
); break;
1300 case ir_txs
: inst
= emit(SHADER_OPCODE_TXS
, dst
); break;
1301 case ir_lod
: inst
= emit(SHADER_OPCODE_LOD
, dst
); break;
1303 inst
->base_mrf
= base_mrf
;
1305 inst
->header_present
= header_present
;
1306 inst
->regs_written
= 4;
1309 fail("Message length >11 disallowed by hardware\n");
1316 fs_visitor::rescale_texcoord(ir_texture
*ir
, fs_reg coordinate
,
1317 bool is_rect
, int sampler
, int texunit
)
1319 fs_inst
*inst
= NULL
;
1320 bool needs_gl_clamp
= true;
1321 fs_reg scale_x
, scale_y
;
1323 /* The 965 requires the EU to do the normalization of GL rectangle
1324 * texture coordinates. We use the program parameter state
1325 * tracking to get the scaling factor.
1329 (brw
->gen
>= 6 && (c
->key
.tex
.gl_clamp_mask
[0] & (1 << sampler
) ||
1330 c
->key
.tex
.gl_clamp_mask
[1] & (1 << sampler
))))) {
1331 struct gl_program_parameter_list
*params
= fp
->Base
.Parameters
;
1332 int tokens
[STATE_LENGTH
] = {
1334 STATE_TEXRECT_SCALE
,
1340 if (dispatch_width
== 16) {
1341 fail("rectangle scale uniform setup not supported on 16-wide\n");
1345 scale_x
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
);
1346 scale_y
= fs_reg(UNIFORM
, c
->prog_data
.nr_params
+ 1);
1348 GLuint index
= _mesa_add_state_reference(params
,
1349 (gl_state_index
*)tokens
);
1350 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1351 &fp
->Base
.Parameters
->ParameterValues
[index
][0].f
;
1352 c
->prog_data
.param
[c
->prog_data
.nr_params
++] =
1353 &fp
->Base
.Parameters
->ParameterValues
[index
][1].f
;
1356 /* The 965 requires the EU to do the normalization of GL rectangle
1357 * texture coordinates. We use the program parameter state
1358 * tracking to get the scaling factor.
1360 if (brw
->gen
< 6 && is_rect
) {
1361 fs_reg dst
= fs_reg(this, ir
->coordinate
->type
);
1362 fs_reg src
= coordinate
;
1365 emit(MUL(dst
, src
, scale_x
));
1368 emit(MUL(dst
, src
, scale_y
));
1369 } else if (is_rect
) {
1370 /* On gen6+, the sampler handles the rectangle coordinates
1371 * natively, without needing rescaling. But that means we have
1372 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
1373 * not [0, 1] like the default case below.
1375 needs_gl_clamp
= false;
1377 for (int i
= 0; i
< 2; i
++) {
1378 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1379 fs_reg chan
= coordinate
;
1380 chan
.reg_offset
+= i
;
1382 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, brw_imm_f(0.0));
1383 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1385 /* Our parameter comes in as 1.0/width or 1.0/height,
1386 * because that's what people normally want for doing
1387 * texture rectangle handling. We need width or height
1388 * for clamping, but we don't care enough to make a new
1389 * parameter type, so just invert back.
1391 fs_reg limit
= fs_reg(this, glsl_type::float_type
);
1392 emit(MOV(limit
, i
== 0 ? scale_x
: scale_y
));
1393 emit(SHADER_OPCODE_RCP
, limit
, limit
);
1395 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, limit
);
1396 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1401 if (ir
->coordinate
&& needs_gl_clamp
) {
1402 for (unsigned int i
= 0;
1403 i
< MIN2(ir
->coordinate
->type
->vector_elements
, 3); i
++) {
1404 if (c
->key
.tex
.gl_clamp_mask
[i
] & (1 << sampler
)) {
1405 fs_reg chan
= coordinate
;
1406 chan
.reg_offset
+= i
;
1408 fs_inst
*inst
= emit(MOV(chan
, chan
));
1409 inst
->saturate
= true;
1417 fs_visitor::visit(ir_texture
*ir
)
1419 fs_inst
*inst
= NULL
;
1422 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, &fp
->Base
);
1423 /* FINISHME: We're failing to recompile our programs when the sampler is
1424 * updated. This only matters for the texture rectangle scale parameters
1425 * (pre-gen6, or gen6+ with GL_CLAMP).
1427 int texunit
= fp
->Base
.SamplerUnits
[sampler
];
1429 /* Should be lowered by do_lower_texture_projection */
1430 assert(!ir
->projector
);
1432 /* Generate code to compute all the subexpression trees. This has to be
1433 * done before loading any values into MRFs for the sampler message since
1434 * generating these values may involve SEND messages that need the MRFs.
1437 if (ir
->coordinate
) {
1438 ir
->coordinate
->accept(this);
1440 coordinate
= rescale_texcoord(ir
, this->result
,
1441 ir
->sampler
->type
->sampler_dimensionality
==
1442 GLSL_SAMPLER_DIM_RECT
,
1446 fs_reg shadow_comparitor
;
1447 if (ir
->shadow_comparitor
) {
1448 ir
->shadow_comparitor
->accept(this);
1449 shadow_comparitor
= this->result
;
1452 fs_reg lod
, lod2
, sample_index
;
1458 ir
->lod_info
.bias
->accept(this);
1462 ir
->lod_info
.grad
.dPdx
->accept(this);
1465 ir
->lod_info
.grad
.dPdy
->accept(this);
1466 lod2
= this->result
;
1471 ir
->lod_info
.lod
->accept(this);
1475 ir
->lod_info
.sample_index
->accept(this);
1476 sample_index
= this->result
;
1480 /* Writemasking doesn't eliminate channels on SIMD8 texture
1481 * samples, so don't worry about them.
1483 fs_reg dst
= fs_reg(this, glsl_type::get_instance(ir
->type
->base_type
, 4, 1));
1485 if (brw
->gen
>= 7) {
1486 inst
= emit_texture_gen7(ir
, dst
, coordinate
, shadow_comparitor
,
1487 lod
, lod2
, sample_index
);
1488 } else if (brw
->gen
>= 5) {
1489 inst
= emit_texture_gen5(ir
, dst
, coordinate
, shadow_comparitor
,
1490 lod
, lod2
, sample_index
);
1492 inst
= emit_texture_gen4(ir
, dst
, coordinate
, shadow_comparitor
,
1496 /* The header is set up by generate_tex() when necessary. */
1497 inst
->src
[0] = reg_undef
;
1499 if (ir
->offset
!= NULL
&& ir
->op
!= ir_txf
)
1500 inst
->texture_offset
= brw_texture_offset(ir
->offset
->as_constant());
1502 inst
->sampler
= sampler
;
1504 if (ir
->shadow_comparitor
)
1505 inst
->shadow_compare
= true;
1507 /* fixup #layers for cube map arrays */
1508 if (ir
->op
== ir_txs
) {
1509 glsl_type
const *type
= ir
->sampler
->type
;
1510 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
1511 type
->sampler_array
) {
1513 depth
.reg_offset
= 2;
1514 emit_math(SHADER_OPCODE_INT_QUOTIENT
, depth
, depth
, fs_reg(6));
1518 swizzle_result(ir
, dst
, sampler
);
1522 * Swizzle the result of a texture result. This is necessary for
1523 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1526 fs_visitor::swizzle_result(ir_texture
*ir
, fs_reg orig_val
, int sampler
)
1528 this->result
= orig_val
;
1530 if (ir
->op
== ir_txs
|| ir
->op
== ir_lod
)
1533 if (ir
->type
== glsl_type::float_type
) {
1534 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1535 assert(ir
->sampler
->type
->sampler_shadow
);
1536 } else if (c
->key
.tex
.swizzles
[sampler
] != SWIZZLE_NOOP
) {
1537 fs_reg swizzled_result
= fs_reg(this, glsl_type::vec4_type
);
1539 for (int i
= 0; i
< 4; i
++) {
1540 int swiz
= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1541 fs_reg l
= swizzled_result
;
1544 if (swiz
== SWIZZLE_ZERO
) {
1545 emit(MOV(l
, fs_reg(0.0f
)));
1546 } else if (swiz
== SWIZZLE_ONE
) {
1547 emit(MOV(l
, fs_reg(1.0f
)));
1549 fs_reg r
= orig_val
;
1550 r
.reg_offset
+= GET_SWZ(c
->key
.tex
.swizzles
[sampler
], i
);
1554 this->result
= swizzled_result
;
1559 fs_visitor::visit(ir_swizzle
*ir
)
1561 ir
->val
->accept(this);
1562 fs_reg val
= this->result
;
1564 if (ir
->type
->vector_elements
== 1) {
1565 this->result
.reg_offset
+= ir
->mask
.x
;
1569 fs_reg result
= fs_reg(this, ir
->type
);
1570 this->result
= result
;
1572 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1573 fs_reg channel
= val
;
1591 channel
.reg_offset
+= swiz
;
1592 emit(MOV(result
, channel
));
1593 result
.reg_offset
++;
1598 fs_visitor::visit(ir_discard
*ir
)
1600 assert(ir
->condition
== NULL
); /* FINISHME */
1602 /* We track our discarded pixels in f0.1. By predicating on it, we can
1603 * update just the flag bits that aren't yet discarded. By emitting a
1604 * CMP of g0 != g0, all our currently executing channels will get turned
1607 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
1608 BRW_REGISTER_TYPE_UW
));
1609 fs_inst
*cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
1610 BRW_CONDITIONAL_NZ
));
1611 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
1612 cmp
->flag_subreg
= 1;
1614 if (brw
->gen
>= 6) {
1615 /* For performance, after a discard, jump to the end of the shader.
1616 * However, many people will do foliage by discarding based on a
1617 * texture's alpha mask, and then continue on to texture with the
1618 * remaining pixels. To avoid trashing the derivatives for those
1619 * texture samples, we'll only jump if all of the pixels in the subspan
1620 * have been discarded.
1622 fs_inst
*discard_jump
= emit(FS_OPCODE_DISCARD_JUMP
);
1623 discard_jump
->flag_subreg
= 1;
1624 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1625 discard_jump
->predicate_inverse
= true;
1630 fs_visitor::visit(ir_constant
*ir
)
1632 /* Set this->result to reg at the bottom of the function because some code
1633 * paths will cause this visitor to be applied to other fields. This will
1634 * cause the value stored in this->result to be modified.
1636 * Make reg constant so that it doesn't get accidentally modified along the
1637 * way. Yes, I actually had this problem. :(
1639 const fs_reg
reg(this, ir
->type
);
1640 fs_reg dst_reg
= reg
;
1642 if (ir
->type
->is_array()) {
1643 const unsigned size
= type_size(ir
->type
->fields
.array
);
1645 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
1646 ir
->array_elements
[i
]->accept(this);
1647 fs_reg src_reg
= this->result
;
1649 dst_reg
.type
= src_reg
.type
;
1650 for (unsigned j
= 0; j
< size
; j
++) {
1651 emit(MOV(dst_reg
, src_reg
));
1652 src_reg
.reg_offset
++;
1653 dst_reg
.reg_offset
++;
1656 } else if (ir
->type
->is_record()) {
1657 foreach_list(node
, &ir
->components
) {
1658 ir_constant
*const field
= (ir_constant
*) node
;
1659 const unsigned size
= type_size(field
->type
);
1661 field
->accept(this);
1662 fs_reg src_reg
= this->result
;
1664 dst_reg
.type
= src_reg
.type
;
1665 for (unsigned j
= 0; j
< size
; j
++) {
1666 emit(MOV(dst_reg
, src_reg
));
1667 src_reg
.reg_offset
++;
1668 dst_reg
.reg_offset
++;
1672 const unsigned size
= type_size(ir
->type
);
1674 for (unsigned i
= 0; i
< size
; i
++) {
1675 switch (ir
->type
->base_type
) {
1676 case GLSL_TYPE_FLOAT
:
1677 emit(MOV(dst_reg
, fs_reg(ir
->value
.f
[i
])));
1679 case GLSL_TYPE_UINT
:
1680 emit(MOV(dst_reg
, fs_reg(ir
->value
.u
[i
])));
1683 emit(MOV(dst_reg
, fs_reg(ir
->value
.i
[i
])));
1685 case GLSL_TYPE_BOOL
:
1686 emit(MOV(dst_reg
, fs_reg((int)ir
->value
.b
[i
])));
1689 assert(!"Non-float/uint/int/bool constant");
1691 dst_reg
.reg_offset
++;
1699 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
1701 ir_expression
*expr
= ir
->as_expression();
1707 assert(expr
->get_num_operands() <= 2);
1708 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1709 assert(expr
->operands
[i
]->type
->is_scalar());
1711 expr
->operands
[i
]->accept(this);
1712 op
[i
] = this->result
;
1714 resolve_ud_negate(&op
[i
]);
1717 switch (expr
->operation
) {
1718 case ir_unop_logic_not
:
1719 inst
= emit(AND(reg_null_d
, op
[0], fs_reg(1)));
1720 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1723 case ir_binop_logic_xor
:
1724 case ir_binop_logic_or
:
1725 case ir_binop_logic_and
:
1729 if (brw
->gen
>= 6) {
1730 emit(CMP(reg_null_d
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1732 inst
= emit(MOV(reg_null_f
, op
[0]));
1733 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1738 if (brw
->gen
>= 6) {
1739 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1741 inst
= emit(MOV(reg_null_d
, op
[0]));
1742 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1746 case ir_binop_greater
:
1747 case ir_binop_gequal
:
1749 case ir_binop_lequal
:
1750 case ir_binop_equal
:
1751 case ir_binop_all_equal
:
1752 case ir_binop_nequal
:
1753 case ir_binop_any_nequal
:
1754 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1755 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1757 emit(CMP(reg_null_d
, op
[0], op
[1],
1758 brw_conditional_for_comparison(expr
->operation
)));
1762 assert(!"not reached");
1763 fail("bad cond code\n");
1772 fs_inst
*inst
= emit(AND(reg_null_d
, this->result
, fs_reg(1)));
1773 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1777 * Emit a gen6 IF statement with the comparison folded into the IF
1781 fs_visitor::emit_if_gen6(ir_if
*ir
)
1783 ir_expression
*expr
= ir
->condition
->as_expression();
1790 assert(expr
->get_num_operands() <= 2);
1791 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
1792 assert(expr
->operands
[i
]->type
->is_scalar());
1794 expr
->operands
[i
]->accept(this);
1795 op
[i
] = this->result
;
1798 switch (expr
->operation
) {
1799 case ir_unop_logic_not
:
1800 case ir_binop_logic_xor
:
1801 case ir_binop_logic_or
:
1802 case ir_binop_logic_and
:
1803 /* For operations on bool arguments, only the low bit of the bool is
1804 * valid, and the others are undefined. Fall back to the condition
1810 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
1811 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1815 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1818 case ir_binop_greater
:
1819 case ir_binop_gequal
:
1821 case ir_binop_lequal
:
1822 case ir_binop_equal
:
1823 case ir_binop_all_equal
:
1824 case ir_binop_nequal
:
1825 case ir_binop_any_nequal
:
1826 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
1827 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
1829 emit(IF(op
[0], op
[1],
1830 brw_conditional_for_comparison(expr
->operation
)));
1833 assert(!"not reached");
1834 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1835 fail("bad condition\n");
1840 emit_bool_to_cond_code(ir
->condition
);
1841 fs_inst
*inst
= emit(BRW_OPCODE_IF
);
1842 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1846 fs_visitor::visit(ir_if
*ir
)
1848 if (brw
->gen
< 6 && dispatch_width
== 16) {
1849 fail("Can't support (non-uniform) control flow on 16-wide\n");
1852 /* Don't point the annotation at the if statement, because then it plus
1853 * the then and else blocks get printed.
1855 this->base_ir
= ir
->condition
;
1857 if (brw
->gen
== 6) {
1860 emit_bool_to_cond_code(ir
->condition
);
1862 emit(IF(BRW_PREDICATE_NORMAL
));
1865 foreach_list(node
, &ir
->then_instructions
) {
1866 ir_instruction
*ir
= (ir_instruction
*)node
;
1872 if (!ir
->else_instructions
.is_empty()) {
1873 emit(BRW_OPCODE_ELSE
);
1875 foreach_list(node
, &ir
->else_instructions
) {
1876 ir_instruction
*ir
= (ir_instruction
*)node
;
1883 emit(BRW_OPCODE_ENDIF
);
1887 fs_visitor::visit(ir_loop
*ir
)
1889 fs_reg counter
= reg_undef
;
1891 if (brw
->gen
< 6 && dispatch_width
== 16) {
1892 fail("Can't support (non-uniform) control flow on 16-wide\n");
1896 this->base_ir
= ir
->counter
;
1897 ir
->counter
->accept(this);
1898 counter
= *(variable_storage(ir
->counter
));
1901 this->base_ir
= ir
->from
;
1902 ir
->from
->accept(this);
1904 emit(MOV(counter
, this->result
));
1908 this->base_ir
= NULL
;
1909 emit(BRW_OPCODE_DO
);
1912 this->base_ir
= ir
->to
;
1913 ir
->to
->accept(this);
1915 emit(CMP(reg_null_d
, counter
, this->result
,
1916 brw_conditional_for_comparison(ir
->cmp
)));
1918 fs_inst
*inst
= emit(BRW_OPCODE_BREAK
);
1919 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1922 foreach_list(node
, &ir
->body_instructions
) {
1923 ir_instruction
*ir
= (ir_instruction
*)node
;
1929 if (ir
->increment
) {
1930 this->base_ir
= ir
->increment
;
1931 ir
->increment
->accept(this);
1932 emit(ADD(counter
, counter
, this->result
));
1935 this->base_ir
= NULL
;
1936 emit(BRW_OPCODE_WHILE
);
1940 fs_visitor::visit(ir_loop_jump
*ir
)
1943 case ir_loop_jump::jump_break
:
1944 emit(BRW_OPCODE_BREAK
);
1946 case ir_loop_jump::jump_continue
:
1947 emit(BRW_OPCODE_CONTINUE
);
1953 fs_visitor::visit(ir_call
*ir
)
1955 assert(!"FINISHME");
1959 fs_visitor::visit(ir_return
*ir
)
1961 assert(!"FINISHME");
1965 fs_visitor::visit(ir_function
*ir
)
1967 /* Ignore function bodies other than main() -- we shouldn't see calls to
1968 * them since they should all be inlined before we get to ir_to_mesa.
1970 if (strcmp(ir
->name
, "main") == 0) {
1971 const ir_function_signature
*sig
;
1974 sig
= ir
->matching_signature(&empty
);
1978 foreach_list(node
, &sig
->body
) {
1979 ir_instruction
*ir
= (ir_instruction
*)node
;
1988 fs_visitor::visit(ir_function_signature
*ir
)
1990 assert(!"not reached");
1995 fs_visitor::emit(fs_inst inst
)
1997 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
2004 fs_visitor::emit(fs_inst
*inst
)
2006 if (force_uncompressed_stack
> 0)
2007 inst
->force_uncompressed
= true;
2008 else if (force_sechalf_stack
> 0)
2009 inst
->force_sechalf
= true;
2011 inst
->annotation
= this->current_annotation
;
2012 inst
->ir
= this->base_ir
;
2014 this->instructions
.push_tail(inst
);
2020 fs_visitor::emit(exec_list list
)
2022 foreach_list_safe(node
, &list
) {
2023 fs_inst
*inst
= (fs_inst
*)node
;
2029 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
2031 fs_visitor::emit_dummy_fs()
2033 int reg_width
= dispatch_width
/ 8;
2035 /* Everyone's favorite color. */
2036 emit(MOV(fs_reg(MRF
, 2 + 0 * reg_width
), fs_reg(1.0f
)));
2037 emit(MOV(fs_reg(MRF
, 2 + 1 * reg_width
), fs_reg(0.0f
)));
2038 emit(MOV(fs_reg(MRF
, 2 + 2 * reg_width
), fs_reg(1.0f
)));
2039 emit(MOV(fs_reg(MRF
, 2 + 3 * reg_width
), fs_reg(0.0f
)));
2042 write
= emit(FS_OPCODE_FB_WRITE
, fs_reg(0), fs_reg(0));
2043 write
->base_mrf
= 2;
2044 write
->mlen
= 4 * reg_width
;
2048 /* The register location here is relative to the start of the URB
2049 * data. It will get adjusted to be a real location before
2050 * generate_code() time.
2053 fs_visitor::interp_reg(int location
, int channel
)
2055 int regnr
= urb_setup
[location
] * 2 + channel
/ 2;
2056 int stride
= (channel
& 1) * 4;
2058 assert(urb_setup
[location
] != -1);
2060 return brw_vec1_grf(regnr
, stride
);
2063 /** Emits the interpolation for the varying inputs. */
2065 fs_visitor::emit_interpolation_setup_gen4()
2067 this->current_annotation
= "compute pixel centers";
2068 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
2069 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
2070 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2071 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2073 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
2074 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
2076 this->current_annotation
= "compute pixel deltas from v0";
2078 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2079 fs_reg(this, glsl_type::vec2_type
);
2080 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2081 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
];
2082 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
].reg_offset
++;
2084 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2085 fs_reg(this, glsl_type::float_type
);
2086 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2087 fs_reg(this, glsl_type::float_type
);
2089 emit(ADD(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2090 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0)))));
2091 emit(ADD(this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2092 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1)))));
2094 this->current_annotation
= "compute pos.w and 1/pos.w";
2095 /* Compute wpos.w. It's always in our setup, since it's needed to
2096 * interpolate the other attributes.
2098 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2099 emit(FS_OPCODE_LINTERP
, wpos_w
,
2100 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2101 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2102 interp_reg(VARYING_SLOT_POS
, 3));
2103 /* Compute the pixel 1/W value from wpos.w. */
2104 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
2105 emit_math(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
2106 this->current_annotation
= NULL
;
2109 /** Emits the interpolation for the varying inputs. */
2111 fs_visitor::emit_interpolation_setup_gen6()
2113 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
2115 /* If the pixel centers end up used, the setup is the same as for gen4. */
2116 this->current_annotation
= "compute pixel centers";
2117 fs_reg int_pixel_x
= fs_reg(this, glsl_type::uint_type
);
2118 fs_reg int_pixel_y
= fs_reg(this, glsl_type::uint_type
);
2119 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2120 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2121 emit(ADD(int_pixel_x
,
2122 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
2123 fs_reg(brw_imm_v(0x10101010))));
2124 emit(ADD(int_pixel_y
,
2125 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
2126 fs_reg(brw_imm_v(0x11001100))));
2128 /* As of gen6, we can no longer mix float and int sources. We have
2129 * to turn the integer pixel centers into floats for their actual
2132 this->pixel_x
= fs_reg(this, glsl_type::float_type
);
2133 this->pixel_y
= fs_reg(this, glsl_type::float_type
);
2134 emit(MOV(this->pixel_x
, int_pixel_x
));
2135 emit(MOV(this->pixel_y
, int_pixel_y
));
2137 this->current_annotation
= "compute pos.w";
2138 this->pixel_w
= fs_reg(brw_vec8_grf(c
->source_w_reg
, 0));
2139 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2140 emit_math(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
2142 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
2143 uint8_t reg
= c
->barycentric_coord_reg
[i
];
2144 this->delta_x
[i
] = fs_reg(brw_vec8_grf(reg
, 0));
2145 this->delta_y
[i
] = fs_reg(brw_vec8_grf(reg
+ 1, 0));
2148 this->current_annotation
= NULL
;
2152 fs_visitor::emit_color_write(int target
, int index
, int first_color_mrf
)
2154 int reg_width
= dispatch_width
/ 8;
2156 fs_reg color
= outputs
[target
];
2159 /* If there's no color data to be written, skip it. */
2160 if (color
.file
== BAD_FILE
)
2163 color
.reg_offset
+= index
;
2165 if (dispatch_width
== 8 || brw
->gen
>= 6) {
2166 /* SIMD8 write looks like:
2172 * gen6 SIMD16 DP write looks like:
2182 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
* reg_width
,
2185 inst
->saturate
= c
->key
.clamp_fragment_color
;
2187 /* pre-gen6 SIMD16 single source DP write looks like:
2197 if (brw
->has_compr4
) {
2198 /* By setting the high bit of the MRF register number, we
2199 * indicate that we want COMPR4 mode - instead of doing the
2200 * usual destination + 1 for the second half we get
2203 inst
= emit(MOV(fs_reg(MRF
, BRW_MRF_COMPR4
+ first_color_mrf
+ index
,
2206 inst
->saturate
= c
->key
.clamp_fragment_color
;
2208 push_force_uncompressed();
2209 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
, color
.type
),
2211 inst
->saturate
= c
->key
.clamp_fragment_color
;
2212 pop_force_uncompressed();
2214 push_force_sechalf();
2215 color
.sechalf
= true;
2216 inst
= emit(MOV(fs_reg(MRF
, first_color_mrf
+ index
+ 4, color
.type
),
2218 inst
->saturate
= c
->key
.clamp_fragment_color
;
2219 pop_force_sechalf();
2220 color
.sechalf
= false;
2226 fs_visitor::emit_fb_writes()
2228 this->current_annotation
= "FB write header";
2229 bool header_present
= true;
2230 /* We can potentially have a message length of up to 15, so we have to set
2231 * base_mrf to either 0 or 1 in order to fit in m0..m15.
2235 int reg_width
= dispatch_width
/ 8;
2236 bool do_dual_src
= this->dual_src_output
.file
!= BAD_FILE
;
2237 bool src0_alpha_to_render_target
= false;
2239 if (dispatch_width
== 16 && do_dual_src
) {
2240 fail("GL_ARB_blend_func_extended not yet supported in 16-wide.");
2241 do_dual_src
= false;
2244 /* From the Sandy Bridge PRM, volume 4, page 198:
2246 * "Dispatched Pixel Enables. One bit per pixel indicating
2247 * which pixels were originally enabled when the thread was
2248 * dispatched. This field is only required for the end-of-
2249 * thread message and on all dual-source messages."
2251 if (brw
->gen
>= 6 &&
2252 !this->fp
->UsesKill
&&
2254 c
->key
.nr_color_regions
== 1) {
2255 header_present
= false;
2258 if (header_present
) {
2259 src0_alpha_to_render_target
= brw
->gen
>= 6 &&
2261 c
->key
.replicate_alpha
;
2266 if (c
->aa_dest_stencil_reg
) {
2267 push_force_uncompressed();
2268 emit(MOV(fs_reg(MRF
, nr
++),
2269 fs_reg(brw_vec8_grf(c
->aa_dest_stencil_reg
, 0))));
2270 pop_force_uncompressed();
2273 /* Reserve space for color. It'll be filled in per MRT below. */
2275 nr
+= 4 * reg_width
;
2278 if (src0_alpha_to_render_target
)
2281 if (c
->source_depth_to_render_target
) {
2282 if (brw
->gen
== 6 && dispatch_width
== 16) {
2283 /* For outputting oDepth on gen6, SIMD8 writes have to be
2284 * used. This would require 8-wide moves of each half to
2285 * message regs, kind of like pre-gen5 SIMD16 FB writes.
2286 * Just bail on doing so for now.
2288 fail("Missing support for simd16 depth writes on gen6\n");
2291 if (fp
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
2292 /* Hand over gl_FragDepth. */
2293 assert(this->frag_depth
.file
!= BAD_FILE
);
2294 emit(MOV(fs_reg(MRF
, nr
), this->frag_depth
));
2296 /* Pass through the payload depth. */
2297 emit(MOV(fs_reg(MRF
, nr
),
2298 fs_reg(brw_vec8_grf(c
->source_depth_reg
, 0))));
2303 if (c
->dest_depth_reg
) {
2304 emit(MOV(fs_reg(MRF
, nr
),
2305 fs_reg(brw_vec8_grf(c
->dest_depth_reg
, 0))));
2310 fs_reg src0
= this->outputs
[0];
2311 fs_reg src1
= this->dual_src_output
;
2313 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2315 for (int i
= 0; i
< 4; i
++) {
2316 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ i
, src0
.type
), src0
));
2318 inst
->saturate
= c
->key
.clamp_fragment_color
;
2321 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2323 for (int i
= 0; i
< 4; i
++) {
2324 fs_inst
*inst
= emit(MOV(fs_reg(MRF
, color_mrf
+ 4 + i
, src1
.type
),
2327 inst
->saturate
= c
->key
.clamp_fragment_color
;
2330 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2331 emit_shader_time_end();
2333 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2335 inst
->base_mrf
= base_mrf
;
2336 inst
->mlen
= nr
- base_mrf
;
2338 inst
->header_present
= header_present
;
2340 c
->prog_data
.dual_src_blend
= true;
2341 this->current_annotation
= NULL
;
2345 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
2346 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
2347 "FB write target %d",
2349 /* If src0_alpha_to_render_target is true, include source zero alpha
2350 * data in RenderTargetWrite message for targets > 0.
2352 int write_color_mrf
= color_mrf
;
2353 if (src0_alpha_to_render_target
&& target
!= 0) {
2355 fs_reg color
= outputs
[0];
2356 color
.reg_offset
+= 3;
2358 inst
= emit(MOV(fs_reg(MRF
, write_color_mrf
, color
.type
),
2360 inst
->saturate
= c
->key
.clamp_fragment_color
;
2361 write_color_mrf
= color_mrf
+ reg_width
;
2364 for (unsigned i
= 0; i
< this->output_components
[target
]; i
++)
2365 emit_color_write(target
, i
, write_color_mrf
);
2368 if (target
== c
->key
.nr_color_regions
- 1) {
2371 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2372 emit_shader_time_end();
2375 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2376 inst
->target
= target
;
2377 inst
->base_mrf
= base_mrf
;
2378 if (src0_alpha_to_render_target
&& target
== 0)
2379 inst
->mlen
= nr
- base_mrf
- reg_width
;
2381 inst
->mlen
= nr
- base_mrf
;
2383 inst
->header_present
= header_present
;
2386 if (c
->key
.nr_color_regions
== 0) {
2387 /* Even if there's no color buffers enabled, we still need to send
2388 * alpha out the pipeline to our null renderbuffer to support
2389 * alpha-testing, alpha-to-coverage, and so on.
2391 emit_color_write(0, 3, color_mrf
);
2393 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
2394 emit_shader_time_end();
2396 fs_inst
*inst
= emit(FS_OPCODE_FB_WRITE
);
2397 inst
->base_mrf
= base_mrf
;
2398 inst
->mlen
= nr
- base_mrf
;
2400 inst
->header_present
= header_present
;
2403 this->current_annotation
= NULL
;
2407 fs_visitor::resolve_ud_negate(fs_reg
*reg
)
2409 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
2413 fs_reg temp
= fs_reg(this, glsl_type::uint_type
);
2414 emit(MOV(temp
, *reg
));
2419 fs_visitor::resolve_bool_comparison(ir_rvalue
*rvalue
, fs_reg
*reg
)
2421 if (rvalue
->type
!= glsl_type::bool_type
)
2424 fs_reg temp
= fs_reg(this, glsl_type::bool_type
);
2425 emit(AND(temp
, *reg
, fs_reg(1)));
2429 fs_visitor::fs_visitor(struct brw_context
*brw
,
2430 struct brw_wm_compile
*c
,
2431 struct gl_shader_program
*shader_prog
,
2432 struct gl_fragment_program
*fp
,
2433 unsigned dispatch_width
)
2434 : dispatch_width(dispatch_width
)
2439 this->shader_prog
= shader_prog
;
2440 this->ctx
= &brw
->ctx
;
2441 this->mem_ctx
= ralloc_context(NULL
);
2443 shader
= (struct brw_shader
*)
2444 shader_prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
2447 this->failed
= false;
2448 this->variable_ht
= hash_table_ctor(0,
2449 hash_table_pointer_hash
,
2450 hash_table_pointer_compare
);
2452 memset(this->outputs
, 0, sizeof(this->outputs
));
2453 memset(this->output_components
, 0, sizeof(this->output_components
));
2454 this->first_non_payload_grf
= 0;
2455 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
2457 this->current_annotation
= NULL
;
2458 this->base_ir
= NULL
;
2460 this->virtual_grf_sizes
= NULL
;
2461 this->virtual_grf_count
= 0;
2462 this->virtual_grf_array_size
= 0;
2463 this->virtual_grf_start
= NULL
;
2464 this->virtual_grf_end
= NULL
;
2465 this->live_intervals_valid
= false;
2467 this->params_remap
= NULL
;
2468 this->nr_params_remap
= 0;
2470 this->force_uncompressed_stack
= 0;
2471 this->force_sechalf_stack
= 0;
2473 memset(&this->param_size
, 0, sizeof(this->param_size
));
2476 fs_visitor::~fs_visitor()
2478 ralloc_free(this->mem_ctx
);
2479 hash_table_dtor(this->variable_ht
);