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 "program/prog_to_nir.h"
34 fs_visitor::emit_nir_code()
36 nir_shader
*nir
= prog
->nir
;
38 /* emit the arrays used for inputs and outputs - load/store intrinsics will
39 * be converted to reads/writes of these arrays
42 if (nir
->num_inputs
> 0) {
43 nir_inputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, nir
->num_inputs
);
44 nir_setup_inputs(nir
);
47 if (nir
->num_outputs
> 0) {
48 nir_outputs
= bld
.vgrf(BRW_REGISTER_TYPE_F
, nir
->num_outputs
);
49 nir_setup_outputs(nir
);
52 if (nir
->num_uniforms
> 0) {
53 nir_setup_uniforms(nir
);
56 nir_emit_system_values(nir
);
58 nir_globals
= ralloc_array(mem_ctx
, fs_reg
, nir
->reg_alloc
);
59 foreach_list_typed(nir_register
, reg
, node
, &nir
->registers
) {
60 unsigned array_elems
=
61 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
62 unsigned size
= array_elems
* reg
->num_components
;
63 nir_globals
[reg
->index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
66 /* get the main function and emit it */
67 nir_foreach_overload(nir
, overload
) {
68 assert(strcmp(overload
->function
->name
, "main") == 0);
69 assert(overload
->impl
);
70 nir_emit_impl(overload
->impl
);
75 fs_visitor::nir_setup_inputs(nir_shader
*shader
)
77 foreach_list_typed(nir_variable
, var
, node
, &shader
->inputs
) {
78 enum brw_reg_type type
= brw_type_for_base_type(var
->type
);
79 fs_reg input
= offset(nir_inputs
, bld
, var
->data
.driver_location
);
83 case MESA_SHADER_VERTEX
: {
84 /* Our ATTR file is indexed by VERT_ATTRIB_*, which is the value
85 * stored in nir_variable::location.
87 * However, NIR's load_input intrinsics use a different index - an
88 * offset into a single contiguous array containing all inputs.
89 * This index corresponds to the nir_variable::driver_location field.
91 * So, we need to copy from fs_reg(ATTR, var->location) to
92 * offset(nir_inputs, var->data.driver_location).
94 unsigned components
= var
->type
->without_array()->components();
95 unsigned array_length
= var
->type
->is_array() ? var
->type
->length
: 1;
96 for (unsigned i
= 0; i
< array_length
; i
++) {
97 for (unsigned j
= 0; j
< components
; j
++) {
98 bld
.MOV(retype(offset(input
, bld
, components
* i
+ j
), type
),
99 offset(fs_reg(ATTR
, var
->data
.location
+ i
, type
), bld
, j
));
104 case MESA_SHADER_GEOMETRY
:
105 case MESA_SHADER_COMPUTE
:
106 unreachable("fs_visitor not used for these stages yet.");
108 case MESA_SHADER_FRAGMENT
:
109 if (var
->data
.location
== VARYING_SLOT_POS
) {
110 reg
= *emit_fragcoord_interpolation(var
->data
.pixel_center_integer
,
111 var
->data
.origin_upper_left
);
112 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
115 emit_general_interpolation(input
, var
->name
, var
->type
,
116 (glsl_interp_qualifier
) var
->data
.interpolation
,
117 var
->data
.location
, var
->data
.centroid
,
126 fs_visitor::nir_setup_outputs(nir_shader
*shader
)
128 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
130 foreach_list_typed(nir_variable
, var
, node
, &shader
->outputs
) {
131 fs_reg reg
= offset(nir_outputs
, bld
, var
->data
.driver_location
);
133 int vector_elements
=
134 var
->type
->is_array() ? var
->type
->fields
.array
->vector_elements
135 : var
->type
->vector_elements
;
137 if (stage
== MESA_SHADER_VERTEX
) {
138 for (int i
= 0; i
< ALIGN(type_size(var
->type
), 4) / 4; i
++) {
139 int output
= var
->data
.location
+ i
;
140 this->outputs
[output
] = offset(reg
, bld
, 4 * i
);
141 this->output_components
[output
] = vector_elements
;
143 } else if (var
->data
.index
> 0) {
144 assert(var
->data
.location
== FRAG_RESULT_DATA0
);
145 assert(var
->data
.index
== 1);
146 this->dual_src_output
= reg
;
147 this->do_dual_src
= true;
148 } else if (var
->data
.location
== FRAG_RESULT_COLOR
) {
149 /* Writing gl_FragColor outputs to all color regions. */
150 for (unsigned int i
= 0; i
< MAX2(key
->nr_color_regions
, 1); i
++) {
151 this->outputs
[i
] = reg
;
152 this->output_components
[i
] = 4;
154 } else if (var
->data
.location
== FRAG_RESULT_DEPTH
) {
155 this->frag_depth
= reg
;
156 } else if (var
->data
.location
== FRAG_RESULT_SAMPLE_MASK
) {
157 this->sample_mask
= reg
;
159 /* gl_FragData or a user-defined FS output */
160 assert(var
->data
.location
>= FRAG_RESULT_DATA0
&&
161 var
->data
.location
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
);
163 /* General color output. */
164 for (unsigned int i
= 0; i
< MAX2(1, var
->type
->length
); i
++) {
165 int output
= var
->data
.location
- FRAG_RESULT_DATA0
+ i
;
166 this->outputs
[output
] = offset(reg
, bld
, vector_elements
* i
);
167 this->output_components
[output
] = vector_elements
;
174 fs_visitor::nir_setup_uniforms(nir_shader
*shader
)
176 uniforms
= shader
->num_uniforms
;
177 num_direct_uniforms
= shader
->num_direct_uniforms
;
179 /* We split the uniform register file in half. The first half is
180 * entirely direct uniforms. The second half is indirect.
182 param_size
[0] = num_direct_uniforms
;
183 if (shader
->num_uniforms
> num_direct_uniforms
)
184 param_size
[num_direct_uniforms
] = shader
->num_uniforms
- num_direct_uniforms
;
186 if (dispatch_width
!= 8)
190 foreach_list_typed(nir_variable
, var
, node
, &shader
->uniforms
) {
191 /* UBO's and atomics don't take up space in the uniform file */
192 if (var
->interface_type
!= NULL
|| var
->type
->contains_atomic())
195 if (strncmp(var
->name
, "gl_", 3) == 0)
196 nir_setup_builtin_uniform(var
);
198 nir_setup_uniform(var
);
201 /* prog_to_nir doesn't create uniform variables; set param up directly. */
202 for (unsigned p
= 0; p
< prog
->Parameters
->NumParameters
; p
++) {
203 for (unsigned int i
= 0; i
< 4; i
++) {
204 stage_prog_data
->param
[4 * p
+ i
] =
205 &prog
->Parameters
->ParameterValues
[p
][i
];
212 fs_visitor::nir_setup_uniform(nir_variable
*var
)
214 int namelen
= strlen(var
->name
);
216 /* The data for our (non-builtin) uniforms is stored in a series of
217 * gl_uniform_driver_storage structs for each subcomponent that
218 * glGetUniformLocation() could name. We know it's been set up in the
219 * same order we'd walk the type, so walk the list of storage and find
220 * anything with our name, or the prefix of a component that starts with
223 unsigned index
= var
->data
.driver_location
;
224 for (unsigned u
= 0; u
< shader_prog
->NumUniformStorage
; u
++) {
225 struct gl_uniform_storage
*storage
= &shader_prog
->UniformStorage
[u
];
227 if (storage
->builtin
)
230 if (strncmp(var
->name
, storage
->name
, namelen
) != 0 ||
231 (storage
->name
[namelen
] != 0 &&
232 storage
->name
[namelen
] != '.' &&
233 storage
->name
[namelen
] != '[')) {
237 unsigned slots
= storage
->type
->component_slots();
238 if (storage
->array_elements
)
239 slots
*= storage
->array_elements
;
241 for (unsigned i
= 0; i
< slots
; i
++) {
242 stage_prog_data
->param
[index
++] = &storage
->storage
[i
];
246 /* Make sure we actually initialized the right amount of stuff here. */
247 assert(var
->data
.driver_location
+ var
->type
->component_slots() == index
);
251 fs_visitor::nir_setup_builtin_uniform(nir_variable
*var
)
253 const nir_state_slot
*const slots
= var
->state_slots
;
254 assert(var
->state_slots
!= NULL
);
256 unsigned uniform_index
= var
->data
.driver_location
;
257 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
258 /* This state reference has already been setup by ir_to_mesa, but we'll
259 * get the same index back here.
261 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
262 (gl_state_index
*)slots
[i
].tokens
);
264 /* Add each of the unique swizzles of the element as a parameter.
265 * This'll end up matching the expected layout of the
266 * array/matrix/structure we're trying to fill in.
269 for (unsigned int j
= 0; j
< 4; j
++) {
270 int swiz
= GET_SWZ(slots
[i
].swizzle
, j
);
271 if (swiz
== last_swiz
)
275 stage_prog_data
->param
[uniform_index
++] =
276 &prog
->Parameters
->ParameterValues
[index
][swiz
];
282 emit_system_values_block(nir_block
*block
, void *void_visitor
)
284 fs_visitor
*v
= (fs_visitor
*)void_visitor
;
287 nir_foreach_instr(block
, instr
) {
288 if (instr
->type
!= nir_instr_type_intrinsic
)
291 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
292 switch (intrin
->intrinsic
) {
293 case nir_intrinsic_load_vertex_id
:
294 unreachable("should be lowered by lower_vertex_id().");
296 case nir_intrinsic_load_vertex_id_zero_base
:
297 assert(v
->stage
== MESA_SHADER_VERTEX
);
298 reg
= &v
->nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
299 if (reg
->file
== BAD_FILE
)
300 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
);
303 case nir_intrinsic_load_base_vertex
:
304 assert(v
->stage
== MESA_SHADER_VERTEX
);
305 reg
= &v
->nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
306 if (reg
->file
== BAD_FILE
)
307 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX
);
310 case nir_intrinsic_load_instance_id
:
311 assert(v
->stage
== MESA_SHADER_VERTEX
);
312 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
313 if (reg
->file
== BAD_FILE
)
314 *reg
= *v
->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID
);
317 case nir_intrinsic_load_sample_pos
:
318 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
319 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
320 if (reg
->file
== BAD_FILE
)
321 *reg
= *v
->emit_samplepos_setup();
324 case nir_intrinsic_load_sample_id
:
325 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
326 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
327 if (reg
->file
== BAD_FILE
)
328 *reg
= *v
->emit_sampleid_setup();
331 case nir_intrinsic_load_sample_mask_in
:
332 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
333 assert(v
->devinfo
->gen
>= 7);
334 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
335 if (reg
->file
== BAD_FILE
)
336 *reg
= fs_reg(retype(brw_vec8_grf(v
->payload
.sample_mask_in_reg
, 0),
337 BRW_REGISTER_TYPE_D
));
349 fs_visitor::nir_emit_system_values(nir_shader
*shader
)
351 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
352 nir_foreach_overload(shader
, overload
) {
353 assert(strcmp(overload
->function
->name
, "main") == 0);
354 assert(overload
->impl
);
355 nir_foreach_block(overload
->impl
, emit_system_values_block
, this);
360 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
362 nir_locals
= reralloc(mem_ctx
, nir_locals
, fs_reg
, impl
->reg_alloc
);
363 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
364 unsigned array_elems
=
365 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
366 unsigned size
= array_elems
* reg
->num_components
;
367 nir_locals
[reg
->index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
370 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
373 nir_emit_cf_list(&impl
->body
);
377 fs_visitor::nir_emit_cf_list(exec_list
*list
)
379 exec_list_validate(list
);
380 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
381 switch (node
->type
) {
383 nir_emit_if(nir_cf_node_as_if(node
));
386 case nir_cf_node_loop
:
387 nir_emit_loop(nir_cf_node_as_loop(node
));
390 case nir_cf_node_block
:
391 nir_emit_block(nir_cf_node_as_block(node
));
395 unreachable("Invalid CFG node block");
401 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
403 /* first, put the condition into f0 */
404 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
405 retype(get_nir_src(if_stmt
->condition
),
406 BRW_REGISTER_TYPE_D
));
407 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
409 bld
.IF(BRW_PREDICATE_NORMAL
);
411 nir_emit_cf_list(&if_stmt
->then_list
);
413 /* note: if the else is empty, dead CF elimination will remove it */
414 bld
.emit(BRW_OPCODE_ELSE
);
416 nir_emit_cf_list(&if_stmt
->else_list
);
418 bld
.emit(BRW_OPCODE_ENDIF
);
420 if (!try_replace_with_sel() && devinfo
->gen
< 6) {
421 no16("Can't support (non-uniform) control flow on SIMD16\n");
426 fs_visitor::nir_emit_loop(nir_loop
*loop
)
428 if (devinfo
->gen
< 6) {
429 no16("Can't support (non-uniform) control flow on SIMD16\n");
432 bld
.emit(BRW_OPCODE_DO
);
434 nir_emit_cf_list(&loop
->body
);
436 bld
.emit(BRW_OPCODE_WHILE
);
440 fs_visitor::nir_emit_block(nir_block
*block
)
442 nir_foreach_instr(block
, instr
) {
443 nir_emit_instr(instr
);
448 fs_visitor::nir_emit_instr(nir_instr
*instr
)
450 const fs_builder abld
= bld
.annotate(NULL
, instr
);
452 switch (instr
->type
) {
453 case nir_instr_type_alu
:
454 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
457 case nir_instr_type_intrinsic
:
458 nir_emit_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
461 case nir_instr_type_tex
:
462 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
465 case nir_instr_type_load_const
:
466 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
469 case nir_instr_type_ssa_undef
:
470 nir_emit_undef(abld
, nir_instr_as_ssa_undef(instr
));
473 case nir_instr_type_jump
:
474 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
478 unreachable("unknown instruction type");
483 brw_type_for_nir_type(nir_alu_type type
)
486 case nir_type_unsigned
:
487 return BRW_REGISTER_TYPE_UD
;
490 return BRW_REGISTER_TYPE_D
;
492 return BRW_REGISTER_TYPE_F
;
494 unreachable("unknown type");
497 return BRW_REGISTER_TYPE_F
;
501 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
502 const fs_reg
&result
)
504 if (!instr
->src
[0].src
.is_ssa
||
505 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
508 nir_intrinsic_instr
*src0
=
509 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
511 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
514 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
515 if (!value1
|| fabsf(value1
->f
[0]) != 1.0f
)
518 nir_const_value
*value2
= nir_src_as_const_value(instr
->src
[2].src
);
519 if (!value2
|| fabsf(value2
->f
[0]) != 1.0f
)
522 fs_reg tmp
= vgrf(glsl_type::int_type
);
524 if (devinfo
->gen
>= 6) {
525 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
526 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
528 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
530 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
531 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
533 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
535 * This negation looks like it's safe in practice, because bits 0:4 will
536 * surely be TRIANGLES
539 if (value1
->f
[0] == -1.0f
) {
543 tmp
.type
= BRW_REGISTER_TYPE_W
;
544 tmp
.subreg_offset
= 2;
547 fs_inst
*or_inst
= bld
.OR(tmp
, g0
, fs_reg(0x3f80));
548 or_inst
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
550 tmp
.type
= BRW_REGISTER_TYPE_D
;
551 tmp
.subreg_offset
= 0;
554 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
555 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
557 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
559 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
560 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
562 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
564 * This negation looks like it's safe in practice, because bits 0:4 will
565 * surely be TRIANGLES
568 if (value1
->f
[0] == -1.0f
) {
572 bld
.OR(tmp
, g1_6
, fs_reg(0x3f800000));
574 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, fs_reg(0xbf800000));
580 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
582 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
585 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
586 result
.type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].output_type
);
589 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
590 op
[i
] = get_nir_src(instr
->src
[i
].src
);
591 op
[i
].type
= brw_type_for_nir_type(nir_op_infos
[instr
->op
].input_types
[i
]);
592 op
[i
].abs
= instr
->src
[i
].abs
;
593 op
[i
].negate
= instr
->src
[i
].negate
;
596 /* We get a bunch of mov's out of the from_ssa pass and they may still
597 * be vectorized. We'll handle them as a special-case. We'll also
598 * handle vecN here because it's basically the same thing.
606 fs_reg temp
= result
;
607 bool need_extra_copy
= false;
608 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
609 if (!instr
->src
[i
].src
.is_ssa
&&
610 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
611 need_extra_copy
= true;
612 temp
= bld
.vgrf(result
.type
, 4);
617 for (unsigned i
= 0; i
< 4; i
++) {
618 if (!(instr
->dest
.write_mask
& (1 << i
)))
621 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
622 inst
= bld
.MOV(offset(temp
, bld
, i
),
623 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
625 inst
= bld
.MOV(offset(temp
, bld
, i
),
626 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
628 inst
->saturate
= instr
->dest
.saturate
;
631 /* In this case the source and destination registers were the same,
632 * so we need to insert an extra set of moves in order to deal with
635 if (need_extra_copy
) {
636 for (unsigned i
= 0; i
< 4; i
++) {
637 if (!(instr
->dest
.write_mask
& (1 << i
)))
640 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
649 /* At this point, we have dealt with any instruction that operates on
650 * more than a single channel. Therefore, we can just adjust the source
651 * and destination registers for that channel and emit the instruction.
653 unsigned channel
= 0;
654 if (nir_op_infos
[instr
->op
].output_size
== 0) {
655 /* Since NIR is doing the scalarizing for us, we should only ever see
656 * vectorized operations with a single channel.
658 assert(_mesa_bitcount(instr
->dest
.write_mask
) == 1);
659 channel
= ffs(instr
->dest
.write_mask
) - 1;
661 result
= offset(result
, bld
, channel
);
664 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
665 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
666 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
672 inst
= bld
.MOV(result
, op
[0]);
673 inst
->saturate
= instr
->dest
.saturate
;
678 bld
.MOV(result
, op
[0]);
682 /* AND(val, 0x80000000) gives the sign bit.
684 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
687 bld
.CMP(bld
.null_reg_f(), op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
);
689 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
690 op
[0].type
= BRW_REGISTER_TYPE_UD
;
691 result
.type
= BRW_REGISTER_TYPE_UD
;
692 bld
.AND(result_int
, op
[0], fs_reg(0x80000000u
));
694 inst
= bld
.OR(result_int
, result_int
, fs_reg(0x3f800000u
));
695 inst
->predicate
= BRW_PREDICATE_NORMAL
;
696 if (instr
->dest
.saturate
) {
697 inst
= bld
.MOV(result
, result
);
698 inst
->saturate
= true;
704 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
705 * -> non-negative val generates 0x00000000.
706 * Predicated OR sets 1 if val is positive.
708 bld
.CMP(bld
.null_reg_d(), op
[0], fs_reg(0), BRW_CONDITIONAL_G
);
709 bld
.ASR(result
, op
[0], fs_reg(31));
710 inst
= bld
.OR(result
, result
, fs_reg(1));
711 inst
->predicate
= BRW_PREDICATE_NORMAL
;
715 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
716 inst
->saturate
= instr
->dest
.saturate
;
720 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
721 inst
->saturate
= instr
->dest
.saturate
;
725 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
726 inst
->saturate
= instr
->dest
.saturate
;
730 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
731 inst
->saturate
= instr
->dest
.saturate
;
735 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
736 inst
->saturate
= instr
->dest
.saturate
;
740 if (fs_key
->high_quality_derivatives
) {
741 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
743 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
745 inst
->saturate
= instr
->dest
.saturate
;
747 case nir_op_fddx_fine
:
748 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
749 inst
->saturate
= instr
->dest
.saturate
;
751 case nir_op_fddx_coarse
:
752 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
753 inst
->saturate
= instr
->dest
.saturate
;
756 if (fs_key
->high_quality_derivatives
) {
757 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
758 fs_reg(fs_key
->render_to_fbo
));
760 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
761 fs_reg(fs_key
->render_to_fbo
));
763 inst
->saturate
= instr
->dest
.saturate
;
765 case nir_op_fddy_fine
:
766 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0],
767 fs_reg(fs_key
->render_to_fbo
));
768 inst
->saturate
= instr
->dest
.saturate
;
770 case nir_op_fddy_coarse
:
771 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0],
772 fs_reg(fs_key
->render_to_fbo
));
773 inst
->saturate
= instr
->dest
.saturate
;
778 inst
= bld
.ADD(result
, op
[0], op
[1]);
779 inst
->saturate
= instr
->dest
.saturate
;
783 inst
= bld
.MUL(result
, op
[0], op
[1]);
784 inst
->saturate
= instr
->dest
.saturate
;
788 bld
.MUL(result
, op
[0], op
[1]);
791 case nir_op_imul_high
:
792 case nir_op_umul_high
: {
793 if (devinfo
->gen
>= 7)
794 no16("SIMD16 explicit accumulator operands unsupported\n");
796 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
), result
.type
);
798 fs_inst
*mul
= bld
.MUL(acc
, op
[0], op
[1]);
799 bld
.MACH(result
, op
[0], op
[1]);
801 /* Until Gen8, integer multiplies read 32-bits from one source, and
802 * 16-bits from the other, and relying on the MACH instruction to
803 * generate the high bits of the result.
805 * On Gen8, the multiply instruction does a full 32x32-bit multiply,
806 * but in order to do a 64x64-bit multiply we have to simulate the
807 * previous behavior and then use a MACH instruction.
809 * FINISHME: Don't use source modifiers on src1.
811 if (devinfo
->gen
>= 8) {
812 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
813 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
814 if (mul
->src
[1].type
== BRW_REGISTER_TYPE_D
) {
815 mul
->src
[1].type
= BRW_REGISTER_TYPE_W
;
816 mul
->src
[1].stride
= 2;
818 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
819 mul
->src
[1].stride
= 2;
827 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
830 case nir_op_uadd_carry
: {
831 if (devinfo
->gen
>= 7)
832 no16("SIMD16 explicit accumulator operands unsupported\n");
834 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
835 BRW_REGISTER_TYPE_UD
);
837 bld
.ADDC(bld
.null_reg_ud(), op
[0], op
[1]);
838 bld
.MOV(result
, fs_reg(acc
));
842 case nir_op_usub_borrow
: {
843 if (devinfo
->gen
>= 7)
844 no16("SIMD16 explicit accumulator operands unsupported\n");
846 struct brw_reg acc
= retype(brw_acc_reg(dispatch_width
),
847 BRW_REGISTER_TYPE_UD
);
849 bld
.SUBB(bld
.null_reg_ud(), op
[0], op
[1]);
850 bld
.MOV(result
, fs_reg(acc
));
855 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
861 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
867 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
872 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_Z
);
877 bld
.CMP(result
, op
[0], op
[1], BRW_CONDITIONAL_NZ
);
881 if (devinfo
->gen
>= 8) {
882 resolve_source_modifiers(&op
[0]);
884 bld
.NOT(result
, op
[0]);
887 if (devinfo
->gen
>= 8) {
888 resolve_source_modifiers(&op
[0]);
889 resolve_source_modifiers(&op
[1]);
891 bld
.XOR(result
, op
[0], op
[1]);
894 if (devinfo
->gen
>= 8) {
895 resolve_source_modifiers(&op
[0]);
896 resolve_source_modifiers(&op
[1]);
898 bld
.OR(result
, op
[0], op
[1]);
901 if (devinfo
->gen
>= 8) {
902 resolve_source_modifiers(&op
[0]);
903 resolve_source_modifiers(&op
[1]);
905 bld
.AND(result
, op
[0], op
[1]);
917 case nir_op_ball_fequal2
:
918 case nir_op_ball_iequal2
:
919 case nir_op_ball_fequal3
:
920 case nir_op_ball_iequal3
:
921 case nir_op_ball_fequal4
:
922 case nir_op_ball_iequal4
:
923 case nir_op_bany_fnequal2
:
924 case nir_op_bany_inequal2
:
925 case nir_op_bany_fnequal3
:
926 case nir_op_bany_inequal3
:
927 case nir_op_bany_fnequal4
:
928 case nir_op_bany_inequal4
:
929 unreachable("Lowered by nir_lower_alu_reductions");
931 case nir_op_fnoise1_1
:
932 case nir_op_fnoise1_2
:
933 case nir_op_fnoise1_3
:
934 case nir_op_fnoise1_4
:
935 case nir_op_fnoise2_1
:
936 case nir_op_fnoise2_2
:
937 case nir_op_fnoise2_3
:
938 case nir_op_fnoise2_4
:
939 case nir_op_fnoise3_1
:
940 case nir_op_fnoise3_2
:
941 case nir_op_fnoise3_3
:
942 case nir_op_fnoise3_4
:
943 case nir_op_fnoise4_1
:
944 case nir_op_fnoise4_2
:
945 case nir_op_fnoise4_3
:
946 case nir_op_fnoise4_4
:
947 unreachable("not reached: should be handled by lower_noise");
950 unreachable("not reached: should be handled by ldexp_to_arith()");
953 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
954 inst
->saturate
= instr
->dest
.saturate
;
958 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
959 inst
->saturate
= instr
->dest
.saturate
;
963 bld
.AND(result
, op
[0], fs_reg(1));
966 bld
.AND(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0], fs_reg(0x3f800000u
));
970 bld
.CMP(result
, op
[0], fs_reg(0.0f
), BRW_CONDITIONAL_NZ
);
973 bld
.CMP(result
, op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
);
977 inst
= bld
.RNDZ(result
, op
[0]);
978 inst
->saturate
= instr
->dest
.saturate
;
982 op
[0].negate
= !op
[0].negate
;
983 fs_reg temp
= vgrf(glsl_type::float_type
);
984 bld
.RNDD(temp
, op
[0]);
986 inst
= bld
.MOV(result
, temp
);
987 inst
->saturate
= instr
->dest
.saturate
;
991 inst
= bld
.RNDD(result
, op
[0]);
992 inst
->saturate
= instr
->dest
.saturate
;
995 inst
= bld
.FRC(result
, op
[0]);
996 inst
->saturate
= instr
->dest
.saturate
;
998 case nir_op_fround_even
:
999 inst
= bld
.RNDE(result
, op
[0]);
1000 inst
->saturate
= instr
->dest
.saturate
;
1006 if (devinfo
->gen
>= 6) {
1007 inst
= bld
.emit(BRW_OPCODE_SEL
, result
, op
[0], op
[1]);
1008 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1010 bld
.CMP(bld
.null_reg_d(), op
[0], op
[1], BRW_CONDITIONAL_L
);
1011 inst
= bld
.SEL(result
, op
[0], op
[1]);
1012 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1014 inst
->saturate
= instr
->dest
.saturate
;
1020 if (devinfo
->gen
>= 6) {
1021 inst
= bld
.emit(BRW_OPCODE_SEL
, result
, op
[0], op
[1]);
1022 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
1024 bld
.CMP(bld
.null_reg_d(), op
[0], op
[1], BRW_CONDITIONAL_GE
);
1025 inst
= bld
.SEL(result
, op
[0], op
[1]);
1026 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1028 inst
->saturate
= instr
->dest
.saturate
;
1031 case nir_op_pack_snorm_2x16
:
1032 case nir_op_pack_snorm_4x8
:
1033 case nir_op_pack_unorm_2x16
:
1034 case nir_op_pack_unorm_4x8
:
1035 case nir_op_unpack_snorm_2x16
:
1036 case nir_op_unpack_snorm_4x8
:
1037 case nir_op_unpack_unorm_2x16
:
1038 case nir_op_unpack_unorm_4x8
:
1039 case nir_op_unpack_half_2x16
:
1040 case nir_op_pack_half_2x16
:
1041 unreachable("not reached: should be handled by lower_packing_builtins");
1043 case nir_op_unpack_half_2x16_split_x
:
1044 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
1045 inst
->saturate
= instr
->dest
.saturate
;
1047 case nir_op_unpack_half_2x16_split_y
:
1048 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
1049 inst
->saturate
= instr
->dest
.saturate
;
1053 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1054 inst
->saturate
= instr
->dest
.saturate
;
1057 case nir_op_bitfield_reverse
:
1058 bld
.BFREV(result
, op
[0]);
1061 case nir_op_bit_count
:
1062 bld
.CBIT(result
, op
[0]);
1065 case nir_op_ufind_msb
:
1066 case nir_op_ifind_msb
: {
1067 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1069 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1070 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1071 * subtract the result from 31 to convert the MSB count into an LSB count.
1074 bld
.CMP(bld
.null_reg_d(), result
, fs_reg(-1), BRW_CONDITIONAL_NZ
);
1075 fs_reg
neg_result(result
);
1076 neg_result
.negate
= true;
1077 inst
= bld
.ADD(result
, neg_result
, fs_reg(31));
1078 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1082 case nir_op_find_lsb
:
1083 bld
.FBL(result
, op
[0]);
1086 case nir_op_ubitfield_extract
:
1087 case nir_op_ibitfield_extract
:
1088 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1091 bld
.BFI1(result
, op
[0], op
[1]);
1094 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1097 case nir_op_bitfield_insert
:
1098 unreachable("not reached: should be handled by "
1099 "lower_instructions::bitfield_insert_to_bfm_bfi");
1102 bld
.SHL(result
, op
[0], op
[1]);
1105 bld
.ASR(result
, op
[0], op
[1]);
1108 bld
.SHR(result
, op
[0], op
[1]);
1111 case nir_op_pack_half_2x16_split
:
1112 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1116 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1117 inst
->saturate
= instr
->dest
.saturate
;
1121 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1122 inst
->saturate
= instr
->dest
.saturate
;
1126 if (optimize_frontfacing_ternary(instr
, result
))
1129 bld
.CMP(bld
.null_reg_d(), op
[0], fs_reg(0), BRW_CONDITIONAL_NZ
);
1130 inst
= bld
.SEL(result
, op
[1], op
[2]);
1131 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1135 unreachable("unhandled instruction");
1138 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1139 * to sign extend the low bit to 0/~0
1141 if (devinfo
->gen
<= 5 &&
1142 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1143 fs_reg masked
= vgrf(glsl_type::int_type
);
1144 bld
.AND(masked
, result
, fs_reg(1));
1145 masked
.negate
= true;
1146 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1151 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1152 nir_load_const_instr
*instr
)
1154 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_D
, instr
->def
.num_components
);
1156 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1157 bld
.MOV(offset(reg
, bld
, i
), fs_reg(instr
->value
.i
[i
]));
1159 nir_ssa_values
[instr
->def
.index
] = reg
;
1163 fs_visitor::nir_emit_undef(const fs_builder
&bld
, nir_ssa_undef_instr
*instr
)
1165 nir_ssa_values
[instr
->def
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_D
,
1166 instr
->def
.num_components
);
1170 fs_reg_for_nir_reg(fs_visitor
*v
, nir_register
*nir_reg
,
1171 unsigned base_offset
, nir_src
*indirect
)
1174 if (nir_reg
->is_global
)
1175 reg
= v
->nir_globals
[nir_reg
->index
];
1177 reg
= v
->nir_locals
[nir_reg
->index
];
1179 reg
= offset(reg
, v
->bld
, base_offset
* nir_reg
->num_components
);
1181 int multiplier
= nir_reg
->num_components
* (v
->dispatch_width
/ 8);
1183 reg
.reladdr
= new(v
->mem_ctx
) fs_reg(v
->vgrf(glsl_type::int_type
));
1184 v
->bld
.MUL(*reg
.reladdr
, v
->get_nir_src(*indirect
),
1185 fs_reg(multiplier
));
1192 fs_visitor::get_nir_src(nir_src src
)
1196 reg
= nir_ssa_values
[src
.ssa
->index
];
1198 reg
= fs_reg_for_nir_reg(this, src
.reg
.reg
, src
.reg
.base_offset
,
1202 /* to avoid floating-point denorm flushing problems, set the type by
1203 * default to D - instructions that need floating point semantics will set
1204 * this to F if they need to
1206 return retype(reg
, BRW_REGISTER_TYPE_D
);
1210 fs_visitor::get_nir_dest(nir_dest dest
)
1213 nir_ssa_values
[dest
.ssa
.index
] = bld
.vgrf(BRW_REGISTER_TYPE_F
,
1214 dest
.ssa
.num_components
);
1215 return nir_ssa_values
[dest
.ssa
.index
];
1218 return fs_reg_for_nir_reg(this, dest
.reg
.reg
, dest
.reg
.base_offset
,
1223 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1226 for (unsigned i
= 0; i
< 4; i
++) {
1227 if (!((wr_mask
>> i
) & 1))
1230 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1231 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1232 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1233 if (new_inst
->src
[j
].file
== GRF
)
1234 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1241 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
1244 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1245 dest
= get_nir_dest(instr
->dest
);
1247 bool has_indirect
= false;
1249 switch (instr
->intrinsic
) {
1250 case nir_intrinsic_discard
:
1251 case nir_intrinsic_discard_if
: {
1252 /* We track our discarded pixels in f0.1. By predicating on it, we can
1253 * update just the flag bits that aren't yet discarded. If there's no
1254 * condition, we emit a CMP of g0 != g0, so all currently executing
1255 * channels will get turned off.
1258 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
1259 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
1260 fs_reg(0), BRW_CONDITIONAL_Z
);
1262 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
1263 BRW_REGISTER_TYPE_UW
));
1264 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
1266 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
1267 cmp
->flag_subreg
= 1;
1269 if (devinfo
->gen
>= 6) {
1270 emit_discard_jump();
1275 case nir_intrinsic_atomic_counter_inc
:
1276 case nir_intrinsic_atomic_counter_dec
:
1277 case nir_intrinsic_atomic_counter_read
: {
1278 unsigned surf_index
= prog_data
->binding_table
.abo_start
+
1279 (unsigned) instr
->const_index
[0];
1280 fs_reg offset
= fs_reg(get_nir_src(instr
->src
[0]));
1282 switch (instr
->intrinsic
) {
1283 case nir_intrinsic_atomic_counter_inc
:
1284 emit_untyped_atomic(BRW_AOP_INC
, surf_index
, dest
, offset
,
1285 fs_reg(), fs_reg());
1287 case nir_intrinsic_atomic_counter_dec
:
1288 emit_untyped_atomic(BRW_AOP_PREDEC
, surf_index
, dest
, offset
,
1289 fs_reg(), fs_reg());
1291 case nir_intrinsic_atomic_counter_read
:
1292 emit_untyped_surface_read(surf_index
, dest
, offset
);
1295 unreachable("Unreachable");
1300 case nir_intrinsic_load_front_face
:
1301 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
1302 *emit_frontfacing_interpolation());
1305 case nir_intrinsic_load_vertex_id
:
1306 unreachable("should be lowered by lower_vertex_id()");
1308 case nir_intrinsic_load_vertex_id_zero_base
: {
1309 fs_reg vertex_id
= nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
1310 assert(vertex_id
.file
!= BAD_FILE
);
1311 dest
.type
= vertex_id
.type
;
1312 bld
.MOV(dest
, vertex_id
);
1316 case nir_intrinsic_load_base_vertex
: {
1317 fs_reg base_vertex
= nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
1318 assert(base_vertex
.file
!= BAD_FILE
);
1319 dest
.type
= base_vertex
.type
;
1320 bld
.MOV(dest
, base_vertex
);
1324 case nir_intrinsic_load_instance_id
: {
1325 fs_reg instance_id
= nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
1326 assert(instance_id
.file
!= BAD_FILE
);
1327 dest
.type
= instance_id
.type
;
1328 bld
.MOV(dest
, instance_id
);
1332 case nir_intrinsic_load_sample_mask_in
: {
1333 fs_reg sample_mask_in
= nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
1334 assert(sample_mask_in
.file
!= BAD_FILE
);
1335 dest
.type
= sample_mask_in
.type
;
1336 bld
.MOV(dest
, sample_mask_in
);
1340 case nir_intrinsic_load_sample_pos
: {
1341 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
1342 assert(sample_pos
.file
!= BAD_FILE
);
1343 dest
.type
= sample_pos
.type
;
1344 bld
.MOV(dest
, sample_pos
);
1345 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
1349 case nir_intrinsic_load_sample_id
: {
1350 fs_reg sample_id
= nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
1351 assert(sample_id
.file
!= BAD_FILE
);
1352 dest
.type
= sample_id
.type
;
1353 bld
.MOV(dest
, sample_id
);
1357 case nir_intrinsic_load_uniform_indirect
:
1358 has_indirect
= true;
1360 case nir_intrinsic_load_uniform
: {
1361 unsigned index
= instr
->const_index
[0];
1364 if (index
< num_direct_uniforms
) {
1365 uniform_reg
= fs_reg(UNIFORM
, 0);
1367 uniform_reg
= fs_reg(UNIFORM
, num_direct_uniforms
);
1368 index
-= num_direct_uniforms
;
1371 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1372 fs_reg src
= offset(retype(uniform_reg
, dest
.type
), bld
, index
);
1374 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[0]));
1378 dest
= offset(dest
, bld
, 1);
1383 case nir_intrinsic_load_ubo_indirect
:
1384 has_indirect
= true;
1386 case nir_intrinsic_load_ubo
: {
1387 nir_const_value
*const_index
= nir_src_as_const_value(instr
->src
[0]);
1391 surf_index
= fs_reg(stage_prog_data
->binding_table
.ubo_start
+
1394 /* The block index is not a constant. Evaluate the index expression
1395 * per-channel and add the base UBO index; we have to select a value
1396 * from any live channel.
1398 surf_index
= vgrf(glsl_type::uint_type
);
1399 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
1400 fs_reg(stage_prog_data
->binding_table
.ubo_start
));
1401 bld
.emit_uniformize(surf_index
, surf_index
);
1403 /* Assume this may touch any UBO. It would be nice to provide
1404 * a tighter bound, but the array information is already lowered away.
1406 brw_mark_surface_used(prog_data
,
1407 stage_prog_data
->binding_table
.ubo_start
+
1408 shader_prog
->NumUniformBlocks
- 1);
1412 /* Turn the byte offset into a dword offset. */
1413 fs_reg base_offset
= vgrf(glsl_type::int_type
);
1414 bld
.SHR(base_offset
, retype(get_nir_src(instr
->src
[1]),
1415 BRW_REGISTER_TYPE_D
),
1418 unsigned vec4_offset
= instr
->const_index
[0] / 4;
1419 for (int i
= 0; i
< instr
->num_components
; i
++)
1420 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
1421 base_offset
, vec4_offset
+ i
);
1423 fs_reg packed_consts
= vgrf(glsl_type::float_type
);
1424 packed_consts
.type
= dest
.type
;
1426 fs_reg
const_offset_reg((unsigned) instr
->const_index
[0] & ~15);
1427 bld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, packed_consts
,
1428 surf_index
, const_offset_reg
);
1430 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
1431 packed_consts
.set_smear(instr
->const_index
[0] % 16 / 4 + i
);
1433 /* The std140 packing rules don't allow vectors to cross 16-byte
1434 * boundaries, and a reg is 32 bytes.
1436 assert(packed_consts
.subreg_offset
< 32);
1438 bld
.MOV(dest
, packed_consts
);
1439 dest
= offset(dest
, bld
, 1);
1445 case nir_intrinsic_load_input_indirect
:
1446 has_indirect
= true;
1448 case nir_intrinsic_load_input
: {
1450 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1451 fs_reg src
= offset(retype(nir_inputs
, dest
.type
), bld
,
1452 instr
->const_index
[0] + index
);
1454 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[0]));
1458 dest
= offset(dest
, bld
, 1);
1463 /* Handle ARB_gpu_shader5 interpolation intrinsics
1465 * It's worth a quick word of explanation as to why we handle the full
1466 * variable-based interpolation intrinsic rather than a lowered version
1467 * with like we do for other inputs. We have to do that because the way
1468 * we set up inputs doesn't allow us to use the already setup inputs for
1469 * interpolation. At the beginning of the shader, we go through all of
1470 * the input variables and do the initial interpolation and put it in
1471 * the nir_inputs array based on its location as determined in
1472 * nir_lower_io. If the input isn't used, dead code cleans up and
1473 * everything works fine. However, when we get to the ARB_gpu_shader5
1474 * interpolation intrinsics, we need to reinterpolate the input
1475 * differently. If we used an intrinsic that just had an index it would
1476 * only give us the offset into the nir_inputs array. However, this is
1477 * useless because that value is post-interpolation and we need
1478 * pre-interpolation. In order to get the actual location of the bits
1479 * we get from the vertex fetching hardware, we need the variable.
1481 case nir_intrinsic_interp_var_at_centroid
:
1482 case nir_intrinsic_interp_var_at_sample
:
1483 case nir_intrinsic_interp_var_at_offset
: {
1484 fs_reg dst_xy
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 2);
1486 /* For most messages, we need one reg of ignored data; the hardware
1487 * requires mlen==1 even when there is no payload. in the per-slot
1488 * offset case, we'll replace this with the proper source data.
1490 fs_reg src
= vgrf(glsl_type::float_type
);
1491 int mlen
= 1; /* one reg unless overriden */
1494 switch (instr
->intrinsic
) {
1495 case nir_intrinsic_interp_var_at_centroid
:
1496 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_CENTROID
,
1497 dst_xy
, src
, fs_reg(0u));
1500 case nir_intrinsic_interp_var_at_sample
: {
1501 /* XXX: We should probably handle non-constant sample id's */
1502 nir_const_value
*const_sample
= nir_src_as_const_value(instr
->src
[0]);
1503 assert(const_sample
);
1504 unsigned msg_data
= const_sample
? const_sample
->i
[0] << 4 : 0;
1505 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_SAMPLE
, dst_xy
, src
,
1510 case nir_intrinsic_interp_var_at_offset
: {
1511 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
1514 unsigned off_x
= MIN2((int)(const_offset
->f
[0] * 16), 7) & 0xf;
1515 unsigned off_y
= MIN2((int)(const_offset
->f
[1] * 16), 7) & 0xf;
1517 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
, dst_xy
, src
,
1518 fs_reg(off_x
| (off_y
<< 4)));
1520 src
= vgrf(glsl_type::ivec2_type
);
1521 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
1522 BRW_REGISTER_TYPE_F
);
1523 for (int i
= 0; i
< 2; i
++) {
1524 fs_reg temp
= vgrf(glsl_type::float_type
);
1525 bld
.MUL(temp
, offset(offset_src
, bld
, i
), fs_reg(16.0f
));
1526 fs_reg itemp
= vgrf(glsl_type::int_type
);
1527 bld
.MOV(itemp
, temp
); /* float to int */
1529 /* Clamp the upper end of the range to +7/16.
1530 * ARB_gpu_shader5 requires that we support a maximum offset
1531 * of +0.5, which isn't representable in a S0.4 value -- if
1532 * we didn't clamp it, we'd end up with -8/16, which is the
1533 * opposite of what the shader author wanted.
1535 * This is legal due to ARB_gpu_shader5's quantization
1538 * "Not all values of <offset> may be supported; x and y
1539 * offsets may be rounded to fixed-point values with the
1540 * number of fraction bits given by the
1541 * implementation-dependent constant
1542 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
1544 set_condmod(BRW_CONDITIONAL_L
,
1545 bld
.SEL(offset(src
, bld
, i
), itemp
, fs_reg(7)));
1548 mlen
= 2 * dispatch_width
/ 8;
1549 inst
= bld
.emit(FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
, dst_xy
, src
,
1556 unreachable("Invalid intrinsic");
1560 /* 2 floats per slot returned */
1561 inst
->regs_written
= 2 * dispatch_width
/ 8;
1562 inst
->pi_noperspective
= instr
->variables
[0]->var
->data
.interpolation
==
1563 INTERP_QUALIFIER_NOPERSPECTIVE
;
1565 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1566 fs_reg src
= interp_reg(instr
->variables
[0]->var
->data
.location
, j
);
1567 src
.type
= dest
.type
;
1569 bld
.emit(FS_OPCODE_LINTERP
, dest
, dst_xy
, src
);
1570 dest
= offset(dest
, bld
, 1);
1575 case nir_intrinsic_store_output_indirect
:
1576 has_indirect
= true;
1578 case nir_intrinsic_store_output
: {
1579 fs_reg src
= get_nir_src(instr
->src
[0]);
1581 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
1582 fs_reg new_dest
= offset(retype(nir_outputs
, src
.type
), bld
,
1583 instr
->const_index
[0] + index
);
1585 src
.reladdr
= new(mem_ctx
) fs_reg(get_nir_src(instr
->src
[1]));
1587 bld
.MOV(new_dest
, src
);
1588 src
= offset(src
, bld
, 1);
1593 case nir_intrinsic_barrier
:
1598 unreachable("unknown intrinsic");
1603 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
1605 unsigned sampler
= instr
->sampler_index
;
1606 fs_reg
sampler_reg(sampler
);
1608 /* FINISHME: We're failing to recompile our programs when the sampler is
1609 * updated. This only matters for the texture rectangle scale parameters
1610 * (pre-gen6, or gen6+ with GL_CLAMP).
1612 int texunit
= prog
->SamplerUnits
[sampler
];
1614 int gather_component
= instr
->component
;
1616 bool is_rect
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
;
1618 bool is_cube_array
= instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
&&
1621 int lod_components
= 0;
1622 int UNUSED offset_components
= 0;
1624 fs_reg coordinate
, shadow_comparitor
, lod
, lod2
, sample_index
, mcs
, tex_offset
;
1626 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1627 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
1628 switch (instr
->src
[i
].src_type
) {
1629 case nir_tex_src_bias
:
1630 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1632 case nir_tex_src_comparitor
:
1633 shadow_comparitor
= retype(src
, BRW_REGISTER_TYPE_F
);
1635 case nir_tex_src_coord
:
1636 switch (instr
->op
) {
1638 case nir_texop_txf_ms
:
1639 coordinate
= retype(src
, BRW_REGISTER_TYPE_D
);
1642 coordinate
= retype(src
, BRW_REGISTER_TYPE_F
);
1646 case nir_tex_src_ddx
:
1647 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1648 lod_components
= nir_tex_instr_src_size(instr
, i
);
1650 case nir_tex_src_ddy
:
1651 lod2
= retype(src
, BRW_REGISTER_TYPE_F
);
1653 case nir_tex_src_lod
:
1654 switch (instr
->op
) {
1656 lod
= retype(src
, BRW_REGISTER_TYPE_UD
);
1659 lod
= retype(src
, BRW_REGISTER_TYPE_D
);
1662 lod
= retype(src
, BRW_REGISTER_TYPE_F
);
1666 case nir_tex_src_ms_index
:
1667 sample_index
= retype(src
, BRW_REGISTER_TYPE_UD
);
1669 case nir_tex_src_offset
:
1670 tex_offset
= retype(src
, BRW_REGISTER_TYPE_D
);
1671 if (instr
->is_array
)
1672 offset_components
= instr
->coord_components
- 1;
1674 offset_components
= instr
->coord_components
;
1676 case nir_tex_src_projector
:
1677 unreachable("should be lowered");
1679 case nir_tex_src_sampler_offset
: {
1680 /* Figure out the highest possible sampler index and mark it as used */
1681 uint32_t max_used
= sampler
+ instr
->sampler_array_size
- 1;
1682 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
1683 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
1685 max_used
+= stage_prog_data
->binding_table
.texture_start
;
1687 brw_mark_surface_used(prog_data
, max_used
);
1689 /* Emit code to evaluate the actual indexing expression */
1690 sampler_reg
= vgrf(glsl_type::uint_type
);
1691 bld
.ADD(sampler_reg
, src
, fs_reg(sampler
));
1692 bld
.emit_uniformize(sampler_reg
, sampler_reg
);
1697 unreachable("unknown texture source");
1701 if (instr
->op
== nir_texop_txf_ms
) {
1702 if (devinfo
->gen
>= 7 &&
1703 key_tex
->compressed_multisample_layout_mask
& (1 << sampler
)) {
1704 mcs
= emit_mcs_fetch(coordinate
, instr
->coord_components
, sampler_reg
);
1710 for (unsigned i
= 0; i
< 3; i
++) {
1711 if (instr
->const_offset
[i
] != 0) {
1712 assert(offset_components
== 0);
1713 tex_offset
= fs_reg(brw_texture_offset(instr
->const_offset
, 3));
1718 enum glsl_base_type dest_base_type
;
1719 switch (instr
->dest_type
) {
1720 case nir_type_float
:
1721 dest_base_type
= GLSL_TYPE_FLOAT
;
1724 dest_base_type
= GLSL_TYPE_INT
;
1726 case nir_type_unsigned
:
1727 dest_base_type
= GLSL_TYPE_UINT
;
1730 unreachable("bad type");
1733 const glsl_type
*dest_type
=
1734 glsl_type::get_instance(dest_base_type
, nir_tex_instr_dest_size(instr
),
1737 ir_texture_opcode op
;
1738 switch (instr
->op
) {
1739 case nir_texop_lod
: op
= ir_lod
; break;
1740 case nir_texop_query_levels
: op
= ir_query_levels
; break;
1741 case nir_texop_tex
: op
= ir_tex
; break;
1742 case nir_texop_tg4
: op
= ir_tg4
; break;
1743 case nir_texop_txb
: op
= ir_txb
; break;
1744 case nir_texop_txd
: op
= ir_txd
; break;
1745 case nir_texop_txf
: op
= ir_txf
; break;
1746 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
1747 case nir_texop_txl
: op
= ir_txl
; break;
1748 case nir_texop_txs
: op
= ir_txs
; break;
1750 unreachable("unknown texture opcode");
1753 emit_texture(op
, dest_type
, coordinate
, instr
->coord_components
,
1754 shadow_comparitor
, lod
, lod2
, lod_components
, sample_index
,
1755 tex_offset
, mcs
, gather_component
,
1756 is_cube_array
, is_rect
, sampler
, sampler_reg
, texunit
);
1758 fs_reg dest
= get_nir_dest(instr
->dest
);
1759 dest
.type
= this->result
.type
;
1760 unsigned num_components
= nir_tex_instr_dest_size(instr
);
1761 emit_percomp(bld
, fs_inst(BRW_OPCODE_MOV
, bld
.dispatch_width(),
1762 dest
, this->result
),
1763 (1 << num_components
) - 1);
1767 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
1769 switch (instr
->type
) {
1770 case nir_jump_break
:
1771 bld
.emit(BRW_OPCODE_BREAK
);
1773 case nir_jump_continue
:
1774 bld
.emit(BRW_OPCODE_CONTINUE
);
1776 case nir_jump_return
:
1778 unreachable("unknown jump");