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 "ir_visitor.h"
30 #include "ir_hierarchical_visitor.h"
34 * pass to lower GLSL IR to NIR
36 * This will lower variable dereferences to loads/stores of corresponding
37 * variables in NIR - the variables will be converted to registers in a later
43 class nir_visitor
: public ir_visitor
46 nir_visitor(nir_shader
*shader
, struct gl_shader
*sh
, gl_shader_stage stage
);
49 virtual void visit(ir_variable
*);
50 virtual void visit(ir_function
*);
51 virtual void visit(ir_function_signature
*);
52 virtual void visit(ir_loop
*);
53 virtual void visit(ir_if
*);
54 virtual void visit(ir_discard
*);
55 virtual void visit(ir_loop_jump
*);
56 virtual void visit(ir_return
*);
57 virtual void visit(ir_call
*);
58 virtual void visit(ir_assignment
*);
59 virtual void visit(ir_emit_vertex
*);
60 virtual void visit(ir_end_primitive
*);
61 virtual void visit(ir_expression
*);
62 virtual void visit(ir_swizzle
*);
63 virtual void visit(ir_texture
*);
64 virtual void visit(ir_constant
*);
65 virtual void visit(ir_dereference_variable
*);
66 virtual void visit(ir_dereference_record
*);
67 virtual void visit(ir_dereference_array
*);
68 virtual void visit(ir_barrier
*);
70 void create_function(ir_function
*ir
);
73 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
74 void add_instr(nir_instr
*instr
, unsigned num_components
);
75 nir_src
evaluate_rvalue(ir_rvalue
*ir
);
77 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
);
78 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
);
79 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
81 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
82 nir_src src2
, nir_src src3
);
89 gl_shader_stage stage
;
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
, options
);
138 nir_visitor
v1(shader
, sh
, sh
->Stage
);
139 nir_function_visitor
v2(&v1
);
141 visit_exec_list(sh
->ir
, &v1
);
146 nir_visitor::nir_visitor(nir_shader
*shader
, struct gl_shader
*sh
,
147 gl_shader_stage stage
)
149 this->supports_ints
= shader
->options
->native_integers
;
150 this->shader
= shader
;
153 this->is_global
= true;
154 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
155 _mesa_key_pointer_equal
);
156 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
157 _mesa_key_pointer_equal
);
160 nir_visitor::~nir_visitor()
162 _mesa_hash_table_destroy(this->var_table
, NULL
);
163 _mesa_hash_table_destroy(this->overload_table
, NULL
);
167 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
170 ralloc_steal(mem_ctx
, this->deref_head
);
171 return this->deref_head
;
174 static nir_constant
*
175 constant_copy(ir_constant
*ir
, void *mem_ctx
)
180 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
182 unsigned total_elems
= ir
->type
->components();
184 switch (ir
->type
->base_type
) {
186 for (i
= 0; i
< total_elems
; i
++)
187 ret
->value
.u
[i
] = ir
->value
.u
[i
];
191 for (i
= 0; i
< total_elems
; i
++)
192 ret
->value
.i
[i
] = ir
->value
.i
[i
];
195 case GLSL_TYPE_FLOAT
:
196 for (i
= 0; i
< total_elems
; i
++)
197 ret
->value
.f
[i
] = ir
->value
.f
[i
];
201 for (i
= 0; i
< total_elems
; i
++)
202 ret
->value
.b
[i
] = ir
->value
.b
[i
];
205 case GLSL_TYPE_STRUCT
:
206 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
209 foreach_in_list(ir_constant
, field
, &ir
->components
) {
210 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
215 case GLSL_TYPE_ARRAY
:
216 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
219 for (i
= 0; i
< ir
->type
->length
; i
++)
220 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
224 unreachable("not reached");
231 nir_visitor::visit(ir_variable
*ir
)
233 nir_variable
*var
= ralloc(shader
, nir_variable
);
234 var
->type
= ir
->type
;
235 var
->name
= ralloc_strdup(var
, ir
->name
);
237 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
238 unsigned size
= ir
->get_interface_type()->length
;
239 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
240 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
241 size
* sizeof(unsigned));
243 var
->max_ifc_array_access
= NULL
;
246 var
->data
.read_only
= ir
->data
.read_only
;
247 var
->data
.centroid
= ir
->data
.centroid
;
248 var
->data
.sample
= ir
->data
.sample
;
249 var
->data
.invariant
= ir
->data
.invariant
;
250 var
->data
.location
= ir
->data
.location
;
252 switch(ir
->data
.mode
) {
254 case ir_var_temporary
:
256 var
->data
.mode
= nir_var_global
;
258 var
->data
.mode
= nir_var_local
;
261 case ir_var_function_in
:
262 case ir_var_function_out
:
263 case ir_var_function_inout
:
264 case ir_var_const_in
:
265 var
->data
.mode
= nir_var_local
;
268 case ir_var_shader_in
:
269 if (stage
== MESA_SHADER_FRAGMENT
&&
270 ir
->data
.location
== VARYING_SLOT_FACE
) {
271 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
272 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
273 var
->data
.mode
= nir_var_system_value
;
275 var
->data
.mode
= nir_var_shader_in
;
279 case ir_var_shader_out
:
280 var
->data
.mode
= nir_var_shader_out
;
284 var
->data
.mode
= nir_var_uniform
;
288 case ir_var_system_value
:
289 var
->data
.mode
= nir_var_system_value
;
293 unreachable("not reached");
296 var
->data
.interpolation
= ir
->data
.interpolation
;
297 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
298 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
299 var
->data
.explicit_location
= ir
->data
.explicit_location
;
300 var
->data
.explicit_index
= ir
->data
.explicit_index
;
301 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
302 var
->data
.has_initializer
= ir
->data
.has_initializer
;
303 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
304 var
->data
.location_frac
= ir
->data
.location_frac
;
305 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
306 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
308 switch (ir
->data
.depth_layout
) {
309 case ir_depth_layout_none
:
310 var
->data
.depth_layout
= nir_depth_layout_none
;
312 case ir_depth_layout_any
:
313 var
->data
.depth_layout
= nir_depth_layout_any
;
315 case ir_depth_layout_greater
:
316 var
->data
.depth_layout
= nir_depth_layout_greater
;
318 case ir_depth_layout_less
:
319 var
->data
.depth_layout
= nir_depth_layout_less
;
321 case ir_depth_layout_unchanged
:
322 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
325 unreachable("not reached");
328 var
->data
.index
= ir
->data
.index
;
329 var
->data
.descriptor_set
= ir
->data
.set
;
330 var
->data
.binding
= ir
->data
.binding
;
331 /* XXX Get rid of buffer_index */
332 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
333 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
334 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
335 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
336 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
337 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
338 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
339 var
->data
.image
.format
= ir
->data
.image_format
;
340 var
->data
.max_array_access
= ir
->data
.max_array_access
;
342 var
->num_state_slots
= ir
->get_num_state_slots();
343 if (var
->num_state_slots
> 0) {
344 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
345 var
->num_state_slots
);
347 ir_state_slot
*state_slots
= ir
->get_state_slots();
348 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
349 for (unsigned j
= 0; j
< 5; j
++)
350 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
351 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
354 var
->state_slots
= NULL
;
357 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
359 var
->interface_type
= ir
->get_interface_type();
361 switch (var
->data
.mode
) {
363 exec_list_push_tail(&impl
->locals
, &var
->node
);
367 exec_list_push_tail(&shader
->globals
, &var
->node
);
370 case nir_var_shader_in
:
371 exec_list_push_tail(&shader
->inputs
, &var
->node
);
374 case nir_var_shader_out
:
375 exec_list_push_tail(&shader
->outputs
, &var
->node
);
378 case nir_var_uniform
:
379 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
382 case nir_var_system_value
:
383 exec_list_push_tail(&shader
->system_values
, &var
->node
);
387 unreachable("not reached");
390 _mesa_hash_table_insert(var_table
, ir
, var
);
395 nir_function_visitor::visit_enter(ir_function
*ir
)
397 visitor
->create_function(ir
);
398 return visit_continue_with_parent
;
403 nir_visitor::create_function(ir_function
*ir
)
405 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
406 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
407 create_overload(sig
, func
);
414 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
416 if (ir
->is_intrinsic
)
419 nir_function_overload
*overload
= nir_function_overload_create(function
);
421 unsigned num_params
= ir
->parameters
.length();
422 overload
->num_params
= num_params
;
423 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
426 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
427 switch (param
->data
.mode
) {
428 case ir_var_function_in
:
429 overload
->params
[i
].param_type
= nir_parameter_in
;
432 case ir_var_function_out
:
433 overload
->params
[i
].param_type
= nir_parameter_out
;
436 case ir_var_function_inout
:
437 overload
->params
[i
].param_type
= nir_parameter_inout
;
441 unreachable("not reached");
444 overload
->params
[i
].type
= param
->type
;
448 overload
->return_type
= ir
->return_type
;
450 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
454 nir_visitor::visit(ir_function
*ir
)
456 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
461 nir_visitor::visit(ir_function_signature
*ir
)
463 if (ir
->is_intrinsic
)
466 struct hash_entry
*entry
=
467 _mesa_hash_table_search(this->overload_table
, ir
);
470 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
472 if (ir
->is_defined
) {
473 nir_function_impl
*impl
= nir_function_impl_create(overload
);
476 unsigned num_params
= overload
->num_params
;
477 impl
->num_params
= num_params
;
478 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
480 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
482 impl
->params
[i
] = this->var
;
486 if (overload
->return_type
== glsl_type::void_type
) {
487 impl
->return_var
= NULL
;
489 impl
->return_var
= ralloc(this->shader
, nir_variable
);
490 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
492 impl
->return_var
->type
= overload
->return_type
;
495 this->is_global
= false;
497 this->cf_node_list
= &impl
->body
;
498 visit_exec_list(&ir
->body
, this);
500 this->is_global
= true;
502 overload
->impl
= NULL
;
507 nir_visitor::visit(ir_loop
*ir
)
509 exec_list
*old_list
= this->cf_node_list
;
511 nir_loop
*loop
= nir_loop_create(this->shader
);
512 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
513 this->cf_node_list
= &loop
->body
;
514 visit_exec_list(&ir
->body_instructions
, this);
516 this->cf_node_list
= old_list
;
520 nir_visitor::visit(ir_if
*ir
)
522 nir_src condition
= evaluate_rvalue(ir
->condition
);
524 exec_list
*old_list
= this->cf_node_list
;
526 nir_if
*if_stmt
= nir_if_create(this->shader
);
527 if_stmt
->condition
= condition
;
528 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
530 this->cf_node_list
= &if_stmt
->then_list
;
531 visit_exec_list(&ir
->then_instructions
, this);
533 this->cf_node_list
= &if_stmt
->else_list
;
534 visit_exec_list(&ir
->else_instructions
, this);
536 this->cf_node_list
= old_list
;
540 nir_visitor::visit(ir_discard
*ir
)
543 * discards aren't treated as control flow, because before we lower them
544 * they can appear anywhere in the shader and the stuff after them may still
545 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
546 * discards will be immediately followed by a return.
549 nir_intrinsic_instr
*discard
;
551 discard
= nir_intrinsic_instr_create(this->shader
,
552 nir_intrinsic_discard_if
);
553 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
555 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
557 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
561 nir_visitor::visit(ir_emit_vertex
*ir
)
563 nir_intrinsic_instr
*instr
=
564 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
565 instr
->const_index
[0] = ir
->stream_id();
566 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
570 nir_visitor::visit(ir_end_primitive
*ir
)
572 nir_intrinsic_instr
*instr
=
573 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
574 instr
->const_index
[0] = ir
->stream_id();
575 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
579 nir_visitor::visit(ir_loop_jump
*ir
)
583 case ir_loop_jump::jump_break
:
584 type
= nir_jump_break
;
586 case ir_loop_jump::jump_continue
:
587 type
= nir_jump_continue
;
590 unreachable("not reached");
593 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
594 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
598 nir_visitor::visit(ir_return
*ir
)
600 if (ir
->value
!= NULL
) {
601 nir_intrinsic_instr
*copy
=
602 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
604 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
605 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
608 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
609 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
613 nir_visitor::visit(ir_call
*ir
)
615 if (ir
->callee
->is_intrinsic
) {
617 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
618 op
= nir_intrinsic_atomic_counter_read_var
;
619 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
620 op
= nir_intrinsic_atomic_counter_inc_var
;
621 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
622 op
= nir_intrinsic_atomic_counter_dec_var
;
623 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
624 op
= nir_intrinsic_image_load
;
625 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
626 op
= nir_intrinsic_image_store
;
627 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
628 op
= nir_intrinsic_image_atomic_add
;
629 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
630 op
= nir_intrinsic_image_atomic_min
;
631 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
632 op
= nir_intrinsic_image_atomic_max
;
633 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
634 op
= nir_intrinsic_image_atomic_and
;
635 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
636 op
= nir_intrinsic_image_atomic_or
;
637 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
638 op
= nir_intrinsic_image_atomic_xor
;
639 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
640 op
= nir_intrinsic_image_atomic_exchange
;
641 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
642 op
= nir_intrinsic_image_atomic_comp_swap
;
643 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
644 op
= nir_intrinsic_memory_barrier
;
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 nir_ssa_undef_instr
*instr_undef
=
672 nir_ssa_undef_instr_create(shader
, 1);
673 nir_instr_insert_after_cf_list(this->cf_node_list
,
674 &instr_undef
->instr
);
676 /* Set the image variable dereference. */
677 exec_node
*param
= ir
->actual_parameters
.get_head();
678 ir_dereference
*image
= (ir_dereference
*)param
;
679 const glsl_type
*type
=
680 image
->variable_referenced()->type
->without_array();
682 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
683 param
= param
->get_next();
685 /* Set the address argument, extending the coordinate vector to four
688 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
689 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
690 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
692 for (int i
= 0; i
< 4; i
++) {
693 if (i
< type
->coordinate_components()) {
694 instr_addr
->src
[i
].src
= src_addr
;
695 instr_addr
->src
[i
].swizzle
[0] = i
;
697 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
701 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
702 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
703 param
= param
->get_next();
705 /* Set the sample argument, which is undefined for single-sample
708 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
709 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
710 param
= param
->get_next();
712 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
715 /* Set the intrinsic parameters. */
716 if (!param
->is_tail_sentinel()) {
717 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
718 param
= param
->get_next();
721 if (!param
->is_tail_sentinel()) {
722 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
723 param
= param
->get_next();
726 /* Set the intrinsic destination. */
727 if (ir
->return_deref
)
728 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
729 ir
->return_deref
->type
->vector_elements
, NULL
);
732 case nir_intrinsic_memory_barrier
:
735 unreachable("not reached");
738 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
740 if (ir
->return_deref
) {
741 nir_intrinsic_instr
*store_instr
=
742 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
743 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
745 store_instr
->variables
[0] =
746 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
747 store_instr
->src
[0] = nir_src_for_ssa(&instr
->dest
.ssa
);
749 nir_instr_insert_after_cf_list(this->cf_node_list
,
750 &store_instr
->instr
);
756 struct hash_entry
*entry
=
757 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
759 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
761 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
764 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
765 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
769 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
770 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
774 nir_visitor::visit(ir_assignment
*ir
)
776 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
778 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
779 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
780 /* We're doing a plain-as-can-be copy, so emit a copy_var */
781 nir_intrinsic_instr
*copy
=
782 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
784 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
785 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
788 nir_if
*if_stmt
= nir_if_create(this->shader
);
789 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
790 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
791 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
793 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
798 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
800 ir
->lhs
->accept(this);
801 nir_deref_var
*lhs_deref
= this->deref_head
;
802 nir_src src
= evaluate_rvalue(ir
->rhs
);
804 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
806 * We have no good way to update only part of a variable, so just load
807 * the LHS and do a vec operation to combine the old with the new, and
809 * back into the LHS. Copy propagation should get rid of the mess.
812 nir_intrinsic_instr
*load
=
813 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
814 load
->num_components
= ir
->lhs
->type
->vector_elements
;
815 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
816 load
->variables
[0] = lhs_deref
;
817 ralloc_steal(load
, load
->variables
[0]);
818 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
821 switch (ir
->lhs
->type
->vector_elements
) {
822 case 1: vec_op
= nir_op_imov
; break;
823 case 2: vec_op
= nir_op_vec2
; break;
824 case 3: vec_op
= nir_op_vec3
; break;
825 case 4: vec_op
= nir_op_vec4
; break;
826 default: unreachable("Invalid number of components"); break;
828 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
829 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
830 vec
->dest
.write_mask
= (1 << num_components
) - 1;
832 unsigned component
= 0;
833 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
834 if (ir
->write_mask
& (1 << i
)) {
835 vec
->src
[i
].src
= src
;
837 /* GLSL IR will give us the input to the write-masked assignment
838 * in a single packed vector. So, for example, if the
839 * writemask is xzw, then we have to swizzle x -> x, y -> z,
840 * and z -> w and get the y component from the load.
842 vec
->src
[i
].swizzle
[0] = component
++;
844 vec
->src
[i
].src
.is_ssa
= true;
845 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
846 vec
->src
[i
].swizzle
[0] = i
;
850 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
853 src
.ssa
= &vec
->dest
.dest
.ssa
;
856 nir_intrinsic_instr
*store
=
857 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
858 store
->num_components
= ir
->lhs
->type
->vector_elements
;
859 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
860 store
->variables
[0] = nir_deref_as_var(store_deref
);
864 nir_if
*if_stmt
= nir_if_create(this->shader
);
865 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
866 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
867 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
869 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
874 * Given an instruction, returns a pointer to its destination or NULL if there
877 * Note that this only handles instructions we generate at this level.
880 get_instr_dest(nir_instr
*instr
)
882 nir_alu_instr
*alu_instr
;
883 nir_intrinsic_instr
*intrinsic_instr
;
884 nir_tex_instr
*tex_instr
;
886 switch (instr
->type
) {
887 case nir_instr_type_alu
:
888 alu_instr
= nir_instr_as_alu(instr
);
889 return &alu_instr
->dest
.dest
;
891 case nir_instr_type_intrinsic
:
892 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
893 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
894 return &intrinsic_instr
->dest
;
898 case nir_instr_type_tex
:
899 tex_instr
= nir_instr_as_tex(instr
);
900 return &tex_instr
->dest
;
903 unreachable("not reached");
910 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
912 nir_dest
*dest
= get_instr_dest(instr
);
914 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
916 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
917 this->result
= instr
;
921 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
924 if (ir
->as_dereference() || ir
->as_constant()) {
926 * A dereference is being used on the right hand side, which means we
927 * must emit a variable load.
930 nir_intrinsic_instr
*load_instr
=
931 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
932 load_instr
->num_components
= ir
->type
->vector_elements
;
933 load_instr
->variables
[0] = this->deref_head
;
934 ralloc_steal(load_instr
, load_instr
->variables
[0]);
935 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
938 nir_dest
*dest
= get_instr_dest(this->result
);
939 assert(dest
->is_ssa
);
941 return nir_src_for_ssa(&dest
->ssa
);
945 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
947 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
948 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
949 instr
->src
[i
].src
= srcs
[i
];
950 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
951 add_instr(&instr
->instr
, dest_size
);
956 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
958 assert(nir_op_infos
[op
].num_inputs
== 1);
959 return emit(op
, dest_size
, &src1
);
963 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
966 assert(nir_op_infos
[op
].num_inputs
== 2);
967 nir_src srcs
[] = { src1
, src2
};
968 return emit(op
, dest_size
, srcs
);
972 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
973 nir_src src2
, nir_src src3
)
975 assert(nir_op_infos
[op
].num_inputs
== 3);
976 nir_src srcs
[] = { src1
, src2
, src3
};
977 return emit(op
, dest_size
, srcs
);
981 nir_visitor::visit(ir_expression
*ir
)
983 /* Some special cases */
984 switch (ir
->operation
) {
985 case ir_binop_ubo_load
: {
986 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
990 op
= nir_intrinsic_load_ubo
;
992 op
= nir_intrinsic_load_ubo_indirect
;
995 ir_constant
*const_block
= ir
->operands
[0]->as_constant();
996 assert(const_block
&& "can't figure out descriptor set index");
997 unsigned index
= const_block
->value
.u
[0];
998 unsigned set
= sh
->UniformBlocks
[index
].Set
;
999 unsigned binding
= sh
->UniformBlocks
[index
].Binding
;
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] = set
;
1004 load
->const_index
[1] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1005 nir_load_const_instr
*load_binding
= nir_load_const_instr_create(shader
, 1);
1006 load_binding
->value
.u
[0] = binding
;
1007 nir_instr_insert_after_cf_list(this->cf_node_list
, &load_binding
->instr
);
1008 load
->src
[0] = nir_src_for_ssa(&load_binding
->def
);
1010 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1011 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1014 * In UBO's, a true boolean value is any non-zero value, but we consider
1015 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1018 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1019 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1020 const_zero
->value
.u
[0] = 0;
1021 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1023 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1024 compare
->src
[0].src
.is_ssa
= true;
1025 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1026 compare
->src
[1].src
.is_ssa
= true;
1027 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1028 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1029 compare
->src
[1].swizzle
[i
] = 0;
1030 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1032 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1038 case ir_unop_interpolate_at_centroid
:
1039 case ir_binop_interpolate_at_offset
:
1040 case ir_binop_interpolate_at_sample
: {
1041 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1042 ir_swizzle
*swizzle
= NULL
;
1044 /* the api does not allow a swizzle here, but the varying packing code
1045 * may have pushed one into here.
1047 swizzle
= ir
->operands
[0]->as_swizzle();
1049 deref
= swizzle
->val
->as_dereference();
1053 deref
->accept(this);
1055 nir_intrinsic_op op
;
1056 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1057 switch (ir
->operation
) {
1058 case ir_unop_interpolate_at_centroid
:
1059 op
= nir_intrinsic_interp_var_at_centroid
;
1061 case ir_binop_interpolate_at_offset
:
1062 op
= nir_intrinsic_interp_var_at_offset
;
1064 case ir_binop_interpolate_at_sample
:
1065 op
= nir_intrinsic_interp_var_at_sample
;
1068 unreachable("Invalid interpolation intrinsic");
1071 /* This case can happen if the vertex shader does not write the
1072 * given varying. In this case, the linker will lower it to a
1073 * global variable. Since interpolating a variable makes no
1074 * sense, we'll just turn it into a load which will probably
1075 * eventually end up as an SSA definition.
1077 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1078 op
= nir_intrinsic_load_var
;
1081 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1082 intrin
->num_components
= deref
->type
->vector_elements
;
1083 intrin
->variables
[0] = this->deref_head
;
1084 ralloc_steal(intrin
, intrin
->variables
[0]);
1086 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1087 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1088 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1090 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1093 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1094 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1095 mov
->src
[0].src
.is_ssa
= true;
1096 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1098 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1099 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1100 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1101 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1102 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1103 mov
->src
[0].swizzle
[i
] = 0;
1105 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1116 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1117 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1119 glsl_base_type types
[4];
1120 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1122 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1124 types
[i
] = GLSL_TYPE_FLOAT
;
1126 glsl_base_type out_type
;
1128 out_type
= ir
->type
->base_type
;
1130 out_type
= GLSL_TYPE_FLOAT
;
1132 unsigned dest_size
= ir
->type
->vector_elements
;
1134 nir_alu_instr
*instr
;
1137 switch (ir
->operation
) {
1138 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1139 case ir_unop_logic_not
:
1140 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1143 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1147 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1150 case ir_unop_saturate
:
1151 assert(types
[0] == GLSL_TYPE_FLOAT
);
1152 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1155 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1158 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1159 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1160 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1161 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1162 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1163 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1164 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1166 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1169 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1172 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1174 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1175 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1176 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1177 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1178 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1181 case ir_unop_bitcast_i2f
:
1182 case ir_unop_bitcast_f2i
:
1183 case ir_unop_bitcast_u2f
:
1184 case ir_unop_bitcast_f2u
:
1186 emit(nir_op_imov
, dest_size
, srcs
);
1189 switch (ir
->operands
[0]->type
->vector_elements
) {
1191 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1195 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1199 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1203 unreachable("not reached");
1206 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1207 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1208 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1209 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1210 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1211 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1212 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1213 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1214 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1215 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1216 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1217 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1218 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1219 case ir_unop_pack_snorm_2x16
:
1220 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1222 case ir_unop_pack_snorm_4x8
:
1223 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1225 case ir_unop_pack_unorm_2x16
:
1226 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1228 case ir_unop_pack_unorm_4x8
:
1229 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1231 case ir_unop_pack_half_2x16
:
1232 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1234 case ir_unop_unpack_snorm_2x16
:
1235 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1237 case ir_unop_unpack_snorm_4x8
:
1238 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1240 case ir_unop_unpack_unorm_2x16
:
1241 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1243 case ir_unop_unpack_unorm_4x8
:
1244 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1246 case ir_unop_unpack_half_2x16
:
1247 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1249 case ir_unop_unpack_half_2x16_split_x
:
1250 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1252 case ir_unop_unpack_half_2x16_split_y
:
1253 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1255 case ir_unop_bitfield_reverse
:
1256 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1258 case ir_unop_bit_count
:
1259 emit(nir_op_bit_count
, dest_size
, srcs
);
1261 case ir_unop_find_msb
:
1263 case GLSL_TYPE_UINT
:
1264 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1267 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1270 unreachable("Invalid type for findMSB()");
1273 case ir_unop_find_lsb
:
1274 emit(nir_op_find_lsb
, dest_size
, srcs
);
1278 switch (ir
->type
->vector_elements
) {
1280 switch (ir
->operands
[0]->type
->vector_elements
) {
1281 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1282 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1283 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1284 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1285 default: unreachable("not reached");
1289 switch (ir
->operands
[0]->type
->vector_elements
) {
1290 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1291 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1292 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1293 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1294 default: unreachable("not reached");
1298 switch (ir
->operands
[0]->type
->vector_elements
) {
1299 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1300 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1301 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1302 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1303 default: unreachable("not reached");
1307 switch (ir
->operands
[0]->type
->vector_elements
) {
1308 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1309 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1310 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1311 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1312 default: unreachable("not reached");
1316 unreachable("not reached");
1327 case ir_binop_bit_and
:
1328 case ir_binop_bit_or
:
1329 case ir_binop_bit_xor
:
1330 case ir_binop_logic_and
:
1331 case ir_binop_logic_or
:
1332 case ir_binop_logic_xor
:
1333 case ir_binop_lshift
:
1334 case ir_binop_rshift
:
1335 switch (ir
->operation
) {
1337 if (out_type
== GLSL_TYPE_FLOAT
)
1343 if (out_type
== GLSL_TYPE_FLOAT
)
1349 if (out_type
== GLSL_TYPE_FLOAT
)
1355 if (out_type
== GLSL_TYPE_FLOAT
)
1357 else if (out_type
== GLSL_TYPE_INT
)
1363 if (out_type
== GLSL_TYPE_FLOAT
)
1369 if (out_type
== GLSL_TYPE_FLOAT
)
1371 else if (out_type
== GLSL_TYPE_INT
)
1377 if (out_type
== GLSL_TYPE_FLOAT
)
1379 else if (out_type
== GLSL_TYPE_INT
)
1384 case ir_binop_bit_and
:
1387 case ir_binop_bit_or
:
1390 case ir_binop_bit_xor
:
1393 case ir_binop_logic_and
:
1399 case ir_binop_logic_or
:
1405 case ir_binop_logic_xor
:
1411 case ir_binop_lshift
:
1414 case ir_binop_rshift
:
1415 if (out_type
== GLSL_TYPE_INT
)
1425 unreachable("not reached");
1428 instr
= emit(op
, dest_size
, srcs
);
1430 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1431 ir
->operands
[1]->type
->vector_elements
== 1) {
1432 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1434 instr
->src
[1].swizzle
[i
] = 0;
1438 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1439 ir
->operands
[0]->type
->vector_elements
== 1) {
1440 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1442 instr
->src
[0].swizzle
[i
] = 0;
1447 case ir_binop_imul_high
:
1448 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1451 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1452 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1454 if (supports_ints
) {
1455 if (types
[0] == GLSL_TYPE_FLOAT
)
1456 emit(nir_op_flt
, dest_size
, srcs
);
1457 else if (types
[0] == GLSL_TYPE_INT
)
1458 emit(nir_op_ilt
, dest_size
, srcs
);
1460 emit(nir_op_ult
, dest_size
, srcs
);
1462 emit(nir_op_slt
, dest_size
, srcs
);
1465 case ir_binop_greater
:
1466 if (supports_ints
) {
1467 if (types
[0] == GLSL_TYPE_FLOAT
)
1468 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1469 else if (types
[0] == GLSL_TYPE_INT
)
1470 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1472 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1474 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1477 case ir_binop_lequal
:
1478 if (supports_ints
) {
1479 if (types
[0] == GLSL_TYPE_FLOAT
)
1480 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1481 else if (types
[0] == GLSL_TYPE_INT
)
1482 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1484 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1486 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1489 case ir_binop_gequal
:
1490 if (supports_ints
) {
1491 if (types
[0] == GLSL_TYPE_FLOAT
)
1492 emit(nir_op_fge
, dest_size
, srcs
);
1493 else if (types
[0] == GLSL_TYPE_INT
)
1494 emit(nir_op_ige
, dest_size
, srcs
);
1496 emit(nir_op_uge
, dest_size
, srcs
);
1498 emit(nir_op_slt
, dest_size
, srcs
);
1501 case ir_binop_equal
:
1502 if (supports_ints
) {
1503 if (types
[0] == GLSL_TYPE_FLOAT
)
1504 emit(nir_op_feq
, dest_size
, srcs
);
1506 emit(nir_op_ieq
, dest_size
, srcs
);
1508 emit(nir_op_seq
, dest_size
, srcs
);
1511 case ir_binop_nequal
:
1512 if (supports_ints
) {
1513 if (types
[0] == GLSL_TYPE_FLOAT
)
1514 emit(nir_op_fne
, dest_size
, srcs
);
1516 emit(nir_op_ine
, dest_size
, srcs
);
1518 emit(nir_op_sne
, dest_size
, srcs
);
1521 case ir_binop_all_equal
:
1522 if (supports_ints
) {
1523 if (types
[0] == GLSL_TYPE_FLOAT
) {
1524 switch (ir
->operands
[0]->type
->vector_elements
) {
1525 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1526 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1527 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1528 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1530 unreachable("not reached");
1533 switch (ir
->operands
[0]->type
->vector_elements
) {
1534 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1535 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1536 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1537 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1539 unreachable("not reached");
1543 switch (ir
->operands
[0]->type
->vector_elements
) {
1544 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1545 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1546 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1547 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1549 unreachable("not reached");
1553 case ir_binop_any_nequal
:
1554 if (supports_ints
) {
1555 if (types
[0] == GLSL_TYPE_FLOAT
) {
1556 switch (ir
->operands
[0]->type
->vector_elements
) {
1557 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1558 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1559 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1560 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1562 unreachable("not reached");
1565 switch (ir
->operands
[0]->type
->vector_elements
) {
1566 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1567 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1568 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1569 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1571 unreachable("not reached");
1575 switch (ir
->operands
[0]->type
->vector_elements
) {
1576 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1577 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1578 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1579 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1581 unreachable("not reached");
1586 switch (ir
->operands
[0]->type
->vector_elements
) {
1587 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1588 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1589 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1591 unreachable("not reached");
1595 case ir_binop_pack_half_2x16_split
:
1596 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1598 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1599 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1600 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1602 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1603 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1604 ir
->operands
[2]->type
->vector_elements
== 1) {
1605 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1607 instr
->src
[2].swizzle
[i
] = 0;
1613 emit(nir_op_bcsel
, dest_size
, srcs
);
1615 emit(nir_op_fcsel
, dest_size
, srcs
);
1618 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1619 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1620 instr
->src
[0].swizzle
[i
] = 0;
1623 case ir_triop_bitfield_extract
:
1624 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1625 nir_op_ubitfield_extract
, dest_size
, srcs
);
1626 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1627 instr
->src
[1].swizzle
[i
] = 0;
1628 instr
->src
[2].swizzle
[i
] = 0;
1631 case ir_quadop_bitfield_insert
:
1632 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1633 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1634 instr
->src
[2].swizzle
[i
] = 0;
1635 instr
->src
[3].swizzle
[i
] = 0;
1638 case ir_quadop_vector
:
1639 switch (ir
->type
->vector_elements
) {
1640 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1641 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1642 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1643 default: unreachable("not reached");
1648 unreachable("not reached");
1653 nir_visitor::visit(ir_swizzle
*ir
)
1655 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1656 ir
->type
->vector_elements
,
1657 evaluate_rvalue(ir
->val
));
1659 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1660 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1661 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1665 nir_visitor::visit(ir_texture
*ir
)
1672 num_srcs
= 1; /* coordinate */
1677 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1678 num_srcs
= 2; /* coordinate, bias/lod */
1682 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1688 if (ir
->lod_info
.lod
!= NULL
)
1689 num_srcs
= 2; /* coordinate, lod */
1691 num_srcs
= 1; /* coordinate */
1695 op
= nir_texop_txf_ms
;
1696 num_srcs
= 2; /* coordinate, sample_index */
1701 if (ir
->lod_info
.lod
!= NULL
)
1702 num_srcs
= 1; /* lod */
1709 num_srcs
= 1; /* coordinate */
1714 num_srcs
= 1; /* coordinate */
1717 case ir_query_levels
:
1718 op
= nir_texop_query_levels
;
1723 unreachable("not reached");
1726 if (ir
->projector
!= NULL
)
1728 if (ir
->shadow_comparitor
!= NULL
)
1730 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1733 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1736 instr
->sampler_dim
=
1737 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1738 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1739 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1740 if (instr
->is_shadow
)
1741 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1742 switch (ir
->type
->base_type
) {
1743 case GLSL_TYPE_FLOAT
:
1744 instr
->dest_type
= nir_type_float
;
1747 instr
->dest_type
= nir_type_int
;
1749 case GLSL_TYPE_UINT
:
1750 instr
->dest_type
= nir_type_unsigned
;
1753 unreachable("not reached");
1756 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1758 unsigned src_number
= 0;
1760 if (ir
->coordinate
!= NULL
) {
1761 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1762 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1763 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1767 if (ir
->projector
!= NULL
) {
1768 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1769 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1773 if (ir
->shadow_comparitor
!= NULL
) {
1774 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1775 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1779 if (ir
->offset
!= NULL
) {
1780 /* we don't support multiple offsets yet */
1781 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1783 ir_constant
*const_offset
= ir
->offset
->as_constant();
1784 if (const_offset
!= NULL
) {
1785 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1786 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1788 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1789 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1796 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1797 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1804 if (ir
->lod_info
.lod
!= NULL
) {
1805 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1806 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1812 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1813 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1815 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1816 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1821 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
1822 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1827 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1834 assert(src_number
== num_srcs
);
1836 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1840 nir_visitor::visit(ir_constant
*ir
)
1843 * We don't know if this variable is an an array or struct that gets
1844 * dereferenced, so do the safe thing an make it a variable with a
1845 * constant initializer and return a dereference.
1848 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1849 var
->name
= ralloc_strdup(var
, "const_temp");
1850 var
->type
= ir
->type
;
1851 var
->data
.mode
= nir_var_local
;
1852 var
->data
.read_only
= true;
1853 var
->constant_initializer
= constant_copy(ir
, var
);
1854 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1856 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1857 this->deref_tail
= &this->deref_head
->deref
;
1861 nir_visitor::visit(ir_dereference_variable
*ir
)
1863 struct hash_entry
*entry
=
1864 _mesa_hash_table_search(this->var_table
, ir
->var
);
1866 nir_variable
*var
= (nir_variable
*) entry
->data
;
1868 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1869 this->deref_head
= deref
;
1870 this->deref_tail
= &deref
->deref
;
1874 nir_visitor::visit(ir_dereference_record
*ir
)
1876 ir
->record
->accept(this);
1878 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1879 assert(field_index
>= 0);
1881 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1882 deref
->deref
.type
= ir
->type
;
1883 this->deref_tail
->child
= &deref
->deref
;
1884 this->deref_tail
= &deref
->deref
;
1888 nir_visitor::visit(ir_dereference_array
*ir
)
1890 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1891 deref
->deref
.type
= ir
->type
;
1893 ir_constant
*const_index
= ir
->array_index
->as_constant();
1894 if (const_index
!= NULL
) {
1895 deref
->deref_array_type
= nir_deref_array_type_direct
;
1896 deref
->base_offset
= const_index
->value
.u
[0];
1898 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1899 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1902 ir
->array
->accept(this);
1904 this->deref_tail
->child
= &deref
->deref
;
1905 ralloc_steal(this->deref_tail
, deref
);
1906 this->deref_tail
= &deref
->deref
;
1910 nir_visitor::visit(ir_barrier
*ir
)
1912 nir_intrinsic_instr
*instr
=
1913 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
1914 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);