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
, gl_shader
*sh
);
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
);
90 nir_function_impl
*impl
;
91 exec_list
*cf_node_list
;
92 nir_instr
*result
; /* result of the expression tree last visited */
94 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
96 /* the head of the dereference chain we're creating */
97 nir_deref_var
*deref_head
;
98 /* the tail of the dereference chain we're creating */
99 nir_deref
*deref_tail
;
101 nir_variable
*var
; /* variable created by ir_variable visitor */
103 /* whether the IR we're operating on is per-function or global */
106 /* map of ir_variable -> nir_variable */
107 struct hash_table
*var_table
;
109 /* map of ir_function_signature -> nir_function_overload */
110 struct hash_table
*overload_table
;
114 * This visitor runs before the main visitor, calling create_function() for
115 * each function so that the main visitor can resolve forward references in
119 class nir_function_visitor
: public ir_hierarchical_visitor
122 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
125 virtual ir_visitor_status
visit_enter(ir_function
*);
128 nir_visitor
*visitor
;
131 }; /* end of anonymous namespace */
134 glsl_to_nir(struct gl_shader
*sh
, const nir_shader_compiler_options
*options
)
136 nir_shader
*shader
= nir_shader_create(NULL
, sh
->Stage
, options
);
138 nir_visitor
v1(shader
, sh
);
139 nir_function_visitor
v2(&v1
);
141 visit_exec_list(sh
->ir
, &v1
);
146 nir_visitor::nir_visitor(nir_shader
*shader
, gl_shader
*sh
)
148 this->supports_ints
= shader
->options
->native_integers
;
149 this->shader
= shader
;
151 this->is_global
= true;
152 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
153 _mesa_key_pointer_equal
);
154 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
155 _mesa_key_pointer_equal
);
158 nir_visitor::~nir_visitor()
160 _mesa_hash_table_destroy(this->var_table
, NULL
);
161 _mesa_hash_table_destroy(this->overload_table
, NULL
);
165 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
168 ralloc_steal(mem_ctx
, this->deref_head
);
169 return this->deref_head
;
172 static nir_constant
*
173 constant_copy(ir_constant
*ir
, void *mem_ctx
)
178 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
180 unsigned total_elems
= ir
->type
->components();
182 switch (ir
->type
->base_type
) {
184 for (i
= 0; i
< total_elems
; i
++)
185 ret
->value
.u
[i
] = ir
->value
.u
[i
];
189 for (i
= 0; i
< total_elems
; i
++)
190 ret
->value
.i
[i
] = ir
->value
.i
[i
];
193 case GLSL_TYPE_FLOAT
:
194 for (i
= 0; i
< total_elems
; i
++)
195 ret
->value
.f
[i
] = ir
->value
.f
[i
];
199 for (i
= 0; i
< total_elems
; i
++)
200 ret
->value
.b
[i
] = ir
->value
.b
[i
];
203 case GLSL_TYPE_STRUCT
:
204 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
207 foreach_in_list(ir_constant
, field
, &ir
->components
) {
208 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
213 case GLSL_TYPE_ARRAY
:
214 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
217 for (i
= 0; i
< ir
->type
->length
; i
++)
218 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
222 unreachable("not reached");
229 nir_visitor::visit(ir_variable
*ir
)
231 nir_variable
*var
= ralloc(shader
, nir_variable
);
232 var
->type
= ir
->type
;
233 var
->name
= ralloc_strdup(var
, ir
->name
);
235 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
236 unsigned size
= ir
->get_interface_type()->length
;
237 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
238 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
239 size
* sizeof(unsigned));
241 var
->max_ifc_array_access
= NULL
;
244 var
->data
.read_only
= ir
->data
.read_only
;
245 var
->data
.centroid
= ir
->data
.centroid
;
246 var
->data
.sample
= ir
->data
.sample
;
247 var
->data
.invariant
= ir
->data
.invariant
;
248 var
->data
.location
= ir
->data
.location
;
250 switch(ir
->data
.mode
) {
252 case ir_var_temporary
:
254 var
->data
.mode
= nir_var_global
;
256 var
->data
.mode
= nir_var_local
;
259 case ir_var_function_in
:
260 case ir_var_function_out
:
261 case ir_var_function_inout
:
262 case ir_var_const_in
:
263 var
->data
.mode
= nir_var_local
;
266 case ir_var_shader_in
:
267 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
268 ir
->data
.location
== VARYING_SLOT_FACE
) {
269 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
270 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
271 var
->data
.mode
= nir_var_system_value
;
273 var
->data
.mode
= nir_var_shader_in
;
277 case ir_var_shader_out
:
278 var
->data
.mode
= nir_var_shader_out
;
282 var
->data
.mode
= nir_var_uniform
;
285 case ir_var_shader_storage
:
286 var
->data
.mode
= nir_var_shader_storage
;
289 case ir_var_system_value
:
290 var
->data
.mode
= nir_var_system_value
;
294 unreachable("not reached");
297 var
->data
.interpolation
= ir
->data
.interpolation
;
298 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
299 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
300 var
->data
.explicit_location
= ir
->data
.explicit_location
;
301 var
->data
.explicit_index
= ir
->data
.explicit_index
;
302 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
303 var
->data
.has_initializer
= ir
->data
.has_initializer
;
304 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
305 var
->data
.location_frac
= ir
->data
.location_frac
;
306 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
307 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
309 switch (ir
->data
.depth_layout
) {
310 case ir_depth_layout_none
:
311 var
->data
.depth_layout
= nir_depth_layout_none
;
313 case ir_depth_layout_any
:
314 var
->data
.depth_layout
= nir_depth_layout_any
;
316 case ir_depth_layout_greater
:
317 var
->data
.depth_layout
= nir_depth_layout_greater
;
319 case ir_depth_layout_less
:
320 var
->data
.depth_layout
= nir_depth_layout_less
;
322 case ir_depth_layout_unchanged
:
323 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
326 unreachable("not reached");
329 var
->data
.index
= ir
->data
.index
;
330 var
->data
.descriptor_set
= ir
->data
.set
;
331 var
->data
.binding
= ir
->data
.binding
;
332 /* XXX Get rid of buffer_index */
333 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
334 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
335 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
336 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
337 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
338 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
339 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
340 var
->data
.image
.format
= ir
->data
.image_format
;
341 var
->data
.max_array_access
= ir
->data
.max_array_access
;
343 var
->num_state_slots
= ir
->get_num_state_slots();
344 if (var
->num_state_slots
> 0) {
345 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
346 var
->num_state_slots
);
348 ir_state_slot
*state_slots
= ir
->get_state_slots();
349 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
350 for (unsigned j
= 0; j
< 5; j
++)
351 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
352 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
355 var
->state_slots
= NULL
;
358 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
360 var
->interface_type
= ir
->get_interface_type();
362 switch (var
->data
.mode
) {
364 exec_list_push_tail(&impl
->locals
, &var
->node
);
368 exec_list_push_tail(&shader
->globals
, &var
->node
);
371 case nir_var_shader_in
:
372 exec_list_push_tail(&shader
->inputs
, &var
->node
);
375 case nir_var_shader_out
:
376 exec_list_push_tail(&shader
->outputs
, &var
->node
);
379 case nir_var_uniform
:
380 case nir_var_shader_storage
:
381 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
384 case nir_var_system_value
:
385 exec_list_push_tail(&shader
->system_values
, &var
->node
);
389 unreachable("not reached");
392 _mesa_hash_table_insert(var_table
, ir
, var
);
397 nir_function_visitor::visit_enter(ir_function
*ir
)
399 visitor
->create_function(ir
);
400 return visit_continue_with_parent
;
405 nir_visitor::create_function(ir_function
*ir
)
407 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
408 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
409 create_overload(sig
, func
);
416 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
418 if (ir
->is_intrinsic
)
421 nir_function_overload
*overload
= nir_function_overload_create(function
);
423 unsigned num_params
= ir
->parameters
.length();
424 overload
->num_params
= num_params
;
425 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
428 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
429 switch (param
->data
.mode
) {
430 case ir_var_function_in
:
431 overload
->params
[i
].param_type
= nir_parameter_in
;
434 case ir_var_function_out
:
435 overload
->params
[i
].param_type
= nir_parameter_out
;
438 case ir_var_function_inout
:
439 overload
->params
[i
].param_type
= nir_parameter_inout
;
443 unreachable("not reached");
446 overload
->params
[i
].type
= param
->type
;
450 overload
->return_type
= ir
->return_type
;
452 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
456 nir_visitor::visit(ir_function
*ir
)
458 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
463 nir_visitor::visit(ir_function_signature
*ir
)
465 if (ir
->is_intrinsic
)
468 struct hash_entry
*entry
=
469 _mesa_hash_table_search(this->overload_table
, ir
);
472 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
474 if (ir
->is_defined
) {
475 nir_function_impl
*impl
= nir_function_impl_create(overload
);
478 unsigned num_params
= overload
->num_params
;
479 impl
->num_params
= num_params
;
480 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
482 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
484 impl
->params
[i
] = this->var
;
488 if (overload
->return_type
== glsl_type::void_type
) {
489 impl
->return_var
= NULL
;
491 impl
->return_var
= ralloc(this->shader
, nir_variable
);
492 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
494 impl
->return_var
->type
= overload
->return_type
;
497 this->is_global
= false;
499 this->cf_node_list
= &impl
->body
;
500 visit_exec_list(&ir
->body
, this);
502 this->is_global
= true;
504 overload
->impl
= NULL
;
509 nir_visitor::visit(ir_loop
*ir
)
511 exec_list
*old_list
= this->cf_node_list
;
513 nir_loop
*loop
= nir_loop_create(this->shader
);
514 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
515 this->cf_node_list
= &loop
->body
;
516 visit_exec_list(&ir
->body_instructions
, this);
518 this->cf_node_list
= old_list
;
522 nir_visitor::visit(ir_if
*ir
)
524 nir_src condition
= evaluate_rvalue(ir
->condition
);
526 exec_list
*old_list
= this->cf_node_list
;
528 nir_if
*if_stmt
= nir_if_create(this->shader
);
529 if_stmt
->condition
= condition
;
530 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
532 this->cf_node_list
= &if_stmt
->then_list
;
533 visit_exec_list(&ir
->then_instructions
, this);
535 this->cf_node_list
= &if_stmt
->else_list
;
536 visit_exec_list(&ir
->else_instructions
, this);
538 this->cf_node_list
= old_list
;
542 nir_visitor::visit(ir_discard
*ir
)
545 * discards aren't treated as control flow, because before we lower them
546 * they can appear anywhere in the shader and the stuff after them may still
547 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
548 * discards will be immediately followed by a return.
551 nir_intrinsic_instr
*discard
;
553 discard
= nir_intrinsic_instr_create(this->shader
,
554 nir_intrinsic_discard_if
);
555 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
557 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
559 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
563 nir_visitor::visit(ir_emit_vertex
*ir
)
565 nir_intrinsic_instr
*instr
=
566 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
567 instr
->const_index
[0] = ir
->stream_id();
568 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
572 nir_visitor::visit(ir_end_primitive
*ir
)
574 nir_intrinsic_instr
*instr
=
575 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
576 instr
->const_index
[0] = ir
->stream_id();
577 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
581 nir_visitor::visit(ir_loop_jump
*ir
)
585 case ir_loop_jump::jump_break
:
586 type
= nir_jump_break
;
588 case ir_loop_jump::jump_continue
:
589 type
= nir_jump_continue
;
592 unreachable("not reached");
595 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
596 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
600 nir_visitor::visit(ir_return
*ir
)
602 if (ir
->value
!= NULL
) {
603 nir_intrinsic_instr
*copy
=
604 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
606 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
607 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
610 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
611 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
615 nir_visitor::visit(ir_call
*ir
)
617 if (ir
->callee
->is_intrinsic
) {
619 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
620 op
= nir_intrinsic_atomic_counter_read_var
;
621 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
622 op
= nir_intrinsic_atomic_counter_inc_var
;
623 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
624 op
= nir_intrinsic_atomic_counter_dec_var
;
625 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
626 op
= nir_intrinsic_image_load
;
627 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
628 op
= nir_intrinsic_image_store
;
629 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
630 op
= nir_intrinsic_image_atomic_add
;
631 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
632 op
= nir_intrinsic_image_atomic_min
;
633 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
634 op
= nir_intrinsic_image_atomic_max
;
635 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
636 op
= nir_intrinsic_image_atomic_and
;
637 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
638 op
= nir_intrinsic_image_atomic_or
;
639 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
640 op
= nir_intrinsic_image_atomic_xor
;
641 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
642 op
= nir_intrinsic_image_atomic_exchange
;
643 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
644 op
= nir_intrinsic_image_atomic_comp_swap
;
645 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
646 op
= nir_intrinsic_memory_barrier
;
647 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
648 op
= nir_intrinsic_image_size
;
650 unreachable("not reached");
653 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
656 case nir_intrinsic_atomic_counter_read_var
:
657 case nir_intrinsic_atomic_counter_inc_var
:
658 case nir_intrinsic_atomic_counter_dec_var
: {
659 ir_dereference
*param
=
660 (ir_dereference
*) ir
->actual_parameters
.get_head();
661 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
662 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
665 case nir_intrinsic_image_load
:
666 case nir_intrinsic_image_store
:
667 case nir_intrinsic_image_atomic_add
:
668 case nir_intrinsic_image_atomic_min
:
669 case nir_intrinsic_image_atomic_max
:
670 case nir_intrinsic_image_atomic_and
:
671 case nir_intrinsic_image_atomic_or
:
672 case nir_intrinsic_image_atomic_xor
:
673 case nir_intrinsic_image_atomic_exchange
:
674 case nir_intrinsic_image_atomic_comp_swap
:
675 case nir_intrinsic_image_size
: {
676 nir_ssa_undef_instr
*instr_undef
=
677 nir_ssa_undef_instr_create(shader
, 1);
678 nir_instr_insert_after_cf_list(this->cf_node_list
,
679 &instr_undef
->instr
);
681 /* Set the image variable dereference. */
682 exec_node
*param
= ir
->actual_parameters
.get_head();
683 ir_dereference
*image
= (ir_dereference
*)param
;
684 const glsl_type
*type
=
685 image
->variable_referenced()->type
->without_array();
687 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
688 param
= param
->get_next();
690 /* Set the intrinsic destination. */
691 if (ir
->return_deref
) {
692 const nir_intrinsic_info
*info
=
693 &nir_intrinsic_infos
[instr
->intrinsic
];
694 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
695 info
->dest_components
, NULL
);
698 if (op
== nir_intrinsic_image_size
)
701 /* Set the address argument, extending the coordinate vector to four
704 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
705 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
706 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
708 for (int i
= 0; i
< 4; i
++) {
709 if (i
< type
->coordinate_components()) {
710 instr_addr
->src
[i
].src
= src_addr
;
711 instr_addr
->src
[i
].swizzle
[0] = i
;
713 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
717 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
718 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
719 param
= param
->get_next();
721 /* Set the sample argument, which is undefined for single-sample
724 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
725 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
726 param
= param
->get_next();
728 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
731 /* Set the intrinsic parameters. */
732 if (!param
->is_tail_sentinel()) {
733 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
734 param
= param
->get_next();
737 if (!param
->is_tail_sentinel()) {
738 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
739 param
= param
->get_next();
743 case nir_intrinsic_memory_barrier
:
746 unreachable("not reached");
749 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
751 if (ir
->return_deref
) {
752 nir_intrinsic_instr
*store_instr
=
753 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
754 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
756 store_instr
->variables
[0] =
757 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
758 store_instr
->src
[0] = nir_src_for_ssa(&instr
->dest
.ssa
);
760 nir_instr_insert_after_cf_list(this->cf_node_list
,
761 &store_instr
->instr
);
767 struct hash_entry
*entry
=
768 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
770 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
772 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
775 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
776 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
780 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
781 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
785 nir_visitor::visit(ir_assignment
*ir
)
787 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
789 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
790 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
791 /* We're doing a plain-as-can-be copy, so emit a copy_var */
792 nir_intrinsic_instr
*copy
=
793 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
795 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
796 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
799 nir_if
*if_stmt
= nir_if_create(this->shader
);
800 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
801 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
802 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
804 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
809 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
811 ir
->lhs
->accept(this);
812 nir_deref_var
*lhs_deref
= this->deref_head
;
813 nir_src src
= evaluate_rvalue(ir
->rhs
);
815 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
817 * We have no good way to update only part of a variable, so just load
818 * the LHS and do a vec operation to combine the old with the new, and
820 * back into the LHS. Copy propagation should get rid of the mess.
823 nir_intrinsic_instr
*load
=
824 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
825 load
->num_components
= ir
->lhs
->type
->vector_elements
;
826 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
827 load
->variables
[0] = lhs_deref
;
828 ralloc_steal(load
, load
->variables
[0]);
829 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
832 switch (ir
->lhs
->type
->vector_elements
) {
833 case 1: vec_op
= nir_op_imov
; break;
834 case 2: vec_op
= nir_op_vec2
; break;
835 case 3: vec_op
= nir_op_vec3
; break;
836 case 4: vec_op
= nir_op_vec4
; break;
837 default: unreachable("Invalid number of components"); break;
839 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
840 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
841 vec
->dest
.write_mask
= (1 << num_components
) - 1;
843 unsigned component
= 0;
844 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
845 if (ir
->write_mask
& (1 << i
)) {
846 vec
->src
[i
].src
= src
;
848 /* GLSL IR will give us the input to the write-masked assignment
849 * in a single packed vector. So, for example, if the
850 * writemask is xzw, then we have to swizzle x -> x, y -> z,
851 * and z -> w and get the y component from the load.
853 vec
->src
[i
].swizzle
[0] = component
++;
855 vec
->src
[i
].src
.is_ssa
= true;
856 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
857 vec
->src
[i
].swizzle
[0] = i
;
861 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
864 src
.ssa
= &vec
->dest
.dest
.ssa
;
867 nir_intrinsic_instr
*store
=
868 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
869 store
->num_components
= ir
->lhs
->type
->vector_elements
;
870 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
871 store
->variables
[0] = nir_deref_as_var(store_deref
);
875 nir_if
*if_stmt
= nir_if_create(this->shader
);
876 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
877 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
878 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
880 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
885 * Given an instruction, returns a pointer to its destination or NULL if there
888 * Note that this only handles instructions we generate at this level.
891 get_instr_dest(nir_instr
*instr
)
893 nir_alu_instr
*alu_instr
;
894 nir_intrinsic_instr
*intrinsic_instr
;
895 nir_tex_instr
*tex_instr
;
897 switch (instr
->type
) {
898 case nir_instr_type_alu
:
899 alu_instr
= nir_instr_as_alu(instr
);
900 return &alu_instr
->dest
.dest
;
902 case nir_instr_type_intrinsic
:
903 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
904 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
905 return &intrinsic_instr
->dest
;
909 case nir_instr_type_tex
:
910 tex_instr
= nir_instr_as_tex(instr
);
911 return &tex_instr
->dest
;
914 unreachable("not reached");
921 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
923 nir_dest
*dest
= get_instr_dest(instr
);
925 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
927 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
928 this->result
= instr
;
932 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
935 if (ir
->as_dereference() || ir
->as_constant()) {
937 * A dereference is being used on the right hand side, which means we
938 * must emit a variable load.
941 nir_intrinsic_instr
*load_instr
=
942 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
943 load_instr
->num_components
= ir
->type
->vector_elements
;
944 load_instr
->variables
[0] = this->deref_head
;
945 ralloc_steal(load_instr
, load_instr
->variables
[0]);
946 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
949 nir_dest
*dest
= get_instr_dest(this->result
);
950 assert(dest
->is_ssa
);
952 return nir_src_for_ssa(&dest
->ssa
);
956 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
958 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
959 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
960 instr
->src
[i
].src
= srcs
[i
];
961 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
962 add_instr(&instr
->instr
, dest_size
);
967 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
969 assert(nir_op_infos
[op
].num_inputs
== 1);
970 return emit(op
, dest_size
, &src1
);
974 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
977 assert(nir_op_infos
[op
].num_inputs
== 2);
978 nir_src srcs
[] = { src1
, src2
};
979 return emit(op
, dest_size
, srcs
);
983 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
984 nir_src src2
, nir_src src3
)
986 assert(nir_op_infos
[op
].num_inputs
== 3);
987 nir_src srcs
[] = { src1
, src2
, src3
};
988 return emit(op
, dest_size
, srcs
);
992 nir_visitor::visit(ir_expression
*ir
)
994 /* Some special cases */
995 switch (ir
->operation
) {
996 case ir_binop_ubo_load
: {
997 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1001 op
= nir_intrinsic_load_ubo
;
1003 op
= nir_intrinsic_load_ubo_indirect
;
1006 ir_constant
*const_block
= ir
->operands
[0]->as_constant();
1007 assert(const_block
&& "can't figure out descriptor set index");
1008 unsigned index
= const_block
->value
.u
[0];
1009 unsigned set
= sh
->UniformBlocks
[index
].Set
;
1010 unsigned binding
= sh
->UniformBlocks
[index
].Binding
;
1012 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1013 load
->num_components
= ir
->type
->vector_elements
;
1014 load
->const_index
[0] = set
;
1015 load
->const_index
[1] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1016 nir_load_const_instr
*load_binding
= nir_load_const_instr_create(shader
, 1);
1017 load_binding
->value
.u
[0] = binding
;
1018 nir_instr_insert_after_cf_list(this->cf_node_list
, &load_binding
->instr
);
1019 load
->src
[0] = nir_src_for_ssa(&load_binding
->def
);
1021 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1022 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1025 * In UBO's, a true boolean value is any non-zero value, but we consider
1026 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1029 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1030 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1031 const_zero
->value
.u
[0] = 0;
1032 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1034 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1035 compare
->src
[0].src
.is_ssa
= true;
1036 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1037 compare
->src
[1].src
.is_ssa
= true;
1038 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1039 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1040 compare
->src
[1].swizzle
[i
] = 0;
1041 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1043 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1049 case ir_unop_interpolate_at_centroid
:
1050 case ir_binop_interpolate_at_offset
:
1051 case ir_binop_interpolate_at_sample
: {
1052 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1053 ir_swizzle
*swizzle
= NULL
;
1055 /* the api does not allow a swizzle here, but the varying packing code
1056 * may have pushed one into here.
1058 swizzle
= ir
->operands
[0]->as_swizzle();
1060 deref
= swizzle
->val
->as_dereference();
1064 deref
->accept(this);
1066 nir_intrinsic_op op
;
1067 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1068 switch (ir
->operation
) {
1069 case ir_unop_interpolate_at_centroid
:
1070 op
= nir_intrinsic_interp_var_at_centroid
;
1072 case ir_binop_interpolate_at_offset
:
1073 op
= nir_intrinsic_interp_var_at_offset
;
1075 case ir_binop_interpolate_at_sample
:
1076 op
= nir_intrinsic_interp_var_at_sample
;
1079 unreachable("Invalid interpolation intrinsic");
1082 /* This case can happen if the vertex shader does not write the
1083 * given varying. In this case, the linker will lower it to a
1084 * global variable. Since interpolating a variable makes no
1085 * sense, we'll just turn it into a load which will probably
1086 * eventually end up as an SSA definition.
1088 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1089 op
= nir_intrinsic_load_var
;
1092 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1093 intrin
->num_components
= deref
->type
->vector_elements
;
1094 intrin
->variables
[0] = this->deref_head
;
1095 ralloc_steal(intrin
, intrin
->variables
[0]);
1097 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1098 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1099 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1101 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1104 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1105 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1106 mov
->src
[0].src
.is_ssa
= true;
1107 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1109 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1110 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1111 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1112 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1113 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1114 mov
->src
[0].swizzle
[i
] = 0;
1116 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1127 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1128 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1130 glsl_base_type types
[4];
1131 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1133 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1135 types
[i
] = GLSL_TYPE_FLOAT
;
1137 glsl_base_type out_type
;
1139 out_type
= ir
->type
->base_type
;
1141 out_type
= GLSL_TYPE_FLOAT
;
1143 unsigned dest_size
= ir
->type
->vector_elements
;
1145 nir_alu_instr
*instr
;
1148 switch (ir
->operation
) {
1149 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1150 case ir_unop_logic_not
:
1151 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1154 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1158 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1161 case ir_unop_saturate
:
1162 assert(types
[0] == GLSL_TYPE_FLOAT
);
1163 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1166 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1169 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1170 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1171 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1172 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1173 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1174 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1175 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1177 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1180 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1183 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1185 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1186 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1187 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1188 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1189 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1192 case ir_unop_bitcast_i2f
:
1193 case ir_unop_bitcast_f2i
:
1194 case ir_unop_bitcast_u2f
:
1195 case ir_unop_bitcast_f2u
:
1196 case ir_unop_subroutine_to_int
:
1198 emit(nir_op_imov
, dest_size
, srcs
);
1201 switch (ir
->operands
[0]->type
->vector_elements
) {
1203 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1207 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1211 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1215 unreachable("not reached");
1218 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1219 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1220 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1221 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1222 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1223 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1224 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1225 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1226 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1227 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1228 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1229 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1230 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1231 case ir_unop_pack_snorm_2x16
:
1232 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1234 case ir_unop_pack_snorm_4x8
:
1235 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1237 case ir_unop_pack_unorm_2x16
:
1238 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1240 case ir_unop_pack_unorm_4x8
:
1241 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1243 case ir_unop_pack_half_2x16
:
1244 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1246 case ir_unop_unpack_snorm_2x16
:
1247 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1249 case ir_unop_unpack_snorm_4x8
:
1250 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1252 case ir_unop_unpack_unorm_2x16
:
1253 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1255 case ir_unop_unpack_unorm_4x8
:
1256 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1258 case ir_unop_unpack_half_2x16
:
1259 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1261 case ir_unop_unpack_half_2x16_split_x
:
1262 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1264 case ir_unop_unpack_half_2x16_split_y
:
1265 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1267 case ir_unop_bitfield_reverse
:
1268 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1270 case ir_unop_bit_count
:
1271 emit(nir_op_bit_count
, dest_size
, srcs
);
1273 case ir_unop_find_msb
:
1275 case GLSL_TYPE_UINT
:
1276 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1279 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1282 unreachable("Invalid type for findMSB()");
1285 case ir_unop_find_lsb
:
1286 emit(nir_op_find_lsb
, dest_size
, srcs
);
1290 switch (ir
->type
->vector_elements
) {
1292 switch (ir
->operands
[0]->type
->vector_elements
) {
1293 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1294 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1295 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1296 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1297 default: unreachable("not reached");
1301 switch (ir
->operands
[0]->type
->vector_elements
) {
1302 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1303 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1304 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1305 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1306 default: unreachable("not reached");
1310 switch (ir
->operands
[0]->type
->vector_elements
) {
1311 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1312 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1313 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1314 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1315 default: unreachable("not reached");
1319 switch (ir
->operands
[0]->type
->vector_elements
) {
1320 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1321 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1322 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1323 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1324 default: unreachable("not reached");
1328 unreachable("not reached");
1339 case ir_binop_bit_and
:
1340 case ir_binop_bit_or
:
1341 case ir_binop_bit_xor
:
1342 case ir_binop_logic_and
:
1343 case ir_binop_logic_or
:
1344 case ir_binop_logic_xor
:
1345 case ir_binop_lshift
:
1346 case ir_binop_rshift
:
1347 switch (ir
->operation
) {
1349 if (out_type
== GLSL_TYPE_FLOAT
)
1355 if (out_type
== GLSL_TYPE_FLOAT
)
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
)
1381 if (out_type
== GLSL_TYPE_FLOAT
)
1383 else if (out_type
== GLSL_TYPE_INT
)
1389 if (out_type
== GLSL_TYPE_FLOAT
)
1391 else if (out_type
== GLSL_TYPE_INT
)
1396 case ir_binop_bit_and
:
1399 case ir_binop_bit_or
:
1402 case ir_binop_bit_xor
:
1405 case ir_binop_logic_and
:
1411 case ir_binop_logic_or
:
1417 case ir_binop_logic_xor
:
1423 case ir_binop_lshift
:
1426 case ir_binop_rshift
:
1427 if (out_type
== GLSL_TYPE_INT
)
1437 unreachable("not reached");
1440 instr
= emit(op
, dest_size
, srcs
);
1442 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1443 ir
->operands
[1]->type
->vector_elements
== 1) {
1444 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1446 instr
->src
[1].swizzle
[i
] = 0;
1450 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1451 ir
->operands
[0]->type
->vector_elements
== 1) {
1452 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1454 instr
->src
[0].swizzle
[i
] = 0;
1459 case ir_binop_imul_high
:
1460 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1463 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1464 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1466 if (supports_ints
) {
1467 if (types
[0] == GLSL_TYPE_FLOAT
)
1468 emit(nir_op_flt
, dest_size
, srcs
);
1469 else if (types
[0] == GLSL_TYPE_INT
)
1470 emit(nir_op_ilt
, dest_size
, srcs
);
1472 emit(nir_op_ult
, dest_size
, srcs
);
1474 emit(nir_op_slt
, dest_size
, srcs
);
1477 case ir_binop_greater
:
1478 if (supports_ints
) {
1479 if (types
[0] == GLSL_TYPE_FLOAT
)
1480 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1481 else if (types
[0] == GLSL_TYPE_INT
)
1482 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1484 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1486 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1489 case ir_binop_lequal
:
1490 if (supports_ints
) {
1491 if (types
[0] == GLSL_TYPE_FLOAT
)
1492 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1493 else if (types
[0] == GLSL_TYPE_INT
)
1494 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1496 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1498 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1501 case ir_binop_gequal
:
1502 if (supports_ints
) {
1503 if (types
[0] == GLSL_TYPE_FLOAT
)
1504 emit(nir_op_fge
, dest_size
, srcs
);
1505 else if (types
[0] == GLSL_TYPE_INT
)
1506 emit(nir_op_ige
, dest_size
, srcs
);
1508 emit(nir_op_uge
, dest_size
, srcs
);
1510 emit(nir_op_slt
, dest_size
, srcs
);
1513 case ir_binop_equal
:
1514 if (supports_ints
) {
1515 if (types
[0] == GLSL_TYPE_FLOAT
)
1516 emit(nir_op_feq
, dest_size
, srcs
);
1518 emit(nir_op_ieq
, dest_size
, srcs
);
1520 emit(nir_op_seq
, dest_size
, srcs
);
1523 case ir_binop_nequal
:
1524 if (supports_ints
) {
1525 if (types
[0] == GLSL_TYPE_FLOAT
)
1526 emit(nir_op_fne
, dest_size
, srcs
);
1528 emit(nir_op_ine
, dest_size
, srcs
);
1530 emit(nir_op_sne
, dest_size
, srcs
);
1533 case ir_binop_all_equal
:
1534 if (supports_ints
) {
1535 if (types
[0] == GLSL_TYPE_FLOAT
) {
1536 switch (ir
->operands
[0]->type
->vector_elements
) {
1537 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1538 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1539 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1540 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1542 unreachable("not reached");
1545 switch (ir
->operands
[0]->type
->vector_elements
) {
1546 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1547 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1548 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1549 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1551 unreachable("not reached");
1555 switch (ir
->operands
[0]->type
->vector_elements
) {
1556 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1557 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1558 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1559 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1561 unreachable("not reached");
1565 case ir_binop_any_nequal
:
1566 if (supports_ints
) {
1567 if (types
[0] == GLSL_TYPE_FLOAT
) {
1568 switch (ir
->operands
[0]->type
->vector_elements
) {
1569 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1570 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1571 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1572 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1574 unreachable("not reached");
1577 switch (ir
->operands
[0]->type
->vector_elements
) {
1578 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1579 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1580 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1581 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1583 unreachable("not reached");
1587 switch (ir
->operands
[0]->type
->vector_elements
) {
1588 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1589 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1590 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1591 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1593 unreachable("not reached");
1598 switch (ir
->operands
[0]->type
->vector_elements
) {
1599 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1600 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1601 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1603 unreachable("not reached");
1607 case ir_binop_pack_half_2x16_split
:
1608 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1610 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1611 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1612 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1614 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1615 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1616 ir
->operands
[2]->type
->vector_elements
== 1) {
1617 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1619 instr
->src
[2].swizzle
[i
] = 0;
1625 emit(nir_op_bcsel
, dest_size
, srcs
);
1627 emit(nir_op_fcsel
, dest_size
, srcs
);
1630 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1631 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1632 instr
->src
[0].swizzle
[i
] = 0;
1635 case ir_triop_bitfield_extract
:
1636 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1637 nir_op_ubitfield_extract
, dest_size
, srcs
);
1638 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1639 instr
->src
[1].swizzle
[i
] = 0;
1640 instr
->src
[2].swizzle
[i
] = 0;
1643 case ir_quadop_bitfield_insert
:
1644 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1645 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1646 instr
->src
[2].swizzle
[i
] = 0;
1647 instr
->src
[3].swizzle
[i
] = 0;
1650 case ir_quadop_vector
:
1651 switch (ir
->type
->vector_elements
) {
1652 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1653 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1654 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1655 default: unreachable("not reached");
1660 unreachable("not reached");
1665 nir_visitor::visit(ir_swizzle
*ir
)
1667 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1668 ir
->type
->vector_elements
,
1669 evaluate_rvalue(ir
->val
));
1671 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1672 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1673 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1677 nir_visitor::visit(ir_texture
*ir
)
1684 num_srcs
= 1; /* coordinate */
1689 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1690 num_srcs
= 2; /* coordinate, bias/lod */
1694 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1700 if (ir
->lod_info
.lod
!= NULL
)
1701 num_srcs
= 2; /* coordinate, lod */
1703 num_srcs
= 1; /* coordinate */
1707 op
= nir_texop_txf_ms
;
1708 num_srcs
= 2; /* coordinate, sample_index */
1713 if (ir
->lod_info
.lod
!= NULL
)
1714 num_srcs
= 1; /* lod */
1721 num_srcs
= 1; /* coordinate */
1726 num_srcs
= 1; /* coordinate */
1729 case ir_query_levels
:
1730 op
= nir_texop_query_levels
;
1735 unreachable("not reached");
1738 if (ir
->projector
!= NULL
)
1740 if (ir
->shadow_comparitor
!= NULL
)
1742 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1745 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1748 instr
->sampler_dim
=
1749 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1750 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1751 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1752 if (instr
->is_shadow
)
1753 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1754 switch (ir
->type
->base_type
) {
1755 case GLSL_TYPE_FLOAT
:
1756 instr
->dest_type
= nir_type_float
;
1759 instr
->dest_type
= nir_type_int
;
1761 case GLSL_TYPE_UINT
:
1762 instr
->dest_type
= nir_type_unsigned
;
1765 unreachable("not reached");
1768 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1770 unsigned src_number
= 0;
1772 if (ir
->coordinate
!= NULL
) {
1773 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1774 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1775 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1779 if (ir
->projector
!= NULL
) {
1780 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1781 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1785 if (ir
->shadow_comparitor
!= NULL
) {
1786 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1787 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1791 if (ir
->offset
!= NULL
) {
1792 /* we don't support multiple offsets yet */
1793 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1795 ir_constant
*const_offset
= ir
->offset
->as_constant();
1796 if (const_offset
!= NULL
) {
1797 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1798 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1800 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1801 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1808 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1809 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1816 if (ir
->lod_info
.lod
!= NULL
) {
1817 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1818 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1824 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1825 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1827 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1828 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1833 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
1834 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1839 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1846 assert(src_number
== num_srcs
);
1848 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1852 nir_visitor::visit(ir_constant
*ir
)
1855 * We don't know if this variable is an an array or struct that gets
1856 * dereferenced, so do the safe thing an make it a variable with a
1857 * constant initializer and return a dereference.
1860 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1861 var
->name
= ralloc_strdup(var
, "const_temp");
1862 var
->type
= ir
->type
;
1863 var
->data
.mode
= nir_var_local
;
1864 var
->data
.read_only
= true;
1865 var
->constant_initializer
= constant_copy(ir
, var
);
1866 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1868 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1869 this->deref_tail
= &this->deref_head
->deref
;
1873 nir_visitor::visit(ir_dereference_variable
*ir
)
1875 struct hash_entry
*entry
=
1876 _mesa_hash_table_search(this->var_table
, ir
->var
);
1878 nir_variable
*var
= (nir_variable
*) entry
->data
;
1880 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1881 this->deref_head
= deref
;
1882 this->deref_tail
= &deref
->deref
;
1886 nir_visitor::visit(ir_dereference_record
*ir
)
1888 ir
->record
->accept(this);
1890 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1891 assert(field_index
>= 0);
1893 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1894 deref
->deref
.type
= ir
->type
;
1895 this->deref_tail
->child
= &deref
->deref
;
1896 this->deref_tail
= &deref
->deref
;
1900 nir_visitor::visit(ir_dereference_array
*ir
)
1902 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1903 deref
->deref
.type
= ir
->type
;
1905 ir_constant
*const_index
= ir
->array_index
->as_constant();
1906 if (const_index
!= NULL
) {
1907 deref
->deref_array_type
= nir_deref_array_type_direct
;
1908 deref
->base_offset
= const_index
->value
.u
[0];
1910 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1911 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1914 ir
->array
->accept(this);
1916 this->deref_tail
->child
= &deref
->deref
;
1917 ralloc_steal(this->deref_tail
, deref
);
1918 this->deref_tail
= &deref
->deref
;
1922 nir_visitor::visit(ir_barrier
*ir
)
1924 nir_intrinsic_instr
*instr
=
1925 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
1926 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);