2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "glsl_to_nir.h"
29 #include "nir_control_flow.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
35 * pass to lower GLSL IR to NIR
37 * This will lower variable dereferences to loads/stores of corresponding
38 * variables in NIR - the variables will be converted to registers in a later
44 class nir_visitor
: public ir_visitor
47 nir_visitor(nir_shader
*shader
);
50 virtual void visit(ir_variable
*);
51 virtual void visit(ir_function
*);
52 virtual void visit(ir_function_signature
*);
53 virtual void visit(ir_loop
*);
54 virtual void visit(ir_if
*);
55 virtual void visit(ir_discard
*);
56 virtual void visit(ir_loop_jump
*);
57 virtual void visit(ir_return
*);
58 virtual void visit(ir_call
*);
59 virtual void visit(ir_assignment
*);
60 virtual void visit(ir_emit_vertex
*);
61 virtual void visit(ir_end_primitive
*);
62 virtual void visit(ir_expression
*);
63 virtual void visit(ir_swizzle
*);
64 virtual void visit(ir_texture
*);
65 virtual void visit(ir_constant
*);
66 virtual void visit(ir_dereference_variable
*);
67 virtual void visit(ir_dereference_record
*);
68 virtual void visit(ir_dereference_array
*);
69 virtual void visit(ir_barrier
*);
71 void create_function(ir_function
*ir
);
74 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
75 void add_instr(nir_instr
*instr
, unsigned num_components
);
76 nir_src
evaluate_rvalue(ir_rvalue
*ir
);
78 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
);
79 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
);
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
82 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
83 nir_src src2
, nir_src src3
);
88 nir_function_impl
*impl
;
89 exec_list
*cf_node_list
;
90 nir_instr
*result
; /* result of the expression tree last visited */
92 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
94 /* the head of the dereference chain we're creating */
95 nir_deref_var
*deref_head
;
96 /* the tail of the dereference chain we're creating */
97 nir_deref
*deref_tail
;
99 nir_variable
*var
; /* variable created by ir_variable visitor */
101 /* whether the IR we're operating on is per-function or global */
104 /* map of ir_variable -> nir_variable */
105 struct hash_table
*var_table
;
107 /* map of ir_function_signature -> nir_function_overload */
108 struct hash_table
*overload_table
;
112 * This visitor runs before the main visitor, calling create_function() for
113 * each function so that the main visitor can resolve forward references in
117 class nir_function_visitor
: public ir_hierarchical_visitor
120 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
123 virtual ir_visitor_status
visit_enter(ir_function
*);
126 nir_visitor
*visitor
;
129 }; /* end of anonymous namespace */
132 glsl_to_nir(struct gl_shader
*sh
, const nir_shader_compiler_options
*options
)
134 nir_shader
*shader
= nir_shader_create(NULL
, sh
->Stage
, options
);
136 nir_visitor
v1(shader
);
137 nir_function_visitor
v2(&v1
);
139 visit_exec_list(sh
->ir
, &v1
);
144 nir_visitor::nir_visitor(nir_shader
*shader
)
146 this->supports_ints
= shader
->options
->native_integers
;
147 this->shader
= shader
;
148 this->is_global
= true;
149 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
150 _mesa_key_pointer_equal
);
151 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
152 _mesa_key_pointer_equal
);
155 nir_visitor::~nir_visitor()
157 _mesa_hash_table_destroy(this->var_table
, NULL
);
158 _mesa_hash_table_destroy(this->overload_table
, NULL
);
162 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
165 ralloc_steal(mem_ctx
, this->deref_head
);
166 return this->deref_head
;
169 static nir_constant
*
170 constant_copy(ir_constant
*ir
, void *mem_ctx
)
175 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
177 unsigned total_elems
= ir
->type
->components();
179 switch (ir
->type
->base_type
) {
181 for (i
= 0; i
< total_elems
; i
++)
182 ret
->value
.u
[i
] = ir
->value
.u
[i
];
186 for (i
= 0; i
< total_elems
; i
++)
187 ret
->value
.i
[i
] = ir
->value
.i
[i
];
190 case GLSL_TYPE_FLOAT
:
191 for (i
= 0; i
< total_elems
; i
++)
192 ret
->value
.f
[i
] = ir
->value
.f
[i
];
196 for (i
= 0; i
< total_elems
; i
++)
197 ret
->value
.b
[i
] = ir
->value
.b
[i
];
200 case GLSL_TYPE_STRUCT
:
201 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
204 foreach_in_list(ir_constant
, field
, &ir
->components
) {
205 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
210 case GLSL_TYPE_ARRAY
:
211 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
214 for (i
= 0; i
< ir
->type
->length
; i
++)
215 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
219 unreachable("not reached");
226 nir_visitor::visit(ir_variable
*ir
)
228 nir_variable
*var
= ralloc(shader
, nir_variable
);
229 var
->type
= ir
->type
;
230 var
->name
= ralloc_strdup(var
, ir
->name
);
232 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
233 unsigned size
= ir
->get_interface_type()->length
;
234 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
235 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
236 size
* sizeof(unsigned));
238 var
->max_ifc_array_access
= NULL
;
241 var
->data
.read_only
= ir
->data
.read_only
;
242 var
->data
.centroid
= ir
->data
.centroid
;
243 var
->data
.sample
= ir
->data
.sample
;
244 var
->data
.invariant
= ir
->data
.invariant
;
245 var
->data
.location
= ir
->data
.location
;
247 switch(ir
->data
.mode
) {
249 case ir_var_temporary
:
251 var
->data
.mode
= nir_var_global
;
253 var
->data
.mode
= nir_var_local
;
256 case ir_var_function_in
:
257 case ir_var_function_out
:
258 case ir_var_function_inout
:
259 case ir_var_const_in
:
260 var
->data
.mode
= nir_var_local
;
263 case ir_var_shader_in
:
264 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
265 ir
->data
.location
== VARYING_SLOT_FACE
) {
266 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
267 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
268 var
->data
.mode
= nir_var_system_value
;
270 var
->data
.mode
= nir_var_shader_in
;
274 case ir_var_shader_out
:
275 var
->data
.mode
= nir_var_shader_out
;
279 var
->data
.mode
= nir_var_uniform
;
282 case ir_var_shader_storage
:
283 var
->data
.mode
= nir_var_shader_storage
;
286 case ir_var_system_value
:
287 var
->data
.mode
= nir_var_system_value
;
291 unreachable("not reached");
294 var
->data
.interpolation
= ir
->data
.interpolation
;
295 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
296 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
297 var
->data
.explicit_location
= ir
->data
.explicit_location
;
298 var
->data
.explicit_index
= ir
->data
.explicit_index
;
299 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
300 var
->data
.has_initializer
= ir
->data
.has_initializer
;
301 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
302 var
->data
.location_frac
= ir
->data
.location_frac
;
303 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
304 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
306 switch (ir
->data
.depth_layout
) {
307 case ir_depth_layout_none
:
308 var
->data
.depth_layout
= nir_depth_layout_none
;
310 case ir_depth_layout_any
:
311 var
->data
.depth_layout
= nir_depth_layout_any
;
313 case ir_depth_layout_greater
:
314 var
->data
.depth_layout
= nir_depth_layout_greater
;
316 case ir_depth_layout_less
:
317 var
->data
.depth_layout
= nir_depth_layout_less
;
319 case ir_depth_layout_unchanged
:
320 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
323 unreachable("not reached");
326 var
->data
.index
= ir
->data
.index
;
327 var
->data
.binding
= ir
->data
.binding
;
328 /* XXX Get rid of buffer_index */
329 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
330 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
331 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
332 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
333 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
334 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
335 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
336 var
->data
.image
.format
= ir
->data
.image_format
;
337 var
->data
.max_array_access
= ir
->data
.max_array_access
;
339 var
->num_state_slots
= ir
->get_num_state_slots();
340 if (var
->num_state_slots
> 0) {
341 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
342 var
->num_state_slots
);
344 ir_state_slot
*state_slots
= ir
->get_state_slots();
345 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
346 for (unsigned j
= 0; j
< 5; j
++)
347 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
348 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
351 var
->state_slots
= NULL
;
354 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
356 var
->interface_type
= ir
->get_interface_type();
358 switch (var
->data
.mode
) {
360 exec_list_push_tail(&impl
->locals
, &var
->node
);
364 exec_list_push_tail(&shader
->globals
, &var
->node
);
367 case nir_var_shader_in
:
368 exec_list_push_tail(&shader
->inputs
, &var
->node
);
371 case nir_var_shader_out
:
372 exec_list_push_tail(&shader
->outputs
, &var
->node
);
375 case nir_var_uniform
:
376 case nir_var_shader_storage
:
377 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
380 case nir_var_system_value
:
381 exec_list_push_tail(&shader
->system_values
, &var
->node
);
385 unreachable("not reached");
388 _mesa_hash_table_insert(var_table
, ir
, var
);
393 nir_function_visitor::visit_enter(ir_function
*ir
)
395 visitor
->create_function(ir
);
396 return visit_continue_with_parent
;
401 nir_visitor::create_function(ir_function
*ir
)
403 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
404 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
405 create_overload(sig
, func
);
412 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
414 if (ir
->is_intrinsic
)
417 nir_function_overload
*overload
= nir_function_overload_create(function
);
419 unsigned num_params
= ir
->parameters
.length();
420 overload
->num_params
= num_params
;
421 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
424 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
425 switch (param
->data
.mode
) {
426 case ir_var_function_in
:
427 overload
->params
[i
].param_type
= nir_parameter_in
;
430 case ir_var_function_out
:
431 overload
->params
[i
].param_type
= nir_parameter_out
;
434 case ir_var_function_inout
:
435 overload
->params
[i
].param_type
= nir_parameter_inout
;
439 unreachable("not reached");
442 overload
->params
[i
].type
= param
->type
;
446 overload
->return_type
= ir
->return_type
;
448 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
452 nir_visitor::visit(ir_function
*ir
)
454 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
459 nir_visitor::visit(ir_function_signature
*ir
)
461 if (ir
->is_intrinsic
)
464 struct hash_entry
*entry
=
465 _mesa_hash_table_search(this->overload_table
, ir
);
468 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
470 if (ir
->is_defined
) {
471 nir_function_impl
*impl
= nir_function_impl_create(overload
);
474 unsigned num_params
= overload
->num_params
;
475 impl
->num_params
= num_params
;
476 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
478 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
480 impl
->params
[i
] = this->var
;
484 if (overload
->return_type
== glsl_type::void_type
) {
485 impl
->return_var
= NULL
;
487 impl
->return_var
= ralloc(this->shader
, nir_variable
);
488 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
490 impl
->return_var
->type
= overload
->return_type
;
493 this->is_global
= false;
495 this->cf_node_list
= &impl
->body
;
496 visit_exec_list(&ir
->body
, this);
498 this->is_global
= true;
500 overload
->impl
= NULL
;
505 nir_visitor::visit(ir_loop
*ir
)
507 exec_list
*old_list
= this->cf_node_list
;
509 nir_loop
*loop
= nir_loop_create(this->shader
);
510 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
511 this->cf_node_list
= &loop
->body
;
512 visit_exec_list(&ir
->body_instructions
, this);
514 this->cf_node_list
= old_list
;
518 nir_visitor::visit(ir_if
*ir
)
520 nir_src condition
= evaluate_rvalue(ir
->condition
);
522 exec_list
*old_list
= this->cf_node_list
;
524 nir_if
*if_stmt
= nir_if_create(this->shader
);
525 if_stmt
->condition
= condition
;
526 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
528 this->cf_node_list
= &if_stmt
->then_list
;
529 visit_exec_list(&ir
->then_instructions
, this);
531 this->cf_node_list
= &if_stmt
->else_list
;
532 visit_exec_list(&ir
->else_instructions
, this);
534 this->cf_node_list
= old_list
;
538 nir_visitor::visit(ir_discard
*ir
)
541 * discards aren't treated as control flow, because before we lower them
542 * they can appear anywhere in the shader and the stuff after them may still
543 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
544 * discards will be immediately followed by a return.
547 nir_intrinsic_instr
*discard
;
549 discard
= nir_intrinsic_instr_create(this->shader
,
550 nir_intrinsic_discard_if
);
551 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
553 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
555 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
559 nir_visitor::visit(ir_emit_vertex
*ir
)
561 nir_intrinsic_instr
*instr
=
562 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
563 instr
->const_index
[0] = ir
->stream_id();
564 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
568 nir_visitor::visit(ir_end_primitive
*ir
)
570 nir_intrinsic_instr
*instr
=
571 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
572 instr
->const_index
[0] = ir
->stream_id();
573 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
577 nir_visitor::visit(ir_loop_jump
*ir
)
581 case ir_loop_jump::jump_break
:
582 type
= nir_jump_break
;
584 case ir_loop_jump::jump_continue
:
585 type
= nir_jump_continue
;
588 unreachable("not reached");
591 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
592 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
596 nir_visitor::visit(ir_return
*ir
)
598 if (ir
->value
!= NULL
) {
599 nir_intrinsic_instr
*copy
=
600 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
602 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
603 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
606 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
607 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
611 nir_visitor::visit(ir_call
*ir
)
613 if (ir
->callee
->is_intrinsic
) {
615 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
616 op
= nir_intrinsic_atomic_counter_read_var
;
617 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
618 op
= nir_intrinsic_atomic_counter_inc_var
;
619 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
620 op
= nir_intrinsic_atomic_counter_dec_var
;
621 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
622 op
= nir_intrinsic_image_load
;
623 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
624 op
= nir_intrinsic_image_store
;
625 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
626 op
= nir_intrinsic_image_atomic_add
;
627 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
628 op
= nir_intrinsic_image_atomic_min
;
629 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
630 op
= nir_intrinsic_image_atomic_max
;
631 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
632 op
= nir_intrinsic_image_atomic_and
;
633 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
634 op
= nir_intrinsic_image_atomic_or
;
635 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
636 op
= nir_intrinsic_image_atomic_xor
;
637 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
638 op
= nir_intrinsic_image_atomic_exchange
;
639 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
640 op
= nir_intrinsic_image_atomic_comp_swap
;
641 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
642 op
= nir_intrinsic_memory_barrier
;
643 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
644 op
= nir_intrinsic_image_size
;
646 unreachable("not reached");
649 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
652 case nir_intrinsic_atomic_counter_read_var
:
653 case nir_intrinsic_atomic_counter_inc_var
:
654 case nir_intrinsic_atomic_counter_dec_var
: {
655 ir_dereference
*param
=
656 (ir_dereference
*) ir
->actual_parameters
.get_head();
657 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
658 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
661 case nir_intrinsic_image_load
:
662 case nir_intrinsic_image_store
:
663 case nir_intrinsic_image_atomic_add
:
664 case nir_intrinsic_image_atomic_min
:
665 case nir_intrinsic_image_atomic_max
:
666 case nir_intrinsic_image_atomic_and
:
667 case nir_intrinsic_image_atomic_or
:
668 case nir_intrinsic_image_atomic_xor
:
669 case nir_intrinsic_image_atomic_exchange
:
670 case nir_intrinsic_image_atomic_comp_swap
:
671 case nir_intrinsic_image_size
: {
672 nir_ssa_undef_instr
*instr_undef
=
673 nir_ssa_undef_instr_create(shader
, 1);
674 nir_instr_insert_after_cf_list(this->cf_node_list
,
675 &instr_undef
->instr
);
677 /* Set the image variable dereference. */
678 exec_node
*param
= ir
->actual_parameters
.get_head();
679 ir_dereference
*image
= (ir_dereference
*)param
;
680 const glsl_type
*type
=
681 image
->variable_referenced()->type
->without_array();
683 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
684 param
= param
->get_next();
686 /* Set the intrinsic destination. */
687 if (ir
->return_deref
) {
688 const nir_intrinsic_info
*info
=
689 &nir_intrinsic_infos
[instr
->intrinsic
];
690 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
691 info
->dest_components
, NULL
);
694 if (op
== nir_intrinsic_image_size
)
697 /* Set the address argument, extending the coordinate vector to four
700 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
701 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
702 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
704 for (int i
= 0; i
< 4; i
++) {
705 if (i
< type
->coordinate_components()) {
706 instr_addr
->src
[i
].src
= src_addr
;
707 instr_addr
->src
[i
].swizzle
[0] = i
;
709 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
713 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
714 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
715 param
= param
->get_next();
717 /* Set the sample argument, which is undefined for single-sample
720 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
721 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
722 param
= param
->get_next();
724 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
727 /* Set the intrinsic parameters. */
728 if (!param
->is_tail_sentinel()) {
729 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
730 param
= param
->get_next();
733 if (!param
->is_tail_sentinel()) {
734 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
735 param
= param
->get_next();
739 case nir_intrinsic_memory_barrier
:
742 unreachable("not reached");
745 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
747 if (ir
->return_deref
) {
748 nir_intrinsic_instr
*store_instr
=
749 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
750 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
752 store_instr
->variables
[0] =
753 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
754 store_instr
->src
[0] = nir_src_for_ssa(&instr
->dest
.ssa
);
756 nir_instr_insert_after_cf_list(this->cf_node_list
,
757 &store_instr
->instr
);
763 struct hash_entry
*entry
=
764 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
766 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
768 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
771 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
772 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
776 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
777 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
781 nir_visitor::visit(ir_assignment
*ir
)
783 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
785 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
786 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
787 /* We're doing a plain-as-can-be copy, so emit a copy_var */
788 nir_intrinsic_instr
*copy
=
789 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
791 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
792 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
795 nir_if
*if_stmt
= nir_if_create(this->shader
);
796 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
797 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
798 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
800 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
805 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
807 ir
->lhs
->accept(this);
808 nir_deref_var
*lhs_deref
= this->deref_head
;
809 nir_src src
= evaluate_rvalue(ir
->rhs
);
811 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
813 * We have no good way to update only part of a variable, so just load
814 * the LHS and do a vec operation to combine the old with the new, and
816 * back into the LHS. Copy propagation should get rid of the mess.
819 nir_intrinsic_instr
*load
=
820 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
821 load
->num_components
= ir
->lhs
->type
->vector_elements
;
822 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
823 load
->variables
[0] = lhs_deref
;
824 ralloc_steal(load
, load
->variables
[0]);
825 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
828 switch (ir
->lhs
->type
->vector_elements
) {
829 case 1: vec_op
= nir_op_imov
; break;
830 case 2: vec_op
= nir_op_vec2
; break;
831 case 3: vec_op
= nir_op_vec3
; break;
832 case 4: vec_op
= nir_op_vec4
; break;
833 default: unreachable("Invalid number of components"); break;
835 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
836 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
837 vec
->dest
.write_mask
= (1 << num_components
) - 1;
839 unsigned component
= 0;
840 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
841 if (ir
->write_mask
& (1 << i
)) {
842 vec
->src
[i
].src
= src
;
844 /* GLSL IR will give us the input to the write-masked assignment
845 * in a single packed vector. So, for example, if the
846 * writemask is xzw, then we have to swizzle x -> x, y -> z,
847 * and z -> w and get the y component from the load.
849 vec
->src
[i
].swizzle
[0] = component
++;
851 vec
->src
[i
].src
.is_ssa
= true;
852 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
853 vec
->src
[i
].swizzle
[0] = i
;
857 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
860 src
.ssa
= &vec
->dest
.dest
.ssa
;
863 nir_intrinsic_instr
*store
=
864 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
865 store
->num_components
= ir
->lhs
->type
->vector_elements
;
866 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
867 store
->variables
[0] = nir_deref_as_var(store_deref
);
871 nir_if
*if_stmt
= nir_if_create(this->shader
);
872 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
873 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
874 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
876 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
881 * Given an instruction, returns a pointer to its destination or NULL if there
884 * Note that this only handles instructions we generate at this level.
887 get_instr_dest(nir_instr
*instr
)
889 nir_alu_instr
*alu_instr
;
890 nir_intrinsic_instr
*intrinsic_instr
;
891 nir_tex_instr
*tex_instr
;
893 switch (instr
->type
) {
894 case nir_instr_type_alu
:
895 alu_instr
= nir_instr_as_alu(instr
);
896 return &alu_instr
->dest
.dest
;
898 case nir_instr_type_intrinsic
:
899 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
900 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
901 return &intrinsic_instr
->dest
;
905 case nir_instr_type_tex
:
906 tex_instr
= nir_instr_as_tex(instr
);
907 return &tex_instr
->dest
;
910 unreachable("not reached");
917 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
919 nir_dest
*dest
= get_instr_dest(instr
);
921 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
923 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
924 this->result
= instr
;
928 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
931 if (ir
->as_dereference() || ir
->as_constant()) {
933 * A dereference is being used on the right hand side, which means we
934 * must emit a variable load.
937 nir_intrinsic_instr
*load_instr
=
938 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
939 load_instr
->num_components
= ir
->type
->vector_elements
;
940 load_instr
->variables
[0] = this->deref_head
;
941 ralloc_steal(load_instr
, load_instr
->variables
[0]);
942 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
945 nir_dest
*dest
= get_instr_dest(this->result
);
946 assert(dest
->is_ssa
);
948 return nir_src_for_ssa(&dest
->ssa
);
952 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
954 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
955 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
956 instr
->src
[i
].src
= srcs
[i
];
957 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
958 add_instr(&instr
->instr
, dest_size
);
963 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
965 assert(nir_op_infos
[op
].num_inputs
== 1);
966 return emit(op
, dest_size
, &src1
);
970 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
973 assert(nir_op_infos
[op
].num_inputs
== 2);
974 nir_src srcs
[] = { src1
, src2
};
975 return emit(op
, dest_size
, srcs
);
979 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
980 nir_src src2
, nir_src src3
)
982 assert(nir_op_infos
[op
].num_inputs
== 3);
983 nir_src srcs
[] = { src1
, src2
, src3
};
984 return emit(op
, dest_size
, srcs
);
988 nir_visitor::visit(ir_expression
*ir
)
990 /* Some special cases */
991 switch (ir
->operation
) {
992 case ir_binop_ubo_load
: {
993 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
997 op
= nir_intrinsic_load_ubo
;
999 op
= nir_intrinsic_load_ubo_indirect
;
1001 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1002 load
->num_components
= ir
->type
->vector_elements
;
1003 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1004 load
->const_index
[1] = 1; /* number of vec4's */
1005 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
1007 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1008 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1011 * In UBO's, a true boolean value is any non-zero value, but we consider
1012 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1015 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1016 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1017 const_zero
->value
.u
[0] = 0;
1018 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1020 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1021 compare
->src
[0].src
.is_ssa
= true;
1022 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1023 compare
->src
[1].src
.is_ssa
= true;
1024 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1025 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1026 compare
->src
[1].swizzle
[i
] = 0;
1027 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1029 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1035 case ir_unop_interpolate_at_centroid
:
1036 case ir_binop_interpolate_at_offset
:
1037 case ir_binop_interpolate_at_sample
: {
1038 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1039 ir_swizzle
*swizzle
= NULL
;
1041 /* the api does not allow a swizzle here, but the varying packing code
1042 * may have pushed one into here.
1044 swizzle
= ir
->operands
[0]->as_swizzle();
1046 deref
= swizzle
->val
->as_dereference();
1050 deref
->accept(this);
1052 nir_intrinsic_op op
;
1053 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1054 switch (ir
->operation
) {
1055 case ir_unop_interpolate_at_centroid
:
1056 op
= nir_intrinsic_interp_var_at_centroid
;
1058 case ir_binop_interpolate_at_offset
:
1059 op
= nir_intrinsic_interp_var_at_offset
;
1061 case ir_binop_interpolate_at_sample
:
1062 op
= nir_intrinsic_interp_var_at_sample
;
1065 unreachable("Invalid interpolation intrinsic");
1068 /* This case can happen if the vertex shader does not write the
1069 * given varying. In this case, the linker will lower it to a
1070 * global variable. Since interpolating a variable makes no
1071 * sense, we'll just turn it into a load which will probably
1072 * eventually end up as an SSA definition.
1074 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1075 op
= nir_intrinsic_load_var
;
1078 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1079 intrin
->num_components
= deref
->type
->vector_elements
;
1080 intrin
->variables
[0] = this->deref_head
;
1081 ralloc_steal(intrin
, intrin
->variables
[0]);
1083 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1084 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1085 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1087 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1090 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1091 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1092 mov
->src
[0].src
.is_ssa
= true;
1093 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1095 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1096 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1097 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1098 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1099 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1100 mov
->src
[0].swizzle
[i
] = 0;
1102 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1113 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1114 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1116 glsl_base_type types
[4];
1117 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1119 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1121 types
[i
] = GLSL_TYPE_FLOAT
;
1123 glsl_base_type out_type
;
1125 out_type
= ir
->type
->base_type
;
1127 out_type
= GLSL_TYPE_FLOAT
;
1129 unsigned dest_size
= ir
->type
->vector_elements
;
1131 nir_alu_instr
*instr
;
1134 switch (ir
->operation
) {
1135 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1136 case ir_unop_logic_not
:
1137 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1140 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1144 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1147 case ir_unop_saturate
:
1148 assert(types
[0] == GLSL_TYPE_FLOAT
);
1149 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1152 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1155 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1156 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1157 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1158 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1159 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1160 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1161 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1163 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1166 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1169 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1171 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1172 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1173 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1174 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1175 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1178 case ir_unop_bitcast_i2f
:
1179 case ir_unop_bitcast_f2i
:
1180 case ir_unop_bitcast_u2f
:
1181 case ir_unop_bitcast_f2u
:
1182 case ir_unop_subroutine_to_int
:
1184 emit(nir_op_imov
, dest_size
, srcs
);
1187 switch (ir
->operands
[0]->type
->vector_elements
) {
1189 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1193 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1197 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1201 unreachable("not reached");
1204 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1205 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1206 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1207 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1208 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1209 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1210 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1211 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1212 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1213 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1214 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1215 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1216 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1217 case ir_unop_pack_snorm_2x16
:
1218 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1220 case ir_unop_pack_snorm_4x8
:
1221 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1223 case ir_unop_pack_unorm_2x16
:
1224 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1226 case ir_unop_pack_unorm_4x8
:
1227 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1229 case ir_unop_pack_half_2x16
:
1230 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1232 case ir_unop_unpack_snorm_2x16
:
1233 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1235 case ir_unop_unpack_snorm_4x8
:
1236 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1238 case ir_unop_unpack_unorm_2x16
:
1239 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1241 case ir_unop_unpack_unorm_4x8
:
1242 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1244 case ir_unop_unpack_half_2x16
:
1245 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1247 case ir_unop_unpack_half_2x16_split_x
:
1248 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1250 case ir_unop_unpack_half_2x16_split_y
:
1251 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1253 case ir_unop_bitfield_reverse
:
1254 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1256 case ir_unop_bit_count
:
1257 emit(nir_op_bit_count
, dest_size
, srcs
);
1259 case ir_unop_find_msb
:
1261 case GLSL_TYPE_UINT
:
1262 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1265 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1268 unreachable("Invalid type for findMSB()");
1271 case ir_unop_find_lsb
:
1272 emit(nir_op_find_lsb
, dest_size
, srcs
);
1276 switch (ir
->type
->vector_elements
) {
1278 switch (ir
->operands
[0]->type
->vector_elements
) {
1279 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1280 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1281 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1282 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1283 default: unreachable("not reached");
1287 switch (ir
->operands
[0]->type
->vector_elements
) {
1288 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1289 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1290 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1291 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1292 default: unreachable("not reached");
1296 switch (ir
->operands
[0]->type
->vector_elements
) {
1297 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1298 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1299 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1300 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1301 default: unreachable("not reached");
1305 switch (ir
->operands
[0]->type
->vector_elements
) {
1306 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1307 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1308 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1309 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1310 default: unreachable("not reached");
1314 unreachable("not reached");
1325 case ir_binop_bit_and
:
1326 case ir_binop_bit_or
:
1327 case ir_binop_bit_xor
:
1328 case ir_binop_logic_and
:
1329 case ir_binop_logic_or
:
1330 case ir_binop_logic_xor
:
1331 case ir_binop_lshift
:
1332 case ir_binop_rshift
:
1333 switch (ir
->operation
) {
1335 if (out_type
== GLSL_TYPE_FLOAT
)
1341 if (out_type
== GLSL_TYPE_FLOAT
)
1347 if (out_type
== GLSL_TYPE_FLOAT
)
1353 if (out_type
== GLSL_TYPE_FLOAT
)
1355 else if (out_type
== GLSL_TYPE_INT
)
1361 if (out_type
== GLSL_TYPE_FLOAT
)
1367 if (out_type
== GLSL_TYPE_FLOAT
)
1369 else if (out_type
== GLSL_TYPE_INT
)
1375 if (out_type
== GLSL_TYPE_FLOAT
)
1377 else if (out_type
== GLSL_TYPE_INT
)
1382 case ir_binop_bit_and
:
1385 case ir_binop_bit_or
:
1388 case ir_binop_bit_xor
:
1391 case ir_binop_logic_and
:
1397 case ir_binop_logic_or
:
1403 case ir_binop_logic_xor
:
1409 case ir_binop_lshift
:
1412 case ir_binop_rshift
:
1413 if (out_type
== GLSL_TYPE_INT
)
1423 unreachable("not reached");
1426 instr
= emit(op
, dest_size
, srcs
);
1428 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1429 ir
->operands
[1]->type
->vector_elements
== 1) {
1430 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1432 instr
->src
[1].swizzle
[i
] = 0;
1436 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1437 ir
->operands
[0]->type
->vector_elements
== 1) {
1438 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1440 instr
->src
[0].swizzle
[i
] = 0;
1445 case ir_binop_imul_high
:
1446 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1449 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1450 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1452 if (supports_ints
) {
1453 if (types
[0] == GLSL_TYPE_FLOAT
)
1454 emit(nir_op_flt
, dest_size
, srcs
);
1455 else if (types
[0] == GLSL_TYPE_INT
)
1456 emit(nir_op_ilt
, dest_size
, srcs
);
1458 emit(nir_op_ult
, dest_size
, srcs
);
1460 emit(nir_op_slt
, dest_size
, srcs
);
1463 case ir_binop_greater
:
1464 if (supports_ints
) {
1465 if (types
[0] == GLSL_TYPE_FLOAT
)
1466 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1467 else if (types
[0] == GLSL_TYPE_INT
)
1468 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1470 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1472 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1475 case ir_binop_lequal
:
1476 if (supports_ints
) {
1477 if (types
[0] == GLSL_TYPE_FLOAT
)
1478 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1479 else if (types
[0] == GLSL_TYPE_INT
)
1480 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1482 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1484 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1487 case ir_binop_gequal
:
1488 if (supports_ints
) {
1489 if (types
[0] == GLSL_TYPE_FLOAT
)
1490 emit(nir_op_fge
, dest_size
, srcs
);
1491 else if (types
[0] == GLSL_TYPE_INT
)
1492 emit(nir_op_ige
, dest_size
, srcs
);
1494 emit(nir_op_uge
, dest_size
, srcs
);
1496 emit(nir_op_slt
, dest_size
, srcs
);
1499 case ir_binop_equal
:
1500 if (supports_ints
) {
1501 if (types
[0] == GLSL_TYPE_FLOAT
)
1502 emit(nir_op_feq
, dest_size
, srcs
);
1504 emit(nir_op_ieq
, dest_size
, srcs
);
1506 emit(nir_op_seq
, dest_size
, srcs
);
1509 case ir_binop_nequal
:
1510 if (supports_ints
) {
1511 if (types
[0] == GLSL_TYPE_FLOAT
)
1512 emit(nir_op_fne
, dest_size
, srcs
);
1514 emit(nir_op_ine
, dest_size
, srcs
);
1516 emit(nir_op_sne
, dest_size
, srcs
);
1519 case ir_binop_all_equal
:
1520 if (supports_ints
) {
1521 if (types
[0] == GLSL_TYPE_FLOAT
) {
1522 switch (ir
->operands
[0]->type
->vector_elements
) {
1523 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1524 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1525 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1526 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1528 unreachable("not reached");
1531 switch (ir
->operands
[0]->type
->vector_elements
) {
1532 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1533 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1534 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1535 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1537 unreachable("not reached");
1541 switch (ir
->operands
[0]->type
->vector_elements
) {
1542 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1543 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1544 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1545 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1547 unreachable("not reached");
1551 case ir_binop_any_nequal
:
1552 if (supports_ints
) {
1553 if (types
[0] == GLSL_TYPE_FLOAT
) {
1554 switch (ir
->operands
[0]->type
->vector_elements
) {
1555 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1556 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1557 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1558 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1560 unreachable("not reached");
1563 switch (ir
->operands
[0]->type
->vector_elements
) {
1564 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1565 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1566 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1567 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1569 unreachable("not reached");
1573 switch (ir
->operands
[0]->type
->vector_elements
) {
1574 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1575 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1576 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1577 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1579 unreachable("not reached");
1584 switch (ir
->operands
[0]->type
->vector_elements
) {
1585 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1586 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1587 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1589 unreachable("not reached");
1593 case ir_binop_pack_half_2x16_split
:
1594 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1596 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1597 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1598 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1600 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1601 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1602 ir
->operands
[2]->type
->vector_elements
== 1) {
1603 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1605 instr
->src
[2].swizzle
[i
] = 0;
1611 emit(nir_op_bcsel
, dest_size
, srcs
);
1613 emit(nir_op_fcsel
, dest_size
, srcs
);
1616 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1617 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1618 instr
->src
[0].swizzle
[i
] = 0;
1621 case ir_triop_bitfield_extract
:
1622 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1623 nir_op_ubitfield_extract
, dest_size
, srcs
);
1624 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1625 instr
->src
[1].swizzle
[i
] = 0;
1626 instr
->src
[2].swizzle
[i
] = 0;
1629 case ir_quadop_bitfield_insert
:
1630 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1631 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1632 instr
->src
[2].swizzle
[i
] = 0;
1633 instr
->src
[3].swizzle
[i
] = 0;
1636 case ir_quadop_vector
:
1637 switch (ir
->type
->vector_elements
) {
1638 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1639 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1640 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1641 default: unreachable("not reached");
1646 unreachable("not reached");
1651 nir_visitor::visit(ir_swizzle
*ir
)
1653 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1654 ir
->type
->vector_elements
,
1655 evaluate_rvalue(ir
->val
));
1657 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1658 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1659 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1663 nir_visitor::visit(ir_texture
*ir
)
1670 num_srcs
= 1; /* coordinate */
1675 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1676 num_srcs
= 2; /* coordinate, bias/lod */
1680 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1686 if (ir
->lod_info
.lod
!= NULL
)
1687 num_srcs
= 2; /* coordinate, lod */
1689 num_srcs
= 1; /* coordinate */
1693 op
= nir_texop_txf_ms
;
1694 num_srcs
= 2; /* coordinate, sample_index */
1699 if (ir
->lod_info
.lod
!= NULL
)
1700 num_srcs
= 1; /* lod */
1707 num_srcs
= 1; /* coordinate */
1712 num_srcs
= 1; /* coordinate */
1715 case ir_query_levels
:
1716 op
= nir_texop_query_levels
;
1721 unreachable("not reached");
1724 if (ir
->projector
!= NULL
)
1726 if (ir
->shadow_comparitor
!= NULL
)
1728 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1731 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1734 instr
->sampler_dim
=
1735 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1736 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1737 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1738 if (instr
->is_shadow
)
1739 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1740 switch (ir
->type
->base_type
) {
1741 case GLSL_TYPE_FLOAT
:
1742 instr
->dest_type
= nir_type_float
;
1745 instr
->dest_type
= nir_type_int
;
1747 case GLSL_TYPE_UINT
:
1748 instr
->dest_type
= nir_type_unsigned
;
1751 unreachable("not reached");
1754 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1756 unsigned src_number
= 0;
1758 if (ir
->coordinate
!= NULL
) {
1759 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1760 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1761 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1765 if (ir
->projector
!= NULL
) {
1766 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1767 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1771 if (ir
->shadow_comparitor
!= NULL
) {
1772 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1773 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1777 if (ir
->offset
!= NULL
) {
1778 /* we don't support multiple offsets yet */
1779 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1781 ir_constant
*const_offset
= ir
->offset
->as_constant();
1782 if (const_offset
!= NULL
) {
1783 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1784 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1786 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1787 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1794 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1795 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1802 if (ir
->lod_info
.lod
!= NULL
) {
1803 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1804 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1810 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1811 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1813 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1814 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1819 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
1820 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1825 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1832 assert(src_number
== num_srcs
);
1834 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1838 nir_visitor::visit(ir_constant
*ir
)
1841 * We don't know if this variable is an an array or struct that gets
1842 * dereferenced, so do the safe thing an make it a variable with a
1843 * constant initializer and return a dereference.
1846 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1847 var
->name
= ralloc_strdup(var
, "const_temp");
1848 var
->type
= ir
->type
;
1849 var
->data
.mode
= nir_var_local
;
1850 var
->data
.read_only
= true;
1851 var
->constant_initializer
= constant_copy(ir
, var
);
1852 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1854 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1855 this->deref_tail
= &this->deref_head
->deref
;
1859 nir_visitor::visit(ir_dereference_variable
*ir
)
1861 struct hash_entry
*entry
=
1862 _mesa_hash_table_search(this->var_table
, ir
->var
);
1864 nir_variable
*var
= (nir_variable
*) entry
->data
;
1866 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1867 this->deref_head
= deref
;
1868 this->deref_tail
= &deref
->deref
;
1872 nir_visitor::visit(ir_dereference_record
*ir
)
1874 ir
->record
->accept(this);
1876 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1877 assert(field_index
>= 0);
1879 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1880 deref
->deref
.type
= ir
->type
;
1881 this->deref_tail
->child
= &deref
->deref
;
1882 this->deref_tail
= &deref
->deref
;
1886 nir_visitor::visit(ir_dereference_array
*ir
)
1888 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1889 deref
->deref
.type
= ir
->type
;
1891 ir_constant
*const_index
= ir
->array_index
->as_constant();
1892 if (const_index
!= NULL
) {
1893 deref
->deref_array_type
= nir_deref_array_type_direct
;
1894 deref
->base_offset
= const_index
->value
.u
[0];
1896 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1897 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1900 ir
->array
->accept(this);
1902 this->deref_tail
->child
= &deref
->deref
;
1903 ralloc_steal(this->deref_tail
, deref
);
1904 this->deref_tail
= &deref
->deref
;
1908 nir_visitor::visit(ir_barrier
*ir
)
1910 nir_intrinsic_instr
*instr
=
1911 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
1912 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);