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.
30 #include <sys/types.h>
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "util/register_allocate.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
44 #include "main/uniforms.h"
45 #include "glsl/glsl_types.h"
46 #include "glsl/ir_optimization.h"
47 #include "program/sampler.h"
51 fs_visitor::emit_vs_system_value(int location
)
53 fs_reg
*reg
= new(this->mem_ctx
)
54 fs_reg(ATTR
, VERT_ATTRIB_MAX
, BRW_REGISTER_TYPE_D
);
55 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
58 case SYSTEM_VALUE_BASE_VERTEX
:
60 vs_prog_data
->uses_vertexid
= true;
62 case SYSTEM_VALUE_VERTEX_ID
:
63 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
65 vs_prog_data
->uses_vertexid
= true;
67 case SYSTEM_VALUE_INSTANCE_ID
:
69 vs_prog_data
->uses_instanceid
= true;
72 unreachable("not reached");
79 fs_visitor::visit(ir_variable
*ir
)
83 if (variable_storage(ir
))
86 if (ir
->data
.mode
== ir_var_shader_in
) {
87 assert(ir
->data
.location
!= -1);
88 if (stage
== MESA_SHADER_VERTEX
) {
89 reg
= new(this->mem_ctx
)
90 fs_reg(ATTR
, ir
->data
.location
,
91 brw_type_for_base_type(ir
->type
->get_scalar_type()));
92 } else if (ir
->data
.location
== VARYING_SLOT_POS
) {
93 reg
= emit_fragcoord_interpolation(ir
->data
.pixel_center_integer
,
94 ir
->data
.origin_upper_left
);
95 } else if (ir
->data
.location
== VARYING_SLOT_FACE
) {
96 reg
= emit_frontfacing_interpolation();
98 reg
= new(this->mem_ctx
) fs_reg(vgrf(ir
->type
));
99 emit_general_interpolation(*reg
, ir
->name
, ir
->type
,
100 (glsl_interp_qualifier
) ir
->data
.interpolation
,
101 ir
->data
.location
, ir
->data
.centroid
,
105 hash_table_insert(this->variable_ht
, reg
, ir
);
107 } else if (ir
->data
.mode
== ir_var_shader_out
) {
108 reg
= new(this->mem_ctx
) fs_reg(vgrf(ir
->type
));
110 if (stage
== MESA_SHADER_VERTEX
) {
111 int vector_elements
=
112 ir
->type
->is_array() ? ir
->type
->fields
.array
->vector_elements
113 : ir
->type
->vector_elements
;
115 for (int i
= 0; i
< (type_size(ir
->type
) + 3) / 4; i
++) {
116 int output
= ir
->data
.location
+ i
;
117 this->outputs
[output
] = *reg
;
118 this->outputs
[output
].reg_offset
= i
* 4;
119 this->output_components
[output
] = vector_elements
;
122 } else if (ir
->data
.index
> 0) {
123 assert(ir
->data
.location
== FRAG_RESULT_DATA0
);
124 assert(ir
->data
.index
== 1);
125 this->dual_src_output
= *reg
;
126 this->do_dual_src
= true;
127 } else if (ir
->data
.location
== FRAG_RESULT_COLOR
) {
128 /* Writing gl_FragColor outputs to all color regions. */
129 assert(stage
== MESA_SHADER_FRAGMENT
);
130 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
131 for (unsigned int i
= 0; i
< MAX2(key
->nr_color_regions
, 1); i
++) {
132 this->outputs
[i
] = *reg
;
133 this->output_components
[i
] = 4;
135 } else if (ir
->data
.location
== FRAG_RESULT_DEPTH
) {
136 this->frag_depth
= *reg
;
137 } else if (ir
->data
.location
== FRAG_RESULT_SAMPLE_MASK
) {
138 this->sample_mask
= *reg
;
140 /* gl_FragData or a user-defined FS output */
141 assert(ir
->data
.location
>= FRAG_RESULT_DATA0
&&
142 ir
->data
.location
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
);
144 int vector_elements
=
145 ir
->type
->is_array() ? ir
->type
->fields
.array
->vector_elements
146 : ir
->type
->vector_elements
;
148 /* General color output. */
149 for (unsigned int i
= 0; i
< MAX2(1, ir
->type
->length
); i
++) {
150 int output
= ir
->data
.location
- FRAG_RESULT_DATA0
+ i
;
151 this->outputs
[output
] = offset(*reg
, vector_elements
* i
);
152 this->output_components
[output
] = vector_elements
;
155 } else if (ir
->data
.mode
== ir_var_uniform
) {
156 int param_index
= uniforms
;
158 /* Thanks to the lower_ubo_reference pass, we will see only
159 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
160 * variables, so no need for them to be in variable_ht.
162 * Some uniforms, such as samplers and atomic counters, have no actual
163 * storage, so we should ignore them.
165 if (ir
->is_in_uniform_block() || type_size(ir
->type
) == 0)
168 if (dispatch_width
== 16) {
169 if (!variable_storage(ir
)) {
170 fail("Failed to find uniform '%s' in SIMD16\n", ir
->name
);
175 param_size
[param_index
] = type_size(ir
->type
);
176 if (!strncmp(ir
->name
, "gl_", 3)) {
177 setup_builtin_uniform_values(ir
);
179 setup_uniform_values(ir
);
182 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
183 reg
->type
= brw_type_for_base_type(ir
->type
);
185 } else if (ir
->data
.mode
== ir_var_system_value
) {
186 switch (ir
->data
.location
) {
187 case SYSTEM_VALUE_BASE_VERTEX
:
188 case SYSTEM_VALUE_VERTEX_ID
:
189 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
190 case SYSTEM_VALUE_INSTANCE_ID
:
191 reg
= emit_vs_system_value(ir
->data
.location
);
193 case SYSTEM_VALUE_SAMPLE_POS
:
194 reg
= emit_samplepos_setup();
196 case SYSTEM_VALUE_SAMPLE_ID
:
197 reg
= emit_sampleid_setup();
199 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
200 assert(brw
->gen
>= 7);
202 fs_reg(retype(brw_vec8_grf(payload
.sample_mask_in_reg
, 0),
203 BRW_REGISTER_TYPE_D
));
209 reg
= new(this->mem_ctx
) fs_reg(vgrf(ir
->type
));
211 hash_table_insert(this->variable_ht
, reg
, ir
);
215 fs_visitor::visit(ir_dereference_variable
*ir
)
217 fs_reg
*reg
= variable_storage(ir
->var
);
220 fail("Failed to find variable storage for %s\n", ir
->var
->name
);
221 this->result
= fs_reg(reg_null_d
);
228 fs_visitor::visit(ir_dereference_record
*ir
)
230 const glsl_type
*struct_type
= ir
->record
->type
;
232 ir
->record
->accept(this);
234 unsigned int off
= 0;
235 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
236 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
238 off
+= type_size(struct_type
->fields
.structure
[i
].type
);
240 this->result
= offset(this->result
, off
);
241 this->result
.type
= brw_type_for_base_type(ir
->type
);
245 fs_visitor::visit(ir_dereference_array
*ir
)
247 ir_constant
*constant_index
;
249 int element_size
= type_size(ir
->type
);
251 constant_index
= ir
->array_index
->as_constant();
253 ir
->array
->accept(this);
255 src
.type
= brw_type_for_base_type(ir
->type
);
257 if (constant_index
) {
258 if (src
.file
== ATTR
) {
259 /* Attribute arrays get loaded as one vec4 per element. In that case
260 * offset the source register.
262 src
.reg
+= constant_index
->value
.i
[0];
264 assert(src
.file
== UNIFORM
|| src
.file
== GRF
|| src
.file
== HW_REG
);
265 src
= offset(src
, constant_index
->value
.i
[0] * element_size
);
268 /* Variable index array dereference. We attach the variable index
269 * component to the reg as a pointer to a register containing the
270 * offset. Currently only uniform arrays are supported in this patch,
271 * and that reladdr pointer is resolved by
272 * move_uniform_array_access_to_pull_constants(). All other array types
273 * are lowered by lower_variable_index_to_cond_assign().
275 ir
->array_index
->accept(this);
278 index_reg
= vgrf(glsl_type::int_type
);
279 emit(BRW_OPCODE_MUL
, index_reg
, this->result
, fs_reg(element_size
));
282 emit(BRW_OPCODE_ADD
, index_reg
, *src
.reladdr
, index_reg
);
285 src
.reladdr
= ralloc(mem_ctx
, fs_reg
);
286 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
292 fs_visitor::emit_lrp(const fs_reg
&dst
, const fs_reg
&x
, const fs_reg
&y
,
296 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
297 fs_reg y_times_a
= vgrf(glsl_type::float_type
);
298 fs_reg one_minus_a
= vgrf(glsl_type::float_type
);
299 fs_reg x_times_one_minus_a
= vgrf(glsl_type::float_type
);
301 emit(MUL(y_times_a
, y
, a
));
303 fs_reg negative_a
= a
;
304 negative_a
.negate
= !a
.negate
;
305 emit(ADD(one_minus_a
, negative_a
, fs_reg(1.0f
)));
306 emit(MUL(x_times_one_minus_a
, x
, one_minus_a
));
308 return emit(ADD(dst
, x_times_one_minus_a
, y_times_a
));
310 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
311 * we need to reorder the operands.
313 return emit(LRP(dst
, a
, y
, x
));
318 fs_visitor::emit_minmax(enum brw_conditional_mod conditionalmod
, const fs_reg
&dst
,
319 const fs_reg
&src0
, const fs_reg
&src1
)
321 assert(conditionalmod
== BRW_CONDITIONAL_GE
||
322 conditionalmod
== BRW_CONDITIONAL_L
);
327 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
328 inst
->conditional_mod
= conditionalmod
;
330 emit(CMP(reg_null_d
, src0
, src1
, conditionalmod
));
332 inst
= emit(BRW_OPCODE_SEL
, dst
, src0
, src1
);
333 inst
->predicate
= BRW_PREDICATE_NORMAL
;
338 fs_visitor::try_emit_saturate(ir_expression
*ir
)
340 if (ir
->operation
!= ir_unop_saturate
)
343 ir_rvalue
*sat_val
= ir
->operands
[0];
345 fs_inst
*pre_inst
= (fs_inst
*) this->instructions
.get_tail();
347 sat_val
->accept(this);
348 fs_reg src
= this->result
;
350 fs_inst
*last_inst
= (fs_inst
*) this->instructions
.get_tail();
352 /* If the last instruction from our accept() generated our
353 * src, just set the saturate flag instead of emmitting a separate mov.
355 fs_inst
*modify
= get_instruction_generating_reg(pre_inst
, last_inst
, src
);
356 if (modify
&& modify
->regs_written
== modify
->dst
.width
/ 8 &&
357 modify
->can_do_saturate()) {
358 modify
->saturate
= true;
367 fs_visitor::try_emit_line(ir_expression
*ir
)
369 /* LINE's src0 must be of type float. */
370 if (ir
->type
!= glsl_type::float_type
)
373 ir_rvalue
*nonmul
= ir
->operands
[1];
374 ir_expression
*mul
= ir
->operands
[0]->as_expression();
376 if (!mul
|| mul
->operation
!= ir_binop_mul
) {
377 nonmul
= ir
->operands
[0];
378 mul
= ir
->operands
[1]->as_expression();
380 if (!mul
|| mul
->operation
!= ir_binop_mul
)
384 ir_constant
*const_add
= nonmul
->as_constant();
388 int add_operand_vf
= brw_float_to_vf(const_add
->value
.f
[0]);
389 if (add_operand_vf
== -1)
392 ir_rvalue
*non_const_mul
= mul
->operands
[1];
393 ir_constant
*const_mul
= mul
->operands
[0]->as_constant();
395 const_mul
= mul
->operands
[1]->as_constant();
400 non_const_mul
= mul
->operands
[0];
403 int mul_operand_vf
= brw_float_to_vf(const_mul
->value
.f
[0]);
404 if (mul_operand_vf
== -1)
407 non_const_mul
->accept(this);
408 fs_reg src1
= this->result
;
410 fs_reg src0
= vgrf(ir
->type
);
411 emit(BRW_OPCODE_MOV
, src0
,
412 fs_reg((uint8_t)mul_operand_vf
, 0, 0, (uint8_t)add_operand_vf
));
414 this->result
= vgrf(ir
->type
);
415 emit(BRW_OPCODE_LINE
, this->result
, src0
, src1
);
420 fs_visitor::try_emit_mad(ir_expression
*ir
)
422 /* 3-src instructions were introduced in gen6. */
426 /* MAD can only handle floating-point data. */
427 if (ir
->type
!= glsl_type::float_type
)
432 bool mul_negate
, mul_abs
;
434 for (int i
= 0; i
< 2; i
++) {
438 mul
= ir
->operands
[i
]->as_expression();
439 nonmul
= ir
->operands
[1 - i
];
441 if (mul
&& mul
->operation
== ir_unop_abs
) {
442 mul
= mul
->operands
[0]->as_expression();
444 } else if (mul
&& mul
->operation
== ir_unop_neg
) {
445 mul
= mul
->operands
[0]->as_expression();
449 if (mul
&& mul
->operation
== ir_binop_mul
)
453 if (!mul
|| mul
->operation
!= ir_binop_mul
)
456 nonmul
->accept(this);
457 fs_reg src0
= this->result
;
459 mul
->operands
[0]->accept(this);
460 fs_reg src1
= this->result
;
461 src1
.negate
^= mul_negate
;
466 mul
->operands
[1]->accept(this);
467 fs_reg src2
= this->result
;
472 this->result
= vgrf(ir
->type
);
473 emit(BRW_OPCODE_MAD
, this->result
, src0
, src1
, src2
);
479 fs_visitor::try_emit_b2f_of_comparison(ir_expression
*ir
)
481 /* On platforms that do not natively generate 0u and ~0u for Boolean
482 * results, b2f expressions that look like
484 * f = b2f(expr cmp 0)
486 * will generate better code by pretending the expression is
488 * f = ir_triop_csel(0.0, 1.0, expr cmp 0)
490 * This is because the last instruction of "expr" can generate the
491 * condition code for the "cmp 0". This avoids having to do the "-(b & 1)"
492 * trick to generate 0u or ~0u for the Boolean result. This means code like
495 * mul.ge.f0(16) null g6<8,8,1>F g14<8,8,1>F
496 * (+f0) sel(16) m6<1>F g16<8,8,1>F 0F
498 * will be generated instead of
500 * mul(16) g2<1>F g12<8,8,1>F g4<8,8,1>F
501 * cmp.ge.f0(16) g2<1>D g4<8,8,1>F 0F
502 * and(16) g4<1>D g2<8,8,1>D 1D
503 * and(16) m6<1>D -g4<8,8,1>D 0x3f800000UD
505 * When the comparison is either == 0.0 or != 0.0 using the knowledge that
506 * the true (or false) case already results in zero would allow better code
507 * generation by possibly avoiding a load-immediate instruction.
509 ir_expression
*cmp
= ir
->operands
[0]->as_expression();
513 if (cmp
->operation
== ir_binop_equal
|| cmp
->operation
== ir_binop_nequal
) {
514 for (unsigned i
= 0; i
< 2; i
++) {
515 ir_constant
*c
= cmp
->operands
[i
]->as_constant();
516 if (c
== NULL
|| !c
->is_zero())
519 ir_expression
*expr
= cmp
->operands
[i
^ 1]->as_expression();
523 for (unsigned j
= 0; j
< 2; j
++) {
524 cmp
->operands
[j
]->accept(this);
525 op
[j
] = this->result
;
527 resolve_ud_negate(&op
[j
]);
530 emit_bool_to_cond_code_of_reg(cmp
, op
);
532 /* In this case we know when the condition is true, op[i ^ 1]
533 * contains zero. Invert the predicate, use op[i ^ 1] as src0,
534 * and immediate 1.0f as src1.
536 this->result
= vgrf(ir
->type
);
537 op
[i
^ 1].type
= BRW_REGISTER_TYPE_F
;
539 fs_inst
*inst
= emit(SEL(this->result
, op
[i
^ 1], fs_reg(1.0f
)));
540 inst
->predicate
= BRW_PREDICATE_NORMAL
;
541 inst
->predicate_inverse
= cmp
->operation
== ir_binop_equal
;
547 emit_bool_to_cond_code(cmp
);
549 fs_reg temp
= vgrf(ir
->type
);
550 emit(MOV(temp
, fs_reg(1.0f
)));
552 this->result
= vgrf(ir
->type
);
553 fs_inst
*inst
= emit(SEL(this->result
, temp
, fs_reg(0.0f
)));
554 inst
->predicate
= BRW_PREDICATE_NORMAL
;
560 pack_pixel_offset(float x
)
562 /* Clamp upper end of the range to +7/16. See explanation in non-constant
563 * offset case below. */
564 int n
= MIN2((int)(x
* 16), 7);
569 fs_visitor::emit_interpolate_expression(ir_expression
*ir
)
571 /* in SIMD16 mode, the pixel interpolator returns coords interleaved
572 * 8 channels at a time, same as the barycentric coords presented in
573 * the FS payload. this requires a bit of extra work to support.
575 no16("interpolate_at_* not yet supported in SIMD16 mode.");
577 assert(stage
== MESA_SHADER_FRAGMENT
);
578 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
580 ir_dereference
* deref
= ir
->operands
[0]->as_dereference();
581 ir_swizzle
* swiz
= NULL
;
583 /* the api does not allow a swizzle here, but the varying packing code
584 * may have pushed one into here.
586 swiz
= ir
->operands
[0]->as_swizzle();
588 deref
= swiz
->val
->as_dereference();
591 ir_variable
* var
= deref
->variable_referenced();
594 /* 1. collect interpolation factors */
596 fs_reg dst_x
= vgrf(glsl_type::get_instance(ir
->type
->base_type
, 2, 1));
597 fs_reg dst_y
= offset(dst_x
, 1);
599 /* for most messages, we need one reg of ignored data; the hardware requires mlen==1
600 * even when there is no payload. in the per-slot offset case, we'll replace this with
601 * the proper source data. */
602 fs_reg src
= vgrf(glsl_type::float_type
);
603 int mlen
= 1; /* one reg unless overriden */
604 int reg_width
= dispatch_width
/ 8;
607 switch (ir
->operation
) {
608 case ir_unop_interpolate_at_centroid
:
609 inst
= emit(FS_OPCODE_INTERPOLATE_AT_CENTROID
, dst_x
, src
, fs_reg(0u));
612 case ir_binop_interpolate_at_sample
: {
613 ir_constant
*sample_num
= ir
->operands
[1]->as_constant();
614 assert(sample_num
|| !"nonconstant sample number should have been lowered.");
616 unsigned msg_data
= sample_num
->value
.i
[0] << 4;
617 inst
= emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE
, dst_x
, src
, fs_reg(msg_data
));
621 case ir_binop_interpolate_at_offset
: {
622 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
624 unsigned msg_data
= pack_pixel_offset(const_offset
->value
.f
[0]) |
625 (pack_pixel_offset(const_offset
->value
.f
[1]) << 4);
626 inst
= emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
, dst_x
, src
,
629 /* pack the operands: hw wants offsets as 4 bit signed ints */
630 ir
->operands
[1]->accept(this);
631 src
= vgrf(glsl_type::ivec2_type
);
633 for (int i
= 0; i
< 2; i
++) {
634 fs_reg temp
= vgrf(glsl_type::float_type
);
635 emit(MUL(temp
, this->result
, fs_reg(16.0f
)));
636 emit(MOV(src2
, temp
)); /* float to int */
638 /* Clamp the upper end of the range to +7/16. ARB_gpu_shader5 requires
639 * that we support a maximum offset of +0.5, which isn't representable
640 * in a S0.4 value -- if we didn't clamp it, we'd end up with -8/16,
641 * which is the opposite of what the shader author wanted.
643 * This is legal due to ARB_gpu_shader5's quantization rules:
645 * "Not all values of <offset> may be supported; x and y offsets may
646 * be rounded to fixed-point values with the number of fraction bits
647 * given by the implementation-dependent constant
648 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
651 fs_inst
*inst
= emit(BRW_OPCODE_SEL
, src2
, src2
, fs_reg(7));
652 inst
->conditional_mod
= BRW_CONDITIONAL_L
; /* min(src2, 7) */
654 src2
= offset(src2
, 1);
655 this->result
= offset(this->result
, 1);
658 mlen
= 2 * reg_width
;
659 inst
= emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
, dst_x
, src
,
666 unreachable("not reached");
670 inst
->regs_written
= 2 * reg_width
; /* 2 floats per slot returned */
671 inst
->pi_noperspective
= var
->determine_interpolation_mode(key
->flat_shade
) ==
672 INTERP_QUALIFIER_NOPERSPECTIVE
;
674 /* 2. emit linterp */
676 fs_reg res
= vgrf(ir
->type
);
679 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
680 int ch
= swiz
? ((*(int *)&swiz
->mask
) >> 2*i
) & 3 : i
;
681 emit(FS_OPCODE_LINTERP
, res
,
683 fs_reg(interp_reg(var
->data
.location
, ch
)));
684 res
= offset(res
, 1);
689 fs_visitor::visit(ir_expression
*ir
)
691 unsigned int operand
;
694 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
696 assert(ir
->get_num_operands() <= 3);
698 if (try_emit_saturate(ir
))
701 /* Deal with the real oddball stuff first */
702 switch (ir
->operation
) {
704 if (brw
->gen
<= 5 && try_emit_line(ir
))
706 if (try_emit_mad(ir
))
711 ir
->operands
[1]->accept(this);
712 op
[1] = this->result
;
713 ir
->operands
[2]->accept(this);
714 op
[2] = this->result
;
716 emit_bool_to_cond_code(ir
->operands
[0]);
718 this->result
= vgrf(ir
->type
);
719 inst
= emit(SEL(this->result
, op
[1], op
[2]));
720 inst
->predicate
= BRW_PREDICATE_NORMAL
;
724 if (brw
->gen
<= 5 && try_emit_b2f_of_comparison(ir
))
728 case ir_unop_interpolate_at_centroid
:
729 case ir_binop_interpolate_at_offset
:
730 case ir_binop_interpolate_at_sample
:
731 emit_interpolate_expression(ir
);
738 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
739 ir
->operands
[operand
]->accept(this);
740 if (this->result
.file
== BAD_FILE
) {
741 fail("Failed to get tree for expression operand:\n");
742 ir
->operands
[operand
]->fprint(stderr
);
743 fprintf(stderr
, "\n");
745 assert(this->result
.file
== GRF
||
746 this->result
.file
== UNIFORM
|| this->result
.file
== ATTR
);
747 op
[operand
] = this->result
;
749 /* Matrix expression operands should have been broken down to vector
750 * operations already.
752 assert(!ir
->operands
[operand
]->type
->is_matrix());
753 /* And then those vector operands should have been broken down to scalar.
755 assert(!ir
->operands
[operand
]->type
->is_vector());
758 /* Storage for our result. If our result goes into an assignment, it will
759 * just get copy-propagated out, so no worries.
761 this->result
= vgrf(ir
->type
);
763 switch (ir
->operation
) {
764 case ir_unop_logic_not
:
765 emit(NOT(this->result
, op
[0]));
768 op
[0].negate
= !op
[0].negate
;
769 emit(MOV(this->result
, op
[0]));
773 op
[0].negate
= false;
774 emit(MOV(this->result
, op
[0]));
777 if (ir
->type
->is_float()) {
778 /* AND(val, 0x80000000) gives the sign bit.
780 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
783 emit(CMP(reg_null_f
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
785 op
[0].type
= BRW_REGISTER_TYPE_UD
;
786 this->result
.type
= BRW_REGISTER_TYPE_UD
;
787 emit(AND(this->result
, op
[0], fs_reg(0x80000000u
)));
789 inst
= emit(OR(this->result
, this->result
, fs_reg(0x3f800000u
)));
790 inst
->predicate
= BRW_PREDICATE_NORMAL
;
792 this->result
.type
= BRW_REGISTER_TYPE_F
;
794 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
795 * -> non-negative val generates 0x00000000.
796 * Predicated OR sets 1 if val is positive.
798 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_G
));
800 emit(ASR(this->result
, op
[0], fs_reg(31)));
802 inst
= emit(OR(this->result
, this->result
, fs_reg(1)));
803 inst
->predicate
= BRW_PREDICATE_NORMAL
;
807 emit_math(SHADER_OPCODE_RCP
, this->result
, op
[0]);
811 emit_math(SHADER_OPCODE_EXP2
, this->result
, op
[0]);
814 emit_math(SHADER_OPCODE_LOG2
, this->result
, op
[0]);
818 unreachable("not reached: should be handled by ir_explog_to_explog2");
820 emit_math(SHADER_OPCODE_SIN
, this->result
, op
[0]);
823 emit_math(SHADER_OPCODE_COS
, this->result
, op
[0]);
827 /* Select one of the two opcodes based on the glHint value. */
828 if (fs_key
->high_quality_derivatives
)
829 emit(FS_OPCODE_DDX_FINE
, this->result
, op
[0]);
831 emit(FS_OPCODE_DDX_COARSE
, this->result
, op
[0]);
834 case ir_unop_dFdx_coarse
:
835 emit(FS_OPCODE_DDX_COARSE
, this->result
, op
[0]);
838 case ir_unop_dFdx_fine
:
839 emit(FS_OPCODE_DDX_FINE
, this->result
, op
[0]);
843 /* Select one of the two opcodes based on the glHint value. */
844 if (fs_key
->high_quality_derivatives
)
845 emit(FS_OPCODE_DDY_FINE
, result
, op
[0], fs_reg(fs_key
->render_to_fbo
));
847 emit(FS_OPCODE_DDY_COARSE
, result
, op
[0], fs_reg(fs_key
->render_to_fbo
));
850 case ir_unop_dFdy_coarse
:
851 emit(FS_OPCODE_DDY_COARSE
, result
, op
[0], fs_reg(fs_key
->render_to_fbo
));
854 case ir_unop_dFdy_fine
:
855 emit(FS_OPCODE_DDY_FINE
, result
, op
[0], fs_reg(fs_key
->render_to_fbo
));
859 emit(ADD(this->result
, op
[0], op
[1]));
862 unreachable("not reached: should be handled by ir_sub_to_add_neg");
865 if (brw
->gen
< 8 && ir
->type
->is_integer()) {
866 /* For integer multiplication, the MUL uses the low 16 bits
867 * of one of the operands (src0 on gen6, src1 on gen7). The
868 * MACH accumulates in the contribution of the upper 16 bits
871 if (ir
->operands
[0]->is_uint16_constant()) {
873 emit(MUL(this->result
, op
[0], op
[1]));
875 emit(MUL(this->result
, op
[1], op
[0]));
876 } else if (ir
->operands
[1]->is_uint16_constant()) {
878 emit(MUL(this->result
, op
[1], op
[0]));
880 emit(MUL(this->result
, op
[0], op
[1]));
883 no16("SIMD16 explicit accumulator operands unsupported\n");
885 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
888 emit(MUL(acc
, op
[0], op
[1]));
889 emit(MACH(reg_null_d
, op
[0], op
[1]));
890 emit(MOV(this->result
, fs_reg(acc
)));
893 emit(MUL(this->result
, op
[0], op
[1]));
896 case ir_binop_imul_high
: {
898 no16("SIMD16 explicit accumulator operands unsupported\n");
900 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
903 fs_inst
*mul
= emit(MUL(acc
, op
[0], op
[1]));
904 emit(MACH(this->result
, op
[0], op
[1]));
906 /* Until Gen8, integer multiplies read 32-bits from one source, and
907 * 16-bits from the other, and relying on the MACH instruction to
908 * generate the high bits of the result.
910 * On Gen8, the multiply instruction does a full 32x32-bit multiply,
911 * but in order to do a 64x64-bit multiply we have to simulate the
912 * previous behavior and then use a MACH instruction.
914 * FINISHME: Don't use source modifiers on src1.
917 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
918 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
919 if (mul
->src
[1].type
== BRW_REGISTER_TYPE_D
) {
920 mul
->src
[1].type
= BRW_REGISTER_TYPE_W
;
922 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
929 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
930 assert(ir
->type
->is_integer());
931 emit_math(SHADER_OPCODE_INT_QUOTIENT
, this->result
, op
[0], op
[1]);
933 case ir_binop_carry
: {
935 no16("SIMD16 explicit accumulator operands unsupported\n");
937 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
938 BRW_REGISTER_TYPE_UD
);
940 emit(ADDC(reg_null_ud
, op
[0], op
[1]));
941 emit(MOV(this->result
, fs_reg(acc
)));
944 case ir_binop_borrow
: {
946 no16("SIMD16 explicit accumulator operands unsupported\n");
948 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
949 BRW_REGISTER_TYPE_UD
);
951 emit(SUBB(reg_null_ud
, op
[0], op
[1]));
952 emit(MOV(this->result
, fs_reg(acc
)));
956 /* Floating point should be lowered by MOD_TO_FLOOR in the compiler. */
957 assert(ir
->type
->is_integer());
958 emit_math(SHADER_OPCODE_INT_REMAINDER
, this->result
, op
[0], op
[1]);
962 case ir_binop_greater
:
963 case ir_binop_lequal
:
964 case ir_binop_gequal
:
966 case ir_binop_all_equal
:
967 case ir_binop_nequal
:
968 case ir_binop_any_nequal
:
970 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
971 resolve_bool_comparison(ir
->operands
[1], &op
[1]);
974 emit(CMP(this->result
, op
[0], op
[1],
975 brw_conditional_for_comparison(ir
->operation
)));
978 case ir_binop_logic_xor
:
979 emit(XOR(this->result
, op
[0], op
[1]));
982 case ir_binop_logic_or
:
983 emit(OR(this->result
, op
[0], op
[1]));
986 case ir_binop_logic_and
:
987 emit(AND(this->result
, op
[0], op
[1]));
992 unreachable("not reached: should be handled by brw_fs_channel_expressions");
995 unreachable("not reached: should be handled by lower_noise");
997 case ir_quadop_vector
:
998 unreachable("not reached: should be handled by lower_quadop_vector");
1000 case ir_binop_vector_extract
:
1001 unreachable("not reached: should be handled by lower_vec_index_to_cond_assign()");
1003 case ir_triop_vector_insert
:
1004 unreachable("not reached: should be handled by lower_vector_insert()");
1006 case ir_binop_ldexp
:
1007 unreachable("not reached: should be handled by ldexp_to_arith()");
1010 emit_math(SHADER_OPCODE_SQRT
, this->result
, op
[0]);
1014 emit_math(SHADER_OPCODE_RSQ
, this->result
, op
[0]);
1017 case ir_unop_bitcast_i2f
:
1018 case ir_unop_bitcast_u2f
:
1019 op
[0].type
= BRW_REGISTER_TYPE_F
;
1020 this->result
= op
[0];
1023 case ir_unop_bitcast_f2u
:
1024 op
[0].type
= BRW_REGISTER_TYPE_UD
;
1025 this->result
= op
[0];
1028 case ir_unop_bitcast_f2i
:
1029 op
[0].type
= BRW_REGISTER_TYPE_D
;
1030 this->result
= op
[0];
1036 emit(MOV(this->result
, op
[0]));
1040 emit(AND(this->result
, op
[0], fs_reg(1)));
1043 if (brw
->gen
<= 5) {
1044 resolve_bool_comparison(ir
->operands
[0], &op
[0]);
1046 op
[0].type
= BRW_REGISTER_TYPE_D
;
1047 this->result
.type
= BRW_REGISTER_TYPE_D
;
1048 emit(AND(this->result
, op
[0], fs_reg(0x3f800000u
)));
1049 this->result
.type
= BRW_REGISTER_TYPE_F
;
1053 emit(CMP(this->result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
1056 emit(CMP(this->result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
1060 emit(RNDZ(this->result
, op
[0]));
1062 case ir_unop_ceil
: {
1063 fs_reg tmp
= vgrf(ir
->type
);
1064 op
[0].negate
= !op
[0].negate
;
1065 emit(RNDD(tmp
, op
[0]));
1067 emit(MOV(this->result
, tmp
));
1071 emit(RNDD(this->result
, op
[0]));
1074 emit(FRC(this->result
, op
[0]));
1076 case ir_unop_round_even
:
1077 emit(RNDE(this->result
, op
[0]));
1082 resolve_ud_negate(&op
[0]);
1083 resolve_ud_negate(&op
[1]);
1084 emit_minmax(ir
->operation
== ir_binop_min
?
1085 BRW_CONDITIONAL_L
: BRW_CONDITIONAL_GE
,
1086 this->result
, op
[0], op
[1]);
1088 case ir_unop_pack_snorm_2x16
:
1089 case ir_unop_pack_snorm_4x8
:
1090 case ir_unop_pack_unorm_2x16
:
1091 case ir_unop_pack_unorm_4x8
:
1092 case ir_unop_unpack_snorm_2x16
:
1093 case ir_unop_unpack_snorm_4x8
:
1094 case ir_unop_unpack_unorm_2x16
:
1095 case ir_unop_unpack_unorm_4x8
:
1096 case ir_unop_unpack_half_2x16
:
1097 case ir_unop_pack_half_2x16
:
1098 unreachable("not reached: should be handled by lower_packing_builtins");
1099 case ir_unop_unpack_half_2x16_split_x
:
1100 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, this->result
, op
[0]);
1102 case ir_unop_unpack_half_2x16_split_y
:
1103 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, this->result
, op
[0]);
1106 emit_math(SHADER_OPCODE_POW
, this->result
, op
[0], op
[1]);
1109 case ir_unop_bitfield_reverse
:
1110 emit(BFREV(this->result
, op
[0]));
1112 case ir_unop_bit_count
:
1113 emit(CBIT(this->result
, op
[0]));
1115 case ir_unop_find_msb
:
1116 temp
= vgrf(glsl_type::uint_type
);
1117 emit(FBH(temp
, op
[0]));
1119 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1120 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1121 * subtract the result from 31 to convert the MSB count into an LSB count.
1124 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
1125 emit(MOV(this->result
, temp
));
1126 emit(CMP(reg_null_d
, this->result
, fs_reg(-1), BRW_CONDITIONAL_NZ
));
1129 inst
= emit(ADD(this->result
, temp
, fs_reg(31)));
1130 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1132 case ir_unop_find_lsb
:
1133 emit(FBL(this->result
, op
[0]));
1135 case ir_unop_saturate
:
1136 inst
= emit(MOV(this->result
, op
[0]));
1137 inst
->saturate
= true;
1139 case ir_triop_bitfield_extract
:
1140 /* Note that the instruction's argument order is reversed from GLSL
1143 emit(BFE(this->result
, op
[2], op
[1], op
[0]));
1146 emit(BFI1(this->result
, op
[0], op
[1]));
1149 emit(BFI2(this->result
, op
[0], op
[1], op
[2]));
1151 case ir_quadop_bitfield_insert
:
1152 unreachable("not reached: should be handled by "
1153 "lower_instructions::bitfield_insert_to_bfm_bfi");
1155 case ir_unop_bit_not
:
1156 emit(NOT(this->result
, op
[0]));
1158 case ir_binop_bit_and
:
1159 emit(AND(this->result
, op
[0], op
[1]));
1161 case ir_binop_bit_xor
:
1162 emit(XOR(this->result
, op
[0], op
[1]));
1164 case ir_binop_bit_or
:
1165 emit(OR(this->result
, op
[0], op
[1]));
1168 case ir_binop_lshift
:
1169 emit(SHL(this->result
, op
[0], op
[1]));
1172 case ir_binop_rshift
:
1173 if (ir
->type
->base_type
== GLSL_TYPE_INT
)
1174 emit(ASR(this->result
, op
[0], op
[1]));
1176 emit(SHR(this->result
, op
[0], op
[1]));
1178 case ir_binop_pack_half_2x16_split
:
1179 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, this->result
, op
[0], op
[1]);
1181 case ir_binop_ubo_load
: {
1182 /* This IR node takes a constant uniform block and a constant or
1183 * variable byte offset within the block and loads a vector from that.
1185 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
1186 ir_constant
*const_offset
= ir
->operands
[1]->as_constant();
1189 if (const_uniform_block
) {
1190 /* The block index is a constant, so just emit the binding table entry
1193 surf_index
= fs_reg(stage_prog_data
->binding_table
.ubo_start
+
1194 const_uniform_block
->value
.u
[0]);
1196 /* The block index is not a constant. Evaluate the index expression
1197 * per-channel and add the base UBO index; the generator will select
1198 * a value from any live channel.
1200 surf_index
= vgrf(glsl_type::uint_type
);
1201 emit(ADD(surf_index
, op
[0],
1202 fs_reg(stage_prog_data
->binding_table
.ubo_start
)))
1203 ->force_writemask_all
= true;
1205 /* Assume this may touch any UBO. It would be nice to provide
1206 * a tighter bound, but the array information is already lowered away.
1208 brw_mark_surface_used(prog_data
,
1209 stage_prog_data
->binding_table
.ubo_start
+
1210 shader_prog
->NumUniformBlocks
- 1);
1214 fs_reg packed_consts
= vgrf(glsl_type::float_type
);
1215 packed_consts
.type
= result
.type
;
1217 fs_reg const_offset_reg
= fs_reg(const_offset
->value
.u
[0] & ~15);
1218 emit(new(mem_ctx
) fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, 8,
1219 packed_consts
, surf_index
, const_offset_reg
));
1221 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1222 packed_consts
.set_smear(const_offset
->value
.u
[0] % 16 / 4 + i
);
1224 /* The std140 packing rules don't allow vectors to cross 16-byte
1225 * boundaries, and a reg is 32 bytes.
1227 assert(packed_consts
.subreg_offset
< 32);
1229 /* UBO bools are any nonzero value. We consider bools to be
1230 * values with the low bit set to 1. Convert them using CMP.
1232 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1233 emit(CMP(result
, packed_consts
, fs_reg(0u), BRW_CONDITIONAL_NZ
));
1235 emit(MOV(result
, packed_consts
));
1238 result
= offset(result
, 1);
1241 /* Turn the byte offset into a dword offset. */
1242 fs_reg base_offset
= vgrf(glsl_type::int_type
);
1243 emit(SHR(base_offset
, op
[1], fs_reg(2)));
1245 for (int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1246 emit(VARYING_PULL_CONSTANT_LOAD(result
, surf_index
,
1249 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1250 emit(CMP(result
, result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
1252 result
= offset(result
, 1);
1256 result
.reg_offset
= 0;
1261 /* Note that the instruction's argument order is reversed from GLSL
1264 emit(MAD(this->result
, op
[2], op
[1], op
[0]));
1268 emit_lrp(this->result
, op
[0], op
[1], op
[2]);
1272 case ir_unop_interpolate_at_centroid
:
1273 case ir_binop_interpolate_at_offset
:
1274 case ir_binop_interpolate_at_sample
:
1275 unreachable("already handled above");
1285 case ir_unop_pack_double_2x32
:
1286 case ir_unop_unpack_double_2x32
:
1287 case ir_unop_frexp_sig
:
1288 case ir_unop_frexp_exp
:
1289 unreachable("fp64 todo");
1295 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
1296 const glsl_type
*type
, bool predicated
)
1298 switch (type
->base_type
) {
1299 case GLSL_TYPE_FLOAT
:
1300 case GLSL_TYPE_UINT
:
1302 case GLSL_TYPE_BOOL
:
1303 for (unsigned int i
= 0; i
< type
->components(); i
++) {
1304 l
.type
= brw_type_for_base_type(type
);
1305 r
.type
= brw_type_for_base_type(type
);
1307 if (predicated
|| !l
.equals(r
)) {
1308 fs_inst
*inst
= emit(MOV(l
, r
));
1309 inst
->predicate
= predicated
? BRW_PREDICATE_NORMAL
: BRW_PREDICATE_NONE
;
1316 case GLSL_TYPE_ARRAY
:
1317 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1318 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
1322 case GLSL_TYPE_STRUCT
:
1323 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1324 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
1329 case GLSL_TYPE_SAMPLER
:
1330 case GLSL_TYPE_IMAGE
:
1331 case GLSL_TYPE_ATOMIC_UINT
:
1334 case GLSL_TYPE_DOUBLE
:
1335 case GLSL_TYPE_VOID
:
1336 case GLSL_TYPE_ERROR
:
1337 case GLSL_TYPE_INTERFACE
:
1338 unreachable("not reached");
1342 /* If the RHS processing resulted in an instruction generating a
1343 * temporary value, and it would be easy to rewrite the instruction to
1344 * generate its result right into the LHS instead, do so. This ends
1345 * up reliably removing instructions where it can be tricky to do so
1346 * later without real UD chain information.
1349 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment
*ir
,
1352 fs_inst
*pre_rhs_inst
,
1353 fs_inst
*last_rhs_inst
)
1355 /* Only attempt if we're doing a direct assignment. */
1356 if (ir
->condition
||
1357 !(ir
->lhs
->type
->is_scalar() ||
1358 (ir
->lhs
->type
->is_vector() &&
1359 ir
->write_mask
== (1 << ir
->lhs
->type
->vector_elements
) - 1)))
1362 /* Make sure the last instruction generated our source reg. */
1363 fs_inst
*modify
= get_instruction_generating_reg(pre_rhs_inst
,
1369 /* If last_rhs_inst wrote a different number of components than our LHS,
1370 * we can't safely rewrite it.
1372 if (alloc
.sizes
[dst
.reg
] != modify
->regs_written
)
1375 /* Success! Rewrite the instruction. */
1382 fs_visitor::visit(ir_assignment
*ir
)
1387 /* FINISHME: arrays on the lhs */
1388 ir
->lhs
->accept(this);
1391 fs_inst
*pre_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
1393 ir
->rhs
->accept(this);
1396 fs_inst
*last_rhs_inst
= (fs_inst
*) this->instructions
.get_tail();
1398 assert(l
.file
!= BAD_FILE
);
1399 assert(r
.file
!= BAD_FILE
);
1401 if (try_rewrite_rhs_to_dst(ir
, l
, r
, pre_rhs_inst
, last_rhs_inst
))
1404 if (ir
->condition
) {
1405 emit_bool_to_cond_code(ir
->condition
);
1408 if (ir
->lhs
->type
->is_scalar() ||
1409 ir
->lhs
->type
->is_vector()) {
1410 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1411 if (ir
->write_mask
& (1 << i
)) {
1412 inst
= emit(MOV(l
, r
));
1414 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1420 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
1425 fs_visitor::emit_texture_gen4(ir_texture_opcode op
, fs_reg dst
,
1426 fs_reg coordinate
, int coord_components
,
1428 fs_reg lod
, fs_reg dPdy
, int grad_components
,
1433 bool simd16
= false;
1439 if (shadow_c
.file
!= BAD_FILE
) {
1440 for (int i
= 0; i
< coord_components
; i
++) {
1441 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1442 coordinate
= offset(coordinate
, 1);
1445 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
1446 * the unused slots must be zeroed.
1448 for (int i
= coord_components
; i
< 3; i
++) {
1449 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
1454 /* There's no plain shadow compare message, so we use shadow
1455 * compare with a bias of 0.0.
1457 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), fs_reg(0.0f
)));
1459 } else if (op
== ir_txb
|| op
== ir_txl
) {
1460 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), lod
));
1463 unreachable("Should not get here.");
1466 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), shadow_c
));
1468 } else if (op
== ir_tex
) {
1469 for (int i
= 0; i
< coord_components
; i
++) {
1470 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1471 coordinate
= offset(coordinate
, 1);
1473 /* zero the others. */
1474 for (int i
= coord_components
; i
<3; i
++) {
1475 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), fs_reg(0.0f
)));
1477 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1479 } else if (op
== ir_txd
) {
1482 for (int i
= 0; i
< coord_components
; i
++) {
1483 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
), coordinate
));
1484 coordinate
= offset(coordinate
, 1);
1486 /* the slots for u and v are always present, but r is optional */
1487 mlen
+= MAX2(coord_components
, 2);
1490 * dPdx = dudx, dvdx, drdx
1491 * dPdy = dudy, dvdy, drdy
1493 * 1-arg: Does not exist.
1495 * 2-arg: dudx dvdx dudy dvdy
1496 * dPdx.x dPdx.y dPdy.x dPdy.y
1499 * 3-arg: dudx dvdx drdx dudy dvdy drdy
1500 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
1501 * m5 m6 m7 m8 m9 m10
1503 for (int i
= 0; i
< grad_components
; i
++) {
1504 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdx
));
1505 dPdx
= offset(dPdx
, 1);
1507 mlen
+= MAX2(grad_components
, 2);
1509 for (int i
= 0; i
< grad_components
; i
++) {
1510 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
), dPdy
));
1511 dPdy
= offset(dPdy
, 1);
1513 mlen
+= MAX2(grad_components
, 2);
1514 } else if (op
== ir_txs
) {
1515 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
1517 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, BRW_REGISTER_TYPE_UD
), lod
));
1520 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
1521 * instructions. We'll need to do SIMD16 here.
1524 assert(op
== ir_txb
|| op
== ir_txl
|| op
== ir_txf
);
1526 for (int i
= 0; i
< coord_components
; i
++) {
1527 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2, coordinate
.type
),
1529 coordinate
= offset(coordinate
, 1);
1532 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
1533 * be necessary for TXF (ld), but seems wise to do for all messages.
1535 for (int i
= coord_components
; i
< 3; i
++) {
1536 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
+ i
* 2), fs_reg(0.0f
)));
1539 /* lod/bias appears after u/v/r. */
1542 emit(MOV(fs_reg(MRF
, base_mrf
+ mlen
, lod
.type
), lod
));
1545 /* The unused upper half. */
1550 /* Now, since we're doing simd16, the return is 2 interleaved
1551 * vec4s where the odd-indexed ones are junk. We'll need to move
1552 * this weirdness around to the expected layout.
1555 dst
= fs_reg(GRF
, alloc
.allocate(8), orig_dst
.type
);
1560 case ir_tex
: opcode
= SHADER_OPCODE_TEX
; break;
1561 case ir_txb
: opcode
= FS_OPCODE_TXB
; break;
1562 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
1563 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
1564 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
1565 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
1567 unreachable("not reached");
1570 fs_inst
*inst
= emit(opcode
, dst
, reg_undef
, fs_reg(sampler
));
1571 inst
->base_mrf
= base_mrf
;
1573 inst
->header_present
= true;
1574 inst
->regs_written
= simd16
? 8 : 4;
1577 for (int i
= 0; i
< 4; i
++) {
1578 emit(MOV(orig_dst
, dst
));
1579 orig_dst
= offset(orig_dst
, 1);
1580 dst
= offset(dst
, 2);
1588 fs_visitor::emit_texture_gen4_simd16(ir_texture_opcode op
, fs_reg dst
,
1589 fs_reg coordinate
, int vector_elements
,
1590 fs_reg shadow_c
, fs_reg lod
,
1593 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
, dispatch_width
);
1594 bool has_lod
= op
== ir_txl
|| op
== ir_txb
|| op
== ir_txf
;
1596 if (has_lod
&& shadow_c
.file
!= BAD_FILE
)
1597 no16("TXB and TXL with shadow comparison unsupported in SIMD16.");
1600 no16("textureGrad unsupported in SIMD16.");
1602 /* Copy the coordinates. */
1603 for (int i
= 0; i
< vector_elements
; i
++) {
1604 emit(MOV(retype(offset(message
, i
), coordinate
.type
), coordinate
));
1605 coordinate
= offset(coordinate
, 1);
1608 fs_reg msg_end
= offset(message
, vector_elements
);
1610 /* Messages other than sample and ld require all three components */
1611 if (has_lod
|| shadow_c
.file
!= BAD_FILE
) {
1612 for (int i
= vector_elements
; i
< 3; i
++) {
1613 emit(MOV(offset(message
, i
), fs_reg(0.0f
)));
1618 fs_reg msg_lod
= retype(offset(message
, 3), op
== ir_txf
?
1619 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
1620 emit(MOV(msg_lod
, lod
));
1621 msg_end
= offset(msg_lod
, 1);
1624 if (shadow_c
.file
!= BAD_FILE
) {
1625 fs_reg msg_ref
= offset(message
, 3 + has_lod
);
1626 emit(MOV(msg_ref
, shadow_c
));
1627 msg_end
= offset(msg_ref
, 1);
1632 case ir_tex
: opcode
= SHADER_OPCODE_TEX
; break;
1633 case ir_txb
: opcode
= FS_OPCODE_TXB
; break;
1634 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
1635 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
1636 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
1637 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
1638 default: unreachable("not reached");
1641 fs_inst
*inst
= emit(opcode
, dst
, reg_undef
, fs_reg(sampler
));
1642 inst
->base_mrf
= message
.reg
- 1;
1643 inst
->mlen
= msg_end
.reg
- inst
->base_mrf
;
1644 inst
->header_present
= true;
1645 inst
->regs_written
= 8;
1650 /* gen5's sampler has slots for u, v, r, array index, then optional
1651 * parameters like shadow comparitor or LOD bias. If optional
1652 * parameters aren't present, those base slots are optional and don't
1653 * need to be included in the message.
1655 * We don't fill in the unnecessary slots regardless, which may look
1656 * surprising in the disassembly.
1659 fs_visitor::emit_texture_gen5(ir_texture_opcode op
, fs_reg dst
,
1660 fs_reg coordinate
, int vector_elements
,
1662 fs_reg lod
, fs_reg lod2
, int grad_components
,
1663 fs_reg sample_index
, uint32_t sampler
,
1666 int reg_width
= dispatch_width
/ 8;
1667 bool header_present
= false;
1669 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
, dispatch_width
);
1670 fs_reg msg_coords
= message
;
1673 /* The offsets set up by the ir_texture visitor are in the
1674 * m1 header, so we can't go headerless.
1676 header_present
= true;
1680 for (int i
= 0; i
< vector_elements
; i
++) {
1681 emit(MOV(retype(offset(msg_coords
, i
), coordinate
.type
), coordinate
));
1682 coordinate
= offset(coordinate
, 1);
1684 fs_reg msg_end
= offset(msg_coords
, vector_elements
);
1685 fs_reg msg_lod
= offset(msg_coords
, 4);
1687 if (shadow_c
.file
!= BAD_FILE
) {
1688 fs_reg msg_shadow
= msg_lod
;
1689 emit(MOV(msg_shadow
, shadow_c
));
1690 msg_lod
= offset(msg_shadow
, 1);
1697 opcode
= SHADER_OPCODE_TEX
;
1700 emit(MOV(msg_lod
, lod
));
1701 msg_end
= offset(msg_lod
, 1);
1703 opcode
= FS_OPCODE_TXB
;
1706 emit(MOV(msg_lod
, lod
));
1707 msg_end
= offset(msg_lod
, 1);
1709 opcode
= SHADER_OPCODE_TXL
;
1714 * dPdx = dudx, dvdx, drdx
1715 * dPdy = dudy, dvdy, drdy
1717 * Load up these values:
1718 * - dudx dudy dvdx dvdy drdx drdy
1719 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1722 for (int i
= 0; i
< grad_components
; i
++) {
1723 emit(MOV(msg_end
, lod
));
1724 lod
= offset(lod
, 1);
1725 msg_end
= offset(msg_end
, 1);
1727 emit(MOV(msg_end
, lod2
));
1728 lod2
= offset(lod2
, 1);
1729 msg_end
= offset(msg_end
, 1);
1732 opcode
= SHADER_OPCODE_TXD
;
1736 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
1737 emit(MOV(msg_lod
, lod
));
1738 msg_end
= offset(msg_lod
, 1);
1740 opcode
= SHADER_OPCODE_TXS
;
1742 case ir_query_levels
:
1744 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1745 msg_end
= offset(msg_lod
, 1);
1747 opcode
= SHADER_OPCODE_TXS
;
1750 msg_lod
= offset(msg_coords
, 3);
1751 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
));
1752 msg_end
= offset(msg_lod
, 1);
1754 opcode
= SHADER_OPCODE_TXF
;
1757 msg_lod
= offset(msg_coords
, 3);
1759 emit(MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1761 emit(MOV(retype(offset(msg_lod
, 1), BRW_REGISTER_TYPE_UD
), sample_index
));
1762 msg_end
= offset(msg_lod
, 2);
1764 opcode
= SHADER_OPCODE_TXF_CMS
;
1767 opcode
= SHADER_OPCODE_LOD
;
1770 opcode
= SHADER_OPCODE_TG4
;
1773 unreachable("not reached");
1776 fs_inst
*inst
= emit(opcode
, dst
, reg_undef
, fs_reg(sampler
));
1777 inst
->base_mrf
= message
.reg
;
1778 inst
->mlen
= msg_end
.reg
- message
.reg
;
1779 inst
->header_present
= header_present
;
1780 inst
->regs_written
= 4 * reg_width
;
1782 if (inst
->mlen
> MAX_SAMPLER_MESSAGE_SIZE
) {
1783 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE
)
1784 " disallowed by hardware\n");
1791 is_high_sampler(struct brw_context
*brw
, fs_reg sampler
)
1793 if (brw
->gen
< 8 && !brw
->is_haswell
)
1796 return sampler
.file
!= IMM
|| sampler
.fixed_hw_reg
.dw1
.ud
>= 16;
1800 fs_visitor::emit_texture_gen7(ir_texture_opcode op
, fs_reg dst
,
1801 fs_reg coordinate
, int coord_components
,
1803 fs_reg lod
, fs_reg lod2
, int grad_components
,
1804 fs_reg sample_index
, fs_reg mcs
, fs_reg sampler
,
1805 fs_reg offset_value
)
1807 int reg_width
= dispatch_width
/ 8;
1808 bool header_present
= false;
1810 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, MAX_SAMPLER_MESSAGE_SIZE
);
1811 for (int i
= 0; i
< MAX_SAMPLER_MESSAGE_SIZE
; i
++) {
1812 sources
[i
] = vgrf(glsl_type::float_type
);
1816 if (op
== ir_tg4
|| offset_value
.file
!= BAD_FILE
||
1817 is_high_sampler(brw
, sampler
)) {
1818 /* For general texture offsets (no txf workaround), we need a header to
1819 * put them in. Note that for SIMD16 we're making space for two actual
1820 * hardware registers here, so the emit will have to fix up for this.
1822 * * ir4_tg4 needs to place its channel select in the header,
1823 * for interaction with ARB_texture_swizzle
1825 * The sampler index is only 4-bits, so for larger sampler numbers we
1826 * need to offset the Sampler State Pointer in the header.
1828 header_present
= true;
1829 sources
[0] = fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
1833 if (shadow_c
.file
!= BAD_FILE
) {
1834 emit(MOV(sources
[length
], shadow_c
));
1838 bool has_nonconstant_offset
=
1839 offset_value
.file
!= BAD_FILE
&& offset_value
.file
!= IMM
;
1840 bool coordinate_done
= false;
1842 /* Set up the LOD info */
1848 emit(MOV(sources
[length
], lod
));
1852 emit(MOV(sources
[length
], lod
));
1856 no16("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1858 /* Load dPdx and the coordinate together:
1859 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1861 for (int i
= 0; i
< coord_components
; i
++) {
1862 emit(MOV(sources
[length
], coordinate
));
1863 coordinate
= offset(coordinate
, 1);
1866 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1867 * only derivatives for (u, v, r).
1869 if (i
< grad_components
) {
1870 emit(MOV(sources
[length
], lod
));
1871 lod
= offset(lod
, 1);
1874 emit(MOV(sources
[length
], lod2
));
1875 lod2
= offset(lod2
, 1);
1880 coordinate_done
= true;
1884 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
));
1887 case ir_query_levels
:
1888 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), fs_reg(0u)));
1892 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1893 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1894 coordinate
= offset(coordinate
, 1);
1897 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
));
1900 for (int i
= 1; i
< coord_components
; i
++) {
1901 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1902 coordinate
= offset(coordinate
, 1);
1906 coordinate_done
= true;
1909 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
));
1912 /* data from the multisample control surface */
1913 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
));
1916 /* there is no offsetting for this message; just copy in the integer
1917 * texture coordinates
1919 for (int i
= 0; i
< coord_components
; i
++) {
1920 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
));
1921 coordinate
= offset(coordinate
, 1);
1925 coordinate_done
= true;
1928 if (has_nonconstant_offset
) {
1929 if (shadow_c
.file
!= BAD_FILE
)
1930 no16("Gen7 does not support gather4_po_c in SIMD16 mode.");
1932 /* More crazy intermixing */
1933 for (int i
= 0; i
< 2; i
++) { /* u, v */
1934 emit(MOV(sources
[length
], coordinate
));
1935 coordinate
= offset(coordinate
, 1);
1939 for (int i
= 0; i
< 2; i
++) { /* offu, offv */
1940 emit(MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
));
1941 offset_value
= offset(offset_value
, 1);
1945 if (coord_components
== 3) { /* r if present */
1946 emit(MOV(sources
[length
], coordinate
));
1947 coordinate
= offset(coordinate
, 1);
1951 coordinate_done
= true;
1956 /* Set up the coordinate (except for cases where it was done above) */
1957 if (!coordinate_done
) {
1958 for (int i
= 0; i
< coord_components
; i
++) {
1959 emit(MOV(sources
[length
], coordinate
));
1960 coordinate
= offset(coordinate
, 1);
1967 mlen
= length
* reg_width
- header_present
;
1969 mlen
= length
* reg_width
;
1971 fs_reg src_payload
= fs_reg(GRF
, alloc
.allocate(mlen
),
1972 BRW_REGISTER_TYPE_F
);
1973 emit(LOAD_PAYLOAD(src_payload
, sources
, length
));
1975 /* Generate the SEND */
1978 case ir_tex
: opcode
= SHADER_OPCODE_TEX
; break;
1979 case ir_txb
: opcode
= FS_OPCODE_TXB
; break;
1980 case ir_txl
: opcode
= SHADER_OPCODE_TXL
; break;
1981 case ir_txd
: opcode
= SHADER_OPCODE_TXD
; break;
1982 case ir_txf
: opcode
= SHADER_OPCODE_TXF
; break;
1983 case ir_txf_ms
: opcode
= SHADER_OPCODE_TXF_CMS
; break;
1984 case ir_txs
: opcode
= SHADER_OPCODE_TXS
; break;
1985 case ir_query_levels
: opcode
= SHADER_OPCODE_TXS
; break;
1986 case ir_lod
: opcode
= SHADER_OPCODE_LOD
; break;
1988 if (has_nonconstant_offset
)
1989 opcode
= SHADER_OPCODE_TG4_OFFSET
;
1991 opcode
= SHADER_OPCODE_TG4
;
1994 unreachable("not reached");
1996 fs_inst
*inst
= emit(opcode
, dst
, src_payload
, sampler
);
1997 inst
->base_mrf
= -1;
1999 inst
->header_present
= header_present
;
2000 inst
->regs_written
= 4 * reg_width
;
2002 if (inst
->mlen
> MAX_SAMPLER_MESSAGE_SIZE
) {
2003 fail("Message length >" STRINGIFY(MAX_SAMPLER_MESSAGE_SIZE
)
2004 " disallowed by hardware\n");
2011 fs_visitor::rescale_texcoord(fs_reg coordinate
, int coord_components
,
2012 bool is_rect
, uint32_t sampler
, int texunit
)
2014 fs_inst
*inst
= NULL
;
2015 bool needs_gl_clamp
= true;
2016 fs_reg scale_x
, scale_y
;
2018 /* The 965 requires the EU to do the normalization of GL rectangle
2019 * texture coordinates. We use the program parameter state
2020 * tracking to get the scaling factor.
2024 (brw
->gen
>= 6 && (key_tex
->gl_clamp_mask
[0] & (1 << sampler
) ||
2025 key_tex
->gl_clamp_mask
[1] & (1 << sampler
))))) {
2026 struct gl_program_parameter_list
*params
= prog
->Parameters
;
2027 int tokens
[STATE_LENGTH
] = {
2029 STATE_TEXRECT_SCALE
,
2035 no16("rectangle scale uniform setup not supported on SIMD16\n");
2036 if (dispatch_width
== 16) {
2040 GLuint index
= _mesa_add_state_reference(params
,
2041 (gl_state_index
*)tokens
);
2042 /* Try to find existing copies of the texrect scale uniforms. */
2043 for (unsigned i
= 0; i
< uniforms
; i
++) {
2044 if (stage_prog_data
->param
[i
] ==
2045 &prog
->Parameters
->ParameterValues
[index
][0]) {
2046 scale_x
= fs_reg(UNIFORM
, i
);
2047 scale_y
= fs_reg(UNIFORM
, i
+ 1);
2052 /* If we didn't already set them up, do so now. */
2053 if (scale_x
.file
== BAD_FILE
) {
2054 scale_x
= fs_reg(UNIFORM
, uniforms
);
2055 scale_y
= fs_reg(UNIFORM
, uniforms
+ 1);
2057 stage_prog_data
->param
[uniforms
++] =
2058 &prog
->Parameters
->ParameterValues
[index
][0];
2059 stage_prog_data
->param
[uniforms
++] =
2060 &prog
->Parameters
->ParameterValues
[index
][1];
2064 /* The 965 requires the EU to do the normalization of GL rectangle
2065 * texture coordinates. We use the program parameter state
2066 * tracking to get the scaling factor.
2068 if (brw
->gen
< 6 && is_rect
) {
2069 fs_reg dst
= fs_reg(GRF
, alloc
.allocate(coord_components
));
2070 fs_reg src
= coordinate
;
2073 emit(MUL(dst
, src
, scale_x
));
2074 dst
= offset(dst
, 1);
2075 src
= offset(src
, 1);
2076 emit(MUL(dst
, src
, scale_y
));
2077 } else if (is_rect
) {
2078 /* On gen6+, the sampler handles the rectangle coordinates
2079 * natively, without needing rescaling. But that means we have
2080 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
2081 * not [0, 1] like the default case below.
2083 needs_gl_clamp
= false;
2085 for (int i
= 0; i
< 2; i
++) {
2086 if (key_tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
2087 fs_reg chan
= coordinate
;
2088 chan
= offset(chan
, i
);
2090 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, fs_reg(0.0f
));
2091 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
2093 /* Our parameter comes in as 1.0/width or 1.0/height,
2094 * because that's what people normally want for doing
2095 * texture rectangle handling. We need width or height
2096 * for clamping, but we don't care enough to make a new
2097 * parameter type, so just invert back.
2099 fs_reg limit
= vgrf(glsl_type::float_type
);
2100 emit(MOV(limit
, i
== 0 ? scale_x
: scale_y
));
2101 emit(SHADER_OPCODE_RCP
, limit
, limit
);
2103 inst
= emit(BRW_OPCODE_SEL
, chan
, chan
, limit
);
2104 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
2109 if (coord_components
> 0 && needs_gl_clamp
) {
2110 for (int i
= 0; i
< MIN2(coord_components
, 3); i
++) {
2111 if (key_tex
->gl_clamp_mask
[i
] & (1 << sampler
)) {
2112 fs_reg chan
= coordinate
;
2113 chan
= offset(chan
, i
);
2115 fs_inst
*inst
= emit(MOV(chan
, chan
));
2116 inst
->saturate
= true;
2123 /* Sample from the MCS surface attached to this multisample texture. */
2125 fs_visitor::emit_mcs_fetch(fs_reg coordinate
, int components
, fs_reg sampler
)
2127 int reg_width
= dispatch_width
/ 8;
2128 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(components
* reg_width
),
2129 BRW_REGISTER_TYPE_F
);
2130 fs_reg dest
= vgrf(glsl_type::uvec4_type
);
2131 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, components
);
2133 /* parameters are: u, v, r; missing parameters are treated as zero */
2134 for (int i
= 0; i
< components
; i
++) {
2135 sources
[i
] = vgrf(glsl_type::float_type
);
2136 emit(MOV(retype(sources
[i
], BRW_REGISTER_TYPE_D
), coordinate
));
2137 coordinate
= offset(coordinate
, 1);
2140 emit(LOAD_PAYLOAD(payload
, sources
, components
));
2142 fs_inst
*inst
= emit(SHADER_OPCODE_TXF_MCS
, dest
, payload
, sampler
);
2143 inst
->base_mrf
= -1;
2144 inst
->mlen
= components
* reg_width
;
2145 inst
->header_present
= false;
2146 inst
->regs_written
= 4 * reg_width
; /* we only care about one reg of
2147 * response, but the sampler always
2155 fs_visitor::emit_texture(ir_texture_opcode op
,
2156 const glsl_type
*dest_type
,
2157 fs_reg coordinate
, int coord_components
,
2159 fs_reg lod
, fs_reg lod2
, int grad_components
,
2160 fs_reg sample_index
,
2161 fs_reg offset_value
,
2163 int gather_component
,
2167 fs_reg sampler_reg
, int texunit
)
2169 fs_inst
*inst
= NULL
;
2172 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
2173 * emitting anything other than setting up the constant result.
2175 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], gather_component
);
2176 if (swiz
== SWIZZLE_ZERO
|| swiz
== SWIZZLE_ONE
) {
2178 fs_reg res
= vgrf(glsl_type::vec4_type
);
2181 for (int i
=0; i
<4; i
++) {
2182 emit(MOV(res
, fs_reg(swiz
== SWIZZLE_ZERO
? 0.0f
: 1.0f
)));
2183 res
= offset(res
, 1);
2189 if (coordinate
.file
!= BAD_FILE
) {
2190 /* FINISHME: Texture coordinate rescaling doesn't work with non-constant
2191 * samplers. This should only be a problem with GL_CLAMP on Gen7.
2193 coordinate
= rescale_texcoord(coordinate
, coord_components
, is_rect
,
2197 /* Writemasking doesn't eliminate channels on SIMD8 texture
2198 * samples, so don't worry about them.
2200 fs_reg dst
= vgrf(glsl_type::get_instance(dest_type
->base_type
, 4, 1));
2202 if (brw
->gen
>= 7) {
2203 inst
= emit_texture_gen7(op
, dst
, coordinate
, coord_components
,
2204 shadow_c
, lod
, lod2
, grad_components
,
2205 sample_index
, mcs
, sampler_reg
,
2207 } else if (brw
->gen
>= 5) {
2208 inst
= emit_texture_gen5(op
, dst
, coordinate
, coord_components
,
2209 shadow_c
, lod
, lod2
, grad_components
,
2210 sample_index
, sampler
,
2211 offset_value
.file
!= BAD_FILE
);
2212 } else if (dispatch_width
== 16) {
2213 inst
= emit_texture_gen4_simd16(op
, dst
, coordinate
, coord_components
,
2214 shadow_c
, lod
, sampler
);
2216 inst
= emit_texture_gen4(op
, dst
, coordinate
, coord_components
,
2217 shadow_c
, lod
, lod2
, grad_components
,
2221 if (shadow_c
.file
!= BAD_FILE
)
2222 inst
->shadow_compare
= true;
2224 if (offset_value
.file
== IMM
)
2225 inst
->offset
= offset_value
.fixed_hw_reg
.dw1
.ud
;
2229 gather_channel(gather_component
, sampler
) << 16; /* M0.2:16-17 */
2232 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[sampler
], dst
);
2235 /* fixup #layers for cube map arrays */
2236 if (op
== ir_txs
&& is_cube_array
) {
2237 fs_reg depth
= offset(dst
, 2);
2238 fs_reg fixed_depth
= vgrf(glsl_type::int_type
);
2239 emit_math(SHADER_OPCODE_INT_QUOTIENT
, fixed_depth
, depth
, fs_reg(6));
2241 fs_reg
*fixed_payload
= ralloc_array(mem_ctx
, fs_reg
, inst
->regs_written
);
2242 int components
= inst
->regs_written
/ (dst
.width
/ 8);
2243 for (int i
= 0; i
< components
; i
++) {
2245 fixed_payload
[i
] = fixed_depth
;
2247 fixed_payload
[i
] = offset(dst
, i
);
2250 emit(LOAD_PAYLOAD(dst
, fixed_payload
, components
));
2253 swizzle_result(op
, dest_type
->vector_elements
, dst
, sampler
);
2257 fs_visitor::visit(ir_texture
*ir
)
2260 _mesa_get_sampler_uniform_value(ir
->sampler
, shader_prog
, prog
);
2262 ir_rvalue
*nonconst_sampler_index
=
2263 _mesa_get_sampler_array_nonconst_index(ir
->sampler
);
2265 /* Handle non-constant sampler array indexing */
2267 if (nonconst_sampler_index
) {
2268 /* The highest sampler which may be used by this operation is
2269 * the last element of the array. Mark it here, because the generator
2270 * doesn't have enough information to determine the bound.
2272 uint32_t array_size
= ir
->sampler
->as_dereference_array()
2273 ->array
->type
->array_size();
2275 uint32_t max_used
= sampler
+ array_size
- 1;
2276 if (ir
->op
== ir_tg4
&& brw
->gen
< 8) {
2277 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
2279 max_used
+= stage_prog_data
->binding_table
.texture_start
;
2282 brw_mark_surface_used(prog_data
, max_used
);
2284 /* Emit code to evaluate the actual indexing expression */
2285 nonconst_sampler_index
->accept(this);
2286 fs_reg temp
= vgrf(glsl_type::uint_type
);
2287 emit(ADD(temp
, this->result
, fs_reg(sampler
)))
2288 ->force_writemask_all
= true;
2291 /* Single sampler, or constant array index; the indexing expression
2292 * is just an immediate.
2294 sampler_reg
= fs_reg(sampler
);
2297 /* FINISHME: We're failing to recompile our programs when the sampler is
2298 * updated. This only matters for the texture rectangle scale parameters
2299 * (pre-gen6, or gen6+ with GL_CLAMP).
2301 int texunit
= prog
->SamplerUnits
[sampler
];
2303 /* Should be lowered by do_lower_texture_projection */
2304 assert(!ir
->projector
);
2306 /* Should be lowered */
2307 assert(!ir
->offset
|| !ir
->offset
->type
->is_array());
2309 /* Generate code to compute all the subexpression trees. This has to be
2310 * done before loading any values into MRFs for the sampler message since
2311 * generating these values may involve SEND messages that need the MRFs.
2314 int coord_components
= 0;
2315 if (ir
->coordinate
) {
2316 coord_components
= ir
->coordinate
->type
->vector_elements
;
2317 ir
->coordinate
->accept(this);
2318 coordinate
= this->result
;
2321 fs_reg shadow_comparitor
;
2322 if (ir
->shadow_comparitor
) {
2323 ir
->shadow_comparitor
->accept(this);
2324 shadow_comparitor
= this->result
;
2327 fs_reg offset_value
;
2329 ir_constant
*const_offset
= ir
->offset
->as_constant();
2331 /* Store the header bitfield in an IMM register. This allows us to
2332 * use offset_value.file to distinguish between no offset, a constant
2333 * offset, and a non-constant offset.
2336 fs_reg(brw_texture_offset(ctx
, const_offset
->value
.i
,
2337 const_offset
->type
->vector_elements
));
2339 ir
->offset
->accept(this);
2340 offset_value
= this->result
;
2344 fs_reg lod
, lod2
, sample_index
, mcs
;
2345 int grad_components
= 0;
2350 case ir_query_levels
:
2353 ir
->lod_info
.bias
->accept(this);
2357 ir
->lod_info
.grad
.dPdx
->accept(this);
2360 ir
->lod_info
.grad
.dPdy
->accept(this);
2361 lod2
= this->result
;
2363 grad_components
= ir
->lod_info
.grad
.dPdx
->type
->vector_elements
;
2368 ir
->lod_info
.lod
->accept(this);
2372 ir
->lod_info
.sample_index
->accept(this);
2373 sample_index
= this->result
;
2375 if (brw
->gen
>= 7 &&
2376 key_tex
->compressed_multisample_layout_mask
& (1 << sampler
)) {
2377 mcs
= emit_mcs_fetch(coordinate
, ir
->coordinate
->type
->vector_elements
,
2384 unreachable("Unrecognized texture opcode");
2387 int gather_component
= 0;
2388 if (ir
->op
== ir_tg4
)
2389 gather_component
= ir
->lod_info
.component
->as_constant()->value
.i
[0];
2392 ir
->sampler
->type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_RECT
;
2394 bool is_cube_array
=
2395 ir
->sampler
->type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2396 ir
->sampler
->type
->sampler_array
;
2398 emit_texture(ir
->op
, ir
->type
, coordinate
, coord_components
,
2399 shadow_comparitor
, lod
, lod2
, grad_components
,
2400 sample_index
, offset_value
, mcs
,
2401 gather_component
, is_cube_array
, is_rect
, sampler
,
2402 sampler_reg
, texunit
);
2406 * Apply workarounds for Gen6 gather with UINT/SINT
2409 fs_visitor::emit_gen6_gather_wa(uint8_t wa
, fs_reg dst
)
2414 int width
= (wa
& WA_8BIT
) ? 8 : 16;
2416 for (int i
= 0; i
< 4; i
++) {
2417 fs_reg dst_f
= retype(dst
, BRW_REGISTER_TYPE_F
);
2418 /* Convert from UNORM to UINT */
2419 emit(MUL(dst_f
, dst_f
, fs_reg((float)((1 << width
) - 1))));
2420 emit(MOV(dst
, dst_f
));
2423 /* Reinterpret the UINT value as a signed INT value by
2424 * shifting the sign bit into place, then shifting back
2427 emit(SHL(dst
, dst
, fs_reg(32 - width
)));
2428 emit(ASR(dst
, dst
, fs_reg(32 - width
)));
2431 dst
= offset(dst
, 1);
2436 * Set up the gather channel based on the swizzle, for gather4.
2439 fs_visitor::gather_channel(int orig_chan
, uint32_t sampler
)
2441 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], orig_chan
);
2443 case SWIZZLE_X
: return 0;
2445 /* gather4 sampler is broken for green channel on RG32F --
2446 * we must ask for blue instead.
2448 if (key_tex
->gather_channel_quirk_mask
& (1 << sampler
))
2451 case SWIZZLE_Z
: return 2;
2452 case SWIZZLE_W
: return 3;
2454 unreachable("Not reached"); /* zero, one swizzles handled already */
2459 * Swizzle the result of a texture result. This is necessary for
2460 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
2463 fs_visitor::swizzle_result(ir_texture_opcode op
, int dest_components
,
2464 fs_reg orig_val
, uint32_t sampler
)
2466 if (op
== ir_query_levels
) {
2467 /* # levels is in .w */
2468 this->result
= offset(orig_val
, 3);
2472 this->result
= orig_val
;
2474 /* txs,lod don't actually sample the texture, so swizzling the result
2477 if (op
== ir_txs
|| op
== ir_lod
|| op
== ir_tg4
)
2480 if (dest_components
== 1) {
2481 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
2482 } else if (key_tex
->swizzles
[sampler
] != SWIZZLE_NOOP
) {
2483 fs_reg swizzled_result
= vgrf(glsl_type::vec4_type
);
2484 swizzled_result
.type
= orig_val
.type
;
2486 for (int i
= 0; i
< 4; i
++) {
2487 int swiz
= GET_SWZ(key_tex
->swizzles
[sampler
], i
);
2488 fs_reg l
= swizzled_result
;
2491 if (swiz
== SWIZZLE_ZERO
) {
2492 emit(MOV(l
, fs_reg(0.0f
)));
2493 } else if (swiz
== SWIZZLE_ONE
) {
2494 emit(MOV(l
, fs_reg(1.0f
)));
2496 emit(MOV(l
, offset(orig_val
,
2497 GET_SWZ(key_tex
->swizzles
[sampler
], i
))));
2500 this->result
= swizzled_result
;
2505 fs_visitor::visit(ir_swizzle
*ir
)
2507 ir
->val
->accept(this);
2508 fs_reg val
= this->result
;
2510 if (ir
->type
->vector_elements
== 1) {
2511 this->result
= offset(this->result
, ir
->mask
.x
);
2515 fs_reg result
= vgrf(ir
->type
);
2516 this->result
= result
;
2518 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2519 fs_reg channel
= val
;
2537 emit(MOV(result
, offset(channel
, swiz
)));
2538 result
= offset(result
, 1);
2543 fs_visitor::visit(ir_discard
*ir
)
2545 /* We track our discarded pixels in f0.1. By predicating on it, we can
2546 * update just the flag bits that aren't yet discarded. If there's no
2547 * condition, we emit a CMP of g0 != g0, so all currently executing
2548 * channels will get turned off.
2551 if (ir
->condition
) {
2552 emit_bool_to_cond_code(ir
->condition
);
2553 cmp
= (fs_inst
*) this->instructions
.get_tail();
2554 cmp
->conditional_mod
= brw_negate_cmod(cmp
->conditional_mod
);
2556 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
2557 BRW_REGISTER_TYPE_UW
));
2558 cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
, BRW_CONDITIONAL_NZ
));
2560 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
2561 cmp
->flag_subreg
= 1;
2563 if (brw
->gen
>= 6) {
2564 emit_discard_jump();
2569 fs_visitor::visit(ir_constant
*ir
)
2571 /* Set this->result to reg at the bottom of the function because some code
2572 * paths will cause this visitor to be applied to other fields. This will
2573 * cause the value stored in this->result to be modified.
2575 * Make reg constant so that it doesn't get accidentally modified along the
2576 * way. Yes, I actually had this problem. :(
2578 const fs_reg reg
= vgrf(ir
->type
);
2579 fs_reg dst_reg
= reg
;
2581 if (ir
->type
->is_array()) {
2582 const unsigned size
= type_size(ir
->type
->fields
.array
);
2584 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
2585 ir
->array_elements
[i
]->accept(this);
2586 fs_reg src_reg
= this->result
;
2588 dst_reg
.type
= src_reg
.type
;
2589 for (unsigned j
= 0; j
< size
; j
++) {
2590 emit(MOV(dst_reg
, src_reg
));
2591 src_reg
= offset(src_reg
, 1);
2592 dst_reg
= offset(dst_reg
, 1);
2595 } else if (ir
->type
->is_record()) {
2596 foreach_in_list(ir_constant
, field
, &ir
->components
) {
2597 const unsigned size
= type_size(field
->type
);
2599 field
->accept(this);
2600 fs_reg src_reg
= this->result
;
2602 dst_reg
.type
= src_reg
.type
;
2603 for (unsigned j
= 0; j
< size
; j
++) {
2604 emit(MOV(dst_reg
, src_reg
));
2605 src_reg
= offset(src_reg
, 1);
2606 dst_reg
= offset(dst_reg
, 1);
2610 const unsigned size
= type_size(ir
->type
);
2612 for (unsigned i
= 0; i
< size
; i
++) {
2613 switch (ir
->type
->base_type
) {
2614 case GLSL_TYPE_FLOAT
:
2615 emit(MOV(dst_reg
, fs_reg(ir
->value
.f
[i
])));
2617 case GLSL_TYPE_UINT
:
2618 emit(MOV(dst_reg
, fs_reg(ir
->value
.u
[i
])));
2621 emit(MOV(dst_reg
, fs_reg(ir
->value
.i
[i
])));
2623 case GLSL_TYPE_BOOL
:
2625 fs_reg(ir
->value
.b
[i
] != 0 ? (int)ctx
->Const
.UniformBooleanTrue
2629 unreachable("Non-float/uint/int/bool constant");
2631 dst_reg
= offset(dst_reg
, 1);
2639 fs_visitor::emit_bool_to_cond_code(ir_rvalue
*ir
)
2641 ir_expression
*expr
= ir
->as_expression();
2643 if (!expr
|| expr
->operation
== ir_binop_ubo_load
) {
2646 fs_inst
*inst
= emit(AND(reg_null_d
, this->result
, fs_reg(1)));
2647 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2653 assert(expr
->get_num_operands() <= 3);
2654 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
2655 assert(expr
->operands
[i
]->type
->is_scalar());
2657 expr
->operands
[i
]->accept(this);
2658 op
[i
] = this->result
;
2660 resolve_ud_negate(&op
[i
]);
2663 emit_bool_to_cond_code_of_reg(expr
, op
);
2667 fs_visitor::emit_bool_to_cond_code_of_reg(ir_expression
*expr
, fs_reg op
[3])
2671 switch (expr
->operation
) {
2672 case ir_unop_logic_not
:
2673 inst
= emit(AND(reg_null_d
, op
[0], fs_reg(1)));
2674 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2677 case ir_binop_logic_xor
:
2678 if (brw
->gen
<= 5) {
2679 fs_reg temp
= vgrf(expr
->type
);
2680 emit(XOR(temp
, op
[0], op
[1]));
2681 inst
= emit(AND(reg_null_d
, temp
, fs_reg(1)));
2683 inst
= emit(XOR(reg_null_d
, op
[0], op
[1]));
2685 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2688 case ir_binop_logic_or
:
2689 if (brw
->gen
<= 5) {
2690 fs_reg temp
= vgrf(expr
->type
);
2691 emit(OR(temp
, op
[0], op
[1]));
2692 inst
= emit(AND(reg_null_d
, temp
, fs_reg(1)));
2694 inst
= emit(OR(reg_null_d
, op
[0], op
[1]));
2696 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2699 case ir_binop_logic_and
:
2700 if (brw
->gen
<= 5) {
2701 fs_reg temp
= vgrf(expr
->type
);
2702 emit(AND(temp
, op
[0], op
[1]));
2703 inst
= emit(AND(reg_null_d
, temp
, fs_reg(1)));
2705 inst
= emit(AND(reg_null_d
, op
[0], op
[1]));
2707 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2711 if (brw
->gen
>= 6) {
2712 emit(CMP(reg_null_d
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
));
2714 inst
= emit(MOV(reg_null_f
, op
[0]));
2715 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2720 if (brw
->gen
>= 6) {
2721 emit(CMP(reg_null_d
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
2723 inst
= emit(MOV(reg_null_d
, op
[0]));
2724 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2728 case ir_binop_greater
:
2729 case ir_binop_gequal
:
2731 case ir_binop_lequal
:
2732 case ir_binop_equal
:
2733 case ir_binop_all_equal
:
2734 case ir_binop_nequal
:
2735 case ir_binop_any_nequal
:
2736 if (brw
->gen
<= 5) {
2737 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
2738 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
2741 emit(CMP(reg_null_d
, op
[0], op
[1],
2742 brw_conditional_for_comparison(expr
->operation
)));
2745 case ir_triop_csel
: {
2746 /* Expand the boolean condition into the flag register. */
2747 inst
= emit(MOV(reg_null_d
, op
[0]));
2748 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2750 /* Select which boolean to return. */
2751 fs_reg temp
= vgrf(expr
->operands
[1]->type
);
2752 inst
= emit(SEL(temp
, op
[1], op
[2]));
2753 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2755 /* Expand the result to a condition code. */
2756 inst
= emit(MOV(reg_null_d
, temp
));
2757 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2762 unreachable("not reached");
2767 * Emit a gen6 IF statement with the comparison folded into the IF
2771 fs_visitor::emit_if_gen6(ir_if
*ir
)
2773 ir_expression
*expr
= ir
->condition
->as_expression();
2775 if (expr
&& expr
->operation
!= ir_binop_ubo_load
) {
2780 assert(expr
->get_num_operands() <= 3);
2781 for (unsigned int i
= 0; i
< expr
->get_num_operands(); i
++) {
2782 assert(expr
->operands
[i
]->type
->is_scalar());
2784 expr
->operands
[i
]->accept(this);
2785 op
[i
] = this->result
;
2788 switch (expr
->operation
) {
2789 case ir_unop_logic_not
:
2790 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_Z
));
2793 case ir_binop_logic_xor
:
2794 emit(IF(op
[0], op
[1], BRW_CONDITIONAL_NZ
));
2797 case ir_binop_logic_or
:
2798 temp
= vgrf(glsl_type::bool_type
);
2799 emit(OR(temp
, op
[0], op
[1]));
2800 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2803 case ir_binop_logic_and
:
2804 temp
= vgrf(glsl_type::bool_type
);
2805 emit(AND(temp
, op
[0], op
[1]));
2806 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2810 inst
= emit(BRW_OPCODE_IF
, reg_null_f
, op
[0], fs_reg(0));
2811 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2815 emit(IF(op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
));
2818 case ir_binop_greater
:
2819 case ir_binop_gequal
:
2821 case ir_binop_lequal
:
2822 case ir_binop_equal
:
2823 case ir_binop_all_equal
:
2824 case ir_binop_nequal
:
2825 case ir_binop_any_nequal
:
2826 if (brw
->gen
<= 5) {
2827 resolve_bool_comparison(expr
->operands
[0], &op
[0]);
2828 resolve_bool_comparison(expr
->operands
[1], &op
[1]);
2831 emit(IF(op
[0], op
[1],
2832 brw_conditional_for_comparison(expr
->operation
)));
2835 case ir_triop_csel
: {
2836 /* Expand the boolean condition into the flag register. */
2837 fs_inst
*inst
= emit(MOV(reg_null_d
, op
[0]));
2838 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
2840 /* Select which boolean to use as the result. */
2841 fs_reg temp
= vgrf(expr
->operands
[1]->type
);
2842 inst
= emit(SEL(temp
, op
[1], op
[2]));
2843 inst
->predicate
= BRW_PREDICATE_NORMAL
;
2845 emit(IF(temp
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2850 unreachable("not reached");
2854 ir
->condition
->accept(this);
2855 emit(IF(this->result
, fs_reg(0), BRW_CONDITIONAL_NZ
));
2859 fs_visitor::try_opt_frontfacing_ternary(ir_if
*ir
)
2861 ir_dereference_variable
*deref
= ir
->condition
->as_dereference_variable();
2862 if (!deref
|| strcmp(deref
->var
->name
, "gl_FrontFacing") != 0)
2865 if (ir
->then_instructions
.length() != 1 ||
2866 ir
->else_instructions
.length() != 1)
2869 ir_assignment
*then_assign
=
2870 ((ir_instruction
*)ir
->then_instructions
.head
)->as_assignment();
2871 ir_assignment
*else_assign
=
2872 ((ir_instruction
*)ir
->else_instructions
.head
)->as_assignment();
2874 if (!then_assign
|| then_assign
->condition
||
2875 !else_assign
|| else_assign
->condition
||
2876 then_assign
->write_mask
!= else_assign
->write_mask
||
2877 !then_assign
->lhs
->equals(else_assign
->lhs
))
2880 ir_constant
*then_rhs
= then_assign
->rhs
->as_constant();
2881 ir_constant
*else_rhs
= else_assign
->rhs
->as_constant();
2883 if (!then_rhs
|| !else_rhs
)
2886 if (then_rhs
->type
->base_type
!= GLSL_TYPE_FLOAT
)
2889 if ((then_rhs
->is_one() && else_rhs
->is_negative_one()) ||
2890 (else_rhs
->is_one() && then_rhs
->is_negative_one())) {
2891 then_assign
->lhs
->accept(this);
2892 fs_reg dst
= this->result
;
2893 dst
.type
= BRW_REGISTER_TYPE_D
;
2894 fs_reg tmp
= vgrf(glsl_type::int_type
);
2896 if (brw
->gen
>= 6) {
2897 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
2898 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
2900 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
2902 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
2903 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
2905 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
2908 if (then_rhs
->is_negative_one()) {
2909 assert(else_rhs
->is_one());
2913 tmp
.type
= BRW_REGISTER_TYPE_W
;
2914 tmp
.subreg_offset
= 2;
2917 fs_inst
*or_inst
= emit(OR(tmp
, g0
, fs_reg(0x3f80)));
2918 or_inst
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
2920 tmp
.type
= BRW_REGISTER_TYPE_D
;
2921 tmp
.subreg_offset
= 0;
2924 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
2925 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
2927 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
2929 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
2930 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
2932 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
2935 if (then_rhs
->is_negative_one()) {
2936 assert(else_rhs
->is_one());
2940 emit(OR(tmp
, g1_6
, fs_reg(0x3f800000)));
2942 emit(AND(dst
, tmp
, fs_reg(0xbf800000)));
2950 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
2952 * Many GLSL shaders contain the following pattern:
2954 * x = condition ? foo : bar
2956 * The compiler emits an ir_if tree for this, since each subexpression might be
2957 * a complex tree that could have side-effects or short-circuit logic.
2959 * However, the common case is to simply select one of two constants or
2960 * variable values---which is exactly what SEL is for. In this case, the
2961 * assembly looks like:
2969 * which can be easily translated into:
2971 * (+f0) SEL dst src0 src1
2973 * If src0 is an immediate value, we promote it to a temporary GRF.
2976 fs_visitor::try_replace_with_sel()
2978 fs_inst
*endif_inst
= (fs_inst
*) instructions
.get_tail();
2979 assert(endif_inst
->opcode
== BRW_OPCODE_ENDIF
);
2981 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
2983 BRW_OPCODE_IF
, BRW_OPCODE_MOV
, BRW_OPCODE_ELSE
, BRW_OPCODE_MOV
,
2986 fs_inst
*match
= (fs_inst
*) endif_inst
->prev
;
2987 for (int i
= 0; i
< 4; i
++) {
2988 if (match
->is_head_sentinel() || match
->opcode
!= opcodes
[4-i
-1])
2990 match
= (fs_inst
*) match
->prev
;
2993 /* The opcodes match; it looks like the right sequence of instructions. */
2994 fs_inst
*else_mov
= (fs_inst
*) endif_inst
->prev
;
2995 fs_inst
*then_mov
= (fs_inst
*) else_mov
->prev
->prev
;
2996 fs_inst
*if_inst
= (fs_inst
*) then_mov
->prev
;
2998 /* Check that the MOVs are the right form. */
2999 if (then_mov
->dst
.equals(else_mov
->dst
) &&
3000 !then_mov
->is_partial_write() &&
3001 !else_mov
->is_partial_write()) {
3003 /* Remove the matched instructions; we'll emit a SEL to replace them. */
3004 while (!if_inst
->next
->is_tail_sentinel())
3005 if_inst
->next
->exec_node::remove();
3006 if_inst
->exec_node::remove();
3008 /* Only the last source register can be a constant, so if the MOV in
3009 * the "then" clause uses a constant, we need to put it in a temporary.
3011 fs_reg
src0(then_mov
->src
[0]);
3012 if (src0
.file
== IMM
) {
3013 src0
= vgrf(glsl_type::float_type
);
3014 src0
.type
= then_mov
->src
[0].type
;
3015 emit(MOV(src0
, then_mov
->src
[0]));
3019 if (if_inst
->conditional_mod
) {
3020 /* Sandybridge-specific IF with embedded comparison */
3021 emit(CMP(reg_null_d
, if_inst
->src
[0], if_inst
->src
[1],
3022 if_inst
->conditional_mod
));
3023 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
3024 sel
->predicate
= BRW_PREDICATE_NORMAL
;
3026 /* Separate CMP and IF instructions */
3027 sel
= emit(BRW_OPCODE_SEL
, then_mov
->dst
, src0
, else_mov
->src
[0]);
3028 sel
->predicate
= if_inst
->predicate
;
3029 sel
->predicate_inverse
= if_inst
->predicate_inverse
;
3039 fs_visitor::visit(ir_if
*ir
)
3041 if (try_opt_frontfacing_ternary(ir
))
3044 /* Don't point the annotation at the if statement, because then it plus
3045 * the then and else blocks get printed.
3047 this->base_ir
= ir
->condition
;
3049 if (brw
->gen
== 6) {
3052 emit_bool_to_cond_code(ir
->condition
);
3054 emit(IF(BRW_PREDICATE_NORMAL
));
3057 foreach_in_list(ir_instruction
, ir_
, &ir
->then_instructions
) {
3058 this->base_ir
= ir_
;
3062 if (!ir
->else_instructions
.is_empty()) {
3063 emit(BRW_OPCODE_ELSE
);
3065 foreach_in_list(ir_instruction
, ir_
, &ir
->else_instructions
) {
3066 this->base_ir
= ir_
;
3071 emit(BRW_OPCODE_ENDIF
);
3073 if (!try_replace_with_sel() && brw
->gen
< 6) {
3074 no16("Can't support (non-uniform) control flow on SIMD16\n");
3079 fs_visitor::visit(ir_loop
*ir
)
3082 no16("Can't support (non-uniform) control flow on SIMD16\n");
3085 this->base_ir
= NULL
;
3086 emit(BRW_OPCODE_DO
);
3088 foreach_in_list(ir_instruction
, ir_
, &ir
->body_instructions
) {
3089 this->base_ir
= ir_
;
3093 this->base_ir
= NULL
;
3094 emit(BRW_OPCODE_WHILE
);
3098 fs_visitor::visit(ir_loop_jump
*ir
)
3101 case ir_loop_jump::jump_break
:
3102 emit(BRW_OPCODE_BREAK
);
3104 case ir_loop_jump::jump_continue
:
3105 emit(BRW_OPCODE_CONTINUE
);
3111 fs_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3113 ir_dereference
*deref
= static_cast<ir_dereference
*>(
3114 ir
->actual_parameters
.get_head());
3115 ir_variable
*location
= deref
->variable_referenced();
3116 unsigned surf_index
= (stage_prog_data
->binding_table
.abo_start
+
3117 location
->data
.binding
);
3119 /* Calculate the surface offset */
3120 fs_reg offset
= vgrf(glsl_type::uint_type
);
3121 ir_dereference_array
*deref_array
= deref
->as_dereference_array();
3124 deref_array
->array_index
->accept(this);
3126 fs_reg tmp
= vgrf(glsl_type::uint_type
);
3127 emit(MUL(tmp
, this->result
, fs_reg(ATOMIC_COUNTER_SIZE
)));
3128 emit(ADD(offset
, tmp
, fs_reg(location
->data
.atomic
.offset
)));
3130 offset
= fs_reg(location
->data
.atomic
.offset
);
3133 /* Emit the appropriate machine instruction */
3134 const char *callee
= ir
->callee
->function_name();
3135 ir
->return_deref
->accept(this);
3136 fs_reg dst
= this->result
;
3138 if (!strcmp("__intrinsic_atomic_read", callee
)) {
3139 emit_untyped_surface_read(surf_index
, dst
, offset
);
3141 } else if (!strcmp("__intrinsic_atomic_increment", callee
)) {
3142 emit_untyped_atomic(BRW_AOP_INC
, surf_index
, dst
, offset
,
3143 fs_reg(), fs_reg());
3145 } else if (!strcmp("__intrinsic_atomic_predecrement", callee
)) {
3146 emit_untyped_atomic(BRW_AOP_PREDEC
, surf_index
, dst
, offset
,
3147 fs_reg(), fs_reg());
3152 fs_visitor::visit(ir_call
*ir
)
3154 const char *callee
= ir
->callee
->function_name();
3156 if (!strcmp("__intrinsic_atomic_read", callee
) ||
3157 !strcmp("__intrinsic_atomic_increment", callee
) ||
3158 !strcmp("__intrinsic_atomic_predecrement", callee
)) {
3159 visit_atomic_counter_intrinsic(ir
);
3161 unreachable("Unsupported intrinsic.");
3166 fs_visitor::visit(ir_return
*)
3168 unreachable("FINISHME");
3172 fs_visitor::visit(ir_function
*ir
)
3174 /* Ignore function bodies other than main() -- we shouldn't see calls to
3175 * them since they should all be inlined before we get to ir_to_mesa.
3177 if (strcmp(ir
->name
, "main") == 0) {
3178 const ir_function_signature
*sig
;
3181 sig
= ir
->matching_signature(NULL
, &empty
, false);
3185 foreach_in_list(ir_instruction
, ir_
, &sig
->body
) {
3186 this->base_ir
= ir_
;
3193 fs_visitor::visit(ir_function_signature
*)
3195 unreachable("not reached");
3199 fs_visitor::visit(ir_emit_vertex
*)
3201 unreachable("not reached");
3205 fs_visitor::visit(ir_end_primitive
*)
3207 unreachable("not reached");
3211 fs_visitor::emit_untyped_atomic(unsigned atomic_op
, unsigned surf_index
,
3212 fs_reg dst
, fs_reg offset
, fs_reg src0
,
3215 int reg_width
= dispatch_width
/ 8;
3218 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 4);
3220 sources
[0] = fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
3221 /* Initialize the sample mask in the message header. */
3222 emit(MOV(sources
[0], fs_reg(0u)))
3223 ->force_writemask_all
= true;
3225 if (stage
== MESA_SHADER_FRAGMENT
) {
3226 if (((brw_wm_prog_data
*)this->prog_data
)->uses_kill
) {
3227 emit(MOV(component(sources
[0], 7), brw_flag_reg(0, 1)))
3228 ->force_writemask_all
= true;
3230 emit(MOV(component(sources
[0], 7),
3231 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD
)))
3232 ->force_writemask_all
= true;
3235 /* The execution mask is part of the side-band information sent together with
3236 * the message payload to the data port. It's implicitly ANDed with the sample
3237 * mask sent in the header to compute the actual set of channels that execute
3238 * the atomic operation.
3240 assert(stage
== MESA_SHADER_VERTEX
|| stage
== MESA_SHADER_COMPUTE
);
3241 emit(MOV(component(sources
[0], 7),
3242 fs_reg(0xffffu
)))->force_writemask_all
= true;
3246 /* Set the atomic operation offset. */
3247 sources
[1] = vgrf(glsl_type::uint_type
);
3248 emit(MOV(sources
[1], offset
));
3251 /* Set the atomic operation arguments. */
3252 if (src0
.file
!= BAD_FILE
) {
3253 sources
[length
] = vgrf(glsl_type::uint_type
);
3254 emit(MOV(sources
[length
], src0
));
3258 if (src1
.file
!= BAD_FILE
) {
3259 sources
[length
] = vgrf(glsl_type::uint_type
);
3260 emit(MOV(sources
[length
], src1
));
3264 int mlen
= 1 + (length
- 1) * reg_width
;
3265 fs_reg src_payload
= fs_reg(GRF
, alloc
.allocate(mlen
),
3266 BRW_REGISTER_TYPE_UD
);
3267 emit(LOAD_PAYLOAD(src_payload
, sources
, length
));
3269 /* Emit the instruction. */
3270 fs_inst
*inst
= emit(SHADER_OPCODE_UNTYPED_ATOMIC
, dst
, src_payload
,
3271 fs_reg(atomic_op
), fs_reg(surf_index
));
3276 fs_visitor::emit_untyped_surface_read(unsigned surf_index
, fs_reg dst
,
3279 int reg_width
= dispatch_width
/ 8;
3281 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
3283 sources
[0] = fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
3284 /* Initialize the sample mask in the message header. */
3285 emit(MOV(sources
[0], fs_reg(0u)))
3286 ->force_writemask_all
= true;
3288 if (stage
== MESA_SHADER_FRAGMENT
) {
3289 if (((brw_wm_prog_data
*)this->prog_data
)->uses_kill
) {
3290 emit(MOV(component(sources
[0], 7), brw_flag_reg(0, 1)))
3291 ->force_writemask_all
= true;
3293 emit(MOV(component(sources
[0], 7),
3294 retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD
)))
3295 ->force_writemask_all
= true;
3298 /* The execution mask is part of the side-band information sent together with
3299 * the message payload to the data port. It's implicitly ANDed with the sample
3300 * mask sent in the header to compute the actual set of channels that execute
3301 * the atomic operation.
3303 assert(stage
== MESA_SHADER_VERTEX
|| stage
== MESA_SHADER_COMPUTE
);
3304 emit(MOV(component(sources
[0], 7),
3305 fs_reg(0xffffu
)))->force_writemask_all
= true;
3308 /* Set the surface read offset. */
3309 sources
[1] = vgrf(glsl_type::uint_type
);
3310 emit(MOV(sources
[1], offset
));
3312 int mlen
= 1 + reg_width
;
3313 fs_reg src_payload
= fs_reg(GRF
, alloc
.allocate(mlen
),
3314 BRW_REGISTER_TYPE_UD
);
3315 fs_inst
*inst
= emit(LOAD_PAYLOAD(src_payload
, sources
, 2));
3317 /* Emit the instruction. */
3318 inst
= emit(SHADER_OPCODE_UNTYPED_SURFACE_READ
, dst
, src_payload
,
3319 fs_reg(surf_index
));
3324 fs_visitor::emit(fs_inst
*inst
)
3326 if (dispatch_width
== 16 && inst
->exec_size
== 8)
3327 inst
->force_uncompressed
= true;
3329 inst
->annotation
= this->current_annotation
;
3330 inst
->ir
= this->base_ir
;
3332 this->instructions
.push_tail(inst
);
3338 fs_visitor::emit(exec_list list
)
3340 foreach_in_list_safe(fs_inst
, inst
, &list
) {
3341 inst
->exec_node::remove();
3346 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
3348 fs_visitor::emit_dummy_fs()
3350 int reg_width
= dispatch_width
/ 8;
3352 /* Everyone's favorite color. */
3353 const float color
[4] = { 1.0, 0.0, 1.0, 0.0 };
3354 for (int i
= 0; i
< 4; i
++) {
3355 emit(MOV(fs_reg(MRF
, 2 + i
* reg_width
, BRW_REGISTER_TYPE_F
,
3356 dispatch_width
), fs_reg(color
[i
])));
3360 write
= emit(FS_OPCODE_FB_WRITE
);
3362 if (brw
->gen
>= 6) {
3363 write
->base_mrf
= 2;
3364 write
->mlen
= 4 * reg_width
;
3366 write
->header_present
= true;
3367 write
->base_mrf
= 0;
3368 write
->mlen
= 2 + 4 * reg_width
;
3371 /* Tell the SF we don't have any inputs. Gen4-5 require at least one
3372 * varying to avoid GPU hangs, so set that.
3374 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3375 wm_prog_data
->num_varying_inputs
= brw
->gen
< 6 ? 1 : 0;
3376 memset(wm_prog_data
->urb_setup
, -1,
3377 sizeof(wm_prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
3379 /* We don't have any uniforms. */
3380 stage_prog_data
->nr_params
= 0;
3381 stage_prog_data
->nr_pull_params
= 0;
3382 stage_prog_data
->curb_read_length
= 0;
3383 stage_prog_data
->dispatch_grf_start_reg
= 2;
3384 wm_prog_data
->dispatch_grf_start_reg_16
= 2;
3385 grf_used
= 1; /* Gen4-5 don't allow zero GRF blocks */
3390 /* The register location here is relative to the start of the URB
3391 * data. It will get adjusted to be a real location before
3392 * generate_code() time.
3395 fs_visitor::interp_reg(int location
, int channel
)
3397 assert(stage
== MESA_SHADER_FRAGMENT
);
3398 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3399 int regnr
= prog_data
->urb_setup
[location
] * 2 + channel
/ 2;
3400 int stride
= (channel
& 1) * 4;
3402 assert(prog_data
->urb_setup
[location
] != -1);
3404 return brw_vec1_grf(regnr
, stride
);
3407 /** Emits the interpolation for the varying inputs. */
3409 fs_visitor::emit_interpolation_setup_gen4()
3411 this->current_annotation
= "compute pixel centers";
3412 this->pixel_x
= vgrf(glsl_type::uint_type
);
3413 this->pixel_y
= vgrf(glsl_type::uint_type
);
3414 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
3415 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
3417 emit(FS_OPCODE_PIXEL_X
, this->pixel_x
);
3418 emit(FS_OPCODE_PIXEL_Y
, this->pixel_y
);
3420 this->current_annotation
= "compute pixel deltas from v0";
3422 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
3423 vgrf(glsl_type::vec2_type
);
3424 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
3425 offset(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
], 1);
3427 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
3428 vgrf(glsl_type::float_type
);
3429 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
] =
3430 vgrf(glsl_type::float_type
);
3432 emit(ADD(this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
3433 this->pixel_x
, fs_reg(negate(brw_vec1_grf(1, 0)))));
3434 emit(ADD(this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
3435 this->pixel_y
, fs_reg(negate(brw_vec1_grf(1, 1)))));
3437 this->current_annotation
= "compute pos.w and 1/pos.w";
3438 /* Compute wpos.w. It's always in our setup, since it's needed to
3439 * interpolate the other attributes.
3441 this->wpos_w
= vgrf(glsl_type::float_type
);
3442 emit(FS_OPCODE_LINTERP
, wpos_w
,
3443 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
3444 this->delta_y
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
3445 interp_reg(VARYING_SLOT_POS
, 3));
3446 /* Compute the pixel 1/W value from wpos.w. */
3447 this->pixel_w
= vgrf(glsl_type::float_type
);
3448 emit_math(SHADER_OPCODE_RCP
, this->pixel_w
, wpos_w
);
3449 this->current_annotation
= NULL
;
3452 /** Emits the interpolation for the varying inputs. */
3454 fs_visitor::emit_interpolation_setup_gen6()
3456 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
3458 /* If the pixel centers end up used, the setup is the same as for gen4. */
3459 this->current_annotation
= "compute pixel centers";
3460 fs_reg int_pixel_x
= vgrf(glsl_type::uint_type
);
3461 fs_reg int_pixel_y
= vgrf(glsl_type::uint_type
);
3462 int_pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
3463 int_pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
3464 emit(ADD(int_pixel_x
,
3465 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
3466 fs_reg(brw_imm_v(0x10101010))));
3467 emit(ADD(int_pixel_y
,
3468 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
3469 fs_reg(brw_imm_v(0x11001100))));
3471 /* As of gen6, we can no longer mix float and int sources. We have
3472 * to turn the integer pixel centers into floats for their actual
3475 this->pixel_x
= vgrf(glsl_type::float_type
);
3476 this->pixel_y
= vgrf(glsl_type::float_type
);
3477 emit(MOV(this->pixel_x
, int_pixel_x
));
3478 emit(MOV(this->pixel_y
, int_pixel_y
));
3480 this->current_annotation
= "compute pos.w";
3481 this->pixel_w
= fs_reg(brw_vec8_grf(payload
.source_w_reg
, 0));
3482 this->wpos_w
= vgrf(glsl_type::float_type
);
3483 emit_math(SHADER_OPCODE_RCP
, this->wpos_w
, this->pixel_w
);
3485 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
3486 uint8_t reg
= payload
.barycentric_coord_reg
[i
];
3487 this->delta_x
[i
] = fs_reg(brw_vec8_grf(reg
, 0));
3488 this->delta_y
[i
] = fs_reg(brw_vec8_grf(reg
+ 1, 0));
3491 this->current_annotation
= NULL
;
3495 fs_visitor::setup_color_payload(fs_reg
*dst
, fs_reg color
, unsigned components
,
3498 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3501 if (color
.file
== BAD_FILE
) {
3502 return 4 * (dispatch_width
/ 8);
3505 uint8_t colors_enabled
;
3506 if (components
== 0) {
3507 /* We want to write one component to the alpha channel */
3508 colors_enabled
= 0x8;
3510 /* Enable the first components-many channels */
3511 colors_enabled
= (1 << components
) - 1;
3514 if (dispatch_width
== 8 || (brw
->gen
>= 6 && !do_dual_src
)) {
3515 /* SIMD8 write looks like:
3521 * gen6 SIMD16 DP write looks like:
3532 for (unsigned i
= 0; i
< 4; ++i
) {
3533 if (colors_enabled
& (1 << i
)) {
3534 dst
[len
] = fs_reg(GRF
, alloc
.allocate(color
.width
/ 8),
3535 color
.type
, color
.width
);
3536 inst
= emit(MOV(dst
[len
], offset(color
, i
)));
3537 inst
->saturate
= key
->clamp_fragment_color
;
3538 } else if (color
.width
== 16) {
3539 /* We need two BAD_FILE slots for a 16-wide color */
3545 } else if (brw
->gen
>= 6 && do_dual_src
) {
3546 /* SIMD16 dual source blending for gen6+.
3548 * From the SNB PRM, volume 4, part 1, page 193:
3550 * "The dual source render target messages only have SIMD8 forms due to
3551 * maximum message length limitations. SIMD16 pixel shaders must send two
3552 * of these messages to cover all of the pixels. Each message contains
3553 * two colors (4 channels each) for each pixel in the message payload."
3555 * So in SIMD16 dual source blending we will send 2 SIMD8 messages,
3556 * each one will call this function twice (one for each color involved),
3557 * so in each pass we only write 4 registers. Notice that the second
3558 * SIMD8 message needs to read color data from the 2nd half of the color
3559 * registers, so it needs to call this with use_2nd_half = true.
3561 for (unsigned i
= 0; i
< 4; ++i
) {
3562 if (colors_enabled
& (1 << i
)) {
3563 dst
[i
] = fs_reg(GRF
, alloc
.allocate(1), color
.type
);
3564 inst
= emit(MOV(dst
[i
], half(offset(color
, i
),
3565 use_2nd_half
? 1 : 0)));
3566 inst
->saturate
= key
->clamp_fragment_color
;
3568 inst
->force_sechalf
= true;
3573 /* pre-gen6 SIMD16 single source DP write looks like:
3583 for (unsigned i
= 0; i
< 4; ++i
) {
3584 if (colors_enabled
& (1 << i
)) {
3585 dst
[i
] = fs_reg(GRF
, alloc
.allocate(1), color
.type
);
3586 inst
= emit(MOV(dst
[i
], half(offset(color
, i
), 0)));
3587 inst
->saturate
= key
->clamp_fragment_color
;
3589 dst
[i
+ 4] = fs_reg(GRF
, alloc
.allocate(1), color
.type
);
3590 inst
= emit(MOV(dst
[i
+ 4], half(offset(color
, i
), 1)));
3591 inst
->saturate
= key
->clamp_fragment_color
;
3592 inst
->force_sechalf
= true;
3599 static enum brw_conditional_mod
3600 cond_for_alpha_func(GLenum func
)
3604 return BRW_CONDITIONAL_G
;
3606 return BRW_CONDITIONAL_GE
;
3608 return BRW_CONDITIONAL_L
;
3610 return BRW_CONDITIONAL_LE
;
3612 return BRW_CONDITIONAL_EQ
;
3614 return BRW_CONDITIONAL_NEQ
;
3616 unreachable("Not reached");
3621 * Alpha test support for when we compile it into the shader instead
3622 * of using the normal fixed-function alpha test.
3625 fs_visitor::emit_alpha_test()
3627 assert(stage
== MESA_SHADER_FRAGMENT
);
3628 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3629 this->current_annotation
= "Alpha test";
3632 if (key
->alpha_test_func
== GL_ALWAYS
)
3635 if (key
->alpha_test_func
== GL_NEVER
) {
3637 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
3638 BRW_REGISTER_TYPE_UW
));
3639 cmp
= emit(CMP(reg_null_f
, some_reg
, some_reg
,
3640 BRW_CONDITIONAL_NEQ
));
3643 fs_reg color
= offset(outputs
[0], 3);
3645 /* f0.1 &= func(color, ref) */
3646 cmp
= emit(CMP(reg_null_f
, color
, fs_reg(key
->alpha_test_ref
),
3647 cond_for_alpha_func(key
->alpha_test_func
)));
3649 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
3650 cmp
->flag_subreg
= 1;
3654 fs_visitor::emit_single_fb_write(fs_reg color0
, fs_reg color1
,
3655 fs_reg src0_alpha
, unsigned components
,
3658 assert(stage
== MESA_SHADER_FRAGMENT
);
3659 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3660 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3662 this->current_annotation
= "FB write header";
3663 bool header_present
= true;
3664 int reg_size
= dispatch_width
/ 8;
3666 /* We can potentially have a message length of up to 15, so we have to set
3667 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3669 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 15);
3672 /* From the Sandy Bridge PRM, volume 4, page 198:
3674 * "Dispatched Pixel Enables. One bit per pixel indicating
3675 * which pixels were originally enabled when the thread was
3676 * dispatched. This field is only required for the end-of-
3677 * thread message and on all dual-source messages."
3679 if (brw
->gen
>= 6 &&
3680 (brw
->is_haswell
|| brw
->gen
>= 8 || !prog_data
->uses_kill
) &&
3681 color1
.file
== BAD_FILE
&&
3682 key
->nr_color_regions
== 1) {
3683 header_present
= false;
3687 /* Allocate 2 registers for a header */
3690 if (payload
.aa_dest_stencil_reg
) {
3691 sources
[length
] = fs_reg(GRF
, alloc
.allocate(1));
3692 emit(MOV(sources
[length
],
3693 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0))));
3697 prog_data
->uses_omask
=
3698 prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
3699 if (prog_data
->uses_omask
) {
3700 this->current_annotation
= "FB write oMask";
3701 assert(this->sample_mask
.file
!= BAD_FILE
);
3702 /* Hand over gl_SampleMask. Only lower 16 bits are relevant. Since
3703 * it's unsinged single words, one vgrf is always 16-wide.
3705 sources
[length
] = fs_reg(GRF
, alloc
.allocate(1),
3706 BRW_REGISTER_TYPE_UW
, 16);
3707 emit(FS_OPCODE_SET_OMASK
, sources
[length
], this->sample_mask
);
3711 if (color0
.file
== BAD_FILE
) {
3712 /* Even if there's no color buffers enabled, we still need to send
3713 * alpha out the pipeline to our null renderbuffer to support
3714 * alpha-testing, alpha-to-coverage, and so on.
3716 length
+= setup_color_payload(sources
+ length
, this->outputs
[0], 0,
3718 } else if (color1
.file
== BAD_FILE
) {
3719 if (src0_alpha
.file
!= BAD_FILE
) {
3720 sources
[length
] = fs_reg(GRF
, alloc
.allocate(reg_size
),
3721 src0_alpha
.type
, src0_alpha
.width
);
3722 fs_inst
*inst
= emit(MOV(sources
[length
], src0_alpha
));
3723 inst
->saturate
= key
->clamp_fragment_color
;
3727 length
+= setup_color_payload(sources
+ length
, color0
, components
,
3730 length
+= setup_color_payload(sources
+ length
, color0
, components
,
3732 length
+= setup_color_payload(sources
+ length
, color1
, components
,
3736 if (source_depth_to_render_target
) {
3737 if (brw
->gen
== 6) {
3738 /* For outputting oDepth on gen6, SIMD8 writes have to be
3739 * used. This would require SIMD8 moves of each half to
3740 * message regs, kind of like pre-gen5 SIMD16 FB writes.
3741 * Just bail on doing so for now.
3743 no16("Missing support for simd16 depth writes on gen6\n");
3746 sources
[length
] = vgrf(glsl_type::float_type
);
3747 if (prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
3748 /* Hand over gl_FragDepth. */
3749 assert(this->frag_depth
.file
!= BAD_FILE
);
3750 emit(MOV(sources
[length
], this->frag_depth
));
3752 /* Pass through the payload depth. */
3753 emit(MOV(sources
[length
],
3754 fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0))));
3759 if (payload
.dest_depth_reg
) {
3760 sources
[length
] = vgrf(glsl_type::float_type
);
3761 emit(MOV(sources
[length
],
3762 fs_reg(brw_vec8_grf(payload
.dest_depth_reg
, 0))));
3768 if (brw
->gen
>= 7) {
3769 /* Send from the GRF */
3770 fs_reg payload
= fs_reg(GRF
, -1, BRW_REGISTER_TYPE_F
);
3771 load
= emit(LOAD_PAYLOAD(payload
, sources
, length
));
3772 payload
.reg
= alloc
.allocate(load
->regs_written
);
3773 payload
.width
= dispatch_width
;
3774 load
->dst
= payload
;
3775 write
= emit(FS_OPCODE_FB_WRITE
, reg_undef
, payload
);
3776 write
->base_mrf
= -1;
3778 /* Send from the MRF */
3779 load
= emit(LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3781 write
= emit(FS_OPCODE_FB_WRITE
);
3782 write
->exec_size
= dispatch_width
;
3783 write
->base_mrf
= 1;
3786 write
->mlen
= load
->regs_written
;
3787 write
->header_present
= header_present
;
3788 if (prog_data
->uses_kill
) {
3789 write
->predicate
= BRW_PREDICATE_NORMAL
;
3790 write
->flag_subreg
= 1;
3796 fs_visitor::emit_fb_writes()
3798 assert(stage
== MESA_SHADER_FRAGMENT
);
3799 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3800 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3802 fs_inst
*inst
= NULL
;
3804 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
3805 "FB dual-source write");
3806 inst
= emit_single_fb_write(this->outputs
[0], this->dual_src_output
,
3810 /* SIMD16 dual source blending requires to send two SIMD8 dual source
3811 * messages, where each message contains color data for 8 pixels. Color
3812 * data for the first group of pixels is stored in the "lower" half of
3813 * the color registers, so in SIMD16, the previous message did:
3819 * Here goes the second message, which packs color data for the
3820 * remaining 8 pixels. Color data for these pixels is stored in the
3821 * "upper" half of the color registers, so we need to do:
3827 if (dispatch_width
== 16) {
3828 inst
= emit_single_fb_write(this->outputs
[0], this->dual_src_output
,
3829 reg_undef
, 4, true);
3833 prog_data
->dual_src_blend
= true;
3835 for (int target
= 0; target
< key
->nr_color_regions
; target
++) {
3836 /* Skip over outputs that weren't written. */
3837 if (this->outputs
[target
].file
== BAD_FILE
)
3840 this->current_annotation
= ralloc_asprintf(this->mem_ctx
,
3841 "FB write target %d",
3844 if (brw
->gen
>= 6 && key
->replicate_alpha
&& target
!= 0)
3845 src0_alpha
= offset(outputs
[0], 3);
3847 inst
= emit_single_fb_write(this->outputs
[target
], reg_undef
,
3849 this->output_components
[target
]);
3850 inst
->target
= target
;
3855 /* Even if there's no color buffers enabled, we still need to send
3856 * alpha out the pipeline to our null renderbuffer to support
3857 * alpha-testing, alpha-to-coverage, and so on.
3859 inst
= emit_single_fb_write(reg_undef
, reg_undef
, reg_undef
, 0);
3864 this->current_annotation
= NULL
;
3868 fs_visitor::setup_uniform_clipplane_values()
3870 gl_clip_plane
*clip_planes
= brw_select_clip_planes(ctx
);
3871 const struct brw_vue_prog_key
*key
=
3872 (const struct brw_vue_prog_key
*) this->key
;
3874 for (int i
= 0; i
< key
->nr_userclip_plane_consts
; i
++) {
3875 this->userplane
[i
] = fs_reg(UNIFORM
, uniforms
);
3876 for (int j
= 0; j
< 4; ++j
) {
3877 stage_prog_data
->param
[uniforms
+ j
] =
3878 (gl_constant_value
*) &clip_planes
[i
][j
];
3884 void fs_visitor::compute_clip_distance()
3886 struct brw_vue_prog_data
*vue_prog_data
=
3887 (struct brw_vue_prog_data
*) prog_data
;
3888 const struct brw_vue_prog_key
*key
=
3889 (const struct brw_vue_prog_key
*) this->key
;
3891 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special Variables):
3893 * "If a linked set of shaders forming the vertex stage contains no
3894 * static write to gl_ClipVertex or gl_ClipDistance, but the
3895 * application has requested clipping against user clip planes through
3896 * the API, then the coordinate written to gl_Position is used for
3897 * comparison against the user clip planes."
3899 * This function is only called if the shader didn't write to
3900 * gl_ClipDistance. Accordingly, we use gl_ClipVertex to perform clipping
3901 * if the user wrote to it; otherwise we use gl_Position.
3904 gl_varying_slot clip_vertex
= VARYING_SLOT_CLIP_VERTEX
;
3905 if (!(vue_prog_data
->vue_map
.slots_valid
& VARYING_BIT_CLIP_VERTEX
))
3906 clip_vertex
= VARYING_SLOT_POS
;
3908 /* If the clip vertex isn't written, skip this. Typically this means
3909 * the GS will set up clipping. */
3910 if (outputs
[clip_vertex
].file
== BAD_FILE
)
3913 setup_uniform_clipplane_values();
3915 current_annotation
= "user clip distances";
3917 this->outputs
[VARYING_SLOT_CLIP_DIST0
] = vgrf(glsl_type::vec4_type
);
3918 this->outputs
[VARYING_SLOT_CLIP_DIST1
] = vgrf(glsl_type::vec4_type
);
3920 for (int i
= 0; i
< key
->nr_userclip_plane_consts
; i
++) {
3921 fs_reg u
= userplane
[i
];
3922 fs_reg output
= outputs
[VARYING_SLOT_CLIP_DIST0
+ i
/ 4];
3923 output
.reg_offset
= i
& 3;
3925 emit(MUL(output
, outputs
[clip_vertex
], u
));
3926 for (int j
= 1; j
< 4; j
++) {
3927 u
.reg
= userplane
[i
].reg
+ j
;
3928 emit(MAD(output
, output
, offset(outputs
[clip_vertex
], j
), u
));
3934 fs_visitor::emit_urb_writes()
3936 int slot
, urb_offset
, length
;
3937 struct brw_vs_prog_data
*vs_prog_data
=
3938 (struct brw_vs_prog_data
*) prog_data
;
3939 const struct brw_vs_prog_key
*key
=
3940 (const struct brw_vs_prog_key
*) this->key
;
3941 const GLbitfield64 psiz_mask
=
3942 VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
| VARYING_BIT_PSIZ
;
3943 const struct brw_vue_map
*vue_map
= &vs_prog_data
->base
.vue_map
;
3947 /* Lower legacy ff and ClipVertex clipping to clip distances */
3948 if (key
->base
.userclip_active
&& !prog
->UsesClipDistanceOut
)
3949 compute_clip_distance();
3951 /* If we don't have any valid slots to write, just do a minimal urb write
3952 * send to terminate the shader. */
3953 if (vue_map
->slots_valid
== 0) {
3955 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
3956 fs_inst
*inst
= emit(MOV(payload
, fs_reg(retype(brw_vec8_grf(1, 0),
3957 BRW_REGISTER_TYPE_UD
))));
3958 inst
->force_writemask_all
= true;
3960 inst
= emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
3970 for (slot
= 0; slot
< vue_map
->num_slots
; slot
++) {
3971 fs_reg reg
, src
, zero
;
3973 int varying
= vue_map
->slot_to_varying
[slot
];
3975 case VARYING_SLOT_PSIZ
:
3977 /* The point size varying slot is the vue header and is always in the
3978 * vue map. But often none of the special varyings that live there
3979 * are written and in that case we can skip writing to the vue
3980 * header, provided the corresponding state properly clamps the
3981 * values further down the pipeline. */
3982 if ((vue_map
->slots_valid
& psiz_mask
) == 0) {
3983 assert(length
== 0);
3988 zero
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
3989 emit(MOV(zero
, fs_reg(0u)));
3991 sources
[length
++] = zero
;
3992 if (vue_map
->slots_valid
& VARYING_BIT_LAYER
)
3993 sources
[length
++] = this->outputs
[VARYING_SLOT_LAYER
];
3995 sources
[length
++] = zero
;
3997 if (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
)
3998 sources
[length
++] = this->outputs
[VARYING_SLOT_VIEWPORT
];
4000 sources
[length
++] = zero
;
4002 if (vue_map
->slots_valid
& VARYING_BIT_PSIZ
)
4003 sources
[length
++] = this->outputs
[VARYING_SLOT_PSIZ
];
4005 sources
[length
++] = zero
;
4008 case BRW_VARYING_SLOT_NDC
:
4009 case VARYING_SLOT_EDGE
:
4010 unreachable("unexpected scalar vs output");
4013 case BRW_VARYING_SLOT_PAD
:
4017 /* gl_Position is always in the vue map, but isn't always written by
4018 * the shader. Other varyings (clip distances) get added to the vue
4019 * map but don't always get written. In those cases, the
4020 * corresponding this->output[] slot will be invalid we and can skip
4021 * the urb write for the varying. If we've already queued up a vue
4022 * slot for writing we flush a mlen 5 urb write, otherwise we just
4023 * advance the urb_offset.
4025 if (this->outputs
[varying
].file
== BAD_FILE
) {
4033 if ((varying
== VARYING_SLOT_COL0
||
4034 varying
== VARYING_SLOT_COL1
||
4035 varying
== VARYING_SLOT_BFC0
||
4036 varying
== VARYING_SLOT_BFC1
) &&
4037 key
->clamp_vertex_color
) {
4038 /* We need to clamp these guys, so do a saturating MOV into a
4039 * temp register and use that for the payload.
4041 for (int i
= 0; i
< 4; i
++) {
4042 reg
= fs_reg(GRF
, alloc
.allocate(1), outputs
[varying
].type
);
4043 src
= offset(this->outputs
[varying
], i
);
4044 fs_inst
*inst
= emit(MOV(reg
, src
));
4045 inst
->saturate
= true;
4046 sources
[length
++] = reg
;
4049 for (int i
= 0; i
< 4; i
++)
4050 sources
[length
++] = offset(this->outputs
[varying
], i
);
4055 current_annotation
= "URB write";
4057 /* If we've queued up 8 registers of payload (2 VUE slots), if this is
4058 * the last slot or if we need to flush (see BAD_FILE varying case
4059 * above), emit a URB write send now to flush out the data.
4061 int last
= slot
== vue_map
->num_slots
- 1;
4062 if (length
== 8 || last
)
4065 fs_reg
*payload_sources
= ralloc_array(mem_ctx
, fs_reg
, length
+ 1);
4066 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(length
+ 1),
4067 BRW_REGISTER_TYPE_F
);
4069 /* We need WE_all on the MOV for the message header (the URB handles)
4070 * so do a MOV to a dummy register and set force_writemask_all on the
4071 * MOV. LOAD_PAYLOAD will preserve that.
4073 fs_reg dummy
= fs_reg(GRF
, alloc
.allocate(1),
4074 BRW_REGISTER_TYPE_UD
);
4075 fs_inst
*inst
= emit(MOV(dummy
, fs_reg(retype(brw_vec8_grf(1, 0),
4076 BRW_REGISTER_TYPE_UD
))));
4077 inst
->force_writemask_all
= true;
4078 payload_sources
[0] = dummy
;
4080 memcpy(&payload_sources
[1], sources
, length
* sizeof sources
[0]);
4081 emit(LOAD_PAYLOAD(payload
, payload_sources
, length
+ 1));
4083 inst
= emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
4085 inst
->mlen
= length
+ 1;
4086 inst
->offset
= urb_offset
;
4087 urb_offset
= slot
+ 1;
4095 fs_visitor::resolve_ud_negate(fs_reg
*reg
)
4097 if (reg
->type
!= BRW_REGISTER_TYPE_UD
||
4101 fs_reg temp
= vgrf(glsl_type::uint_type
);
4102 emit(MOV(temp
, *reg
));
4107 * Resolve the result of a Gen4-5 CMP instruction to a proper boolean.
4109 * CMP on Gen4-5 only sets the LSB of the result; the rest are undefined.
4110 * If we need a proper boolean value, we have to fix it up to be 0 or ~0.
4113 fs_visitor::resolve_bool_comparison(ir_rvalue
*rvalue
, fs_reg
*reg
)
4115 assert(brw
->gen
<= 5);
4117 if (rvalue
->type
!= glsl_type::bool_type
)
4120 fs_reg and_result
= vgrf(glsl_type::bool_type
);
4121 fs_reg neg_result
= vgrf(glsl_type::bool_type
);
4122 emit(AND(and_result
, *reg
, fs_reg(1)));
4123 emit(MOV(neg_result
, negate(and_result
)));
4127 fs_visitor::fs_visitor(struct brw_context
*brw
,
4129 const struct brw_wm_prog_key
*key
,
4130 struct brw_wm_prog_data
*prog_data
,
4131 struct gl_shader_program
*shader_prog
,
4132 struct gl_fragment_program
*fp
,
4133 unsigned dispatch_width
)
4134 : backend_visitor(brw
, shader_prog
, &fp
->Base
, &prog_data
->base
,
4135 MESA_SHADER_FRAGMENT
),
4136 reg_null_f(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_F
)),
4137 reg_null_d(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_D
)),
4138 reg_null_ud(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_UD
)),
4139 key(key
), prog_data(&prog_data
->base
),
4140 dispatch_width(dispatch_width
), promoted_constants(0)
4142 this->mem_ctx
= mem_ctx
;
4146 fs_visitor::fs_visitor(struct brw_context
*brw
,
4148 const struct brw_vs_prog_key
*key
,
4149 struct brw_vs_prog_data
*prog_data
,
4150 struct gl_shader_program
*shader_prog
,
4151 struct gl_vertex_program
*cp
,
4152 unsigned dispatch_width
)
4153 : backend_visitor(brw
, shader_prog
, &cp
->Base
, &prog_data
->base
.base
,
4154 MESA_SHADER_VERTEX
),
4155 reg_null_f(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_F
)),
4156 reg_null_d(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_D
)),
4157 reg_null_ud(retype(brw_null_vec(dispatch_width
), BRW_REGISTER_TYPE_UD
)),
4158 key(key
), prog_data(&prog_data
->base
.base
),
4159 dispatch_width(dispatch_width
), promoted_constants(0)
4161 this->mem_ctx
= mem_ctx
;
4169 case MESA_SHADER_FRAGMENT
:
4170 key_tex
= &((const brw_wm_prog_key
*) key
)->tex
;
4172 case MESA_SHADER_VERTEX
:
4173 case MESA_SHADER_GEOMETRY
:
4174 key_tex
= &((const brw_vue_prog_key
*) key
)->tex
;
4177 unreachable("unhandled shader stage");
4180 this->failed
= false;
4181 this->simd16_unsupported
= false;
4182 this->no16_msg
= NULL
;
4183 this->variable_ht
= hash_table_ctor(0,
4184 hash_table_pointer_hash
,
4185 hash_table_pointer_compare
);
4187 this->nir_locals
= NULL
;
4188 this->nir_globals
= NULL
;
4190 memset(&this->payload
, 0, sizeof(this->payload
));
4191 memset(this->outputs
, 0, sizeof(this->outputs
));
4192 memset(this->output_components
, 0, sizeof(this->output_components
));
4193 this->source_depth_to_render_target
= false;
4194 this->runtime_check_aads_emit
= false;
4195 this->first_non_payload_grf
= 0;
4196 this->max_grf
= brw
->gen
>= 7 ? GEN7_MRF_HACK_START
: BRW_MAX_GRF
;
4198 this->current_annotation
= NULL
;
4199 this->base_ir
= NULL
;
4201 this->virtual_grf_start
= NULL
;
4202 this->virtual_grf_end
= NULL
;
4203 this->live_intervals
= NULL
;
4204 this->regs_live_at_ip
= NULL
;
4207 this->last_scratch
= 0;
4208 this->pull_constant_loc
= NULL
;
4209 this->push_constant_loc
= NULL
;
4211 this->spilled_any_registers
= false;
4212 this->do_dual_src
= false;
4214 if (dispatch_width
== 8)
4215 this->param_size
= rzalloc_array(mem_ctx
, int, stage_prog_data
->nr_params
);
4218 fs_visitor::~fs_visitor()
4220 hash_table_dtor(this->variable_ht
);