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 "util/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
->data
.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();
65 reg
= emit_general_interpolation(ir
);
68 hash_table_insert(this->variable_ht
, reg
, ir
);
70 } else if (ir
->data
.mode
== ir_var_shader_out
) {
71 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
73 if (ir
->data
.index
> 0) {
74 assert(ir
->data
.location
== FRAG_RESULT_DATA0
);
75 assert(ir
->data
.index
== 1);
76 this->dual_src_output
= *reg
;
77 this->do_dual_src
= true;
78 } else if (ir
->data
.location
== FRAG_RESULT_COLOR
) {
79 /* Writing gl_FragColor outputs to all color regions. */
80 assert(stage
== MESA_SHADER_FRAGMENT
);
81 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
82 for (unsigned int i
= 0; i
< MAX2(key
->nr_color_regions
, 1); i
++) {
83 this->outputs
[i
] = *reg
;
84 this->output_components
[i
] = 4;
86 } else if (ir
->data
.location
== FRAG_RESULT_DEPTH
) {
87 this->frag_depth
= *reg
;
88 } else if (ir
->data
.location
== FRAG_RESULT_SAMPLE_MASK
) {
89 this->sample_mask
= *reg
;
91 /* gl_FragData or a user-defined FS output */
92 assert(ir
->data
.location
>= FRAG_RESULT_DATA0
&&
93 ir
->data
.location
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
);
96 ir
->type
->is_array() ? ir
->type
->fields
.array
->vector_elements
97 : ir
->type
->vector_elements
;
99 /* General color output. */
100 for (unsigned int i
= 0; i
< MAX2(1, ir
->type
->length
); i
++) {
101 int output
= ir
->data
.location
- FRAG_RESULT_DATA0
+ i
;
102 this->outputs
[output
] = offset(*reg
, vector_elements
* i
);
103 this->output_components
[output
] = vector_elements
;
106 } else if (ir
->data
.mode
== ir_var_uniform
) {
107 int param_index
= uniforms
;
109 /* Thanks to the lower_ubo_reference pass, we will see only
110 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
111 * variables, so no need for them to be in variable_ht.
113 * Some uniforms, such as samplers and atomic counters, have no actual
114 * storage, so we should ignore them.
116 if (ir
->is_in_uniform_block() || type_size(ir
->type
) == 0)
119 if (dispatch_width
== 16) {
120 if (!variable_storage(ir
)) {
121 fail("Failed to find uniform '%s' in SIMD16\n", ir
->name
);
126 param_size
[param_index
] = type_size(ir
->type
);
127 if (!strncmp(ir
->name
, "gl_", 3)) {
128 setup_builtin_uniform_values(ir
);
130 setup_uniform_values(ir
);
133 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
134 reg
->type
= brw_type_for_base_type(ir
->type
);
136 } else if (ir
->data
.mode
== ir_var_system_value
) {
137 if (ir
->data
.location
== SYSTEM_VALUE_SAMPLE_POS
) {
138 reg
= emit_samplepos_setup();
139 } else if (ir
->data
.location
== SYSTEM_VALUE_SAMPLE_ID
) {
140 reg
= emit_sampleid_setup(ir
);
141 } else if (ir
->data
.location
== SYSTEM_VALUE_SAMPLE_MASK_IN
) {
142 assert(brw
->gen
>= 7);
144 fs_reg(retype(brw_vec8_grf(payload
.sample_mask_in_reg
, 0),
145 BRW_REGISTER_TYPE_D
));
150 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
152 hash_table_insert(this->variable_ht
, reg
, ir
);
156 fs_visitor::visit(ir_dereference_variable
*ir
)
158 fs_reg
*reg
= variable_storage(ir
->var
);
161 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
162 this->result
= fs_reg(reg_null_d
);
169 fs_visitor::visit(ir_dereference_record
*ir
)
171 const glsl_type
*struct_type
= ir
->record
->type
;
173 ir
->record
->accept(this);
175 unsigned int off
= 0;
176 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
177 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
179 off
+= type_size(struct_type
->fields
.structure
[i
].type
);
181 this->result
= offset(this->result
, off
);
182 this->result
.type
= brw_type_for_base_type(ir
->type
);
186 fs_visitor::visit(ir_dereference_array
*ir
)
188 ir_constant
*constant_index
;
190 int element_size
= type_size(ir
->type
);
192 constant_index
= ir
->array_index
->as_constant();
194 ir
->array
->accept(this);
196 src
.type
= brw_type_for_base_type(ir
->type
);
198 if (constant_index
) {
199 assert(src
.file
== UNIFORM
|| src
.file
== GRF
|| src
.file
== HW_REG
);
200 src
= offset(src
, constant_index
->value
.i
[0] * element_size
);
202 /* Variable index array dereference. We attach the variable index
203 * component to the reg as a pointer to a register containing the
204 * offset. Currently only uniform arrays are supported in this patch,
205 * and that reladdr pointer is resolved by
206 * move_uniform_array_access_to_pull_constants(). All other array types
207 * are lowered by lower_variable_index_to_cond_assign().
209 ir
->array_index
->accept(this);
212 index_reg
= fs_reg(this, glsl_type::int_type
);
213 emit(BRW_OPCODE_MUL
, index_reg
, this->result
, fs_reg(element_size
));
216 emit(BRW_OPCODE_ADD
, index_reg
, *src
.reladdr
, index_reg
);
219 src
.reladdr
= ralloc(mem_ctx
, fs_reg
);
220 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
226 fs_visitor::emit_lrp(const fs_reg
&dst
, const fs_reg
&x
, const fs_reg
&y
,
230 !x
.is_valid_3src() ||
231 !y
.is_valid_3src() ||
232 !a
.is_valid_3src()) {
233 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
234 fs_reg y_times_a
= fs_reg(this, glsl_type::float_type
);
235 fs_reg one_minus_a
= fs_reg(this, glsl_type::float_type
);
236 fs_reg x_times_one_minus_a
= fs_reg(this, glsl_type::float_type
);
238 emit(MUL(y_times_a
, y
, a
));
240 fs_reg negative_a
= a
;
241 negative_a
.negate
= !a
.negate
;
242 emit(ADD(one_minus_a
, negative_a
, fs_reg(1.0f
)));
243 emit(MUL(x_times_one_minus_a
, x
, one_minus_a
));
245 emit(ADD(dst
, x_times_one_minus_a
, y_times_a
));
247 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
248 * we need to reorder the operands.
250 emit(LRP(dst
, a
, y
, x
));
255 fs_visitor::emit_minmax(enum brw_conditional_mod conditionalmod
, const fs_reg
&dst
,
256 const fs_reg
&src0
, const fs_reg
&src1
)
261 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
262 inst
->conditional_mod
= conditionalmod
;
264 emit(CMP(reg_null_d
, src0
, src1
, conditionalmod
));
266 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
267 inst
->predicate
= BRW_PREDICATE_NORMAL
;
272 fs_visitor::try_emit_saturate(ir_expression
*ir
)
274 if (ir
->operation
!= ir_unop_saturate
)
277 ir_rvalue
*sat_val
= ir
->operands
[0];
279 fs_inst
*pre_inst
= (fs_inst
*) this->instructions
.get_tail();
281 sat_val
->accept(this);
282 fs_reg src
= this->result
;
284 fs_inst
*last_inst
= (fs_inst
*) this->instructions
.get_tail();
286 /* If the last instruction from our accept() generated our
287 * src, just set the saturate flag instead of emmitting a separate mov.
289 fs_inst
*modify
= get_instruction_generating_reg(pre_inst
, last_inst
, src
);
290 if (modify
&& modify
->regs_written
== modify
->dst
.width
/ 8 &&
291 modify
->can_do_saturate()) {
292 modify
->saturate
= true;
301 fs_visitor::try_emit_mad(ir_expression
*ir
)
303 /* 3-src instructions were introduced in gen6. */
307 /* MAD can only handle floating-point data. */
308 if (ir
->type
!= glsl_type::float_type
)
311 ir_rvalue
*nonmul
= ir
->operands
[1];
312 ir_expression
*mul
= ir
->operands
[0]->as_expression();
314 if (!mul
|| mul
->operation
!= ir_binop_mul
) {
315 nonmul
= ir
->operands
[0];
316 mul
= ir
->operands
[1]->as_expression();
318 if (!mul
|| mul
->operation
!= ir_binop_mul
)
322 if (nonmul
->as_constant() ||
323 mul
->operands
[0]->as_constant() ||
324 mul
->operands
[1]->as_constant())
327 nonmul
->accept(this);
328 fs_reg src0
= this->result
;
330 mul
->operands
[0]->accept(this);
331 fs_reg src1
= this->result
;
333 mul
->operands
[1]->accept(this);
334 fs_reg src2
= this->result
;
336 this->result
= fs_reg(this, ir
->type
);
337 emit(BRW_OPCODE_MAD
, this->result
, src0
, src1
, src2
);
343 pack_pixel_offset(float x
)
345 /* Clamp upper end of the range to +7/16. See explanation in non-constant
346 * offset case below. */
347 int n
= MIN2((int)(x
* 16), 7);
352 fs_visitor::emit_interpolate_expression(ir_expression
*ir
)
354 /* in SIMD16 mode, the pixel interpolator returns coords interleaved
355 * 8 channels at a time, same as the barycentric coords presented in
356 * the FS payload. this requires a bit of extra work to support.
358 no16("interpolate_at_* not yet supported in SIMD16 mode.");
360 assert(stage
== MESA_SHADER_FRAGMENT
);
361 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
363 ir_dereference
* deref
= ir
->operands
[0]->as_dereference();
364 ir_swizzle
* swiz
= NULL
;
366 /* the api does not allow a swizzle here, but the varying packing code
367 * may have pushed one into here.
369 swiz
= ir
->operands
[0]->as_swizzle();
371 deref
= swiz
->val
->as_dereference();
374 ir_variable
* var
= deref
->variable_referenced();
377 /* 1. collect interpolation factors */
379 fs_reg dst_x
= fs_reg(this, glsl_type::get_instance(ir
->type
->base_type
, 2, 1));
380 fs_reg dst_y
= offset(dst_x
, 1);
382 /* for most messages, we need one reg of ignored data; the hardware requires mlen==1
383 * even when there is no payload. in the per-slot offset case, we'll replace this with
384 * the proper source data. */
385 fs_reg src
= fs_reg(this, glsl_type::float_type
);
386 int mlen
= 1; /* one reg unless overriden */
387 int reg_width
= dispatch_width
/ 8;
390 switch (ir
->operation
) {
391 case ir_unop_interpolate_at_centroid
:
392 inst
= emit(FS_OPCODE_INTERPOLATE_AT_CENTROID
, dst_x
, src
, fs_reg(0u));
395 case ir_binop_interpolate_at_sample
: {
396 ir_constant
*sample_num
= ir
->operands
[1]->as_constant();
397 assert(sample_num
|| !"nonconstant sample number should have been lowered.");
399 unsigned msg_data
= sample_num
->value
.i
[0] << 4;
400 inst
= emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE
, dst_x
, src
, fs_reg(msg_data
));
404 case ir_binop_interpolate_at_offset
: {
405 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
407 unsigned msg_data
= pack_pixel_offset(const_offset
->value
.f
[0]) |
408 (pack_pixel_offset(const_offset
->value
.f
[1]) << 4);
409 inst
= emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
, dst_x
, src
,
412 /* pack the operands: hw wants offsets as 4 bit signed ints */
413 ir
->operands
[1]->accept(this);
414 src
= fs_reg(this, glsl_type::ivec2_type
);
416 for (int i
= 0; i
< 2; i
++) {
417 fs_reg temp
= fs_reg(this, glsl_type::float_type
);
418 emit(MUL(temp
, this->result
, fs_reg(16.0f
)));
419 emit(MOV(src2
, temp
)); /* float to int */
421 /* Clamp the upper end of the range to +7/16. ARB_gpu_shader5 requires
422 * that we support a maximum offset of +0.5, which isn't representable
423 * in a S0.4 value -- if we didn't clamp it, we'd end up with -8/16,
424 * which is the opposite of what the shader author wanted.
426 * This is legal due to ARB_gpu_shader5's quantization rules:
428 * "Not all values of <offset> may be supported; x and y offsets may
429 * be rounded to fixed-point values with the number of fraction bits
430 * given by the implementation-dependent constant
431 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
434 fs_inst
*inst
= emit(BRW_OPCODE_SEL
, src2
, src2
, fs_reg(7));
435 inst
->conditional_mod
= BRW_CONDITIONAL_L
; /* min(src2, 7) */
437 src2
= offset(src2
, 1);
438 this->result
= offset(this->result
, 1);
441 mlen
= 2 * reg_width
;
442 inst
= emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
, dst_x
, src
,
449 unreachable("not reached");
453 inst
->regs_written
= 2 * reg_width
; /* 2 floats per slot returned */
454 inst
->pi_noperspective
= var
->determine_interpolation_mode(key
->flat_shade
) ==
455 INTERP_QUALIFIER_NOPERSPECTIVE
;
457 /* 2. emit linterp */
459 fs_reg
res(this, ir
->type
);
462 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
463 int ch
= swiz
? ((*(int *)&swiz
->mask
) >> 2*i
) & 3 : i
;
464 emit(FS_OPCODE_LINTERP
, res
,
466 fs_reg(interp_reg(var
->data
.location
, ch
)));
467 res
= offset(res
, 1);
472 fs_visitor::visit(ir_expression
*ir
)
474 unsigned int operand
;
478 assert(ir
->get_num_operands() <= 3);
480 if (try_emit_saturate(ir
))
483 /* Deal with the real oddball stuff first */
484 switch (ir
->operation
) {
486 if (try_emit_mad(ir
))
490 case ir_unop_interpolate_at_centroid
:
491 case ir_binop_interpolate_at_offset
:
492 case ir_binop_interpolate_at_sample
:
493 emit_interpolate_expression(ir
);
500 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
501 ir
->operands
[operand
]->accept(this);
502 if (this->result
.file
== BAD_FILE
) {
503 fail("Failed to get tree for expression operand:\n");
504 ir
->operands
[operand
]->fprint(stderr
);
505 fprintf(stderr
, "\n");
507 assert(this->result
.is_valid_3src());
508 op
[operand
] = this->result
;
510 /* Matrix expression operands should have been broken down to vector
511 * operations already.
513 assert(!ir
->operands
[operand
]->type
->is_matrix());
514 /* And then those vector operands should have been broken down to scalar.
516 assert(!ir
->operands
[operand
]->type
->is_vector());
519 /* Storage for our result. If our result goes into an assignment, it will
520 * just get copy-propagated out, so no worries.
522 this->result
= fs_reg(this, ir
->type
);
524 switch (ir
->operation
) {
525 case ir_unop_logic_not
:
526 if (ctx
->Const
.UniformBooleanTrue
!= 1) {
527 emit(NOT(this->result
, op
[0]));
529 emit(XOR(this->result
, op
[0], fs_reg(1)));
533 op
[0].negate
= !op
[0].negate
;
534 emit(MOV(this->result
, op
[0]));
538 op
[0].negate
= false;
539 emit(MOV(this->result
, op
[0]));
542 if (ir
->type
->is_float()) {
543 /* AND(val, 0x80000000) gives the sign bit.
545 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
548 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
550 op
[0].type
= BRW_REGISTER_TYPE_UD
;
551 this->result
.type
= BRW_REGISTER_TYPE_UD
;
552 emit(AND(this->result
, op
[0], fs_reg(0x80000000u
)));
554 inst
= emit(OR(this->result
, this->result
, fs_reg(0x3f800000u
)));
555 inst
->predicate
= BRW_PREDICATE_NORMAL
;
557 this->result
.type
= BRW_REGISTER_TYPE_F
;
559 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
560 * -> non-negative val generates 0x00000000.
561 * Predicated OR sets 1 if val is positive.
563 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_G
));
565 emit(ASR(this->result
, op
[0], fs_reg(31)));
567 inst
= emit(OR(this->result
, this->result
, fs_reg(1)));
568 inst
->predicate
= BRW_PREDICATE_NORMAL
;
572 emit_math(SHADER_OPCODE_RCP
, this->result
, op
[0]);
576 emit_math(SHADER_OPCODE_EXP2
, this->result
, op
[0]);
579 emit_math(SHADER_OPCODE_LOG2
, this->result
, op
[0]);
583 unreachable("not reached: should be handled by ir_explog_to_explog2");
585 case ir_unop_sin_reduced
:
586 emit_math(SHADER_OPCODE_SIN
, this->result
, op
[0]);
589 case ir_unop_cos_reduced
:
590 emit_math(SHADER_OPCODE_COS
, this->result
, op
[0]);
594 emit(FS_OPCODE_DDX
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_BY_HINT
));
596 case ir_unop_dFdx_coarse
:
597 emit(FS_OPCODE_DDX
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_COARSE
));
599 case ir_unop_dFdx_fine
:
600 emit(FS_OPCODE_DDX
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_FINE
));
603 emit(FS_OPCODE_DDY
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_BY_HINT
));
605 case ir_unop_dFdy_coarse
:
606 emit(FS_OPCODE_DDY
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_COARSE
));
608 case ir_unop_dFdy_fine
:
609 emit(FS_OPCODE_DDY
, this->result
, op
[0], fs_reg(BRW_DERIVATIVE_FINE
));
613 emit(ADD(this->result
, op
[0], op
[1]));
616 unreachable("not reached: should be handled by ir_sub_to_add_neg");
619 if (brw
->gen
< 8 && ir
->type
->is_integer()) {
620 /* For integer multiplication, the MUL uses the low 16 bits
621 * of one of the operands (src0 on gen6, src1 on gen7). The
622 * MACH accumulates in the contribution of the upper 16 bits
625 if (ir
->operands
[0]->is_uint16_constant()) {
627 emit(MUL(this->result
, op
[0], op
[1]));
629 emit(MUL(this->result
, op
[1], op
[0]));
630 } else if (ir
->operands
[1]->is_uint16_constant()) {
632 emit(MUL(this->result
, op
[1], op
[0]));
634 emit(MUL(this->result
, op
[0], op
[1]));
637 no16("SIMD16 explicit accumulator operands unsupported\n");
639 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
642 emit(MUL(acc
, op
[0], op
[1]));
643 emit(MACH(reg_null_d
, op
[0], op
[1]));
644 emit(MOV(this->result
, fs_reg(acc
)));
647 emit(MUL(this->result
, op
[0], op
[1]));
650 case ir_binop_imul_high
: {
652 no16("SIMD16 explicit accumulator operands unsupported\n");
654 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
657 fs_inst
*mul
= emit(MUL(acc
, op
[0], op
[1]));
658 emit(MACH(this->result
, op
[0], op
[1]));
660 /* Until Gen8, integer multiplies read 32-bits from one source, and
661 * 16-bits from the other, and relying on the MACH instruction to
662 * generate the high bits of the result.
664 * On Gen8, the multiply instruction does a full 32x32-bit multiply,
665 * but in order to do a 64x64-bit multiply we have to simulate the
666 * previous behavior and then use a MACH instruction.
668 * FINISHME: Don't use source modifiers on src1.
671 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
672 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
673 if (mul
->src
[1].type
== BRW_REGISTER_TYPE_D
) {
674 mul
->src
[1].type
= BRW_REGISTER_TYPE_W
;
676 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
683 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
684 assert(ir
->type
->is_integer());
685 emit_math(SHADER_OPCODE_INT_QUOTIENT
, this->result
, op
[0], op
[1]);
687 case ir_binop_carry
: {
689 no16("SIMD16 explicit accumulator operands unsupported\n");
691 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
692 BRW_REGISTER_TYPE_UD
);
694 emit(ADDC(reg_null_ud
, op
[0], op
[1]));
695 emit(MOV(this->result
, fs_reg(acc
)));
698 case ir_binop_borrow
: {
700 no16("SIMD16 explicit accumulator operands unsupported\n");
702 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
703 BRW_REGISTER_TYPE_UD
);
705 emit(SUBB(reg_null_ud
, op
[0], op
[1]));
706 emit(MOV(this->result
, fs_reg(acc
)));
710 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
711 assert(ir
->type
->is_integer());
712 emit_math(SHADER_OPCODE_INT_REMAINDER
, this->result
, op
[0], op
[1]);
716 case ir_binop_greater
:
717 case ir_binop_lequal
:
718 case ir_binop_gequal
:
720 case ir_binop_all_equal
:
721 case ir_binop_nequal
:
722 case ir_binop_any_nequal
:
723 if (ctx
->Const
.UniformBooleanTrue
== 1) {
724 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
725 resolve_bool_comparison(ir
->operands
[1], &op
[1]);
728 emit(CMP(this->result
, op
[0], op
[1],
729 brw_conditional_for_comparison(ir
->operation
)));
732 case ir_binop_logic_xor
:
733 emit(XOR(this->result
, op
[0], op
[1]));
736 case ir_binop_logic_or
:
737 emit(OR(this->result
, op
[0], op
[1]));
740 case ir_binop_logic_and
:
741 emit(AND(this->result
, op
[0], op
[1]));
746 unreachable("not reached: should be handled by brw_fs_channel_expressions");
749 unreachable("not reached: should be handled by lower_noise");
751 case ir_quadop_vector
:
752 unreachable("not reached: should be handled by lower_quadop_vector");
754 case ir_binop_vector_extract
:
755 unreachable("not reached: should be handled by lower_vec_index_to_cond_assign()");
757 case ir_triop_vector_insert
:
758 unreachable("not reached: should be handled by lower_vector_insert()");
761 unreachable("not reached: should be handled by ldexp_to_arith()");
764 emit_math(SHADER_OPCODE_SQRT
, this->result
, op
[0]);
768 emit_math(SHADER_OPCODE_RSQ
, this->result
, op
[0]);
771 case ir_unop_bitcast_i2f
:
772 case ir_unop_bitcast_u2f
:
773 op
[0].type
= BRW_REGISTER_TYPE_F
;
774 this->result
= op
[0];
777 case ir_unop_bitcast_f2u
:
778 op
[0].type
= BRW_REGISTER_TYPE_UD
;
779 this->result
= op
[0];
782 case ir_unop_bitcast_f2i
:
783 op
[0].type
= BRW_REGISTER_TYPE_D
;
784 this->result
= op
[0];
790 emit(MOV(this->result
, op
[0]));
794 emit(AND(this->result
, op
[0], fs_reg(1)));
797 if (ctx
->Const
.UniformBooleanTrue
!= 1) {
798 op
[0].type
= BRW_REGISTER_TYPE_UD
;
799 this->result
.type
= BRW_REGISTER_TYPE_UD
;
800 emit(AND(this->result
, op
[0], fs_reg(0x3f800000u
)));
801 this->result
.type
= BRW_REGISTER_TYPE_F
;
803 temp
= fs_reg(this, glsl_type::int_type
);
804 emit(AND(temp
, op
[0], fs_reg(1)));
805 emit(MOV(this->result
, temp
));
810 emit(CMP(this->result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
813 emit(CMP(this->result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
817 emit(RNDZ(this->result
, op
[0]));
820 op
[0].negate
= !op
[0].negate
;
821 emit(RNDD(this->result
, op
[0]));
822 this->result
.negate
= true;
825 emit(RNDD(this->result
, op
[0]));
828 emit(FRC(this->result
, op
[0]));
830 case ir_unop_round_even
:
831 emit(RNDE(this->result
, op
[0]));
836 resolve_ud_negate(&op
[0]);
837 resolve_ud_negate(&op
[1]);
838 emit_minmax(ir
->operation
== ir_binop_min
?
839 BRW_CONDITIONAL_L
: BRW_CONDITIONAL_GE
,
840 this->result
, op
[0], op
[1]);
842 case ir_unop_pack_snorm_2x16
:
843 case ir_unop_pack_snorm_4x8
:
844 case ir_unop_pack_unorm_2x16
:
845 case ir_unop_pack_unorm_4x8
:
846 case ir_unop_unpack_snorm_2x16
:
847 case ir_unop_unpack_snorm_4x8
:
848 case ir_unop_unpack_unorm_2x16
:
849 case ir_unop_unpack_unorm_4x8
:
850 case ir_unop_unpack_half_2x16
:
851 case ir_unop_pack_half_2x16
:
852 unreachable("not reached: should be handled by lower_packing_builtins");
853 case ir_unop_unpack_half_2x16_split_x
:
854 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, this->result
, op
[0]);
856 case ir_unop_unpack_half_2x16_split_y
:
857 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, this->result
, op
[0]);
860 emit_math(SHADER_OPCODE_POW
, this->result
, op
[0], op
[1]);
863 case ir_unop_bitfield_reverse
:
864 emit(BFREV(this->result
, op
[0]));
866 case ir_unop_bit_count
:
867 emit(CBIT(this->result
, op
[0]));
869 case ir_unop_find_msb
:
870 temp
= fs_reg(this, glsl_type::uint_type
);
871 emit(FBH(temp
, op
[0]));
873 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
874 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
875 * subtract the result from 31 to convert the MSB count into an LSB count.
878 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
879 emit(MOV(this->result
, temp
));
880 emit(CMP(reg_null_d
, this->result
, fs_reg(-1), BRW_CONDITIONAL_NZ
));
883 inst
= emit(ADD(this->result
, temp
, fs_reg(31)));
884 inst
->predicate
= BRW_PREDICATE_NORMAL
;
886 case ir_unop_find_lsb
:
887 emit(FBL(this->result
, op
[0]));
889 case ir_unop_saturate
:
890 inst
= emit(MOV(this->result
, op
[0]));
891 inst
->saturate
= true;
893 case ir_triop_bitfield_extract
:
894 /* Note that the instruction's argument order is reversed from GLSL
897 emit(BFE(this->result
, op
[2], op
[1], op
[0]));
900 emit(BFI1(this->result
, op
[0], op
[1]));
903 emit(BFI2(this->result
, op
[0], op
[1], op
[2]));
905 case ir_quadop_bitfield_insert
:
906 unreachable("not reached: should be handled by "
907 "lower_instructions::bitfield_insert_to_bfm_bfi");
909 case ir_unop_bit_not
:
910 emit(NOT(this->result
, op
[0]));
912 case ir_binop_bit_and
:
913 emit(AND(this->result
, op
[0], op
[1]));
915 case ir_binop_bit_xor
:
916 emit(XOR(this->result
, op
[0], op
[1]));
918 case ir_binop_bit_or
:
919 emit(OR(this->result
, op
[0], op
[1]));
922 case ir_binop_lshift
:
923 emit(SHL(this->result
, op
[0], op
[1]));
926 case ir_binop_rshift
:
927 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
928 emit(ASR(this->result
, op
[0], op
[1]));
930 emit(SHR(this->result
, op
[0], op
[1]));
932 case ir_binop_pack_half_2x16_split
:
933 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, this->result
, op
[0], op
[1]);
935 case ir_binop_ubo_load
: {
936 /* This IR node takes a constant uniform block and a constant or
937 * variable byte offset within the block and loads a vector from that.
939 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
940 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
943 if (const_uniform_block
) {
944 /* The block index is a constant, so just emit the binding table entry
947 surf_index
= fs_reg(stage_prog_data
->binding_table
.ubo_start
+
948 const_uniform_block
->value
.u
[0]);
950 /* The block index is not a constant. Evaluate the index expression
951 * per-channel and add the base UBO index; the generator will select
952 * a value from any live channel.
954 surf_index
= fs_reg(this, glsl_type::uint_type
);
955 emit(ADD(surf_index
, op
[0],
956 fs_reg(stage_prog_data
->binding_table
.ubo_start
)))
957 ->force_writemask_all
= true;
959 /* Assume this may touch any UBO. It would be nice to provide
960 * a tighter bound, but the array information is already lowered away.
962 brw_mark_surface_used(prog_data
,
963 stage_prog_data
->binding_table
.ubo_start
+
964 shader_prog
->NumUniformBlocks
- 1);
968 fs_reg packed_consts
= fs_reg(this, glsl_type::float_type
);
969 packed_consts
.type
= result
.type
;
971 fs_reg const_offset_reg
= fs_reg(const_offset
->value
.u
[0] & ~15);
972 emit(new(mem_ctx
) fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, 8,
973 packed_consts
, surf_index
, const_offset_reg
));
975 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
976 packed_consts
.set_smear(const_offset
->value
.u
[0] % 16 / 4 + i
);
978 /* The std140 packing rules don't allow vectors to cross 16-byte
979 * boundaries, and a reg is 32 bytes.
981 assert(packed_consts
.subreg_offset
< 32);
983 /* UBO bools are any nonzero value. We consider bools to be
984 * values with the low bit set to 1. Convert them using CMP.
986 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
987 emit(CMP(result
, packed_consts
, fs_reg(0u), BRW_CONDITIONAL_NZ
));
989 emit(MOV(result
, packed_consts
));
992 result
= offset(result
, 1);
995 /* Turn the byte offset into a dword offset. */
996 fs_reg base_offset
= fs_reg(this, glsl_type::int_type
);
997 emit(SHR(base_offset
, op
[1], fs_reg(2)));
999 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1000 emit(VARYING_PULL_CONSTANT_LOAD(result
, surf_index
,
1003 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1004 emit(CMP(result
, result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
1006 result
= offset(result
, 1);
1010 result
.reg_offset
= 0;
1015 /* Note that the instruction's argument order is reversed from GLSL
1018 emit(MAD(this->result
, op
[2], op
[1], op
[0]));
1022 emit_lrp(this->result
, op
[0], op
[1], op
[2]);
1026 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1027 inst
= emit(BRW_OPCODE_SEL
, this->result
, op
[1], op
[2]);
1028 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1031 case ir_unop_interpolate_at_centroid
:
1032 case ir_binop_interpolate_at_offset
:
1033 case ir_binop_interpolate_at_sample
:
1034 unreachable("already handled above");
1040 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
1041 const glsl_type
*type
, bool predicated
)
1043 switch (type
->base_type
) {
1044 case GLSL_TYPE_FLOAT
:
1045 case GLSL_TYPE_UINT
:
1047 case GLSL_TYPE_BOOL
:
1048 for (unsigned int i
= 0; i
< type
->components(); i
++) {
1049 l
.type
= brw_type_for_base_type(type
);
1050 r
.type
= brw_type_for_base_type(type
);
1052 if (predicated
|| !l
.equals(r
)) {
1053 fs_inst
*inst
= emit(MOV(l
, r
));
1054 inst
->predicate
= predicated
? BRW_PREDICATE_NORMAL
: BRW_PREDICATE_NONE
;
1061 case GLSL_TYPE_ARRAY
:
1062 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1063 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
1067 case GLSL_TYPE_STRUCT
:
1068 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1069 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
1074 case GLSL_TYPE_SAMPLER
:
1075 case GLSL_TYPE_IMAGE
:
1076 case GLSL_TYPE_ATOMIC_UINT
:
1079 case GLSL_TYPE_VOID
:
1080 case GLSL_TYPE_ERROR
:
1081 case GLSL_TYPE_INTERFACE
:
1082 unreachable("not reached");
1086 /* If the RHS processing resulted in an instruction generating a
1087 * temporary value, and it would be easy to rewrite the instruction to
1088 * generate its result right into the LHS instead, do so. This ends
1089 * up reliably removing instructions where it can be tricky to do so
1090 * later without real UD chain information.
1093 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1096 fs_inst
*pre_rhs_inst
,
1097 fs_inst
*last_rhs_inst
)
1099 /* Only attempt if we're doing a direct assignment. */
1100 if (ir
->condition
||
1101 !(ir
->lhs
->type
->is_scalar() ||
1102 (ir
->lhs
->type
->is_vector() &&
1103 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1)))
1106 /* Make sure the last instruction generated our source reg. */
1107 fs_inst
*modify
= get_instruction_generating_reg(pre_rhs_inst
,
1113 /* If last_rhs_inst wrote a different number of components than our LHS,
1114 * we can't safely rewrite it.
1116 if (virtual_grf_sizes
[dst
.reg
] != modify
->regs_written
)
1119 /* Success! Rewrite the instruction. */
1126 fs_visitor::visit(ir_assignment
*ir
)
1131 /* FINISHME: arrays on the lhs */
1132 ir
->lhs
->accept(this);
1135 fs_inst
*pre_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
1137 ir
->rhs
->accept(this);
1140 fs_inst
*last_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
1142 assert(l
.file
!= BAD_FILE
);
1143 assert(r
.file
!= BAD_FILE
);
1145 if (try_rewrite_rhs_to_dst(ir
, l
, r
, pre_rhs_inst
, last_rhs_inst
))
1148 if (ir
->condition
) {
1149 emit_bool_to_cond_code(ir
->condition
);
1152 if (ir
->lhs
->type
->is_scalar() ||
1153 ir
->lhs
->type
->is_vector()) {
1154 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1155 if (ir
->write_mask
& (1 << i
)) {
1156 inst
= emit(MOV(l
, r
));
1158 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1164 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
1169 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1170 fs_reg shadow_c
, fs_reg lod
, fs_reg dPdy
,
1175 bool simd16
= false;
1181 if (shadow_c
.file
!= BAD_FILE
) {
1182 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1183 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1184 coordinate
= offset(coordinate
, 1);
1187 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
1188 * the unused slots must be zeroed.
1190 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
1191 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
1195 if (ir
->op
== ir_tex
) {
1196 /* There's no plain shadow compare message, so we use shadow
1197 * compare with a bias of 0.0.
1199 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
)));
1201 } else if (ir
->op
== ir_txb
|| ir
->op
== ir_txl
) {
1202 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1205 unreachable("Should not get here.");
1208 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1210 } else if (ir
->op
== ir_tex
) {
1211 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1212 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1213 coordinate
= offset(coordinate
, 1);
1215 /* zero the others. */
1216 for (int i
= ir
->coordinate
->type
->vector_elements
; i
<3; i
++) {
1217 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
1219 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1221 } else if (ir
->op
== ir_txd
) {
1224 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1225 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1226 coordinate
= offset(coordinate
, 1);
1228 /* the slots for u and v are always present, but r is optional */
1229 mlen
+= MAX2(ir
->coordinate
->type
->vector_elements
, 2);
1232 * dPdx = dudx, dvdx, drdx
1233 * dPdy = dudy, dvdy, drdy
1235 * 1-arg: Does not exist.
1237 * 2-arg: dudx dvdx dudy dvdy
1238 * dPdx.x dPdx.y dPdy.x dPdy.y
1241 * 3-arg: dudx dvdx drdx dudy dvdy drdy
1242 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
1243 * m5 m6 m7 m8 m9 m10
1245 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
1246 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdx
));
1247 dPdx
= offset(dPdx
, 1);
1249 mlen
+= MAX2(ir
->lod_info
.grad
.dPdx
->type
->vector_elements
, 2);
1251 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdy
->type
->vector_elements
; i
++) {
1252 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdy
));
1253 dPdy
= offset(dPdy
, 1);
1255 mlen
+= MAX2(ir
->lod_info
.grad
.dPdy
->type
->vector_elements
, 2);
1256 } else if (ir
->op
== ir_txs
) {
1257 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
1259 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1262 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
1263 * instructions. We'll need to do SIMD16 here.
1266 assert(ir
->op
== ir_txb
|| ir
->op
== ir_txl
|| ir
->op
== ir_txf
);
1268 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1269 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2, coordinate
.type
),
1271 coordinate
= offset(coordinate
, 1);
1274 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
1275 * be necessary for TXF (ld), but seems wise to do for all messages.
1277 for (int i
= ir
->coordinate
->type
->vector_elements
; i
< 3; i
++) {
1278 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2), fs_reg(0.0f
)));
1281 /* lod/bias appears after u/v/r. */
1284 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, lod
.type
), lod
));
1287 /* The unused upper half. */
1292 /* Now, since we're doing simd16, the return is 2 interleaved
1293 * vec4s where the odd-indexed ones are junk. We'll need to move
1294 * this weirdness around to the expected layout.
1297 dst
= fs_reg(GRF
, virtual_grf_alloc(8),
1299 brw_type_for_base_type(ir
->type
) :
1300 BRW_REGISTER_TYPE_F
));
1306 case ir_tex
: opcode
= SHADER_OPCODE_TEX
; break;
1307 case ir_txb
: opcode
= FS_OPCODE_TXB
; break;
1308 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
1309 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
1310 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
1311 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
1313 unreachable("not reached");
1316 fs_inst
*inst
= emit(opcode
, dst
, reg_undef
, fs_reg(sampler
));
1317 inst
->base_mrf
= base_mrf
;
1319 inst
->header_present
= true;
1320 inst
->regs_written
= simd16
? 8 : 4;
1323 for (int i
= 0; i
< 4; i
++) {
1324 emit(MOV(orig_dst
, dst
));
1325 orig_dst
= offset(orig_dst
, 1);
1326 dst
= offset(dst
, 2);
1333 /* gen5's sampler has slots for u, v, r, array index, then optional
1334 * parameters like shadow comparitor or LOD bias. If optional
1335 * parameters aren't present, those base slots are optional and don't
1336 * need to be included in the message.
1338 * We don't fill in the unnecessary slots regardless, which may look
1339 * surprising in the disassembly.
1342 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1343 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1344 fs_reg sample_index
, uint32_t sampler
,
1347 int reg_width
= dispatch_width
/ 8;
1348 bool header_present
= false;
1349 const int vector_elements
=
1350 ir
->coordinate
? ir
->coordinate
->type
->vector_elements
: 0;
1352 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
, dispatch_width
);
1353 fs_reg msg_coords
= message
;
1356 /* The offsets set up by the ir_texture visitor are in the
1357 * m1 header, so we can't go headerless.
1359 header_present
= true;
1363 for (int i
= 0; i
< vector_elements
; i
++) {
1364 emit(MOV(retype(offset(msg_coords
, i
), coordinate
.type
), coordinate
));
1365 coordinate
= offset(coordinate
, 1);
1367 fs_reg msg_end
= offset(msg_coords
, vector_elements
);
1368 fs_reg msg_lod
= offset(msg_coords
, 4);
1370 if (shadow_c
.file
!= BAD_FILE
) {
1371 fs_reg msg_shadow
= msg_lod
;
1372 emit(MOV(msg_shadow
, shadow_c
));
1373 msg_lod
= offset(msg_shadow
, 1);
1380 opcode
= SHADER_OPCODE_TEX
;
1383 emit(MOV(msg_lod
, lod
));
1384 msg_end
= offset(msg_lod
, 1);
1386 opcode
= FS_OPCODE_TXB
;
1389 emit(MOV(msg_lod
, lod
));
1390 msg_end
= offset(msg_lod
, 1);
1392 opcode
= SHADER_OPCODE_TXL
;
1397 * dPdx = dudx, dvdx, drdx
1398 * dPdy = dudy, dvdy, drdy
1400 * Load up these values:
1401 * - dudx dudy dvdx dvdy drdx drdy
1402 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1405 for (int i
= 0; i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
; i
++) {
1406 emit(MOV(msg_end
, lod
));
1407 lod
= offset(lod
, 1);
1408 msg_end
= offset(msg_end
, 1);
1410 emit(MOV(msg_end
, lod2
));
1411 lod2
= offset(lod2
, 1);
1412 msg_end
= offset(msg_end
, 1);
1415 opcode
= SHADER_OPCODE_TXD
;
1419 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
1420 emit(MOV(msg_lod
, lod
));
1421 msg_end
= offset(msg_lod
, 1);
1423 opcode
= SHADER_OPCODE_TXS
;
1425 case ir_query_levels
:
1427 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1428 msg_end
= offset(msg_lod
, 1);
1430 opcode
= SHADER_OPCODE_TXS
;
1433 msg_lod
= offset(msg_coords
, 3);
1434 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
));
1435 msg_end
= offset(msg_lod
, 1);
1437 opcode
= SHADER_OPCODE_TXF
;
1440 msg_lod
= offset(msg_coords
, 3);
1442 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1444 emit(MOV(retype(offset(msg_lod
, 1), BRW_REGISTER_TYPE_UD
), sample_index
));
1445 msg_end
= offset(msg_lod
, 2);
1447 opcode
= SHADER_OPCODE_TXF_CMS
;
1450 opcode
= SHADER_OPCODE_LOD
;
1453 opcode
= SHADER_OPCODE_TG4
;
1456 unreachable("not reached");
1459 fs_inst
*inst
= emit(opcode
, dst
, reg_undef
, fs_reg(sampler
));
1460 inst
->base_mrf
= message
.reg
;
1461 inst
->mlen
= msg_end
.reg
- message
.reg
;
1462 inst
->header_present
= header_present
;
1463 inst
->regs_written
= 4 * reg_width
;
1465 if (inst
->mlen
> MAX_SAMPLER_MESSAGE_SIZE
) {
1466 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE
)
1467 " disallowed by hardware\n");
1474 is_high_sampler(struct brw_context
*brw
, fs_reg sampler
)
1476 if (brw
->gen
< 8 && !brw
->is_haswell
)
1479 return sampler
.file
!= IMM
|| sampler
.fixed_hw_reg
.dw1
.ud
>= 16;
1483 fs_visitor::emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
1484 fs_reg shadow_c
, fs_reg lod
, fs_reg lod2
,
1485 fs_reg sample_index
, fs_reg mcs
, fs_reg sampler
,
1486 fs_reg offset_value
)
1488 int reg_width
= dispatch_width
/ 8;
1489 bool header_present
= false;
1491 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, MAX_SAMPLER_MESSAGE_SIZE
);
1492 for (int i
= 0; i
< MAX_SAMPLER_MESSAGE_SIZE
; i
++) {
1493 sources
[i
] = fs_reg(this, glsl_type::float_type
);
1497 if (ir
->op
== ir_tg4
|| offset_value
.file
!= BAD_FILE
||
1498 is_high_sampler(brw
, sampler
)) {
1499 /* For general texture offsets (no txf workaround), we need a header to
1500 * put them in. Note that for SIMD16 we're making space for two actual
1501 * hardware registers here, so the emit will have to fix up for this.
1503 * * ir4_tg4 needs to place its channel select in the header,
1504 * for interaction with ARB_texture_swizzle
1506 * The sampler index is only 4-bits, so for larger sampler numbers we
1507 * need to offset the Sampler State Pointer in the header.
1509 header_present
= true;
1510 sources
[0] = fs_reg(GRF
, virtual_grf_alloc(1), BRW_REGISTER_TYPE_UD
);
1514 if (shadow_c
.file
!= BAD_FILE
) {
1515 emit(MOV(sources
[length
], shadow_c
));
1519 bool has_nonconstant_offset
=
1520 offset_value
.file
!= BAD_FILE
&& offset_value
.file
!= IMM
;
1521 bool coordinate_done
= false;
1523 /* Set up the LOD info */
1529 emit(MOV(sources
[length
], lod
));
1533 emit(MOV(sources
[length
], lod
));
1537 no16("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1539 /* Load dPdx and the coordinate together:
1540 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1542 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1543 emit(MOV(sources
[length
], coordinate
));
1544 coordinate
= offset(coordinate
, 1);
1547 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1548 * only derivatives for (u, v, r).
1550 if (i
< ir
->lod_info
.grad
.dPdx
->type
->vector_elements
) {
1551 emit(MOV(sources
[length
], lod
));
1552 lod
= offset(lod
, 1);
1555 emit(MOV(sources
[length
], lod2
));
1556 lod2
= offset(lod2
, 1);
1561 coordinate_done
= true;
1565 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
));
1568 case ir_query_levels
:
1569 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1573 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1574 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1575 coordinate
= offset(coordinate
, 1);
1578 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
));
1581 for (int i
= 1; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1582 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1583 coordinate
= offset(coordinate
, 1);
1587 coordinate_done
= true;
1590 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
));
1593 /* data from the multisample control surface */
1594 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
));
1597 /* there is no offsetting for this message; just copy in the integer
1598 * texture coordinates
1600 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1601 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1602 coordinate
= offset(coordinate
, 1);
1606 coordinate_done
= true;
1609 if (has_nonconstant_offset
) {
1610 if (shadow_c
.file
!= BAD_FILE
)
1611 no16("Gen7 does not support gather4_po_c in SIMD16 mode.");
1613 /* More crazy intermixing */
1614 for (int i
= 0; i
< 2; i
++) { /* u, v */
1615 emit(MOV(sources
[length
], coordinate
));
1616 coordinate
= offset(coordinate
, 1);
1620 for (int i
= 0; i
< 2; i
++) { /* offu, offv */
1621 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
));
1622 offset_value
= offset(offset_value
, 1);
1626 if (ir
->coordinate
->type
->vector_elements
== 3) { /* r if present */
1627 emit(MOV(sources
[length
], coordinate
));
1628 coordinate
= offset(coordinate
, 1);
1632 coordinate_done
= true;
1637 /* Set up the coordinate (except for cases where it was done above) */
1638 if (ir
->coordinate
&& !coordinate_done
) {
1639 for (int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
1640 emit(MOV(sources
[length
], coordinate
));
1641 coordinate
= offset(coordinate
, 1);
1648 mlen
= length
* reg_width
- header_present
;
1650 mlen
= length
* reg_width
;
1652 fs_reg src_payload
= fs_reg(GRF
, virtual_grf_alloc(mlen
),
1653 BRW_REGISTER_TYPE_F
);
1654 emit(LOAD_PAYLOAD(src_payload
, sources
, length
));
1656 /* Generate the SEND */
1659 case ir_tex
: opcode
= SHADER_OPCODE_TEX
; break;
1660 case ir_txb
: opcode
= FS_OPCODE_TXB
; break;
1661 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
1662 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
1663 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
1664 case ir_txf_ms
: opcode
= SHADER_OPCODE_TXF_CMS
; break;
1665 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
1666 case ir_query_levels
: opcode
= SHADER_OPCODE_TXS
; break;
1667 case ir_lod
: opcode
= SHADER_OPCODE_LOD
; break;
1669 if (has_nonconstant_offset
)
1670 opcode
= SHADER_OPCODE_TG4_OFFSET
;
1672 opcode
= SHADER_OPCODE_TG4
;
1675 unreachable("not reached");
1677 fs_inst
*inst
= emit(opcode
, dst
, src_payload
, sampler
);
1678 inst
->base_mrf
= -1;
1680 inst
->header_present
= header_present
;
1681 inst
->regs_written
= 4 * reg_width
;
1683 if (inst
->mlen
> MAX_SAMPLER_MESSAGE_SIZE
) {
1684 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE
)
1685 " disallowed by hardware\n");
1692 fs_visitor::rescale_texcoord(fs_reg coordinate
, const glsl_type
*coord_type
,
1693 bool is_rect
, uint32_t sampler
, int texunit
)
1695 fs_inst
*inst
= NULL
;
1696 bool needs_gl_clamp
= true;
1697 fs_reg scale_x
, scale_y
;
1698 const struct brw_sampler_prog_key_data
*tex
=
1699 (stage
== MESA_SHADER_FRAGMENT
) ?
1700 &((brw_wm_prog_key
*) this->key
)->tex
: NULL
;
1703 /* The 965 requires the EU to do the normalization of GL rectangle
1704 * texture coordinates. We use the program parameter state
1705 * tracking to get the scaling factor.
1709 (brw
->gen
>= 6 && (tex
->gl_clamp_mask
[0] & (1 << sampler
) ||
1710 tex
->gl_clamp_mask
[1] & (1 << sampler
))))) {
1711 struct gl_program_parameter_list
*params
= prog
->Parameters
;
1712 int tokens
[STATE_LENGTH
] = {
1714 STATE_TEXRECT_SCALE
,
1720 no16("rectangle scale uniform setup not supported on SIMD16\n");
1721 if (dispatch_width
== 16) {
1725 GLuint index
= _mesa_add_state_reference(params
,
1726 (gl_state_index
*)tokens
);
1727 /* Try to find existing copies of the texrect scale uniforms. */
1728 for (unsigned i
= 0; i
< uniforms
; i
++) {
1729 if (stage_prog_data
->param
[i
] ==
1730 &prog
->Parameters
->ParameterValues
[index
][0]) {
1731 scale_x
= fs_reg(UNIFORM
, i
);
1732 scale_y
= fs_reg(UNIFORM
, i
+ 1);
1737 /* If we didn't already set them up, do so now. */
1738 if (scale_x
.file
== BAD_FILE
) {
1739 scale_x
= fs_reg(UNIFORM
, uniforms
);
1740 scale_y
= fs_reg(UNIFORM
, uniforms
+ 1);
1742 stage_prog_data
->param
[uniforms
++] =
1743 &prog
->Parameters
->ParameterValues
[index
][0];
1744 stage_prog_data
->param
[uniforms
++] =
1745 &prog
->Parameters
->ParameterValues
[index
][1];
1749 /* The 965 requires the EU to do the normalization of GL rectangle
1750 * texture coordinates. We use the program parameter state
1751 * tracking to get the scaling factor.
1753 if (brw
->gen
< 6 && is_rect
) {
1754 fs_reg dst
= fs_reg(this, coord_type
);
1755 fs_reg src
= coordinate
;
1758 emit(MUL(dst
, src
, scale_x
));
1759 dst
= offset(dst
, 1);
1760 src
= offset(src
, 1);
1761 emit(MUL(dst
, src
, scale_y
));
1762 } else if (is_rect
) {
1763 /* On gen6+, the sampler handles the rectangle coordinates
1764 * natively, without needing rescaling. But that means we have
1765 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
1766 * not [0, 1] like the default case below.
1768 needs_gl_clamp
= false;
1770 for (int i
= 0; i
< 2; i
++) {
1771 if (tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
1772 fs_reg chan
= coordinate
;
1773 chan
= offset(chan
, i
);
1775 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, fs_reg(0.0f
));
1776 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1778 /* Our parameter comes in as 1.0/width or 1.0/height,
1779 * because that's what people normally want for doing
1780 * texture rectangle handling. We need width or height
1781 * for clamping, but we don't care enough to make a new
1782 * parameter type, so just invert back.
1784 fs_reg limit
= fs_reg(this, glsl_type::float_type
);
1785 emit(MOV(limit
, i
== 0 ? scale_x
: scale_y
));
1786 emit(SHADER_OPCODE_RCP
, limit
, limit
);
1788 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, limit
);
1789 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1794 if (coord_type
&& needs_gl_clamp
) {
1795 for (unsigned int i
= 0; i
< MIN2(coord_type
->vector_elements
, 3); i
++) {
1796 if (tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
1797 fs_reg chan
= coordinate
;
1798 chan
= offset(chan
, i
);
1800 fs_inst
*inst
= emit(MOV(chan
, chan
));
1801 inst
->saturate
= true;
1808 /* Sample from the MCS surface attached to this multisample texture. */
1810 fs_visitor::emit_mcs_fetch(fs_reg coordinate
, int components
, fs_reg sampler
)
1812 int reg_width
= dispatch_width
/ 8;
1813 fs_reg payload
= fs_reg(GRF
, virtual_grf_alloc(components
* reg_width
),
1814 BRW_REGISTER_TYPE_F
);
1815 fs_reg dest
= fs_reg(this, glsl_type::uvec4_type
);
1816 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, components
);
1818 /* parameters are: u, v, r; missing parameters are treated as zero */
1819 for (int i
= 0; i
< components
; i
++) {
1820 sources
[i
] = fs_reg(this, glsl_type::float_type
);
1821 emit(MOV(retype(sources
[i
], BRW_REGISTER_TYPE_D
), coordinate
));
1822 coordinate
= offset(coordinate
, 1);
1825 emit(LOAD_PAYLOAD(payload
, sources
, components
));
1827 fs_inst
*inst
= emit(SHADER_OPCODE_TXF_MCS
, dest
, payload
, sampler
);
1828 inst
->base_mrf
= -1;
1829 inst
->mlen
= components
* reg_width
;
1830 inst
->header_present
= false;
1831 inst
->regs_written
= 4 * reg_width
; /* we only care about one reg of
1832 * response, but the sampler always
1840 fs_visitor::visit(ir_texture
*ir
)
1842 const struct brw_sampler_prog_key_data
*tex
=
1843 (stage
== MESA_SHADER_FRAGMENT
) ?
1844 &((brw_wm_prog_key
*) this->key
)->tex
: NULL
;
1846 fs_inst
*inst
= NULL
;
1849 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, prog
);
1851 ir_rvalue
*nonconst_sampler_index
=
1852 _mesa_get_sampler_array_nonconst_index(ir
->sampler
);
1854 /* Handle non-constant sampler array indexing */
1856 if (nonconst_sampler_index
) {
1857 /* The highest sampler which may be used by this operation is
1858 * the last element of the array. Mark it here, because the generator
1859 * doesn't have enough information to determine the bound.
1861 uint32_t array_size
= ir
->sampler
->as_dereference_array()
1862 ->array
->type
->array_size();
1864 uint32_t max_used
= sampler
+ array_size
- 1;
1865 if (ir
->op
== ir_tg4
&& brw
->gen
< 8) {
1866 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
1868 max_used
+= stage_prog_data
->binding_table
.texture_start
;
1871 brw_mark_surface_used(prog_data
, max_used
);
1873 /* Emit code to evaluate the actual indexing expression */
1874 nonconst_sampler_index
->accept(this);
1875 fs_reg
temp(this, glsl_type::uint_type
);
1876 emit(ADD(temp
, this->result
, fs_reg(sampler
)))
1877 ->force_writemask_all
= true;
1880 /* Single sampler, or constant array index; the indexing expression
1881 * is just an immediate.
1883 sampler_reg
= fs_reg(sampler
);
1886 /* FINISHME: We're failing to recompile our programs when the sampler is
1887 * updated. This only matters for the texture rectangle scale parameters
1888 * (pre-gen6, or gen6+ with GL_CLAMP).
1890 int texunit
= prog
->SamplerUnits
[sampler
];
1892 if (ir
->op
== ir_tg4
) {
1893 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
1894 * emitting anything other than setting up the constant result.
1896 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
1897 int swiz
= GET_SWZ(tex
->swizzles
[sampler
], chan
->value
.i
[0]);
1898 if (swiz
== SWIZZLE_ZERO
|| swiz
== SWIZZLE_ONE
) {
1900 fs_reg res
= fs_reg(this, glsl_type::vec4_type
);
1903 for (int i
=0; i
<4; i
++) {
1904 emit(MOV(res
, fs_reg(swiz
== SWIZZLE_ZERO
? 0.0f
: 1.0f
)));
1905 res
= offset(res
, 1);
1911 /* Should be lowered by do_lower_texture_projection */
1912 assert(!ir
->projector
);
1914 /* Should be lowered */
1915 assert(!ir
->offset
|| !ir
->offset
->type
->is_array());
1917 /* Generate code to compute all the subexpression trees. This has to be
1918 * done before loading any values into MRFs for the sampler message since
1919 * generating these values may involve SEND messages that need the MRFs.
1922 if (ir
->coordinate
) {
1923 ir
->coordinate
->accept(this);
1925 coordinate
= rescale_texcoord(this->result
,
1926 ir
->coordinate
->type
,
1927 ir
->sampler
->type
->sampler_dimensionality
==
1928 GLSL_SAMPLER_DIM_RECT
,
1932 fs_reg shadow_comparitor
;
1933 if (ir
->shadow_comparitor
) {
1934 ir
->shadow_comparitor
->accept(this);
1935 shadow_comparitor
= this->result
;
1938 fs_reg offset_value
;
1940 ir_constant
*const_offset
= ir
->offset
->as_constant();
1942 /* Store the header bitfield in an IMM register. This allows us to
1943 * use offset_value.file to distinguish between no offset, a constant
1944 * offset, and a non-constant offset.
1947 fs_reg(brw_texture_offset(ctx
, const_offset
->value
.i
,
1948 const_offset
->type
->vector_elements
));
1950 ir
->offset
->accept(this);
1951 offset_value
= this->result
;
1955 fs_reg lod
, lod2
, sample_index
, mcs
;
1960 case ir_query_levels
:
1963 ir
->lod_info
.bias
->accept(this);
1967 ir
->lod_info
.grad
.dPdx
->accept(this);
1970 ir
->lod_info
.grad
.dPdy
->accept(this);
1971 lod2
= this->result
;
1976 ir
->lod_info
.lod
->accept(this);
1980 ir
->lod_info
.sample_index
->accept(this);
1981 sample_index
= this->result
;
1983 if (brw
->gen
>= 7 && tex
->compressed_multisample_layout_mask
& (1<<sampler
))
1984 mcs
= emit_mcs_fetch(coordinate
, ir
->coordinate
->type
->vector_elements
,
1990 unreachable("Unrecognized texture opcode");
1993 /* Writemasking doesn't eliminate channels on SIMD8 texture
1994 * samples, so don't worry about them.
1996 fs_reg dst
= fs_reg(this, glsl_type::get_instance(ir
->type
->base_type
, 4, 1));
1998 if (brw
->gen
>= 7) {
1999 inst
= emit_texture_gen7(ir
, dst
, coordinate
, shadow_comparitor
,
2000 lod
, lod2
, sample_index
, mcs
, sampler_reg
,
2002 } else if (brw
->gen
>= 5) {
2003 inst
= emit_texture_gen5(ir
, dst
, coordinate
, shadow_comparitor
,
2004 lod
, lod2
, sample_index
, sampler
,
2005 ir
->offset
!= NULL
);
2007 inst
= emit_texture_gen4(ir
, dst
, coordinate
, shadow_comparitor
,
2008 lod
, lod2
, sampler
);
2011 if (offset_value
.file
== IMM
)
2012 inst
->texture_offset
= offset_value
.fixed_hw_reg
.dw1
.ud
;
2014 if (ir
->op
== ir_tg4
)
2015 inst
->texture_offset
|= gather_channel(ir
, sampler
) << 16; // M0.2:16-17
2017 if (ir
->shadow_comparitor
)
2018 inst
->shadow_compare
= true;
2020 /* fixup #layers for cube map arrays */
2021 if (ir
->op
== ir_txs
) {
2022 glsl_type
const *type
= ir
->sampler
->type
;
2023 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2024 type
->sampler_array
) {
2025 fs_reg depth
= offset(dst
, 2);
2026 fs_reg fixed_depth
= fs_reg(this, glsl_type::int_type
);
2027 emit_math(SHADER_OPCODE_INT_QUOTIENT
, fixed_depth
, depth
, fs_reg(6));
2029 fs_reg
*fixed_payload
= ralloc_array(mem_ctx
, fs_reg
, inst
->regs_written
);
2030 int components
= inst
->regs_written
/ (dst
.width
/ 8);
2031 for (int i
= 0; i
< components
; i
++) {
2033 fixed_payload
[i
] = fixed_depth
;
2035 fixed_payload
[i
] = offset(dst
, i
);
2038 emit(LOAD_PAYLOAD(dst
, fixed_payload
, components
));
2042 if (brw
->gen
== 6 && ir
->op
== ir_tg4
) {
2043 emit_gen6_gather_wa(tex
->gen6_gather_wa
[sampler
], dst
);
2046 swizzle_result(ir
, dst
, sampler
);
2050 * Apply workarounds for Gen6 gather with UINT/SINT
2053 fs_visitor::emit_gen6_gather_wa(uint8_t wa
, fs_reg dst
)
2058 int width
= (wa
& WA_8BIT
) ? 8 : 16;
2060 for (int i
= 0; i
< 4; i
++) {
2061 fs_reg dst_f
= retype(dst
, BRW_REGISTER_TYPE_F
);
2062 /* Convert from UNORM to UINT */
2063 emit(MUL(dst_f
, dst_f
, fs_reg((float)((1 << width
) - 1))));
2064 emit(MOV(dst
, dst_f
));
2067 /* Reinterpret the UINT value as a signed INT value by
2068 * shifting the sign bit into place, then shifting back
2071 emit(SHL(dst
, dst
, fs_reg(32 - width
)));
2072 emit(ASR(dst
, dst
, fs_reg(32 - width
)));
2075 dst
= offset(dst
, 1);
2080 * Set up the gather channel based on the swizzle, for gather4.
2083 fs_visitor::gather_channel(ir_texture
*ir
, uint32_t sampler
)
2085 const struct brw_sampler_prog_key_data
*tex
=
2086 (stage
== MESA_SHADER_FRAGMENT
) ?
2087 &((brw_wm_prog_key
*) this->key
)->tex
: NULL
;
2089 ir_constant
*chan
= ir
->lod_info
.component
->as_constant();
2090 int swiz
= GET_SWZ(tex
->swizzles
[sampler
], chan
->value
.i
[0]);
2092 case SWIZZLE_X
: return 0;
2094 /* gather4 sampler is broken for green channel on RG32F --
2095 * we must ask for blue instead.
2097 if (tex
->gather_channel_quirk_mask
& (1<<sampler
))
2100 case SWIZZLE_Z
: return 2;
2101 case SWIZZLE_W
: return 3;
2103 unreachable("Not reached"); /* zero, one swizzles handled already */
2108 * Swizzle the result of a texture result. This is necessary for
2109 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
2112 fs_visitor::swizzle_result(ir_texture
*ir
, fs_reg orig_val
, uint32_t sampler
)
2114 if (ir
->op
== ir_query_levels
) {
2115 /* # levels is in .w */
2116 this->result
= offset(orig_val
, 3);
2120 this->result
= orig_val
;
2122 /* txs,lod don't actually sample the texture, so swizzling the result
2125 if (ir
->op
== ir_txs
|| ir
->op
== ir_lod
|| ir
->op
== ir_tg4
)
2128 const struct brw_sampler_prog_key_data
*tex
=
2129 (stage
== MESA_SHADER_FRAGMENT
) ?
2130 &((brw_wm_prog_key
*) this->key
)->tex
: NULL
;
2133 if (ir
->type
== glsl_type::float_type
) {
2134 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
2135 assert(ir
->sampler
->type
->sampler_shadow
);
2136 } else if (tex
->swizzles
[sampler
] != SWIZZLE_NOOP
) {
2137 fs_reg swizzled_result
= fs_reg(this, glsl_type::vec4_type
);
2139 for (int i
= 0; i
< 4; i
++) {
2140 int swiz
= GET_SWZ(tex
->swizzles
[sampler
], i
);
2141 fs_reg l
= swizzled_result
;
2144 if (swiz
== SWIZZLE_ZERO
) {
2145 emit(MOV(l
, fs_reg(0.0f
)));
2146 } else if (swiz
== SWIZZLE_ONE
) {
2147 emit(MOV(l
, fs_reg(1.0f
)));
2149 emit(MOV(l
, offset(orig_val
,
2150 GET_SWZ(tex
->swizzles
[sampler
], i
))));
2153 this->result
= swizzled_result
;
2158 fs_visitor::visit(ir_swizzle
*ir
)
2160 ir
->val
->accept(this);
2161 fs_reg val
= this->result
;
2163 if (ir
->type
->vector_elements
== 1) {
2164 this->result
= offset(this->result
, ir
->mask
.x
);
2168 fs_reg result
= fs_reg(this, ir
->type
);
2169 this->result
= result
;
2171 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2172 fs_reg channel
= val
;
2190 emit(MOV(result
, offset(channel
, swiz
)));
2191 result
= offset(result
, 1);
2196 fs_visitor::visit(ir_discard
*ir
)
2198 assert(ir
->condition
== NULL
); /* FINISHME */
2200 /* We track our discarded pixels in f0.1. By predicating on it, we can
2201 * update just the flag bits that aren't yet discarded. By emitting a
2202 * CMP of g0 != g0, all our currently executing channels will get turned
2205 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
2206 BRW_REGISTER_TYPE_UW
));
2207 fs_inst
*cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
2208 BRW_CONDITIONAL_NZ
));
2209 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
2210 cmp
->flag_subreg
= 1;
2212 if (brw
->gen
>= 6) {
2213 /* For performance, after a discard, jump to the end of the shader.
2214 * Only jump if all relevant channels have been discarded.
2216 fs_inst
*discard_jump
= emit(FS_OPCODE_DISCARD_JUMP
);
2217 discard_jump
->flag_subreg
= 1;
2219 discard_jump
->predicate
= (dispatch_width
== 8)
2220 ? BRW_PREDICATE_ALIGN1_ANY8H
2221 : BRW_PREDICATE_ALIGN1_ANY16H
;
2222 discard_jump
->predicate_inverse
= true;
2227 fs_visitor::visit(ir_constant
*ir
)
2229 /* Set this->result to reg at the bottom of the function because some code
2230 * paths will cause this visitor to be applied to other fields. This will
2231 * cause the value stored in this->result to be modified.
2233 * Make reg constant so that it doesn't get accidentally modified along the
2234 * way. Yes, I actually had this problem. :(
2236 const fs_reg
reg(this, ir
->type
);
2237 fs_reg dst_reg
= reg
;
2239 if (ir
->type
->is_array()) {
2240 const unsigned size
= type_size(ir
->type
->fields
.array
);
2242 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
2243 ir
->array_elements
[i
]->accept(this);
2244 fs_reg src_reg
= this->result
;
2246 dst_reg
.type
= src_reg
.type
;
2247 for (unsigned j
= 0; j
< size
; j
++) {
2248 emit(MOV(dst_reg
, src_reg
));
2249 src_reg
= offset(src_reg
, 1);
2250 dst_reg
= offset(dst_reg
, 1);
2253 } else if (ir
->type
->is_record()) {
2254 foreach_in_list(ir_constant
, field
, &ir
->components
) {
2255 const unsigned size
= type_size(field
->type
);
2257 field
->accept(this);
2258 fs_reg src_reg
= this->result
;
2260 dst_reg
.type
= src_reg
.type
;
2261 for (unsigned j
= 0; j
< size
; j
++) {
2262 emit(MOV(dst_reg
, src_reg
));
2263 src_reg
= offset(src_reg
, 1);
2264 dst_reg
= offset(dst_reg
, 1);
2268 const unsigned size
= type_size(ir
->type
);
2270 for (unsigned i
= 0; i
< size
; i
++) {
2271 switch (ir
->type
->base_type
) {
2272 case GLSL_TYPE_FLOAT
:
2273 emit(MOV(dst_reg
, fs_reg(ir
->value
.f
[i
])));
2275 case GLSL_TYPE_UINT
:
2276 emit(MOV(dst_reg
, fs_reg(ir
->value
.u
[i
])));
2279 emit(MOV(dst_reg
, fs_reg(ir
->value
.i
[i
])));
2281 case GLSL_TYPE_BOOL
:
2283 fs_reg(ir
->value
.b
[i
] != 0 ? ctx
->Const
.UniformBooleanTrue
2287 unreachable("Non-float/uint/int/bool constant");
2289 dst_reg
= offset(dst_reg
, 1);
2297 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
2299 ir_expression
*expr
= ir
->as_expression();
2301 if (!expr
|| expr
->operation
== ir_binop_ubo_load
) {
2304 fs_inst
*inst
= emit(AND(reg_null_d
, this->result
, fs_reg(1)));
2305 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2312 assert(expr
->get_num_operands() <= 3);
2313 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
2314 assert(expr
->operands
[i
]->type
->is_scalar());
2316 expr
->operands
[i
]->accept(this);
2317 op
[i
] = this->result
;
2319 resolve_ud_negate(&op
[i
]);
2322 switch (expr
->operation
) {
2323 case ir_unop_logic_not
:
2324 inst
= emit(AND(reg_null_d
, op
[0], fs_reg(1)));
2325 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2328 case ir_binop_logic_xor
:
2329 if (ctx
->Const
.UniformBooleanTrue
== 1) {
2330 fs_reg dst
= fs_reg(this, glsl_type::uint_type
);
2331 emit(XOR(dst
, op
[0], op
[1]));
2332 inst
= emit(AND(reg_null_d
, dst
, fs_reg(1)));
2333 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2335 inst
= emit(XOR(reg_null_d
, op
[0], op
[1]));
2336 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2340 case ir_binop_logic_or
:
2341 if (ctx
->Const
.UniformBooleanTrue
== 1) {
2342 fs_reg dst
= fs_reg(this, glsl_type::uint_type
);
2343 emit(OR(dst
, op
[0], op
[1]));
2344 inst
= emit(AND(reg_null_d
, dst
, fs_reg(1)));
2345 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2347 inst
= emit(OR(reg_null_d
, op
[0], op
[1]));
2348 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2352 case ir_binop_logic_and
:
2353 if (ctx
->Const
.UniformBooleanTrue
== 1) {
2354 fs_reg dst
= fs_reg(this, glsl_type::uint_type
);
2355 emit(AND(dst
, op
[0], op
[1]));
2356 inst
= emit(AND(reg_null_d
, dst
, fs_reg(1)));
2357 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2359 inst
= emit(AND(reg_null_d
, op
[0], op
[1]));
2360 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2365 if (brw
->gen
>= 6) {
2366 emit(CMP(reg_null_d
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
2368 inst
= emit(MOV(reg_null_f
, op
[0]));
2369 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2374 if (brw
->gen
>= 6) {
2375 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
2377 inst
= emit(MOV(reg_null_d
, op
[0]));
2378 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2382 case ir_binop_greater
:
2383 case ir_binop_gequal
:
2385 case ir_binop_lequal
:
2386 case ir_binop_equal
:
2387 case ir_binop_all_equal
:
2388 case ir_binop_nequal
:
2389 case ir_binop_any_nequal
:
2390 if (ctx
->Const
.UniformBooleanTrue
== 1) {
2391 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
2392 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
2395 emit(CMP(reg_null_d
, op
[0], op
[1],
2396 brw_conditional_for_comparison(expr
->operation
)));
2399 case ir_triop_csel
: {
2400 /* Expand the boolean condition into the flag register. */
2401 inst
= emit(MOV(reg_null_d
, op
[0]));
2402 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2404 /* Select which boolean to return. */
2405 fs_reg
temp(this, expr
->operands
[1]->type
);
2406 inst
= emit(SEL(temp
, op
[1], op
[2]));
2407 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2409 /* Expand the result to a condition code. */
2410 inst
= emit(MOV(reg_null_d
, temp
));
2411 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2416 unreachable("not reached");
2421 * Emit a gen6 IF statement with the comparison folded into the IF
2425 fs_visitor::emit_if_gen6(ir_if
*ir
)
2427 ir_expression
*expr
= ir
->condition
->as_expression();
2429 if (expr
&& expr
->operation
!= ir_binop_ubo_load
) {
2434 assert(expr
->get_num_operands() <= 3);
2435 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
2436 assert(expr
->operands
[i
]->type
->is_scalar());
2438 expr
->operands
[i
]->accept(this);
2439 op
[i
] = this->result
;
2442 switch (expr
->operation
) {
2443 case ir_unop_logic_not
:
2444 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_Z
));
2447 case ir_binop_logic_xor
:
2448 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
2451 case ir_binop_logic_or
:
2452 temp
= fs_reg(this, glsl_type::bool_type
);
2453 emit(OR(temp
, op
[0], op
[1]));
2454 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2457 case ir_binop_logic_and
:
2458 temp
= fs_reg(this, glsl_type::bool_type
);
2459 emit(AND(temp
, op
[0], op
[1]));
2460 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2464 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
2465 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2469 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
2472 case ir_binop_greater
:
2473 case ir_binop_gequal
:
2475 case ir_binop_lequal
:
2476 case ir_binop_equal
:
2477 case ir_binop_all_equal
:
2478 case ir_binop_nequal
:
2479 case ir_binop_any_nequal
:
2480 if (ctx
->Const
.UniformBooleanTrue
== 1) {
2481 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
2482 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
2485 emit(IF(op
[0], op
[1],
2486 brw_conditional_for_comparison(expr
->operation
)));
2489 case ir_triop_csel
: {
2490 /* Expand the boolean condition into the flag register. */
2491 fs_inst
*inst
= emit(MOV(reg_null_d
, op
[0]));
2492 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2494 /* Select which boolean to use as the result. */
2495 fs_reg
temp(this, expr
->operands
[1]->type
);
2496 inst
= emit(SEL(temp
, op
[1], op
[2]));
2497 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2499 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2504 unreachable("not reached");
2508 ir
->condition
->accept(this);
2509 emit(IF(this->result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2513 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
2515 * Many GLSL shaders contain the following pattern:
2517 * x = condition ? foo : bar
2519 * The compiler emits an ir_if tree for this, since each subexpression might be
2520 * a complex tree that could have side-effects or short-circuit logic.
2522 * However, the common case is to simply select one of two constants or
2523 * variable values---which is exactly what SEL is for. In this case, the
2524 * assembly looks like:
2532 * which can be easily translated into:
2534 * (+f0) SEL dst src0 src1
2536 * If src0 is an immediate value, we promote it to a temporary GRF.
2539 fs_visitor::try_replace_with_sel()
2541 fs_inst
*endif_inst
= (fs_inst
*) instructions
.get_tail();
2542 assert(endif_inst
->opcode
== BRW_OPCODE_ENDIF
);
2544 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
2546 BRW_OPCODE_IF
, BRW_OPCODE_MOV
, BRW_OPCODE_ELSE
, BRW_OPCODE_MOV
,
2549 fs_inst
*match
= (fs_inst
*) endif_inst
->prev
;
2550 for (int i
= 0; i
< 4; i
++) {
2551 if (match
->is_head_sentinel() || match
->opcode
!= opcodes
[4-i
-1])
2553 match
= (fs_inst
*) match
->prev
;
2556 /* The opcodes match; it looks like the right sequence of instructions. */
2557 fs_inst
*else_mov
= (fs_inst
*) endif_inst
->prev
;
2558 fs_inst
*then_mov
= (fs_inst
*) else_mov
->prev
->prev
;
2559 fs_inst
*if_inst
= (fs_inst
*) then_mov
->prev
;
2561 /* Check that the MOVs are the right form. */
2562 if (then_mov
->dst
.equals(else_mov
->dst
) &&
2563 !then_mov
->is_partial_write() &&
2564 !else_mov
->is_partial_write()) {
2566 /* Remove the matched instructions; we'll emit a SEL to replace them. */
2567 while (!if_inst
->next
->is_tail_sentinel())
2568 if_inst
->next
->exec_node::remove();
2569 if_inst
->exec_node::remove();
2571 /* Only the last source register can be a constant, so if the MOV in
2572 * the "then" clause uses a constant, we need to put it in a temporary.
2574 fs_reg
src0(then_mov
->src
[0]);
2575 if (src0
.file
== IMM
) {
2576 src0
= fs_reg(this, glsl_type::float_type
);
2577 src0
.type
= then_mov
->src
[0].type
;
2578 emit(MOV(src0
, then_mov
->src
[0]));
2582 if (if_inst
->conditional_mod
) {
2583 /* Sandybridge-specific IF with embedded comparison */
2584 emit(CMP(reg_null_d
, if_inst
->src
[0], if_inst
->src
[1],
2585 if_inst
->conditional_mod
));
2586 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
2587 sel
->predicate
= BRW_PREDICATE_NORMAL
;
2589 /* Separate CMP and IF instructions */
2590 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
2591 sel
->predicate
= if_inst
->predicate
;
2592 sel
->predicate_inverse
= if_inst
->predicate_inverse
;
2598 fs_visitor::visit(ir_if
*ir
)
2601 no16("Can't support (non-uniform) control flow on SIMD16\n");
2604 /* Don't point the annotation at the if statement, because then it plus
2605 * the then and else blocks get printed.
2607 this->base_ir
= ir
->condition
;
2609 if (brw
->gen
== 6) {
2612 emit_bool_to_cond_code(ir
->condition
);
2614 emit(IF(BRW_PREDICATE_NORMAL
));
2617 foreach_in_list(ir_instruction
, ir_
, &ir
->then_instructions
) {
2618 this->base_ir
= ir_
;
2622 if (!ir
->else_instructions
.is_empty()) {
2623 emit(BRW_OPCODE_ELSE
);
2625 foreach_in_list(ir_instruction
, ir_
, &ir
->else_instructions
) {
2626 this->base_ir
= ir_
;
2631 emit(BRW_OPCODE_ENDIF
);
2633 try_replace_with_sel();
2637 fs_visitor::visit(ir_loop
*ir
)
2640 no16("Can't support (non-uniform) control flow on SIMD16\n");
2643 this->base_ir
= NULL
;
2644 emit(BRW_OPCODE_DO
);
2646 foreach_in_list(ir_instruction
, ir_
, &ir
->body_instructions
) {
2647 this->base_ir
= ir_
;
2651 this->base_ir
= NULL
;
2652 emit(BRW_OPCODE_WHILE
);
2656 fs_visitor::visit(ir_loop_jump
*ir
)
2659 case ir_loop_jump::jump_break
:
2660 emit(BRW_OPCODE_BREAK
);
2662 case ir_loop_jump::jump_continue
:
2663 emit(BRW_OPCODE_CONTINUE
);
2669 fs_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
2671 ir_dereference
*deref
= static_cast<ir_dereference
*>(
2672 ir
->actual_parameters
.get_head());
2673 ir_variable
*location
= deref
->variable_referenced();
2674 unsigned surf_index
= (stage_prog_data
->binding_table
.abo_start
+
2675 location
->data
.binding
);
2677 /* Calculate the surface offset */
2678 fs_reg
offset(this, glsl_type::uint_type
);
2679 ir_dereference_array
*deref_array
= deref
->as_dereference_array();
2682 deref_array
->array_index
->accept(this);
2684 fs_reg
tmp(this, glsl_type::uint_type
);
2685 emit(MUL(tmp
, this->result
, fs_reg(ATOMIC_COUNTER_SIZE
)));
2686 emit(ADD(offset
, tmp
, fs_reg(location
->data
.atomic
.offset
)));
2688 offset
= fs_reg(location
->data
.atomic
.offset
);
2691 /* Emit the appropriate machine instruction */
2692 const char *callee
= ir
->callee
->function_name();
2693 ir
->return_deref
->accept(this);
2694 fs_reg dst
= this->result
;
2696 if (!strcmp("__intrinsic_atomic_read", callee
)) {
2697 emit_untyped_surface_read(surf_index
, dst
, offset
);
2699 } else if (!strcmp("__intrinsic_atomic_increment", callee
)) {
2700 emit_untyped_atomic(BRW_AOP_INC
, surf_index
, dst
, offset
,
2701 fs_reg(), fs_reg());
2703 } else if (!strcmp("__intrinsic_atomic_predecrement", callee
)) {
2704 emit_untyped_atomic(BRW_AOP_PREDEC
, surf_index
, dst
, offset
,
2705 fs_reg(), fs_reg());
2710 fs_visitor::visit(ir_call
*ir
)
2712 const char *callee
= ir
->callee
->function_name();
2714 if (!strcmp("__intrinsic_atomic_read", callee
) ||
2715 !strcmp("__intrinsic_atomic_increment", callee
) ||
2716 !strcmp("__intrinsic_atomic_predecrement", callee
)) {
2717 visit_atomic_counter_intrinsic(ir
);
2719 unreachable("Unsupported intrinsic.");
2724 fs_visitor::visit(ir_return
*)
2726 unreachable("FINISHME");
2730 fs_visitor::visit(ir_function
*ir
)
2732 /* Ignore function bodies other than main() -- we shouldn't see calls to
2733 * them since they should all be inlined before we get to ir_to_mesa.
2735 if (strcmp(ir
->name
, "main") == 0) {
2736 const ir_function_signature
*sig
;
2739 sig
= ir
->matching_signature(NULL
, &empty
, false);
2743 foreach_in_list(ir_instruction
, ir_
, &sig
->body
) {
2744 this->base_ir
= ir_
;
2751 fs_visitor::visit(ir_function_signature
*)
2753 unreachable("not reached");
2757 fs_visitor::visit(ir_emit_vertex
*)
2759 unreachable("not reached");
2763 fs_visitor::visit(ir_end_primitive
*)
2765 unreachable("not reached");
2769 fs_visitor::emit_untyped_atomic(unsigned atomic_op
, unsigned surf_index
,
2770 fs_reg dst
, fs_reg offset
, fs_reg src0
,
2774 (stage
== MESA_SHADER_FRAGMENT
) &&
2775 ((brw_wm_prog_data
*) this->prog_data
)->uses_kill
;
2776 int reg_width
= dispatch_width
/ 8;
2779 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 4);
2781 sources
[0] = fs_reg(GRF
, virtual_grf_alloc(1), BRW_REGISTER_TYPE_UD
);
2782 /* Initialize the sample mask in the message header. */
2783 emit(MOV(sources
[0], fs_reg(0u)))
2784 ->force_writemask_all
= true;
2787 emit(MOV(component(sources
[0], 7), brw_flag_reg(0, 1)))
2788 ->force_writemask_all
= true;
2790 emit(MOV(component(sources
[0], 7),
2791 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD
)))
2792 ->force_writemask_all
= true;
2796 /* Set the atomic operation offset. */
2797 sources
[1] = fs_reg(this, glsl_type::uint_type
);
2798 emit(MOV(sources
[1], offset
));
2801 /* Set the atomic operation arguments. */
2802 if (src0
.file
!= BAD_FILE
) {
2803 sources
[length
] = fs_reg(this, glsl_type::uint_type
);
2804 emit(MOV(sources
[length
], src0
));
2808 if (src1
.file
!= BAD_FILE
) {
2809 sources
[length
] = fs_reg(this, glsl_type::uint_type
);
2810 emit(MOV(sources
[length
], src1
));
2814 int mlen
= 1 + (length
- 1) * reg_width
;
2815 fs_reg src_payload
= fs_reg(GRF
, virtual_grf_alloc(mlen
),
2816 BRW_REGISTER_TYPE_UD
);
2817 emit(LOAD_PAYLOAD(src_payload
, sources
, length
));
2819 /* Emit the instruction. */
2820 fs_inst
*inst
= emit(SHADER_OPCODE_UNTYPED_ATOMIC
, dst
, src_payload
,
2821 fs_reg(atomic_op
), fs_reg(surf_index
));
2826 fs_visitor::emit_untyped_surface_read(unsigned surf_index
, fs_reg dst
,
2830 (stage
== MESA_SHADER_FRAGMENT
) &&
2831 ((brw_wm_prog_data
*) this->prog_data
)->uses_kill
;
2832 int reg_width
= dispatch_width
/ 8;
2834 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
2836 sources
[0] = fs_reg(GRF
, virtual_grf_alloc(1), BRW_REGISTER_TYPE_UD
);
2837 /* Initialize the sample mask in the message header. */
2838 emit(MOV(sources
[0], fs_reg(0u)))
2839 ->force_writemask_all
= true;
2842 emit(MOV(component(sources
[0], 7), brw_flag_reg(0, 1)))
2843 ->force_writemask_all
= true;
2845 emit(MOV(component(sources
[0], 7),
2846 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD
)))
2847 ->force_writemask_all
= true;
2850 /* Set the surface read offset. */
2851 sources
[1] = fs_reg(this, glsl_type::uint_type
);
2852 emit(MOV(sources
[1], offset
));
2854 int mlen
= 1 + reg_width
;
2855 fs_reg src_payload
= fs_reg(GRF
, virtual_grf_alloc(mlen
),
2856 BRW_REGISTER_TYPE_UD
);
2857 fs_inst
*inst
= emit(LOAD_PAYLOAD(src_payload
, sources
, 2));
2859 /* Emit the instruction. */
2860 inst
= emit(SHADER_OPCODE_UNTYPED_SURFACE_READ
, dst
, src_payload
,
2861 fs_reg(surf_index
));
2866 fs_visitor::emit(fs_inst
*inst
)
2868 if (force_uncompressed_stack
> 0)
2869 inst
->exec_size
= 8;
2871 if (dispatch_width
== 16 && inst
->exec_size
== 8)
2872 inst
->force_uncompressed
= true;
2874 inst
->annotation
= this->current_annotation
;
2875 inst
->ir
= this->base_ir
;
2877 this->instructions
.push_tail(inst
);
2883 fs_visitor::emit(exec_list list
)
2885 foreach_in_list_safe(fs_inst
, inst
, &list
) {
2886 inst
->exec_node::remove();
2891 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
2893 fs_visitor::emit_dummy_fs()
2895 int reg_width
= dispatch_width
/ 8;
2897 /* Everyone's favorite color. */
2898 emit(MOV(fs_reg(MRF
, 2 + 0 * reg_width
), fs_reg(1.0f
)));
2899 emit(MOV(fs_reg(MRF
, 2 + 1 * reg_width
), fs_reg(0.0f
)));
2900 emit(MOV(fs_reg(MRF
, 2 + 2 * reg_width
), fs_reg(1.0f
)));
2901 emit(MOV(fs_reg(MRF
, 2 + 3 * reg_width
), fs_reg(0.0f
)));
2904 write
= emit(FS_OPCODE_FB_WRITE
, fs_reg(0), fs_reg(0));
2905 write
->base_mrf
= 2;
2906 write
->mlen
= 4 * reg_width
;
2910 /* The register location here is relative to the start of the URB
2911 * data. It will get adjusted to be a real location before
2912 * generate_code() time.
2915 fs_visitor::interp_reg(int location
, int channel
)
2917 assert(stage
== MESA_SHADER_FRAGMENT
);
2918 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
2919 int regnr
= prog_data
->urb_setup
[location
] * 2 + channel
/ 2;
2920 int stride
= (channel
& 1) * 4;
2922 assert(prog_data
->urb_setup
[location
] != -1);
2924 return brw_vec1_grf(regnr
, stride
);
2927 /** Emits the interpolation for the varying inputs. */
2929 fs_visitor::emit_interpolation_setup_gen4()
2931 this->current_annotation
= "compute pixel centers";
2932 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
2933 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
2934 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2935 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2937 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
2938 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
2940 this->current_annotation
= "compute pixel deltas from v0";
2942 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2943 fs_reg(this, glsl_type::vec2_type
);
2944 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2945 offset(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
], 1);
2947 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2948 fs_reg(this, glsl_type::float_type
);
2949 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
2950 fs_reg(this, glsl_type::float_type
);
2952 emit(ADD(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2953 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0)))));
2954 emit(ADD(this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2955 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1)))));
2957 this->current_annotation
= "compute pos.w and 1/pos.w";
2958 /* Compute wpos.w. It's always in our setup, since it's needed to
2959 * interpolate the other attributes.
2961 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
2962 emit(FS_OPCODE_LINTERP
, wpos_w
,
2963 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2964 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
2965 interp_reg(VARYING_SLOT_POS
, 3));
2966 /* Compute the pixel 1/W value from wpos.w. */
2967 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
2968 emit_math(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
2969 this->current_annotation
= NULL
;
2972 /** Emits the interpolation for the varying inputs. */
2974 fs_visitor::emit_interpolation_setup_gen6()
2976 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
2978 /* If the pixel centers end up used, the setup is the same as for gen4. */
2979 this->current_annotation
= "compute pixel centers";
2980 fs_reg int_pixel_x
= fs_reg(this, glsl_type::uint_type
);
2981 fs_reg int_pixel_y
= fs_reg(this, glsl_type::uint_type
);
2982 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
2983 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
2984 emit(ADD(int_pixel_x
,
2985 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
2986 fs_reg(brw_imm_v(0x10101010))));
2987 emit(ADD(int_pixel_y
,
2988 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
2989 fs_reg(brw_imm_v(0x11001100))));
2991 /* As of gen6, we can no longer mix float and int sources. We have
2992 * to turn the integer pixel centers into floats for their actual
2995 this->pixel_x
= fs_reg(this, glsl_type::float_type
);
2996 this->pixel_y
= fs_reg(this, glsl_type::float_type
);
2997 emit(MOV(this->pixel_x
, int_pixel_x
));
2998 emit(MOV(this->pixel_y
, int_pixel_y
));
3000 this->current_annotation
= "compute pos.w";
3001 this->pixel_w
= fs_reg(brw_vec8_grf(payload
.source_w_reg
, 0));
3002 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
3003 emit_math(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
3005 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
3006 uint8_t reg
= payload
.barycentric_coord_reg
[i
];
3007 this->delta_x
[i
] = fs_reg(brw_vec8_grf(reg
, 0));
3008 this->delta_y
[i
] = fs_reg(brw_vec8_grf(reg
+ 1, 0));
3011 this->current_annotation
= NULL
;
3015 fs_visitor::setup_color_payload(fs_reg
*dst
, fs_reg color
, unsigned components
)
3017 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3020 if (color
.file
== BAD_FILE
) {
3021 return 4 * (dispatch_width
/ 8);
3024 uint8_t colors_enabled
;
3025 if (components
== 0) {
3026 /* We want to write one component to the alpha channel */
3027 colors_enabled
= 0x8;
3029 /* Enable the first components-many channels */
3030 colors_enabled
= (1 << components
) - 1;
3033 if (dispatch_width
== 8 || brw
->gen
>= 6) {
3034 /* SIMD8 write looks like:
3040 * gen6 SIMD16 DP write looks like:
3051 for (unsigned i
= 0; i
< 4; ++i
) {
3052 if (colors_enabled
& (1 << i
)) {
3053 dst
[len
] = fs_reg(GRF
, virtual_grf_alloc(color
.width
/ 8),
3054 color
.type
, color
.width
);
3055 inst
= emit(MOV(dst
[len
], offset(color
, i
)));
3056 inst
->saturate
= key
->clamp_fragment_color
;
3057 } else if (color
.width
== 16) {
3058 /* We need two BAD_FILE slots for a 16-wide color */
3065 /* pre-gen6 SIMD16 single source DP write looks like:
3075 for (unsigned i
= 0; i
< 4; ++i
) {
3076 if (colors_enabled
& (1 << i
)) {
3077 dst
[i
] = fs_reg(GRF
, virtual_grf_alloc(1), color
.type
);
3078 inst
= emit(MOV(dst
[i
], half(offset(color
, i
), 0)));
3079 inst
->saturate
= key
->clamp_fragment_color
;
3081 dst
[i
+ 4] = fs_reg(GRF
, virtual_grf_alloc(1), color
.type
);
3082 inst
= emit(MOV(dst
[i
+ 4], half(offset(color
, i
), 1)));
3083 inst
->saturate
= key
->clamp_fragment_color
;
3084 inst
->force_sechalf
= true;
3091 static enum brw_conditional_mod
3092 cond_for_alpha_func(GLenum func
)
3096 return BRW_CONDITIONAL_G
;
3098 return BRW_CONDITIONAL_GE
;
3100 return BRW_CONDITIONAL_L
;
3102 return BRW_CONDITIONAL_LE
;
3104 return BRW_CONDITIONAL_EQ
;
3106 return BRW_CONDITIONAL_NEQ
;
3108 unreachable("Not reached");
3113 * Alpha test support for when we compile it into the shader instead
3114 * of using the normal fixed-function alpha test.
3117 fs_visitor::emit_alpha_test()
3119 assert(stage
== MESA_SHADER_FRAGMENT
);
3120 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3121 this->current_annotation
= "Alpha test";
3124 if (key
->alpha_test_func
== GL_ALWAYS
)
3127 if (key
->alpha_test_func
== GL_NEVER
) {
3129 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
3130 BRW_REGISTER_TYPE_UW
));
3131 cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
3132 BRW_CONDITIONAL_NEQ
));
3135 fs_reg color
= offset(outputs
[0], 3);
3137 /* f0.1 &= func(color, ref) */
3138 cmp
= emit(CMP(reg_null_f
, color
, fs_reg(key
->alpha_test_ref
),
3139 cond_for_alpha_func(key
->alpha_test_func
)));
3141 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
3142 cmp
->flag_subreg
= 1;
3146 fs_visitor::emit_single_fb_write(fs_reg color0
, fs_reg color1
,
3147 fs_reg src0_alpha
, unsigned components
)
3149 assert(stage
== MESA_SHADER_FRAGMENT
);
3150 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3151 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3153 this->current_annotation
= "FB write header";
3154 bool header_present
= true;
3155 int reg_size
= dispatch_width
/ 8;
3157 /* We can potentially have a message length of up to 15, so we have to set
3158 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3160 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 15);
3163 /* From the Sandy Bridge PRM, volume 4, page 198:
3165 * "Dispatched Pixel Enables. One bit per pixel indicating
3166 * which pixels were originally enabled when the thread was
3167 * dispatched. This field is only required for the end-of-
3168 * thread message and on all dual-source messages."
3170 if (brw
->gen
>= 6 &&
3171 (brw
->is_haswell
|| brw
->gen
>= 8 || !prog_data
->uses_kill
) &&
3172 color1
.file
== BAD_FILE
&&
3173 key
->nr_color_regions
== 1) {
3174 header_present
= false;
3178 /* Allocate 2 registers for a header */
3181 if (payload
.aa_dest_stencil_reg
) {
3182 sources
[length
] = fs_reg(GRF
, virtual_grf_alloc(1));
3183 emit(MOV(sources
[length
],
3184 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0))));
3188 prog_data
->uses_omask
=
3189 prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
3190 if (prog_data
->uses_omask
) {
3191 this->current_annotation
= "FB write oMask";
3192 assert(this->sample_mask
.file
!= BAD_FILE
);
3193 /* Hand over gl_SampleMask. Only lower 16 bits are relevant. Since
3194 * it's unsinged single words, one vgrf is always 16-wide.
3196 sources
[length
] = fs_reg(GRF
, virtual_grf_alloc(1),
3197 BRW_REGISTER_TYPE_UW
, 16);
3198 emit(FS_OPCODE_SET_OMASK
, sources
[length
], this->sample_mask
);
3202 if (color0
.file
== BAD_FILE
) {
3203 /* Even if there's no color buffers enabled, we still need to send
3204 * alpha out the pipeline to our null renderbuffer to support
3205 * alpha-testing, alpha-to-coverage, and so on.
3207 length
+= setup_color_payload(sources
+ length
, this->outputs
[0], 0);
3208 } else if (color1
.file
== BAD_FILE
) {
3209 if (src0_alpha
.file
!= BAD_FILE
) {
3210 sources
[length
] = fs_reg(GRF
, virtual_grf_alloc(reg_size
),
3211 src0_alpha
.type
, src0_alpha
.width
);
3212 fs_inst
*inst
= emit(MOV(sources
[length
], src0_alpha
));
3213 inst
->saturate
= key
->clamp_fragment_color
;
3217 length
+= setup_color_payload(sources
+ length
, color0
, components
);
3219 length
+= setup_color_payload(sources
+ length
, color0
, components
);
3220 length
+= setup_color_payload(sources
+ length
, color1
, components
);
3223 if (source_depth_to_render_target
) {
3224 if (brw
->gen
== 6) {
3225 /* For outputting oDepth on gen6, SIMD8 writes have to be
3226 * used. This would require SIMD8 moves of each half to
3227 * message regs, kind of like pre-gen5 SIMD16 FB writes.
3228 * Just bail on doing so for now.
3230 no16("Missing support for simd16 depth writes on gen6\n");
3233 sources
[length
] = fs_reg(this, glsl_type::float_type
);
3234 if (prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
3235 /* Hand over gl_FragDepth. */
3236 assert(this->frag_depth
.file
!= BAD_FILE
);
3237 emit(MOV(sources
[length
], this->frag_depth
));
3239 /* Pass through the payload depth. */
3240 emit(MOV(sources
[length
],
3241 fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0))));
3246 if (payload
.dest_depth_reg
) {
3247 sources
[length
] = fs_reg(this, glsl_type::float_type
);
3248 emit(MOV(sources
[length
],
3249 fs_reg(brw_vec8_grf(payload
.dest_depth_reg
, 0))));
3255 if (brw
->gen
>= 7) {
3256 /* Send from the GRF */
3257 fs_reg payload
= fs_reg(GRF
, -1, BRW_REGISTER_TYPE_F
);
3258 load
= emit(LOAD_PAYLOAD(payload
, sources
, length
));
3259 payload
.reg
= virtual_grf_alloc(load
->regs_written
);
3260 load
->dst
= payload
;
3261 write
= emit(FS_OPCODE_FB_WRITE
, reg_undef
, payload
);
3262 write
->base_mrf
= -1;
3264 /* Send from the MRF */
3265 load
= emit(LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3267 write
= emit(FS_OPCODE_FB_WRITE
);
3268 write
->base_mrf
= 1;
3271 write
->mlen
= load
->regs_written
;
3272 write
->header_present
= header_present
;
3273 if ((brw
->gen
>= 8 || brw
->is_haswell
) && prog_data
->uses_kill
) {
3274 write
->predicate
= BRW_PREDICATE_NORMAL
;
3275 write
->flag_subreg
= 1;
3281 fs_visitor::emit_fb_writes()
3283 assert(stage
== MESA_SHADER_FRAGMENT
);
3284 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3285 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3288 no16("GL_ARB_blend_func_extended not yet supported in SIMD16.");
3289 if (dispatch_width
== 16)
3290 do_dual_src
= false;
3295 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
3296 emit_shader_time_end();
3298 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
3299 "FB dual-source write");
3300 inst
= emit_single_fb_write(this->outputs
[0], this->dual_src_output
,
3303 prog_data
->dual_src_blend
= true;
3304 } else if (key
->nr_color_regions
> 0) {
3305 for (int target
= 0; target
< key
->nr_color_regions
; target
++) {
3306 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
3307 "FB write target %d",
3310 if (brw
->gen
>= 6 && key
->replicate_alpha
&& target
!= 0)
3311 src0_alpha
= offset(outputs
[0], 3);
3313 if (target
== key
->nr_color_regions
- 1 &&
3314 (INTEL_DEBUG
& DEBUG_SHADER_TIME
))
3315 emit_shader_time_end();
3317 inst
= emit_single_fb_write(this->outputs
[target
], reg_undef
,
3319 this->output_components
[target
]);
3320 inst
->target
= target
;
3323 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
3324 emit_shader_time_end();
3326 /* Even if there's no color buffers enabled, we still need to send
3327 * alpha out the pipeline to our null renderbuffer to support
3328 * alpha-testing, alpha-to-coverage, and so on.
3330 inst
= emit_single_fb_write(reg_undef
, reg_undef
, reg_undef
, 0);
3335 this->current_annotation
= NULL
;
3339 fs_visitor::resolve_ud_negate(fs_reg
*reg
)
3341 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
3345 fs_reg temp
= fs_reg(this, glsl_type::uint_type
);
3346 emit(MOV(temp
, *reg
));
3351 fs_visitor::resolve_bool_comparison(ir_rvalue
*rvalue
, fs_reg
*reg
)
3353 assert(ctx
->Const
.UniformBooleanTrue
== 1);
3355 if (rvalue
->type
!= glsl_type::bool_type
)
3358 fs_reg temp
= fs_reg(this, glsl_type::bool_type
);
3359 emit(AND(temp
, *reg
, fs_reg(1)));
3363 fs_visitor::fs_visitor(struct brw_context
*brw
,
3365 const struct brw_wm_prog_key
*key
,
3366 struct brw_wm_prog_data
*prog_data
,
3367 struct gl_shader_program
*shader_prog
,
3368 struct gl_fragment_program
*fp
,
3369 unsigned dispatch_width
)
3370 : backend_visitor(brw
, shader_prog
, &fp
->Base
, &prog_data
->base
,
3371 MESA_SHADER_FRAGMENT
),
3372 reg_null_f(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_F
)),
3373 reg_null_d(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_D
)),
3374 reg_null_ud(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_UD
)),
3375 key(key
), prog_data(&prog_data
->base
),
3376 dispatch_width(dispatch_width
)
3378 this->mem_ctx
= mem_ctx
;
3385 this->failed
= false;
3386 this->simd16_unsupported
= false;
3387 this->no16_msg
= NULL
;
3388 this->variable_ht
= hash_table_ctor(0,
3389 hash_table_pointer_hash
,
3390 hash_table_pointer_compare
);
3392 memset(&this->payload
, 0, sizeof(this->payload
));
3393 memset(this->outputs
, 0, sizeof(this->outputs
));
3394 memset(this->output_components
, 0, sizeof(this->output_components
));
3395 this->source_depth_to_render_target
= false;
3396 this->runtime_check_aads_emit
= false;
3397 this->first_non_payload_grf
= 0;
3398 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
3400 this->current_annotation
= NULL
;
3401 this->base_ir
= NULL
;
3403 this->virtual_grf_sizes
= NULL
;
3404 this->virtual_grf_count
= 0;
3405 this->virtual_grf_array_size
= 0;
3406 this->virtual_grf_start
= NULL
;
3407 this->virtual_grf_end
= NULL
;
3408 this->live_intervals
= NULL
;
3409 this->regs_live_at_ip
= NULL
;
3412 this->last_scratch
= 0;
3413 this->pull_constant_loc
= NULL
;
3414 this->push_constant_loc
= NULL
;
3416 this->force_uncompressed_stack
= 0;
3418 this->spilled_any_registers
= false;
3419 this->do_dual_src
= false;
3421 if (dispatch_width
== 8)
3422 this->param_size
= rzalloc_array(mem_ctx
, int, stage_prog_data
->nr_params
);
3425 fs_visitor::~fs_visitor()
3427 hash_table_dtor(this->variable_ht
);