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
25 #include "glsl/ir_optimization.h"
26 #include "glsl/nir/glsl_to_nir.h"
27 #include "main/shaderimage.h"
28 #include "program/prog_to_nir.h"
30 #include "brw_fs_surface_builder.h"
36 fs_visitor::emit_nir_code()
38 nir_shader
*nir
= prog
->nir
;
40 /* emit the arrays used for inputs and outputs - load/store intrinsics will
41 * be converted to reads/writes of these arrays
43 nir_setup_inputs(nir
);
44 nir_setup_outputs(nir
);
45 nir_setup_uniforms(nir
);
46 nir_emit_system_values(nir
);
48 /* get the main function and emit it */
49 nir_foreach_overload(nir
, overload
) {
50 assert(strcmp(overload
->function
->name
, "main") == 0);
51 assert(overload
->impl
);
52 nir_emit_impl(overload
->impl
);
57 fs_visitor::nir_setup_inputs(nir_shader
*shader
)
59 nir_inputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, shader
->num_inputs
);
61 foreach_list_typed(nir_variable
, var
, node
, &shader
->inputs
) {
62 enum brw_reg_type type
= brw_type_for_base_type(var
->type
);
63 fs_reg input
= offset(nir_inputs
, bld
, var
->data
.driver_location
);
67 case MESA_SHADER_VERTEX
: {
68 /* Our ATTR file is indexed by VERT_ATTRIB_*, which is the value
69 * stored in nir_variable::location.
71 * However, NIR's load_input intrinsics use a different index - an
72 * offset into a single contiguous array containing all inputs.
73 * This index corresponds to the nir_variable::driver_location field.
75 * So, we need to copy from fs_reg(ATTR, var->location) to
76 * offset(nir_inputs, var->data.driver_location).
78 const glsl_type
*const t
= var
->type
->without_array();
79 const unsigned components
= t
->components();
80 const unsigned cols
= t
->matrix_columns
;
81 const unsigned elts
= t
->vector_elements
;
82 unsigned array_length
= var
->type
->is_array() ? var
->type
->length
: 1;
83 for (unsigned i
= 0; i
< array_length
; i
++) {
84 for (unsigned j
= 0; j
< cols
; j
++) {
85 for (unsigned k
= 0; k
< elts
; k
++) {
86 bld
.MOV(offset(retype(input
, type
), bld
,
87 components
* i
+ elts
* j
+ k
),
88 offset(fs_reg(ATTR
, var
->data
.location
+ i
, type
),
95 case MESA_SHADER_GEOMETRY
:
96 case MESA_SHADER_COMPUTE
:
97 case MESA_SHADER_TESS_CTRL
:
98 case MESA_SHADER_TESS_EVAL
:
99 unreachable("fs_visitor not used for these stages yet.");
101 case MESA_SHADER_FRAGMENT
:
102 if (var
->data
.location
== VARYING_SLOT_POS
) {
103 reg
= *emit_fragcoord_interpolation(var
->data
.pixel_center_integer
,
104 var
->data
.origin_upper_left
);
105 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
108 emit_general_interpolation(input
, var
->name
, var
->type
,
109 (glsl_interp_qualifier
) var
->data
.interpolation
,
110 var
->data
.location
, var
->data
.centroid
,
119 fs_visitor::nir_setup_outputs(nir_shader
*shader
)
121 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
123 nir_outputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, shader
->num_outputs
);
125 foreach_list_typed(nir_variable
, var
, node
, &shader
->outputs
) {
126 fs_reg reg
= offset(nir_outputs
, bld
, var
->data
.driver_location
);
128 int vector_elements
=
129 var
->type
->is_array() ? var
->type
->fields
.array
->vector_elements
130 : var
->type
->vector_elements
;
133 case MESA_SHADER_VERTEX
:
134 for (int i
= 0; i
< ALIGN(type_size_scalar(var
->type
), 4) / 4; i
++) {
135 int output
= var
->data
.location
+ i
;
136 this->outputs
[output
] = offset(reg
, bld
, 4 * i
);
137 this->output_components
[output
] = vector_elements
;
140 case MESA_SHADER_FRAGMENT
:
141 if (var
->data
.index
> 0) {
142 assert(var
->data
.location
== FRAG_RESULT_DATA0
);
143 assert(var
->data
.index
== 1);
144 this->dual_src_output
= reg
;
145 this->do_dual_src
= true;
146 } else if (var
->data
.location
== FRAG_RESULT_COLOR
) {
147 /* Writing gl_FragColor outputs to all color regions. */
148 for (unsigned int i
= 0; i
< MAX2(key
->nr_color_regions
, 1); i
++) {
149 this->outputs
[i
] = reg
;
150 this->output_components
[i
] = 4;
152 } else if (var
->data
.location
== FRAG_RESULT_DEPTH
) {
153 this->frag_depth
= reg
;
154 } else if (var
->data
.location
== FRAG_RESULT_SAMPLE_MASK
) {
155 this->sample_mask
= reg
;
157 /* gl_FragData or a user-defined FS output */
158 assert(var
->data
.location
>= FRAG_RESULT_DATA0
&&
159 var
->data
.location
< FRAG_RESULT_DATA0
+BRW_MAX_DRAW_BUFFERS
);
161 /* General color output. */
162 for (unsigned int i
= 0; i
< MAX2(1, var
->type
->length
); i
++) {
163 int output
= var
->data
.location
- FRAG_RESULT_DATA0
+ i
;
164 this->outputs
[output
] = offset(reg
, bld
, vector_elements
* i
);
165 this->output_components
[output
] = vector_elements
;
170 unreachable("unhandled shader stage");
176 fs_visitor::nir_setup_uniforms(nir_shader
*shader
)
178 num_direct_uniforms
= shader
->num_direct_uniforms
;
180 if (dispatch_width
!= 8)
183 /* We split the uniform register file in half. The first half is
184 * entirely direct uniforms. The second half is indirect.
186 if (num_direct_uniforms
> 0)
187 param_size
[0] = num_direct_uniforms
;
188 if (shader
->num_uniforms
> num_direct_uniforms
)
189 param_size
[num_direct_uniforms
] = shader
->num_uniforms
- num_direct_uniforms
;
191 uniforms
= shader
->num_uniforms
;
194 foreach_list_typed(nir_variable
, var
, node
, &shader
->uniforms
) {
195 /* UBO's and atomics don't take up space in the uniform file */
196 if (var
->interface_type
!= NULL
|| var
->type
->contains_atomic())
199 if (strncmp(var
->name
, "gl_", 3) == 0)
200 nir_setup_builtin_uniform(var
);
202 nir_setup_uniform(var
);
205 /* prog_to_nir doesn't create uniform variables; set param up directly. */
206 for (unsigned p
= 0; p
< prog
->Parameters
->NumParameters
; p
++) {
207 for (unsigned int i
= 0; i
< 4; i
++) {
208 stage_prog_data
->param
[4 * p
+ i
] =
209 &prog
->Parameters
->ParameterValues
[p
][i
];
216 fs_visitor::nir_setup_uniform(nir_variable
*var
)
218 int namelen
= strlen(var
->name
);
220 /* The data for our (non-builtin) uniforms is stored in a series of
221 * gl_uniform_driver_storage structs for each subcomponent that
222 * glGetUniformLocation() could name. We know it's been set up in the
223 * same order we'd walk the type, so walk the list of storage and find
224 * anything with our name, or the prefix of a component that starts with
227 unsigned index
= var
->data
.driver_location
;
228 for (unsigned u
= 0; u
< shader_prog
->NumUniformStorage
; u
++) {
229 struct gl_uniform_storage
*storage
= &shader_prog
->UniformStorage
[u
];
231 if (storage
->builtin
)
234 if (strncmp(var
->name
, storage
->name
, namelen
) != 0 ||
235 (storage
->name
[namelen
] != 0 &&
236 storage
->name
[namelen
] != '.' &&
237 storage
->name
[namelen
] != '[')) {
241 if (storage
->type
->is_image()) {
242 setup_image_uniform_values(index
, storage
);
244 unsigned slots
= storage
->type
->component_slots();
245 if (storage
->array_elements
)
246 slots
*= storage
->array_elements
;
248 for (unsigned i
= 0; i
< slots
; i
++) {
249 stage_prog_data
->param
[index
++] = &storage
->storage
[i
];
256 fs_visitor::nir_setup_builtin_uniform(nir_variable
*var
)
258 const nir_state_slot
*const slots
= var
->state_slots
;
259 assert(var
->state_slots
!= NULL
);
261 unsigned uniform_index
= var
->data
.driver_location
;
262 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
263 /* This state reference has already been setup by ir_to_mesa, but we'll
264 * get the same index back here.
266 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
267 (gl_state_index
*)slots
[i
].tokens
);
269 /* Add each of the unique swizzles of the element as a parameter.
270 * This'll end up matching the expected layout of the
271 * array/matrix/structure we're trying to fill in.
274 for (unsigned int j
= 0; j
< 4; j
++) {
275 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
276 if (swiz
== last_swiz
)
280 stage_prog_data
->param
[uniform_index
++] =
281 &prog
->Parameters
->ParameterValues
[index
][swiz
];
287 emit_system_values_block(nir_block
*block
, void *void_visitor
)
289 fs_visitor
*v
= (fs_visitor
*)void_visitor
;
292 nir_foreach_instr(block
, instr
) {
293 if (instr
->type
!= nir_instr_type_intrinsic
)
296 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
297 switch (intrin
->intrinsic
) {
298 case nir_intrinsic_load_vertex_id
:
299 unreachable("should be lowered by lower_vertex_id().");
301 case nir_intrinsic_load_vertex_id_zero_base
:
302 assert(v
->stage
== MESA_SHADER_VERTEX
);
303 reg
= &v
->nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
304 if (reg
->file
== BAD_FILE
)
305 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
);
308 case nir_intrinsic_load_base_vertex
:
309 assert(v
->stage
== MESA_SHADER_VERTEX
);
310 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
311 if (reg
->file
== BAD_FILE
)
312 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX
);
315 case nir_intrinsic_load_instance_id
:
316 assert(v
->stage
== MESA_SHADER_VERTEX
);
317 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
318 if (reg
->file
== BAD_FILE
)
319 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID
);
322 case nir_intrinsic_load_sample_pos
:
323 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
324 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
325 if (reg
->file
== BAD_FILE
)
326 *reg
= *v
->emit_samplepos_setup();
329 case nir_intrinsic_load_sample_id
:
330 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
331 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
332 if (reg
->file
== BAD_FILE
)
333 *reg
= *v
->emit_sampleid_setup();
336 case nir_intrinsic_load_sample_mask_in
:
337 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
338 assert(v
->devinfo
->gen
>= 7);
339 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
340 if (reg
->file
== BAD_FILE
)
341 *reg
= fs_reg(retype(brw_vec8_grf(v
->payload
.sample_mask_in_reg
, 0),
342 BRW_REGISTER_TYPE_D
));
354 fs_visitor::nir_emit_system_values(nir_shader
*shader
)
356 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
357 nir_foreach_overload(shader
, overload
) {
358 assert(strcmp(overload
->function
->name
, "main") == 0);
359 assert(overload
->impl
);
360 nir_foreach_block(overload
->impl
, emit_system_values_block
, this);
365 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
367 nir_locals
= reralloc(mem_ctx
, nir_locals
, fs_reg
, impl
->reg_alloc
);
368 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
369 unsigned array_elems
=
370 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
371 unsigned size
= array_elems
* reg
->num_components
;
372 nir_locals
[reg
->index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
375 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
378 nir_emit_cf_list(&impl
->body
);
382 fs_visitor::nir_emit_cf_list(exec_list
*list
)
384 exec_list_validate(list
);
385 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
386 switch (node
->type
) {
388 nir_emit_if(nir_cf_node_as_if(node
));
391 case nir_cf_node_loop
:
392 nir_emit_loop(nir_cf_node_as_loop(node
));
395 case nir_cf_node_block
:
396 nir_emit_block(nir_cf_node_as_block(node
));
400 unreachable("Invalid CFG node block");
406 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
408 /* first, put the condition into f0 */
409 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
410 retype(get_nir_src(if_stmt
->condition
),
411 BRW_REGISTER_TYPE_D
));
412 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
414 bld
.IF(BRW_PREDICATE_NORMAL
);
416 nir_emit_cf_list(&if_stmt
->then_list
);
418 /* note: if the else is empty, dead CF elimination will remove it */
419 bld
.emit(BRW_OPCODE_ELSE
);
421 nir_emit_cf_list(&if_stmt
->else_list
);
423 bld
.emit(BRW_OPCODE_ENDIF
);
425 try_replace_with_sel();
429 fs_visitor::nir_emit_loop(nir_loop
*loop
)
431 bld
.emit(BRW_OPCODE_DO
);
433 nir_emit_cf_list(&loop
->body
);
435 bld
.emit(BRW_OPCODE_WHILE
);
439 fs_visitor::nir_emit_block(nir_block
*block
)
441 nir_foreach_instr(block
, instr
) {
442 nir_emit_instr(instr
);
447 fs_visitor::nir_emit_instr(nir_instr
*instr
)
449 const fs_builder abld
= bld
.annotate(NULL
, instr
);
451 switch (instr
->type
) {
452 case nir_instr_type_alu
:
453 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
456 case nir_instr_type_intrinsic
:
457 nir_emit_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
460 case nir_instr_type_tex
:
461 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
464 case nir_instr_type_load_const
:
465 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
468 case nir_instr_type_ssa_undef
:
469 nir_emit_undef(abld
, nir_instr_as_ssa_undef(instr
));
472 case nir_instr_type_jump
:
473 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
477 unreachable("unknown instruction type");
482 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
483 const fs_reg
&result
)
485 if (!instr
->src
[0].src
.is_ssa
||
486 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
489 nir_intrinsic_instr
*src0
=
490 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
492 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
495 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
496 if (!value1
|| fabsf(value1
->f
[0]) != 1.0f
)
499 nir_const_value
*value2
= nir_src_as_const_value(instr
->src
[2].src
);
500 if (!value2
|| fabsf(value2
->f
[0]) != 1.0f
)
503 fs_reg tmp
= vgrf(glsl_type::int_type
);
505 if (devinfo
->gen
>= 6) {
506 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
507 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
509 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
511 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
512 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
514 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
516 * This negation looks like it's safe in practice, because bits 0:4 will
517 * surely be TRIANGLES
520 if (value1
->f
[0] == -1.0f
) {
524 tmp
.type
= BRW_REGISTER_TYPE_W
;
525 tmp
.subreg_offset
= 2;
528 fs_inst
*or_inst
= bld
.OR(tmp
, g0
, fs_reg(0x3f80));
529 or_inst
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
531 tmp
.type
= BRW_REGISTER_TYPE_D
;
532 tmp
.subreg_offset
= 0;
535 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
536 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
538 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
540 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
541 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
543 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
545 * This negation looks like it's safe in practice, because bits 0:4 will
546 * surely be TRIANGLES
549 if (value1
->f
[0] == -1.0f
) {
553 bld
.OR(tmp
, g1_6
, fs_reg(0x3f800000));
555 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, fs_reg(0xbf800000));
561 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
563 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
566 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
567 result
.type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].output_type
);
570 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
571 op
[i
] = get_nir_src(instr
->src
[i
].src
);
572 op
[i
].type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].input_types
[i
]);
573 op
[i
].abs
= instr
->src
[i
].abs
;
574 op
[i
].negate
= instr
->src
[i
].negate
;
577 /* We get a bunch of mov's out of the from_ssa pass and they may still
578 * be vectorized. We'll handle them as a special-case. We'll also
579 * handle vecN here because it's basically the same thing.
587 fs_reg temp
= result
;
588 bool need_extra_copy
= false;
589 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
590 if (!instr
->src
[i
].src
.is_ssa
&&
591 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
592 need_extra_copy
= true;
593 temp
= bld
.vgrf(result
.type
, 4);
598 for (unsigned i
= 0; i
< 4; i
++) {
599 if (!(instr
->dest
.write_mask
& (1 << i
)))
602 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
603 inst
= bld
.MOV(offset(temp
, bld
, i
),
604 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
606 inst
= bld
.MOV(offset(temp
, bld
, i
),
607 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
609 inst
->saturate
= instr
->dest
.saturate
;
612 /* In this case the source and destination registers were the same,
613 * so we need to insert an extra set of moves in order to deal with
616 if (need_extra_copy
) {
617 for (unsigned i
= 0; i
< 4; i
++) {
618 if (!(instr
->dest
.write_mask
& (1 << i
)))
621 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
630 /* At this point, we have dealt with any instruction that operates on
631 * more than a single channel. Therefore, we can just adjust the source
632 * and destination registers for that channel and emit the instruction.
634 unsigned channel
= 0;
635 if (nir_op_infos
[instr
->op
].output_size
== 0) {
636 /* Since NIR is doing the scalarizing for us, we should only ever see
637 * vectorized operations with a single channel.
639 assert(_mesa_bitcount(instr
->dest
.write_mask
) == 1);
640 channel
= ffs(instr
->dest
.write_mask
) - 1;
642 result
= offset(result
, bld
, channel
);
645 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
646 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
647 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
653 inst
= bld
.MOV(result
, op
[0]);
654 inst
->saturate
= instr
->dest
.saturate
;
659 bld
.MOV(result
, op
[0]);
663 /* AND(val, 0x80000000) gives the sign bit.
665 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
668 bld
.CMP(bld
.null_reg_f(), op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
);
670 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
671 op
[0].type
= BRW_REGISTER_TYPE_UD
;
672 result
.type
= BRW_REGISTER_TYPE_UD
;
673 bld
.AND(result_int
, op
[0], fs_reg(0x80000000u
));
675 inst
= bld
.OR(result_int
, result_int
, fs_reg(0x3f800000u
));
676 inst
->predicate
= BRW_PREDICATE_NORMAL
;
677 if (instr
->dest
.saturate
) {
678 inst
= bld
.MOV(result
, result
);
679 inst
->saturate
= true;
685 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
686 * -> non-negative val generates 0x00000000.
687 * Predicated OR sets 1 if val is positive.
689 bld
.CMP(bld
.null_reg_d(), op
[0], fs_reg(0), BRW_CONDITIONAL_G
);
690 bld
.ASR(result
, op
[0], fs_reg(31));
691 inst
= bld
.OR(result
, result
, fs_reg(1));
692 inst
->predicate
= BRW_PREDICATE_NORMAL
;
696 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
697 inst
->saturate
= instr
->dest
.saturate
;
701 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
702 inst
->saturate
= instr
->dest
.saturate
;
706 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
707 inst
->saturate
= instr
->dest
.saturate
;
711 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
712 inst
->saturate
= instr
->dest
.saturate
;
716 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
717 inst
->saturate
= instr
->dest
.saturate
;
721 if (fs_key
->high_quality_derivatives
) {
722 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
724 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
726 inst
->saturate
= instr
->dest
.saturate
;
728 case nir_op_fddx_fine
:
729 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
730 inst
->saturate
= instr
->dest
.saturate
;
732 case nir_op_fddx_coarse
:
733 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
734 inst
->saturate
= instr
->dest
.saturate
;
737 if (fs_key
->high_quality_derivatives
) {
738 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
739 fs_reg(fs_key
->render_to_fbo
));
741 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
742 fs_reg(fs_key
->render_to_fbo
));
744 inst
->saturate
= instr
->dest
.saturate
;
746 case nir_op_fddy_fine
:
747 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
748 fs_reg(fs_key
->render_to_fbo
));
749 inst
->saturate
= instr
->dest
.saturate
;
751 case nir_op_fddy_coarse
:
752 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
753 fs_reg(fs_key
->render_to_fbo
));
754 inst
->saturate
= instr
->dest
.saturate
;
759 inst
= bld
.ADD(result
, op
[0], op
[1]);
760 inst
->saturate
= instr
->dest
.saturate
;
764 inst
= bld
.MUL(result
, op
[0], op
[1]);
765 inst
->saturate
= instr
->dest
.saturate
;
769 bld
.MUL(result
, op
[0], op
[1]);
772 case nir_op_imul_high
:
773 case nir_op_umul_high
:
774 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
779 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
782 case nir_op_uadd_carry
:
783 unreachable("Should have been lowered by carry_to_arith().");
785 case nir_op_usub_borrow
:
786 unreachable("Should have been lowered by borrow_to_arith().");
789 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
795 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
801 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
806 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_Z
);
811 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_NZ
);
815 if (devinfo
->gen
>= 8) {
816 op
[0] = resolve_source_modifiers(op
[0]);
818 bld
.NOT(result
, op
[0]);
821 if (devinfo
->gen
>= 8) {
822 op
[0] = resolve_source_modifiers(op
[0]);
823 op
[1] = resolve_source_modifiers(op
[1]);
825 bld
.XOR(result
, op
[0], op
[1]);
828 if (devinfo
->gen
>= 8) {
829 op
[0] = resolve_source_modifiers(op
[0]);
830 op
[1] = resolve_source_modifiers(op
[1]);
832 bld
.OR(result
, op
[0], op
[1]);
835 if (devinfo
->gen
>= 8) {
836 op
[0] = resolve_source_modifiers(op
[0]);
837 op
[1] = resolve_source_modifiers(op
[1]);
839 bld
.AND(result
, op
[0], op
[1]);
851 case nir_op_ball_fequal2
:
852 case nir_op_ball_iequal2
:
853 case nir_op_ball_fequal3
:
854 case nir_op_ball_iequal3
:
855 case nir_op_ball_fequal4
:
856 case nir_op_ball_iequal4
:
857 case nir_op_bany_fnequal2
:
858 case nir_op_bany_inequal2
:
859 case nir_op_bany_fnequal3
:
860 case nir_op_bany_inequal3
:
861 case nir_op_bany_fnequal4
:
862 case nir_op_bany_inequal4
:
863 unreachable("Lowered by nir_lower_alu_reductions");
865 case nir_op_fnoise1_1
:
866 case nir_op_fnoise1_2
:
867 case nir_op_fnoise1_3
:
868 case nir_op_fnoise1_4
:
869 case nir_op_fnoise2_1
:
870 case nir_op_fnoise2_2
:
871 case nir_op_fnoise2_3
:
872 case nir_op_fnoise2_4
:
873 case nir_op_fnoise3_1
:
874 case nir_op_fnoise3_2
:
875 case nir_op_fnoise3_3
:
876 case nir_op_fnoise3_4
:
877 case nir_op_fnoise4_1
:
878 case nir_op_fnoise4_2
:
879 case nir_op_fnoise4_3
:
880 case nir_op_fnoise4_4
:
881 unreachable("not reached: should be handled by lower_noise");
884 unreachable("not reached: should be handled by ldexp_to_arith()");
887 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
888 inst
->saturate
= instr
->dest
.saturate
;
892 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
893 inst
->saturate
= instr
->dest
.saturate
;
898 bld
.MOV(result
, negate(op
[0]));
902 bld
.CMP(result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
);
905 bld
.CMP(result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
);
909 inst
= bld
.RNDZ(result
, op
[0]);
910 inst
->saturate
= instr
->dest
.saturate
;
914 op
[0].negate
= !op
[0].negate
;
915 fs_reg temp
= vgrf(glsl_type::float_type
);
916 bld
.RNDD(temp
, op
[0]);
918 inst
= bld
.MOV(result
, temp
);
919 inst
->saturate
= instr
->dest
.saturate
;
923 inst
= bld
.RNDD(result
, op
[0]);
924 inst
->saturate
= instr
->dest
.saturate
;
927 inst
= bld
.FRC(result
, op
[0]);
928 inst
->saturate
= instr
->dest
.saturate
;
930 case nir_op_fround_even
:
931 inst
= bld
.RNDE(result
, op
[0]);
932 inst
->saturate
= instr
->dest
.saturate
;
938 if (devinfo
->gen
>= 6) {
939 inst
= bld
.emit(BRW_OPCODE_SEL
, result
, op
[0], op
[1]);
940 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
942 bld
.CMP(bld
.null_reg_d(), op
[0], op
[1], BRW_CONDITIONAL_L
);
943 inst
= bld
.SEL(result
, op
[0], op
[1]);
944 inst
->predicate
= BRW_PREDICATE_NORMAL
;
946 inst
->saturate
= instr
->dest
.saturate
;
952 if (devinfo
->gen
>= 6) {
953 inst
= bld
.emit(BRW_OPCODE_SEL
, result
, op
[0], op
[1]);
954 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
956 bld
.CMP(bld
.null_reg_d(), op
[0], op
[1], BRW_CONDITIONAL_GE
);
957 inst
= bld
.SEL(result
, op
[0], op
[1]);
958 inst
->predicate
= BRW_PREDICATE_NORMAL
;
960 inst
->saturate
= instr
->dest
.saturate
;
963 case nir_op_pack_snorm_2x16
:
964 case nir_op_pack_snorm_4x8
:
965 case nir_op_pack_unorm_2x16
:
966 case nir_op_pack_unorm_4x8
:
967 case nir_op_unpack_snorm_2x16
:
968 case nir_op_unpack_snorm_4x8
:
969 case nir_op_unpack_unorm_2x16
:
970 case nir_op_unpack_unorm_4x8
:
971 case nir_op_unpack_half_2x16
:
972 case nir_op_pack_half_2x16
:
973 unreachable("not reached: should be handled by lower_packing_builtins");
975 case nir_op_unpack_half_2x16_split_x
:
976 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
977 inst
->saturate
= instr
->dest
.saturate
;
979 case nir_op_unpack_half_2x16_split_y
:
980 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
981 inst
->saturate
= instr
->dest
.saturate
;
985 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
986 inst
->saturate
= instr
->dest
.saturate
;
989 case nir_op_bitfield_reverse
:
990 bld
.BFREV(result
, op
[0]);
993 case nir_op_bit_count
:
994 bld
.CBIT(result
, op
[0]);
997 case nir_op_ufind_msb
:
998 case nir_op_ifind_msb
: {
999 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1001 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1002 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1003 * subtract the result from 31 to convert the MSB count into an LSB count.
1006 bld
.CMP(bld
.null_reg_d(), result
, fs_reg(-1), BRW_CONDITIONAL_NZ
);
1007 fs_reg
neg_result(result
);
1008 neg_result
.negate
= true;
1009 inst
= bld
.ADD(result
, neg_result
, fs_reg(31));
1010 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1014 case nir_op_find_lsb
:
1015 bld
.FBL(result
, op
[0]);
1018 case nir_op_ubitfield_extract
:
1019 case nir_op_ibitfield_extract
:
1020 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1023 bld
.BFI1(result
, op
[0], op
[1]);
1026 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1029 case nir_op_bitfield_insert
:
1030 unreachable("not reached: should be handled by "
1031 "lower_instructions::bitfield_insert_to_bfm_bfi");
1034 bld
.SHL(result
, op
[0], op
[1]);
1037 bld
.ASR(result
, op
[0], op
[1]);
1040 bld
.SHR(result
, op
[0], op
[1]);
1043 case nir_op_pack_half_2x16_split
:
1044 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1048 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1049 inst
->saturate
= instr
->dest
.saturate
;
1053 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1054 inst
->saturate
= instr
->dest
.saturate
;
1058 if (optimize_frontfacing_ternary(instr
, result
))
1061 bld
.CMP(bld
.null_reg_d(), op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
);
1062 inst
= bld
.SEL(result
, op
[1], op
[2]);
1063 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1067 unreachable("unhandled instruction");
1070 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1071 * to sign extend the low bit to 0/~0
1073 if (devinfo
->gen
<= 5 &&
1074 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1075 fs_reg masked
= vgrf(glsl_type::int_type
);
1076 bld
.AND(masked
, result
, fs_reg(1));
1077 masked
.negate
= true;
1078 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1083 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1084 nir_load_const_instr
*instr
)
1086 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_D
, instr
->def
.num_components
);
1088 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1089 bld
.MOV(offset(reg
, bld
, i
), fs_reg(instr
->value
.i
[i
]));
1091 nir_ssa_values
[instr
->def
.index
] = reg
;
1095 fs_visitor::nir_emit_undef(const fs_builder
&bld
, nir_ssa_undef_instr
*instr
)
1097 nir_ssa_values
[instr
->def
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_D
,
1098 instr
->def
.num_components
);
1102 fs_reg_for_nir_reg(fs_visitor
*v
, nir_register
*nir_reg
,
1103 unsigned base_offset
, nir_src
*indirect
)
1107 assert(!nir_reg
->is_global
);
1109 reg
= v
->nir_locals
[nir_reg
->index
];
1111 reg
= offset(reg
, v
->bld
, base_offset
* nir_reg
->num_components
);
1113 int multiplier
= nir_reg
->num_components
* (v
->dispatch_width
/ 8);
1115 reg
.reladdr
= new(v
->mem_ctx
) fs_reg(v
->vgrf(glsl_type::int_type
));
1116 v
->bld
.MUL(*reg
.reladdr
, v
->get_nir_src(*indirect
),
1117 fs_reg(multiplier
));
1124 fs_visitor::get_nir_src(nir_src src
)
1128 reg
= nir_ssa_values
[src
.ssa
->index
];
1130 reg
= fs_reg_for_nir_reg(this, src
.reg
.reg
, src
.reg
.base_offset
,
1134 /* to avoid floating-point denorm flushing problems, set the type by
1135 * default to D - instructions that need floating point semantics will set
1136 * this to F if they need to
1138 return retype(reg
, BRW_REGISTER_TYPE_D
);
1142 fs_visitor::get_nir_dest(nir_dest dest
)
1145 nir_ssa_values
[dest
.ssa
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
,
1146 dest
.ssa
.num_components
);
1147 return nir_ssa_values
[dest
.ssa
.index
];
1150 return fs_reg_for_nir_reg(this, dest
.reg
.reg
, dest
.reg
.base_offset
,
1155 fs_visitor::get_nir_image_deref(const nir_deref_var
*deref
)
1157 fs_reg
image(UNIFORM
, deref
->var
->data
.driver_location
,
1158 BRW_REGISTER_TYPE_UD
);
1160 if (deref
->deref
.child
) {
1161 const nir_deref_array
*deref_array
=
1162 nir_deref_as_array(deref
->deref
.child
);
1163 assert(deref
->deref
.child
->deref_type
== nir_deref_type_array
&&
1164 deref_array
->deref
.child
== NULL
);
1165 const unsigned size
= glsl_get_length(deref
->var
->type
);
1166 const unsigned base
= MIN2(deref_array
->base_offset
, size
- 1);
1168 image
= offset(image
, bld
, base
* BRW_IMAGE_PARAM_SIZE
);
1170 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1171 fs_reg
*tmp
= new(mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1173 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
) {
1174 /* IVB hangs when trying to access an invalid surface index with
1175 * the dataport. According to the spec "if the index used to
1176 * select an individual element is negative or greater than or
1177 * equal to the size of the array, the results of the operation
1178 * are undefined but may not lead to termination" -- which is one
1179 * of the possible outcomes of the hang. Clamp the index to
1180 * prevent access outside of the array bounds.
1182 bld
.emit_minmax(*tmp
, retype(get_nir_src(deref_array
->indirect
),
1183 BRW_REGISTER_TYPE_UD
),
1184 fs_reg(size
- base
- 1), BRW_CONDITIONAL_L
);
1186 bld
.MOV(*tmp
, get_nir_src(deref_array
->indirect
));
1189 bld
.MUL(*tmp
, *tmp
, fs_reg(BRW_IMAGE_PARAM_SIZE
));
1190 image
.reladdr
= tmp
;
1198 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1201 for (unsigned i
= 0; i
< 4; i
++) {
1202 if (!((wr_mask
>> i
) & 1))
1205 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1206 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1207 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1208 if (new_inst
->src
[j
].file
== GRF
)
1209 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1216 * Get the matching channel register datatype for an image intrinsic of the
1217 * specified GLSL image type.
1220 get_image_base_type(const glsl_type
*type
)
1222 switch ((glsl_base_type
)type
->sampler_type
) {
1223 case GLSL_TYPE_UINT
:
1224 return BRW_REGISTER_TYPE_UD
;
1226 return BRW_REGISTER_TYPE_D
;
1227 case GLSL_TYPE_FLOAT
:
1228 return BRW_REGISTER_TYPE_F
;
1230 unreachable("Not reached.");
1235 * Get the appropriate atomic op for an image atomic intrinsic.
1238 get_image_atomic_op(nir_intrinsic_op op
, const glsl_type
*type
)
1241 case nir_intrinsic_image_atomic_add
:
1243 case nir_intrinsic_image_atomic_min
:
1244 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1245 BRW_AOP_IMIN
: BRW_AOP_UMIN
);
1246 case nir_intrinsic_image_atomic_max
:
1247 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1248 BRW_AOP_IMAX
: BRW_AOP_UMAX
);
1249 case nir_intrinsic_image_atomic_and
:
1251 case nir_intrinsic_image_atomic_or
:
1253 case nir_intrinsic_image_atomic_xor
:
1255 case nir_intrinsic_image_atomic_exchange
:
1257 case nir_intrinsic_image_atomic_comp_swap
:
1258 return BRW_AOP_CMPWR
;
1260 unreachable("Not reachable.");
1265 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
1268 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1269 dest
= get_nir_dest(instr
->dest
);
1271 bool has_indirect
= false;
1273 switch (instr
->intrinsic
) {
1274 case nir_intrinsic_discard
:
1275 case nir_intrinsic_discard_if
: {
1276 /* We track our discarded pixels in f0.1. By predicating on it, we can
1277 * update just the flag bits that aren't yet discarded. If there's no
1278 * condition, we emit a CMP of g0 != g0, so all currently executing
1279 * channels will get turned off.
1282 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
1283 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
1284 fs_reg(0), BRW_CONDITIONAL_Z
);
1286 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
1287 BRW_REGISTER_TYPE_UW
));
1288 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
1290 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
1291 cmp
->flag_subreg
= 1;
1293 if (devinfo
->gen
>= 6) {
1294 emit_discard_jump();
1299 case nir_intrinsic_atomic_counter_inc
:
1300 case nir_intrinsic_atomic_counter_dec
:
1301 case nir_intrinsic_atomic_counter_read
: {
1302 using namespace surface_access
;
1304 /* Get the arguments of the atomic intrinsic. */
1305 const fs_reg offset
= get_nir_src(instr
->src
[0]);
1306 const unsigned surface
= (stage_prog_data
->binding_table
.abo_start
+
1307 instr
->const_index
[0]);
1310 /* Emit a surface read or atomic op. */
1311 switch (instr
->intrinsic
) {
1312 case nir_intrinsic_atomic_counter_read
:
1313 tmp
= emit_untyped_read(bld
, fs_reg(surface
), offset
, 1, 1);
1316 case nir_intrinsic_atomic_counter_inc
:
1317 tmp
= emit_untyped_atomic(bld
, fs_reg(surface
), offset
, fs_reg(),
1318 fs_reg(), 1, 1, BRW_AOP_INC
);
1321 case nir_intrinsic_atomic_counter_dec
:
1322 tmp
= emit_untyped_atomic(bld
, fs_reg(surface
), offset
, fs_reg(),
1323 fs_reg(), 1, 1, BRW_AOP_PREDEC
);
1327 unreachable("Unreachable");
1330 /* Assign the result. */
1331 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), tmp
);
1333 /* Mark the surface as used. */
1334 brw_mark_surface_used(stage_prog_data
, surface
);
1338 case nir_intrinsic_image_load
:
1339 case nir_intrinsic_image_store
:
1340 case nir_intrinsic_image_atomic_add
:
1341 case nir_intrinsic_image_atomic_min
:
1342 case nir_intrinsic_image_atomic_max
:
1343 case nir_intrinsic_image_atomic_and
:
1344 case nir_intrinsic_image_atomic_or
:
1345 case nir_intrinsic_image_atomic_xor
:
1346 case nir_intrinsic_image_atomic_exchange
:
1347 case nir_intrinsic_image_atomic_comp_swap
: {
1348 using namespace image_access
;
1350 /* Get the referenced image variable and type. */
1351 const nir_variable
*var
= instr
->variables
[0]->var
;
1352 const glsl_type
*type
= var
->type
->without_array();
1353 const brw_reg_type base_type
= get_image_base_type(type
);
1355 /* Get some metadata from the image intrinsic. */
1356 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1357 const unsigned arr_dims
= type
->sampler_array
? 1 : 0;
1358 const unsigned surf_dims
= type
->coordinate_components() - arr_dims
;
1359 const mesa_format format
=
1360 (var
->data
.image
.write_only
? MESA_FORMAT_NONE
:
1361 _mesa_get_shader_image_format(var
->data
.image
.format
));
1363 /* Get the arguments of the image intrinsic. */
1364 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
1365 const fs_reg addr
= retype(get_nir_src(instr
->src
[0]),
1366 BRW_REGISTER_TYPE_UD
);
1367 const fs_reg src0
= (info
->num_srcs
>= 3 ?
1368 retype(get_nir_src(instr
->src
[2]), base_type
) :
1370 const fs_reg src1
= (info
->num_srcs
>= 4 ?
1371 retype(get_nir_src(instr
->src
[3]), base_type
) :
1375 /* Emit an image load, store or atomic op. */
1376 if (instr
->intrinsic
== nir_intrinsic_image_load
)
1377 tmp
= emit_image_load(bld
, image
, addr
, surf_dims
, arr_dims
, format
);
1379 else if (instr
->intrinsic
== nir_intrinsic_image_store
)
1380 emit_image_store(bld
, image
, addr
, src0
, surf_dims
, arr_dims
, format
);
1383 tmp
= emit_image_atomic(bld
, image
, addr
, src0
, src1
,
1384 surf_dims
, arr_dims
, info
->dest_components
,
1385 get_image_atomic_op(instr
->intrinsic
, type
));
1387 /* Assign the result. */
1388 for (unsigned c
= 0; c
< info
->dest_components
; ++c
)
1389 bld
.MOV(offset(retype(dest
, base_type
), bld
, c
),
1390 offset(tmp
, bld
, c
));
1394 case nir_intrinsic_memory_barrier
: {
1395 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 16 / dispatch_width
);
1396 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
1401 case nir_intrinsic_image_size
: {
1402 /* Get the referenced image variable and type. */
1403 const nir_variable
*var
= instr
->variables
[0]->var
;
1404 const glsl_type
*type
= var
->type
->without_array();
1406 /* Get the size of the image. */
1407 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
1408 const fs_reg size
= offset(image
, bld
, BRW_IMAGE_PARAM_SIZE_OFFSET
);
1410 /* For 1DArray image types, the array index is stored in the Z component.
1411 * Fix this by swizzling the Z component to the Y component.
1413 const bool is_1d_array_image
=
1414 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
&&
1415 type
->sampler_array
;
1417 /* For CubeArray images, we should count the number of cubes instead
1418 * of the number of faces. Fix it by dividing the (Z component) by 6.
1420 const bool is_cube_array_image
=
1421 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
1422 type
->sampler_array
;
1424 /* Copy all the components. */
1425 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1426 for (unsigned c
= 0; c
< info
->dest_components
; ++c
) {
1427 if ((int)c
>= type
->coordinate_components()) {
1428 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
1430 } else if (c
== 1 && is_1d_array_image
) {
1431 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
1432 offset(size
, bld
, 2));
1433 } else if (c
== 2 && is_cube_array_image
) {
1434 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
1435 offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
1436 offset(size
, bld
, c
), fs_reg(6));
1438 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
1439 offset(size
, bld
, c
));
1446 case nir_intrinsic_load_front_face
:
1447 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
1448 *emit_frontfacing_interpolation());
1451 case nir_intrinsic_load_vertex_id
:
1452 unreachable("should be lowered by lower_vertex_id()");
1454 case nir_intrinsic_load_vertex_id_zero_base
: {
1455 fs_reg vertex_id
= nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
1456 assert(vertex_id
.file
!= BAD_FILE
);
1457 dest
.type
= vertex_id
.type
;
1458 bld
.MOV(dest
, vertex_id
);
1462 case nir_intrinsic_load_base_vertex
: {
1463 fs_reg base_vertex
= nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
1464 assert(base_vertex
.file
!= BAD_FILE
);
1465 dest
.type
= base_vertex
.type
;
1466 bld
.MOV(dest
, base_vertex
);
1470 case nir_intrinsic_load_instance_id
: {
1471 fs_reg instance_id
= nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
1472 assert(instance_id
.file
!= BAD_FILE
);
1473 dest
.type
= instance_id
.type
;
1474 bld
.MOV(dest
, instance_id
);
1478 case nir_intrinsic_load_sample_mask_in
: {
1479 fs_reg sample_mask_in
= nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
1480 assert(sample_mask_in
.file
!= BAD_FILE
);
1481 dest
.type
= sample_mask_in
.type
;
1482 bld
.MOV(dest
, sample_mask_in
);
1486 case nir_intrinsic_load_sample_pos
: {
1487 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
1488 assert(sample_pos
.file
!= BAD_FILE
);
1489 dest
.type
= sample_pos
.type
;
1490 bld
.MOV(dest
, sample_pos
);
1491 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
1495 case nir_intrinsic_load_sample_id
: {
1496 fs_reg sample_id
= nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
1497 assert(sample_id
.file
!= BAD_FILE
);
1498 dest
.type
= sample_id
.type
;
1499 bld
.MOV(dest
, sample_id
);
1503 case nir_intrinsic_load_uniform_indirect
:
1504 has_indirect
= true;
1506 case nir_intrinsic_load_uniform
: {
1507 unsigned index
= instr
->const_index
[0];
1510 if (index
< num_direct_uniforms
) {
1511 uniform_reg
= fs_reg(UNIFORM
, 0);
1513 uniform_reg
= fs_reg(UNIFORM
, num_direct_uniforms
);
1514 index
-= num_direct_uniforms
;
1517 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1518 fs_reg src
= offset(retype(uniform_reg
, dest
.type
), bld
, index
);
1520 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[0]));
1524 dest
= offset(dest
, bld
, 1);
1529 case nir_intrinsic_load_ubo_indirect
:
1530 has_indirect
= true;
1532 case nir_intrinsic_load_ubo
: {
1533 nir_const_value
*const_index
= nir_src_as_const_value(instr
->src
[0]);
1537 surf_index
= fs_reg(stage_prog_data
->binding_table
.ubo_start
+
1540 /* The block index is not a constant. Evaluate the index expression
1541 * per-channel and add the base UBO index; we have to select a value
1542 * from any live channel.
1544 surf_index
= vgrf(glsl_type::uint_type
);
1545 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
1546 fs_reg(stage_prog_data
->binding_table
.ubo_start
));
1547 surf_index
= bld
.emit_uniformize(surf_index
);
1549 /* Assume this may touch any UBO. It would be nice to provide
1550 * a tighter bound, but the array information is already lowered away.
1552 brw_mark_surface_used(prog_data
,
1553 stage_prog_data
->binding_table
.ubo_start
+
1554 shader_prog
->NumUniformBlocks
- 1);
1558 /* Turn the byte offset into a dword offset. */
1559 fs_reg base_offset
= vgrf(glsl_type::int_type
);
1560 bld
.SHR(base_offset
, retype(get_nir_src(instr
->src
[1]),
1561 BRW_REGISTER_TYPE_D
),
1564 unsigned vec4_offset
= instr
->const_index
[0] / 4;
1565 for (int i
= 0; i
< instr
->num_components
; i
++)
1566 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
1567 base_offset
, vec4_offset
+ i
);
1569 fs_reg packed_consts
= vgrf(glsl_type::float_type
);
1570 packed_consts
.type
= dest
.type
;
1572 fs_reg
const_offset_reg((unsigned) instr
->const_index
[0] & ~15);
1573 bld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, packed_consts
,
1574 surf_index
, const_offset_reg
);
1576 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
1577 packed_consts
.set_smear(instr
->const_index
[0] % 16 / 4 + i
);
1579 /* The std140 packing rules don't allow vectors to cross 16-byte
1580 * boundaries, and a reg is 32 bytes.
1582 assert(packed_consts
.subreg_offset
< 32);
1584 bld
.MOV(dest
, packed_consts
);
1585 dest
= offset(dest
, bld
, 1);
1591 case nir_intrinsic_load_input_indirect
:
1592 has_indirect
= true;
1594 case nir_intrinsic_load_input
: {
1596 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1597 fs_reg src
= offset(retype(nir_inputs
, dest
.type
), bld
,
1598 instr
->const_index
[0] + index
);
1600 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[0]));
1604 dest
= offset(dest
, bld
, 1);
1609 /* Handle ARB_gpu_shader5 interpolation intrinsics
1611 * It's worth a quick word of explanation as to why we handle the full
1612 * variable-based interpolation intrinsic rather than a lowered version
1613 * with like we do for other inputs. We have to do that because the way
1614 * we set up inputs doesn't allow us to use the already setup inputs for
1615 * interpolation. At the beginning of the shader, we go through all of
1616 * the input variables and do the initial interpolation and put it in
1617 * the nir_inputs array based on its location as determined in
1618 * nir_lower_io. If the input isn't used, dead code cleans up and
1619 * everything works fine. However, when we get to the ARB_gpu_shader5
1620 * interpolation intrinsics, we need to reinterpolate the input
1621 * differently. If we used an intrinsic that just had an index it would
1622 * only give us the offset into the nir_inputs array. However, this is
1623 * useless because that value is post-interpolation and we need
1624 * pre-interpolation. In order to get the actual location of the bits
1625 * we get from the vertex fetching hardware, we need the variable.
1627 case nir_intrinsic_interp_var_at_centroid
:
1628 case nir_intrinsic_interp_var_at_sample
:
1629 case nir_intrinsic_interp_var_at_offset
: {
1630 assert(stage
== MESA_SHADER_FRAGMENT
);
1632 ((struct brw_wm_prog_data
*) prog_data
)->pulls_bary
= true;
1634 fs_reg dst_xy
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 2);
1636 /* For most messages, we need one reg of ignored data; the hardware
1637 * requires mlen==1 even when there is no payload. in the per-slot
1638 * offset case, we'll replace this with the proper source data.
1640 fs_reg src
= vgrf(glsl_type::float_type
);
1641 int mlen
= 1; /* one reg unless overriden */
1644 switch (instr
->intrinsic
) {
1645 case nir_intrinsic_interp_var_at_centroid
:
1646 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_CENTROID
,
1647 dst_xy
, src
, fs_reg(0u));
1650 case nir_intrinsic_interp_var_at_sample
: {
1651 /* XXX: We should probably handle non-constant sample id's */
1652 nir_const_value
*const_sample
= nir_src_as_const_value(instr
->src
[0]);
1653 assert(const_sample
);
1654 unsigned msg_data
= const_sample
? const_sample
->i
[0] << 4 : 0;
1655 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE
, dst_xy
, src
,
1660 case nir_intrinsic_interp_var_at_offset
: {
1661 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
1664 unsigned off_x
= MIN2((int)(const_offset
->f
[0] * 16), 7) & 0xf;
1665 unsigned off_y
= MIN2((int)(const_offset
->f
[1] * 16), 7) & 0xf;
1667 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
, dst_xy
, src
,
1668 fs_reg(off_x
| (off_y
<< 4)));
1670 src
= vgrf(glsl_type::ivec2_type
);
1671 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
1672 BRW_REGISTER_TYPE_F
);
1673 for (int i
= 0; i
< 2; i
++) {
1674 fs_reg temp
= vgrf(glsl_type::float_type
);
1675 bld
.MUL(temp
, offset(offset_src
, bld
, i
), fs_reg(16.0f
));
1676 fs_reg itemp
= vgrf(glsl_type::int_type
);
1677 bld
.MOV(itemp
, temp
); /* float to int */
1679 /* Clamp the upper end of the range to +7/16.
1680 * ARB_gpu_shader5 requires that we support a maximum offset
1681 * of +0.5, which isn't representable in a S0.4 value -- if
1682 * we didn't clamp it, we'd end up with -8/16, which is the
1683 * opposite of what the shader author wanted.
1685 * This is legal due to ARB_gpu_shader5's quantization
1688 * "Not all values of <offset> may be supported; x and y
1689 * offsets may be rounded to fixed-point values with the
1690 * number of fraction bits given by the
1691 * implementation-dependent constant
1692 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
1694 set_condmod(BRW_CONDITIONAL_L
,
1695 bld
.SEL(offset(src
, bld
, i
), itemp
, fs_reg(7)));
1698 mlen
= 2 * dispatch_width
/ 8;
1699 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
, dst_xy
, src
,
1706 unreachable("Invalid intrinsic");
1710 /* 2 floats per slot returned */
1711 inst
->regs_written
= 2 * dispatch_width
/ 8;
1712 inst
->pi_noperspective
= instr
->variables
[0]->var
->data
.interpolation
==
1713 INTERP_QUALIFIER_NOPERSPECTIVE
;
1715 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1716 fs_reg src
= interp_reg(instr
->variables
[0]->var
->data
.location
, j
);
1717 src
.type
= dest
.type
;
1719 bld
.emit(FS_OPCODE_LINTERP
, dest
, dst_xy
, src
);
1720 dest
= offset(dest
, bld
, 1);
1725 case nir_intrinsic_store_output_indirect
:
1726 has_indirect
= true;
1728 case nir_intrinsic_store_output
: {
1729 fs_reg src
= get_nir_src(instr
->src
[0]);
1731 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1732 fs_reg new_dest
= offset(retype(nir_outputs
, src
.type
), bld
,
1733 instr
->const_index
[0] + index
);
1735 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[1]));
1737 bld
.MOV(new_dest
, src
);
1738 src
= offset(src
, bld
, 1);
1743 case nir_intrinsic_barrier
:
1748 unreachable("unknown intrinsic");
1753 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
1755 unsigned sampler
= instr
->sampler_index
;
1756 fs_reg
sampler_reg(sampler
);
1758 /* FINISHME: We're failing to recompile our programs when the sampler is
1759 * updated. This only matters for the texture rectangle scale parameters
1760 * (pre-gen6, or gen6+ with GL_CLAMP).
1762 int texunit
= prog
->SamplerUnits
[sampler
];
1764 int gather_component
= instr
->component
;
1766 bool is_rect
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
1768 bool is_cube_array
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
1771 int lod_components
= 0;
1772 int UNUSED offset_components
= 0;
1774 fs_reg coordinate
, shadow_comparitor
, lod
, lod2
, sample_index
, mcs
, tex_offset
;
1776 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1777 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
1778 switch (instr
->src
[i
].src_type
) {
1779 case nir_tex_src_bias
:
1780 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1782 case nir_tex_src_comparitor
:
1783 shadow_comparitor
= retype(src
, BRW_REGISTER_TYPE_F
);
1785 case nir_tex_src_coord
:
1786 switch (instr
->op
) {
1788 case nir_texop_txf_ms
:
1789 coordinate
= retype(src
, BRW_REGISTER_TYPE_D
);
1792 coordinate
= retype(src
, BRW_REGISTER_TYPE_F
);
1796 case nir_tex_src_ddx
:
1797 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1798 lod_components
= nir_tex_instr_src_size(instr
, i
);
1800 case nir_tex_src_ddy
:
1801 lod2
= retype(src
, BRW_REGISTER_TYPE_F
);
1803 case nir_tex_src_lod
:
1804 switch (instr
->op
) {
1806 lod
= retype(src
, BRW_REGISTER_TYPE_UD
);
1809 lod
= retype(src
, BRW_REGISTER_TYPE_D
);
1812 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1816 case nir_tex_src_ms_index
:
1817 sample_index
= retype(src
, BRW_REGISTER_TYPE_UD
);
1819 case nir_tex_src_offset
:
1820 tex_offset
= retype(src
, BRW_REGISTER_TYPE_D
);
1821 if (instr
->is_array
)
1822 offset_components
= instr
->coord_components
- 1;
1824 offset_components
= instr
->coord_components
;
1826 case nir_tex_src_projector
:
1827 unreachable("should be lowered");
1829 case nir_tex_src_sampler_offset
: {
1830 /* Figure out the highest possible sampler index and mark it as used */
1831 uint32_t max_used
= sampler
+ instr
->sampler_array_size
- 1;
1832 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
1833 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
1835 max_used
+= stage_prog_data
->binding_table
.texture_start
;
1837 brw_mark_surface_used(prog_data
, max_used
);
1839 /* Emit code to evaluate the actual indexing expression */
1840 sampler_reg
= vgrf(glsl_type::uint_type
);
1841 bld
.ADD(sampler_reg
, src
, fs_reg(sampler
));
1842 sampler_reg
= bld
.emit_uniformize(sampler_reg
);
1847 unreachable("unknown texture source");
1851 if (instr
->op
== nir_texop_txf_ms
) {
1852 if (devinfo
->gen
>= 7 &&
1853 key_tex
->compressed_multisample_layout_mask
& (1 << sampler
)) {
1854 mcs
= emit_mcs_fetch(coordinate
, instr
->coord_components
, sampler_reg
);
1860 for (unsigned i
= 0; i
< 3; i
++) {
1861 if (instr
->const_offset
[i
] != 0) {
1862 assert(offset_components
== 0);
1863 tex_offset
= fs_reg(brw_texture_offset(instr
->const_offset
, 3));
1868 enum glsl_base_type dest_base_type
=
1869 brw_glsl_base_type_for_nir_type (instr
->dest_type
);
1871 const glsl_type
*dest_type
=
1872 glsl_type::get_instance(dest_base_type
, nir_tex_instr_dest_size(instr
),
1875 ir_texture_opcode op
;
1876 switch (instr
->op
) {
1877 case nir_texop_lod
: op
= ir_lod
; break;
1878 case nir_texop_query_levels
: op
= ir_query_levels
; break;
1879 case nir_texop_tex
: op
= ir_tex
; break;
1880 case nir_texop_tg4
: op
= ir_tg4
; break;
1881 case nir_texop_txb
: op
= ir_txb
; break;
1882 case nir_texop_txd
: op
= ir_txd
; break;
1883 case nir_texop_txf
: op
= ir_txf
; break;
1884 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
1885 case nir_texop_txl
: op
= ir_txl
; break;
1886 case nir_texop_txs
: op
= ir_txs
; break;
1888 unreachable("unknown texture opcode");
1891 emit_texture(op
, dest_type
, coordinate
, instr
->coord_components
,
1892 shadow_comparitor
, lod
, lod2
, lod_components
, sample_index
,
1893 tex_offset
, mcs
, gather_component
,
1894 is_cube_array
, is_rect
, sampler
, sampler_reg
, texunit
);
1896 fs_reg dest
= get_nir_dest(instr
->dest
);
1897 dest
.type
= this->result
.type
;
1898 unsigned num_components
= nir_tex_instr_dest_size(instr
);
1899 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
1900 dest
, this->result
),
1901 (1 << num_components
) - 1);
1905 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
1907 switch (instr
->type
) {
1908 case nir_jump_break
:
1909 bld
.emit(BRW_OPCODE_BREAK
);
1911 case nir_jump_continue
:
1912 bld
.emit(BRW_OPCODE_CONTINUE
);
1914 case nir_jump_return
:
1916 unreachable("unknown jump");