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
, 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
);
87 gl_shader_stage stage
;
88 nir_function_impl
*impl
;
89 exec_list
*cf_node_list
;
90 nir_instr
*result
; /* result of the expression tree last visited */
92 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
94 /* the head of the dereference chain we're creating */
95 nir_deref_var
*deref_head
;
96 /* the tail of the dereference chain we're creating */
97 nir_deref
*deref_tail
;
99 nir_variable
*var
; /* variable created by ir_variable visitor */
101 /* whether the IR we're operating on is per-function or global */
104 /* map of ir_variable -> nir_variable */
105 struct hash_table
*var_table
;
107 /* map of ir_function_signature -> nir_function_overload */
108 struct hash_table
*overload_table
;
112 * This visitor runs before the main visitor, calling create_function() for
113 * each function so that the main visitor can resolve forward references in
117 class nir_function_visitor
: public ir_hierarchical_visitor
120 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
123 virtual ir_visitor_status
visit_enter(ir_function
*);
126 nir_visitor
*visitor
;
129 }; /* end of anonymous namespace */
132 glsl_to_nir(struct gl_shader
*sh
, const nir_shader_compiler_options
*options
)
134 nir_shader
*shader
= nir_shader_create(NULL
, options
);
136 nir_visitor
v1(shader
, sh
->Stage
);
137 nir_function_visitor
v2(&v1
);
139 visit_exec_list(sh
->ir
, &v1
);
144 nir_visitor::nir_visitor(nir_shader
*shader
, gl_shader_stage stage
)
146 this->supports_ints
= shader
->options
->native_integers
;
147 this->shader
= shader
;
149 this->is_global
= true;
150 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
151 _mesa_key_pointer_equal
);
152 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
153 _mesa_key_pointer_equal
);
156 nir_visitor::~nir_visitor()
158 _mesa_hash_table_destroy(this->var_table
, NULL
);
159 _mesa_hash_table_destroy(this->overload_table
, NULL
);
163 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
166 ralloc_steal(mem_ctx
, this->deref_head
);
167 return this->deref_head
;
170 static nir_constant
*
171 constant_copy(ir_constant
*ir
, void *mem_ctx
)
176 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
178 unsigned total_elems
= ir
->type
->components();
180 switch (ir
->type
->base_type
) {
182 for (i
= 0; i
< total_elems
; i
++)
183 ret
->value
.u
[i
] = ir
->value
.u
[i
];
187 for (i
= 0; i
< total_elems
; i
++)
188 ret
->value
.i
[i
] = ir
->value
.i
[i
];
191 case GLSL_TYPE_FLOAT
:
192 for (i
= 0; i
< total_elems
; i
++)
193 ret
->value
.f
[i
] = ir
->value
.f
[i
];
197 for (i
= 0; i
< total_elems
; i
++)
198 ret
->value
.b
[i
] = ir
->value
.b
[i
];
201 case GLSL_TYPE_STRUCT
:
202 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
205 foreach_in_list(ir_constant
, field
, &ir
->components
) {
206 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
211 case GLSL_TYPE_ARRAY
:
212 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
215 for (i
= 0; i
< ir
->type
->length
; i
++)
216 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
220 unreachable("not reached");
227 nir_visitor::visit(ir_variable
*ir
)
229 nir_variable
*var
= ralloc(shader
, nir_variable
);
230 var
->type
= ir
->type
;
231 var
->name
= ralloc_strdup(var
, ir
->name
);
233 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
234 unsigned size
= ir
->get_interface_type()->length
;
235 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
236 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
237 size
* sizeof(unsigned));
239 var
->max_ifc_array_access
= NULL
;
242 var
->data
.read_only
= ir
->data
.read_only
;
243 var
->data
.centroid
= ir
->data
.centroid
;
244 var
->data
.sample
= ir
->data
.sample
;
245 var
->data
.invariant
= ir
->data
.invariant
;
246 var
->data
.location
= ir
->data
.location
;
248 switch(ir
->data
.mode
) {
250 case ir_var_temporary
:
252 var
->data
.mode
= nir_var_global
;
254 var
->data
.mode
= nir_var_local
;
257 case ir_var_function_in
:
258 case ir_var_function_out
:
259 case ir_var_function_inout
:
260 case ir_var_const_in
:
261 var
->data
.mode
= nir_var_local
;
264 case ir_var_shader_in
:
265 if (stage
== MESA_SHADER_FRAGMENT
&&
266 ir
->data
.location
== VARYING_SLOT_FACE
) {
267 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
268 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
269 var
->data
.mode
= nir_var_system_value
;
271 var
->data
.mode
= nir_var_shader_in
;
275 case ir_var_shader_out
:
276 var
->data
.mode
= nir_var_shader_out
;
280 var
->data
.mode
= nir_var_uniform
;
283 case ir_var_shader_storage
:
284 var
->data
.mode
= nir_var_shader_storage
;
287 case ir_var_system_value
:
288 var
->data
.mode
= nir_var_system_value
;
292 unreachable("not reached");
295 var
->data
.interpolation
= ir
->data
.interpolation
;
296 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
297 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
298 var
->data
.explicit_location
= ir
->data
.explicit_location
;
299 var
->data
.explicit_index
= ir
->data
.explicit_index
;
300 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
301 var
->data
.has_initializer
= ir
->data
.has_initializer
;
302 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
303 var
->data
.location_frac
= ir
->data
.location_frac
;
304 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
305 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
307 switch (ir
->data
.depth_layout
) {
308 case ir_depth_layout_none
:
309 var
->data
.depth_layout
= nir_depth_layout_none
;
311 case ir_depth_layout_any
:
312 var
->data
.depth_layout
= nir_depth_layout_any
;
314 case ir_depth_layout_greater
:
315 var
->data
.depth_layout
= nir_depth_layout_greater
;
317 case ir_depth_layout_less
:
318 var
->data
.depth_layout
= nir_depth_layout_less
;
320 case ir_depth_layout_unchanged
:
321 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
324 unreachable("not reached");
327 var
->data
.index
= ir
->data
.index
;
328 var
->data
.binding
= ir
->data
.binding
;
329 /* XXX Get rid of buffer_index */
330 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
331 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
332 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
333 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
334 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
335 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
336 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
337 var
->data
.image
.format
= ir
->data
.image_format
;
338 var
->data
.max_array_access
= ir
->data
.max_array_access
;
340 var
->num_state_slots
= ir
->get_num_state_slots();
341 if (var
->num_state_slots
> 0) {
342 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
343 var
->num_state_slots
);
345 ir_state_slot
*state_slots
= ir
->get_state_slots();
346 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
347 for (unsigned j
= 0; j
< 5; j
++)
348 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
349 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
352 var
->state_slots
= NULL
;
355 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
357 var
->interface_type
= ir
->get_interface_type();
359 switch (var
->data
.mode
) {
361 exec_list_push_tail(&impl
->locals
, &var
->node
);
365 exec_list_push_tail(&shader
->globals
, &var
->node
);
368 case nir_var_shader_in
:
369 exec_list_push_tail(&shader
->inputs
, &var
->node
);
372 case nir_var_shader_out
:
373 exec_list_push_tail(&shader
->outputs
, &var
->node
);
376 case nir_var_uniform
:
377 case nir_var_shader_storage
:
378 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
381 case nir_var_system_value
:
382 exec_list_push_tail(&shader
->system_values
, &var
->node
);
386 unreachable("not reached");
389 _mesa_hash_table_insert(var_table
, ir
, var
);
394 nir_function_visitor::visit_enter(ir_function
*ir
)
396 visitor
->create_function(ir
);
397 return visit_continue_with_parent
;
402 nir_visitor::create_function(ir_function
*ir
)
404 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
405 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
406 create_overload(sig
, func
);
413 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
415 if (ir
->is_intrinsic
)
418 nir_function_overload
*overload
= nir_function_overload_create(function
);
420 unsigned num_params
= ir
->parameters
.length();
421 overload
->num_params
= num_params
;
422 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
425 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
426 switch (param
->data
.mode
) {
427 case ir_var_function_in
:
428 overload
->params
[i
].param_type
= nir_parameter_in
;
431 case ir_var_function_out
:
432 overload
->params
[i
].param_type
= nir_parameter_out
;
435 case ir_var_function_inout
:
436 overload
->params
[i
].param_type
= nir_parameter_inout
;
440 unreachable("not reached");
443 overload
->params
[i
].type
= param
->type
;
447 overload
->return_type
= ir
->return_type
;
449 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
453 nir_visitor::visit(ir_function
*ir
)
455 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
460 nir_visitor::visit(ir_function_signature
*ir
)
462 if (ir
->is_intrinsic
)
465 struct hash_entry
*entry
=
466 _mesa_hash_table_search(this->overload_table
, ir
);
469 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
471 if (ir
->is_defined
) {
472 nir_function_impl
*impl
= nir_function_impl_create(overload
);
475 unsigned num_params
= overload
->num_params
;
476 impl
->num_params
= num_params
;
477 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
479 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
481 impl
->params
[i
] = this->var
;
485 if (overload
->return_type
== glsl_type::void_type
) {
486 impl
->return_var
= NULL
;
488 impl
->return_var
= ralloc(this->shader
, nir_variable
);
489 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
491 impl
->return_var
->type
= overload
->return_type
;
494 this->is_global
= false;
496 this->cf_node_list
= &impl
->body
;
497 visit_exec_list(&ir
->body
, this);
499 this->is_global
= true;
501 overload
->impl
= NULL
;
506 nir_visitor::visit(ir_loop
*ir
)
508 exec_list
*old_list
= this->cf_node_list
;
510 nir_loop
*loop
= nir_loop_create(this->shader
);
511 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
512 this->cf_node_list
= &loop
->body
;
513 visit_exec_list(&ir
->body_instructions
, this);
515 this->cf_node_list
= old_list
;
519 nir_visitor::visit(ir_if
*ir
)
521 nir_src condition
= evaluate_rvalue(ir
->condition
);
523 exec_list
*old_list
= this->cf_node_list
;
525 nir_if
*if_stmt
= nir_if_create(this->shader
);
526 if_stmt
->condition
= condition
;
527 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
529 this->cf_node_list
= &if_stmt
->then_list
;
530 visit_exec_list(&ir
->then_instructions
, this);
532 this->cf_node_list
= &if_stmt
->else_list
;
533 visit_exec_list(&ir
->else_instructions
, this);
535 this->cf_node_list
= old_list
;
539 nir_visitor::visit(ir_discard
*ir
)
542 * discards aren't treated as control flow, because before we lower them
543 * they can appear anywhere in the shader and the stuff after them may still
544 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
545 * discards will be immediately followed by a return.
548 nir_intrinsic_instr
*discard
;
550 discard
= nir_intrinsic_instr_create(this->shader
,
551 nir_intrinsic_discard_if
);
552 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
554 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
556 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
560 nir_visitor::visit(ir_emit_vertex
*ir
)
562 nir_intrinsic_instr
*instr
=
563 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
564 instr
->const_index
[0] = ir
->stream_id();
565 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
569 nir_visitor::visit(ir_end_primitive
*ir
)
571 nir_intrinsic_instr
*instr
=
572 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
573 instr
->const_index
[0] = ir
->stream_id();
574 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
578 nir_visitor::visit(ir_loop_jump
*ir
)
582 case ir_loop_jump::jump_break
:
583 type
= nir_jump_break
;
585 case ir_loop_jump::jump_continue
:
586 type
= nir_jump_continue
;
589 unreachable("not reached");
592 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
593 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
597 nir_visitor::visit(ir_return
*ir
)
599 if (ir
->value
!= NULL
) {
600 nir_intrinsic_instr
*copy
=
601 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
603 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
604 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
607 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
608 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
612 nir_visitor::visit(ir_call
*ir
)
614 if (ir
->callee
->is_intrinsic
) {
616 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
617 op
= nir_intrinsic_atomic_counter_read_var
;
618 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
619 op
= nir_intrinsic_atomic_counter_inc_var
;
620 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
621 op
= nir_intrinsic_atomic_counter_dec_var
;
622 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
623 op
= nir_intrinsic_image_load
;
624 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
625 op
= nir_intrinsic_image_store
;
626 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
627 op
= nir_intrinsic_image_atomic_add
;
628 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
629 op
= nir_intrinsic_image_atomic_min
;
630 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
631 op
= nir_intrinsic_image_atomic_max
;
632 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
633 op
= nir_intrinsic_image_atomic_and
;
634 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
635 op
= nir_intrinsic_image_atomic_or
;
636 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
637 op
= nir_intrinsic_image_atomic_xor
;
638 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
639 op
= nir_intrinsic_image_atomic_exchange
;
640 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
641 op
= nir_intrinsic_image_atomic_comp_swap
;
642 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
643 op
= nir_intrinsic_memory_barrier
;
645 unreachable("not reached");
648 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
651 case nir_intrinsic_atomic_counter_read_var
:
652 case nir_intrinsic_atomic_counter_inc_var
:
653 case nir_intrinsic_atomic_counter_dec_var
: {
654 ir_dereference
*param
=
655 (ir_dereference
*) ir
->actual_parameters
.get_head();
656 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
657 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
660 case nir_intrinsic_image_load
:
661 case nir_intrinsic_image_store
:
662 case nir_intrinsic_image_atomic_add
:
663 case nir_intrinsic_image_atomic_min
:
664 case nir_intrinsic_image_atomic_max
:
665 case nir_intrinsic_image_atomic_and
:
666 case nir_intrinsic_image_atomic_or
:
667 case nir_intrinsic_image_atomic_xor
:
668 case nir_intrinsic_image_atomic_exchange
:
669 case nir_intrinsic_image_atomic_comp_swap
: {
670 nir_ssa_undef_instr
*instr_undef
=
671 nir_ssa_undef_instr_create(shader
, 1);
672 nir_instr_insert_after_cf_list(this->cf_node_list
,
673 &instr_undef
->instr
);
675 /* Set the image variable dereference. */
676 exec_node
*param
= ir
->actual_parameters
.get_head();
677 ir_dereference
*image
= (ir_dereference
*)param
;
678 const glsl_type
*type
=
679 image
->variable_referenced()->type
->without_array();
681 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
682 param
= param
->get_next();
684 /* Set the address argument, extending the coordinate vector to four
687 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
688 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
689 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
691 for (int i
= 0; i
< 4; i
++) {
692 if (i
< type
->coordinate_components()) {
693 instr_addr
->src
[i
].src
= src_addr
;
694 instr_addr
->src
[i
].swizzle
[0] = i
;
696 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
700 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
701 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
702 param
= param
->get_next();
704 /* Set the sample argument, which is undefined for single-sample
707 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
708 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
709 param
= param
->get_next();
711 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
714 /* Set the intrinsic parameters. */
715 if (!param
->is_tail_sentinel()) {
716 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
717 param
= param
->get_next();
720 if (!param
->is_tail_sentinel()) {
721 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
722 param
= param
->get_next();
725 /* Set the intrinsic destination. */
726 if (ir
->return_deref
)
727 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
728 ir
->return_deref
->type
->vector_elements
, NULL
);
731 case nir_intrinsic_memory_barrier
:
734 unreachable("not reached");
737 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
739 if (ir
->return_deref
) {
740 nir_intrinsic_instr
*store_instr
=
741 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
742 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
744 store_instr
->variables
[0] =
745 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
746 store_instr
->src
[0] = nir_src_for_ssa(&instr
->dest
.ssa
);
748 nir_instr_insert_after_cf_list(this->cf_node_list
,
749 &store_instr
->instr
);
755 struct hash_entry
*entry
=
756 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
758 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
760 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
763 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
764 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
768 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
769 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
773 nir_visitor::visit(ir_assignment
*ir
)
775 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
777 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
778 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
779 /* We're doing a plain-as-can-be copy, so emit a copy_var */
780 nir_intrinsic_instr
*copy
=
781 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
783 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
784 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
787 nir_if
*if_stmt
= nir_if_create(this->shader
);
788 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
789 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
790 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
792 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
797 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
799 ir
->lhs
->accept(this);
800 nir_deref_var
*lhs_deref
= this->deref_head
;
801 nir_src src
= evaluate_rvalue(ir
->rhs
);
803 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
805 * We have no good way to update only part of a variable, so just load
806 * the LHS and do a vec operation to combine the old with the new, and
808 * back into the LHS. Copy propagation should get rid of the mess.
811 nir_intrinsic_instr
*load
=
812 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
813 load
->num_components
= ir
->lhs
->type
->vector_elements
;
814 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
815 load
->variables
[0] = lhs_deref
;
816 ralloc_steal(load
, load
->variables
[0]);
817 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
820 switch (ir
->lhs
->type
->vector_elements
) {
821 case 1: vec_op
= nir_op_imov
; break;
822 case 2: vec_op
= nir_op_vec2
; break;
823 case 3: vec_op
= nir_op_vec3
; break;
824 case 4: vec_op
= nir_op_vec4
; break;
825 default: unreachable("Invalid number of components"); break;
827 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
828 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
829 vec
->dest
.write_mask
= (1 << num_components
) - 1;
831 unsigned component
= 0;
832 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
833 if (ir
->write_mask
& (1 << i
)) {
834 vec
->src
[i
].src
= src
;
836 /* GLSL IR will give us the input to the write-masked assignment
837 * in a single packed vector. So, for example, if the
838 * writemask is xzw, then we have to swizzle x -> x, y -> z,
839 * and z -> w and get the y component from the load.
841 vec
->src
[i
].swizzle
[0] = component
++;
843 vec
->src
[i
].src
.is_ssa
= true;
844 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
845 vec
->src
[i
].swizzle
[0] = i
;
849 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
852 src
.ssa
= &vec
->dest
.dest
.ssa
;
855 nir_intrinsic_instr
*store
=
856 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
857 store
->num_components
= ir
->lhs
->type
->vector_elements
;
858 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
859 store
->variables
[0] = nir_deref_as_var(store_deref
);
863 nir_if
*if_stmt
= nir_if_create(this->shader
);
864 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
865 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
866 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
868 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
873 * Given an instruction, returns a pointer to its destination or NULL if there
876 * Note that this only handles instructions we generate at this level.
879 get_instr_dest(nir_instr
*instr
)
881 nir_alu_instr
*alu_instr
;
882 nir_intrinsic_instr
*intrinsic_instr
;
883 nir_tex_instr
*tex_instr
;
885 switch (instr
->type
) {
886 case nir_instr_type_alu
:
887 alu_instr
= nir_instr_as_alu(instr
);
888 return &alu_instr
->dest
.dest
;
890 case nir_instr_type_intrinsic
:
891 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
892 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
893 return &intrinsic_instr
->dest
;
897 case nir_instr_type_tex
:
898 tex_instr
= nir_instr_as_tex(instr
);
899 return &tex_instr
->dest
;
902 unreachable("not reached");
909 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
911 nir_dest
*dest
= get_instr_dest(instr
);
913 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
915 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
916 this->result
= instr
;
920 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
923 if (ir
->as_dereference() || ir
->as_constant()) {
925 * A dereference is being used on the right hand side, which means we
926 * must emit a variable load.
929 nir_intrinsic_instr
*load_instr
=
930 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
931 load_instr
->num_components
= ir
->type
->vector_elements
;
932 load_instr
->variables
[0] = this->deref_head
;
933 ralloc_steal(load_instr
, load_instr
->variables
[0]);
934 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
937 nir_dest
*dest
= get_instr_dest(this->result
);
938 assert(dest
->is_ssa
);
940 return nir_src_for_ssa(&dest
->ssa
);
944 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
946 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
947 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
948 instr
->src
[i
].src
= srcs
[i
];
949 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
950 add_instr(&instr
->instr
, dest_size
);
955 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
957 assert(nir_op_infos
[op
].num_inputs
== 1);
958 return emit(op
, dest_size
, &src1
);
962 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
965 assert(nir_op_infos
[op
].num_inputs
== 2);
966 nir_src srcs
[] = { src1
, src2
};
967 return emit(op
, dest_size
, srcs
);
971 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
972 nir_src src2
, nir_src src3
)
974 assert(nir_op_infos
[op
].num_inputs
== 3);
975 nir_src srcs
[] = { src1
, src2
, src3
};
976 return emit(op
, dest_size
, srcs
);
980 nir_visitor::visit(ir_expression
*ir
)
982 /* Some special cases */
983 switch (ir
->operation
) {
984 case ir_binop_ubo_load
: {
985 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
989 op
= nir_intrinsic_load_ubo
;
991 op
= nir_intrinsic_load_ubo_indirect
;
993 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
994 load
->num_components
= ir
->type
->vector_elements
;
995 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
996 load
->const_index
[1] = 1; /* number of vec4's */
997 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
999 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1000 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1003 * In UBO's, a true boolean value is any non-zero value, but we consider
1004 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1007 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1008 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1009 const_zero
->value
.u
[0] = 0;
1010 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1012 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1013 compare
->src
[0].src
.is_ssa
= true;
1014 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1015 compare
->src
[1].src
.is_ssa
= true;
1016 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1017 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1018 compare
->src
[1].swizzle
[i
] = 0;
1019 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1021 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1027 case ir_unop_interpolate_at_centroid
:
1028 case ir_binop_interpolate_at_offset
:
1029 case ir_binop_interpolate_at_sample
: {
1030 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1031 ir_swizzle
*swizzle
= NULL
;
1033 /* the api does not allow a swizzle here, but the varying packing code
1034 * may have pushed one into here.
1036 swizzle
= ir
->operands
[0]->as_swizzle();
1038 deref
= swizzle
->val
->as_dereference();
1042 deref
->accept(this);
1044 nir_intrinsic_op op
;
1045 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1046 switch (ir
->operation
) {
1047 case ir_unop_interpolate_at_centroid
:
1048 op
= nir_intrinsic_interp_var_at_centroid
;
1050 case ir_binop_interpolate_at_offset
:
1051 op
= nir_intrinsic_interp_var_at_offset
;
1053 case ir_binop_interpolate_at_sample
:
1054 op
= nir_intrinsic_interp_var_at_sample
;
1057 unreachable("Invalid interpolation intrinsic");
1060 /* This case can happen if the vertex shader does not write the
1061 * given varying. In this case, the linker will lower it to a
1062 * global variable. Since interpolating a variable makes no
1063 * sense, we'll just turn it into a load which will probably
1064 * eventually end up as an SSA definition.
1066 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1067 op
= nir_intrinsic_load_var
;
1070 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1071 intrin
->num_components
= deref
->type
->vector_elements
;
1072 intrin
->variables
[0] = this->deref_head
;
1073 ralloc_steal(intrin
, intrin
->variables
[0]);
1075 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1076 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1077 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1079 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1082 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1083 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1084 mov
->src
[0].src
.is_ssa
= true;
1085 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1087 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1088 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1089 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1090 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1091 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1092 mov
->src
[0].swizzle
[i
] = 0;
1094 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1105 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1106 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1108 glsl_base_type types
[4];
1109 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1111 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1113 types
[i
] = GLSL_TYPE_FLOAT
;
1115 glsl_base_type out_type
;
1117 out_type
= ir
->type
->base_type
;
1119 out_type
= GLSL_TYPE_FLOAT
;
1121 unsigned dest_size
= ir
->type
->vector_elements
;
1123 nir_alu_instr
*instr
;
1126 switch (ir
->operation
) {
1127 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1128 case ir_unop_logic_not
:
1129 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1132 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1136 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1139 case ir_unop_saturate
:
1140 assert(types
[0] == GLSL_TYPE_FLOAT
);
1141 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1144 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1147 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1148 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1149 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1150 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1151 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1152 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1153 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1155 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1158 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1161 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1163 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1164 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1165 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1166 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1167 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1170 case ir_unop_bitcast_i2f
:
1171 case ir_unop_bitcast_f2i
:
1172 case ir_unop_bitcast_u2f
:
1173 case ir_unop_bitcast_f2u
:
1174 case ir_unop_subroutine_to_int
:
1176 emit(nir_op_imov
, dest_size
, srcs
);
1179 switch (ir
->operands
[0]->type
->vector_elements
) {
1181 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1185 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1189 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1193 unreachable("not reached");
1196 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1197 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1198 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1199 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1200 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1201 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1202 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1203 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1204 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1205 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1206 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1207 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1208 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1209 case ir_unop_pack_snorm_2x16
:
1210 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1212 case ir_unop_pack_snorm_4x8
:
1213 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1215 case ir_unop_pack_unorm_2x16
:
1216 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1218 case ir_unop_pack_unorm_4x8
:
1219 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1221 case ir_unop_pack_half_2x16
:
1222 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1224 case ir_unop_unpack_snorm_2x16
:
1225 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1227 case ir_unop_unpack_snorm_4x8
:
1228 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1230 case ir_unop_unpack_unorm_2x16
:
1231 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1233 case ir_unop_unpack_unorm_4x8
:
1234 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1236 case ir_unop_unpack_half_2x16
:
1237 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1239 case ir_unop_unpack_half_2x16_split_x
:
1240 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1242 case ir_unop_unpack_half_2x16_split_y
:
1243 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1245 case ir_unop_bitfield_reverse
:
1246 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1248 case ir_unop_bit_count
:
1249 emit(nir_op_bit_count
, dest_size
, srcs
);
1251 case ir_unop_find_msb
:
1253 case GLSL_TYPE_UINT
:
1254 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1257 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1260 unreachable("Invalid type for findMSB()");
1263 case ir_unop_find_lsb
:
1264 emit(nir_op_find_lsb
, dest_size
, srcs
);
1268 switch (ir
->type
->vector_elements
) {
1270 switch (ir
->operands
[0]->type
->vector_elements
) {
1271 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1272 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1273 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1274 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1275 default: unreachable("not reached");
1279 switch (ir
->operands
[0]->type
->vector_elements
) {
1280 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1281 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1282 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1283 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1284 default: unreachable("not reached");
1288 switch (ir
->operands
[0]->type
->vector_elements
) {
1289 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1290 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1291 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1292 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1293 default: unreachable("not reached");
1297 switch (ir
->operands
[0]->type
->vector_elements
) {
1298 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1299 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1300 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1301 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1302 default: unreachable("not reached");
1306 unreachable("not reached");
1317 case ir_binop_bit_and
:
1318 case ir_binop_bit_or
:
1319 case ir_binop_bit_xor
:
1320 case ir_binop_logic_and
:
1321 case ir_binop_logic_or
:
1322 case ir_binop_logic_xor
:
1323 case ir_binop_lshift
:
1324 case ir_binop_rshift
:
1325 switch (ir
->operation
) {
1327 if (out_type
== GLSL_TYPE_FLOAT
)
1333 if (out_type
== GLSL_TYPE_FLOAT
)
1339 if (out_type
== GLSL_TYPE_FLOAT
)
1345 if (out_type
== GLSL_TYPE_FLOAT
)
1347 else if (out_type
== GLSL_TYPE_INT
)
1353 if (out_type
== GLSL_TYPE_FLOAT
)
1359 if (out_type
== GLSL_TYPE_FLOAT
)
1361 else if (out_type
== GLSL_TYPE_INT
)
1367 if (out_type
== GLSL_TYPE_FLOAT
)
1369 else if (out_type
== GLSL_TYPE_INT
)
1374 case ir_binop_bit_and
:
1377 case ir_binop_bit_or
:
1380 case ir_binop_bit_xor
:
1383 case ir_binop_logic_and
:
1389 case ir_binop_logic_or
:
1395 case ir_binop_logic_xor
:
1401 case ir_binop_lshift
:
1404 case ir_binop_rshift
:
1405 if (out_type
== GLSL_TYPE_INT
)
1415 unreachable("not reached");
1418 instr
= emit(op
, dest_size
, srcs
);
1420 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1421 ir
->operands
[1]->type
->vector_elements
== 1) {
1422 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1424 instr
->src
[1].swizzle
[i
] = 0;
1428 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1429 ir
->operands
[0]->type
->vector_elements
== 1) {
1430 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1432 instr
->src
[0].swizzle
[i
] = 0;
1437 case ir_binop_imul_high
:
1438 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1441 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1442 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1444 if (supports_ints
) {
1445 if (types
[0] == GLSL_TYPE_FLOAT
)
1446 emit(nir_op_flt
, dest_size
, srcs
);
1447 else if (types
[0] == GLSL_TYPE_INT
)
1448 emit(nir_op_ilt
, dest_size
, srcs
);
1450 emit(nir_op_ult
, dest_size
, srcs
);
1452 emit(nir_op_slt
, dest_size
, srcs
);
1455 case ir_binop_greater
:
1456 if (supports_ints
) {
1457 if (types
[0] == GLSL_TYPE_FLOAT
)
1458 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1459 else if (types
[0] == GLSL_TYPE_INT
)
1460 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1462 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1464 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1467 case ir_binop_lequal
:
1468 if (supports_ints
) {
1469 if (types
[0] == GLSL_TYPE_FLOAT
)
1470 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1471 else if (types
[0] == GLSL_TYPE_INT
)
1472 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1474 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1476 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1479 case ir_binop_gequal
:
1480 if (supports_ints
) {
1481 if (types
[0] == GLSL_TYPE_FLOAT
)
1482 emit(nir_op_fge
, dest_size
, srcs
);
1483 else if (types
[0] == GLSL_TYPE_INT
)
1484 emit(nir_op_ige
, dest_size
, srcs
);
1486 emit(nir_op_uge
, dest_size
, srcs
);
1488 emit(nir_op_slt
, dest_size
, srcs
);
1491 case ir_binop_equal
:
1492 if (supports_ints
) {
1493 if (types
[0] == GLSL_TYPE_FLOAT
)
1494 emit(nir_op_feq
, dest_size
, srcs
);
1496 emit(nir_op_ieq
, dest_size
, srcs
);
1498 emit(nir_op_seq
, dest_size
, srcs
);
1501 case ir_binop_nequal
:
1502 if (supports_ints
) {
1503 if (types
[0] == GLSL_TYPE_FLOAT
)
1504 emit(nir_op_fne
, dest_size
, srcs
);
1506 emit(nir_op_ine
, dest_size
, srcs
);
1508 emit(nir_op_sne
, dest_size
, srcs
);
1511 case ir_binop_all_equal
:
1512 if (supports_ints
) {
1513 if (types
[0] == GLSL_TYPE_FLOAT
) {
1514 switch (ir
->operands
[0]->type
->vector_elements
) {
1515 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1516 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1517 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1518 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1520 unreachable("not reached");
1523 switch (ir
->operands
[0]->type
->vector_elements
) {
1524 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1525 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1526 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1527 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1529 unreachable("not reached");
1533 switch (ir
->operands
[0]->type
->vector_elements
) {
1534 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1535 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1536 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1537 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1539 unreachable("not reached");
1543 case ir_binop_any_nequal
:
1544 if (supports_ints
) {
1545 if (types
[0] == GLSL_TYPE_FLOAT
) {
1546 switch (ir
->operands
[0]->type
->vector_elements
) {
1547 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1548 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1549 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1550 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1552 unreachable("not reached");
1555 switch (ir
->operands
[0]->type
->vector_elements
) {
1556 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1557 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1558 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1559 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1561 unreachable("not reached");
1565 switch (ir
->operands
[0]->type
->vector_elements
) {
1566 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1567 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1568 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1569 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1571 unreachable("not reached");
1576 switch (ir
->operands
[0]->type
->vector_elements
) {
1577 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1578 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1579 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1581 unreachable("not reached");
1585 case ir_binop_pack_half_2x16_split
:
1586 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1588 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1589 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1590 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1592 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1593 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1594 ir
->operands
[2]->type
->vector_elements
== 1) {
1595 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1597 instr
->src
[2].swizzle
[i
] = 0;
1603 emit(nir_op_bcsel
, dest_size
, srcs
);
1605 emit(nir_op_fcsel
, dest_size
, srcs
);
1608 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1609 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1610 instr
->src
[0].swizzle
[i
] = 0;
1613 case ir_triop_bitfield_extract
:
1614 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1615 nir_op_ubitfield_extract
, dest_size
, srcs
);
1616 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1617 instr
->src
[1].swizzle
[i
] = 0;
1618 instr
->src
[2].swizzle
[i
] = 0;
1621 case ir_quadop_bitfield_insert
:
1622 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1623 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1624 instr
->src
[2].swizzle
[i
] = 0;
1625 instr
->src
[3].swizzle
[i
] = 0;
1628 case ir_quadop_vector
:
1629 switch (ir
->type
->vector_elements
) {
1630 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1631 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1632 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1633 default: unreachable("not reached");
1638 unreachable("not reached");
1643 nir_visitor::visit(ir_swizzle
*ir
)
1645 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1646 ir
->type
->vector_elements
,
1647 evaluate_rvalue(ir
->val
));
1649 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1650 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1651 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1655 nir_visitor::visit(ir_texture
*ir
)
1662 num_srcs
= 1; /* coordinate */
1667 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1668 num_srcs
= 2; /* coordinate, bias/lod */
1672 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1678 if (ir
->lod_info
.lod
!= NULL
)
1679 num_srcs
= 2; /* coordinate, lod */
1681 num_srcs
= 1; /* coordinate */
1685 op
= nir_texop_txf_ms
;
1686 num_srcs
= 2; /* coordinate, sample_index */
1691 if (ir
->lod_info
.lod
!= NULL
)
1692 num_srcs
= 1; /* lod */
1699 num_srcs
= 1; /* coordinate */
1704 num_srcs
= 1; /* coordinate */
1707 case ir_query_levels
:
1708 op
= nir_texop_query_levels
;
1713 unreachable("not reached");
1716 if (ir
->projector
!= NULL
)
1718 if (ir
->shadow_comparitor
!= NULL
)
1720 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1723 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1726 instr
->sampler_dim
=
1727 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1728 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1729 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1730 if (instr
->is_shadow
)
1731 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1732 switch (ir
->type
->base_type
) {
1733 case GLSL_TYPE_FLOAT
:
1734 instr
->dest_type
= nir_type_float
;
1737 instr
->dest_type
= nir_type_int
;
1739 case GLSL_TYPE_UINT
:
1740 instr
->dest_type
= nir_type_unsigned
;
1743 unreachable("not reached");
1746 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1748 unsigned src_number
= 0;
1750 if (ir
->coordinate
!= NULL
) {
1751 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1752 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1753 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1757 if (ir
->projector
!= NULL
) {
1758 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1759 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1763 if (ir
->shadow_comparitor
!= NULL
) {
1764 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1765 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1769 if (ir
->offset
!= NULL
) {
1770 /* we don't support multiple offsets yet */
1771 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1773 ir_constant
*const_offset
= ir
->offset
->as_constant();
1774 if (const_offset
!= NULL
) {
1775 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1776 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1778 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1779 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1786 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1787 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1794 if (ir
->lod_info
.lod
!= NULL
) {
1795 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1796 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1802 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1803 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1805 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1806 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1811 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
1812 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1817 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1824 assert(src_number
== num_srcs
);
1826 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1830 nir_visitor::visit(ir_constant
*ir
)
1833 * We don't know if this variable is an an array or struct that gets
1834 * dereferenced, so do the safe thing an make it a variable with a
1835 * constant initializer and return a dereference.
1838 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1839 var
->name
= ralloc_strdup(var
, "const_temp");
1840 var
->type
= ir
->type
;
1841 var
->data
.mode
= nir_var_local
;
1842 var
->data
.read_only
= true;
1843 var
->constant_initializer
= constant_copy(ir
, var
);
1844 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1846 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1847 this->deref_tail
= &this->deref_head
->deref
;
1851 nir_visitor::visit(ir_dereference_variable
*ir
)
1853 struct hash_entry
*entry
=
1854 _mesa_hash_table_search(this->var_table
, ir
->var
);
1856 nir_variable
*var
= (nir_variable
*) entry
->data
;
1858 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1859 this->deref_head
= deref
;
1860 this->deref_tail
= &deref
->deref
;
1864 nir_visitor::visit(ir_dereference_record
*ir
)
1866 ir
->record
->accept(this);
1868 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1869 assert(field_index
>= 0);
1871 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1872 deref
->deref
.type
= ir
->type
;
1873 this->deref_tail
->child
= &deref
->deref
;
1874 this->deref_tail
= &deref
->deref
;
1878 nir_visitor::visit(ir_dereference_array
*ir
)
1880 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1881 deref
->deref
.type
= ir
->type
;
1883 ir_constant
*const_index
= ir
->array_index
->as_constant();
1884 if (const_index
!= NULL
) {
1885 deref
->deref_array_type
= nir_deref_array_type_direct
;
1886 deref
->base_offset
= const_index
->value
.u
[0];
1888 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1889 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1892 ir
->array
->accept(this);
1894 this->deref_tail
->child
= &deref
->deref
;
1895 ralloc_steal(this->deref_tail
, deref
);
1896 this->deref_tail
= &deref
->deref
;
1900 nir_visitor::visit(ir_barrier
*ir
)
1902 nir_intrinsic_instr
*instr
=
1903 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
1904 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);