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
, bool supports_ints
);
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
*);
69 void create_function(ir_function
*ir
);
72 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
73 void add_instr(nir_instr
*instr
, unsigned num_components
);
74 nir_src
evaluate_rvalue(ir_rvalue
*ir
);
76 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
);
77 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
);
78 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
81 nir_src src2
, nir_src src3
);
86 nir_function_impl
*impl
;
87 exec_list
*cf_node_list
;
88 nir_instr
*result
; /* result of the expression tree last visited */
90 /* the head of the dereference chain we're creating */
91 nir_deref_var
*deref_head
;
92 /* the tail of the dereference chain we're creating */
93 nir_deref
*deref_tail
;
95 nir_variable
*var
; /* variable created by ir_variable visitor */
97 /* whether the IR we're operating on is per-function or global */
100 /* map of ir_variable -> nir_variable */
101 struct hash_table
*var_table
;
103 /* map of ir_function_signature -> nir_function_overload */
104 struct hash_table
*overload_table
;
108 * This visitor runs before the main visitor, calling create_function() for
109 * each function so that the main visitor can resolve forward references in
113 class nir_function_visitor
: public ir_hierarchical_visitor
116 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
119 virtual ir_visitor_status
visit_enter(ir_function
*);
122 nir_visitor
*visitor
;
125 }; /* end of anonymous namespace */
128 glsl_to_nir(exec_list
*ir
, _mesa_glsl_parse_state
*state
,
129 bool native_integers
)
131 nir_shader
*shader
= nir_shader_create(NULL
);
134 shader
->num_user_structures
= state
->num_user_structures
;
135 shader
->user_structures
= ralloc_array(shader
, glsl_type
*,
136 shader
->num_user_structures
);
137 memcpy(shader
->user_structures
, state
->user_structures
,
138 shader
->num_user_structures
* sizeof(glsl_type
*));
140 shader
->num_user_structures
= 0;
141 shader
->user_structures
= NULL
;
144 nir_visitor
v1(shader
, native_integers
);
145 nir_function_visitor
v2(&v1
);
147 visit_exec_list(ir
, &v1
);
152 nir_visitor::nir_visitor(nir_shader
*shader
, bool supports_ints
)
154 this->supports_ints
= supports_ints
;
155 this->shader
= shader
;
156 this->is_global
= true;
157 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
158 _mesa_key_pointer_equal
);
159 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
160 _mesa_key_pointer_equal
);
163 nir_visitor::~nir_visitor()
165 _mesa_hash_table_destroy(this->var_table
, NULL
);
166 _mesa_hash_table_destroy(this->overload_table
, NULL
);
169 static nir_constant
*
170 constant_copy(ir_constant
*ir
, void *mem_ctx
)
175 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
177 unsigned total_elems
= ir
->type
->components();
179 switch (ir
->type
->base_type
) {
181 for (i
= 0; i
< total_elems
; i
++)
182 ret
->value
.u
[i
] = ir
->value
.u
[i
];
186 for (i
= 0; i
< total_elems
; i
++)
187 ret
->value
.i
[i
] = ir
->value
.i
[i
];
190 case GLSL_TYPE_FLOAT
:
191 for (i
= 0; i
< total_elems
; i
++)
192 ret
->value
.f
[i
] = ir
->value
.f
[i
];
196 for (i
= 0; i
< total_elems
; i
++)
197 ret
->value
.b
[i
] = ir
->value
.b
[i
];
200 case GLSL_TYPE_STRUCT
:
201 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
204 foreach_in_list(ir_constant
, field
, &ir
->components
) {
205 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
210 case GLSL_TYPE_ARRAY
:
211 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
214 for (i
= 0; i
< ir
->type
->length
; i
++)
215 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
219 unreachable("not reached");
226 nir_visitor::visit(ir_variable
*ir
)
228 nir_variable
*var
= ralloc(shader
, nir_variable
);
229 var
->type
= ir
->type
;
230 var
->name
= ralloc_strdup(var
, ir
->name
);
232 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
233 unsigned size
= ir
->get_interface_type()->length
;
234 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
235 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
236 size
* sizeof(unsigned));
238 var
->max_ifc_array_access
= NULL
;
241 var
->data
.read_only
= ir
->data
.read_only
;
242 var
->data
.centroid
= ir
->data
.centroid
;
243 var
->data
.sample
= ir
->data
.sample
;
244 var
->data
.invariant
= ir
->data
.invariant
;
246 switch(ir
->data
.mode
) {
248 case ir_var_temporary
:
250 var
->data
.mode
= nir_var_global
;
252 var
->data
.mode
= nir_var_local
;
255 case ir_var_function_in
:
256 case ir_var_function_out
:
257 case ir_var_function_inout
:
258 case ir_var_const_in
:
259 var
->data
.mode
= nir_var_local
;
262 case ir_var_shader_in
:
263 var
->data
.mode
= nir_var_shader_in
;
266 case ir_var_shader_out
:
267 var
->data
.mode
= nir_var_shader_out
;
271 var
->data
.mode
= nir_var_uniform
;
275 case ir_var_system_value
:
276 var
->data
.mode
= nir_var_system_value
;
280 unreachable("not reached");
283 var
->data
.interpolation
= ir
->data
.interpolation
;
284 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
285 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
286 var
->data
.explicit_location
= ir
->data
.explicit_location
;
287 var
->data
.explicit_index
= ir
->data
.explicit_index
;
288 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
289 var
->data
.has_initializer
= ir
->data
.has_initializer
;
290 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
291 var
->data
.location_frac
= ir
->data
.location_frac
;
292 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
293 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
295 switch (ir
->data
.depth_layout
) {
296 case ir_depth_layout_none
:
297 var
->data
.depth_layout
= nir_depth_layout_none
;
299 case ir_depth_layout_any
:
300 var
->data
.depth_layout
= nir_depth_layout_any
;
302 case ir_depth_layout_greater
:
303 var
->data
.depth_layout
= nir_depth_layout_greater
;
305 case ir_depth_layout_less
:
306 var
->data
.depth_layout
= nir_depth_layout_less
;
308 case ir_depth_layout_unchanged
:
309 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
312 unreachable("not reached");
315 var
->data
.location
= ir
->data
.location
;
316 var
->data
.index
= ir
->data
.index
;
317 var
->data
.binding
= ir
->data
.binding
;
318 /* XXX Get rid of buffer_index */
319 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
320 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
321 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
322 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
323 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
324 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
325 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
326 var
->data
.image
.format
= ir
->data
.image_format
;
327 var
->data
.max_array_access
= ir
->data
.max_array_access
;
329 var
->num_state_slots
= ir
->get_num_state_slots();
330 if (var
->num_state_slots
> 0) {
331 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
332 var
->num_state_slots
);
334 ir_state_slot
*state_slots
= ir
->get_state_slots();
335 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
336 for (unsigned j
= 0; j
< 5; j
++)
337 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
338 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
341 var
->state_slots
= NULL
;
344 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
346 var
->interface_type
= ir
->get_interface_type();
348 switch (var
->data
.mode
) {
350 exec_list_push_tail(&impl
->locals
, &var
->node
);
354 exec_list_push_tail(&shader
->globals
, &var
->node
);
357 case nir_var_shader_in
:
358 _mesa_hash_table_insert(shader
->inputs
, var
->name
, var
);
361 case nir_var_shader_out
:
362 _mesa_hash_table_insert(shader
->outputs
, var
->name
, var
);
365 case nir_var_uniform
:
366 _mesa_hash_table_insert(shader
->uniforms
, var
->name
, var
);
369 case nir_var_system_value
:
370 exec_list_push_tail(&shader
->system_values
, &var
->node
);
374 unreachable("not reached");
377 _mesa_hash_table_insert(var_table
, ir
, var
);
382 nir_function_visitor::visit_enter(ir_function
*ir
)
384 visitor
->create_function(ir
);
385 return visit_continue_with_parent
;
390 nir_visitor::create_function(ir_function
*ir
)
392 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
393 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
394 create_overload(sig
, func
);
401 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
403 if (ir
->is_intrinsic
)
406 nir_function_overload
*overload
= nir_function_overload_create(function
);
408 unsigned num_params
= ir
->parameters
.length();
409 overload
->num_params
= num_params
;
410 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
413 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
414 switch (param
->data
.mode
) {
415 case ir_var_function_in
:
416 overload
->params
[i
].param_type
= nir_parameter_in
;
419 case ir_var_function_out
:
420 overload
->params
[i
].param_type
= nir_parameter_out
;
423 case ir_var_function_inout
:
424 overload
->params
[i
].param_type
= nir_parameter_inout
;
428 unreachable("not reached");
431 overload
->params
[i
].type
= param
->type
;
435 overload
->return_type
= ir
->return_type
;
437 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
441 nir_visitor::visit(ir_function
*ir
)
443 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
448 nir_visitor::visit(ir_function_signature
*ir
)
450 if (ir
->is_intrinsic
)
453 struct hash_entry
*entry
=
454 _mesa_hash_table_search(this->overload_table
, ir
);
457 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
459 if (ir
->is_defined
) {
460 nir_function_impl
*impl
= nir_function_impl_create(overload
);
463 unsigned num_params
= overload
->num_params
;
464 impl
->num_params
= num_params
;
465 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
467 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
469 impl
->params
[i
] = this->var
;
473 if (overload
->return_type
== glsl_type::void_type
) {
474 impl
->return_var
= NULL
;
476 impl
->return_var
= ralloc(this->shader
, nir_variable
);
477 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
479 impl
->return_var
->type
= overload
->return_type
;
482 this->is_global
= false;
484 this->cf_node_list
= &impl
->body
;
485 visit_exec_list(&ir
->body
, this);
487 this->is_global
= true;
489 overload
->impl
= NULL
;
494 nir_visitor::visit(ir_loop
*ir
)
496 exec_list
*old_list
= this->cf_node_list
;
498 nir_loop
*loop
= nir_loop_create(this->shader
);
499 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
500 this->cf_node_list
= &loop
->body
;
501 visit_exec_list(&ir
->body_instructions
, this);
503 this->cf_node_list
= old_list
;
507 nir_visitor::visit(ir_if
*ir
)
509 nir_src condition
= evaluate_rvalue(ir
->condition
);
511 exec_list
*old_list
= this->cf_node_list
;
513 nir_if
*if_stmt
= nir_if_create(this->shader
);
514 if_stmt
->condition
= condition
;
515 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
517 this->cf_node_list
= &if_stmt
->then_list
;
518 visit_exec_list(&ir
->then_instructions
, this);
520 this->cf_node_list
= &if_stmt
->else_list
;
521 visit_exec_list(&ir
->else_instructions
, this);
523 this->cf_node_list
= old_list
;
527 nir_visitor::visit(ir_discard
*ir
)
530 * discards aren't treated as control flow, because before we lower them
531 * they can appear anywhere in the shader and the stuff after them may still
532 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
533 * discards will be immediately followed by a return.
536 nir_intrinsic_instr
*discard
=
537 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
538 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
542 nir_visitor::visit(ir_emit_vertex
*ir
)
544 nir_intrinsic_instr
*instr
=
545 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
546 instr
->const_index
[0] = ir
->stream_id();
547 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
551 nir_visitor::visit(ir_end_primitive
*ir
)
553 nir_intrinsic_instr
*instr
=
554 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
555 instr
->const_index
[0] = ir
->stream_id();
556 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
560 nir_visitor::visit(ir_loop_jump
*ir
)
564 case ir_loop_jump::jump_break
:
565 type
= nir_jump_break
;
567 case ir_loop_jump::jump_continue
:
568 type
= nir_jump_continue
;
571 unreachable("not reached");
574 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
575 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
579 nir_visitor::visit(ir_return
*ir
)
581 if (ir
->value
!= NULL
) {
582 ir
->value
->accept(this);
583 nir_intrinsic_instr
*copy
=
584 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
586 copy
->variables
[0] = nir_deref_var_create(this->shader
,
587 this->impl
->return_var
);
588 copy
->variables
[1] = this->deref_head
;
591 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
592 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
596 nir_visitor::visit(ir_call
*ir
)
598 if (ir
->callee
->is_intrinsic
) {
600 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
601 op
= nir_intrinsic_atomic_counter_read_var
;
602 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
603 op
= nir_intrinsic_atomic_counter_inc_var
;
604 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
605 op
= nir_intrinsic_atomic_counter_dec_var
;
610 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
611 ir_dereference
*param
=
612 (ir_dereference
*) ir
->actual_parameters
.get_head();
614 instr
->variables
[0] = this->deref_head
;
615 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
617 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
619 nir_intrinsic_instr
*store_instr
=
620 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
621 store_instr
->num_components
= 1;
623 ir
->return_deref
->accept(this);
624 store_instr
->variables
[0] = this->deref_head
;
625 store_instr
->src
[0].is_ssa
= true;
626 store_instr
->src
[0].ssa
= &instr
->dest
.ssa
;
628 nir_instr_insert_after_cf_list(this->cf_node_list
, &store_instr
->instr
);
633 struct hash_entry
*entry
=
634 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
636 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
638 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
641 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
643 instr
->params
[i
] = this->deref_head
;
647 ir
->return_deref
->accept(this);
648 instr
->return_deref
= this->deref_head
;
649 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
653 nir_visitor::visit(ir_assignment
*ir
)
655 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
657 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
658 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
659 /* We're doing a plain-as-can-be copy, so emit a copy_var */
660 nir_intrinsic_instr
*copy
=
661 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
663 ir
->lhs
->accept(this);
664 copy
->variables
[0] = this->deref_head
;
666 ir
->rhs
->accept(this);
667 copy
->variables
[1] = this->deref_head
;
671 nir_if
*if_stmt
= nir_if_create(this->shader
);
672 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
673 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
674 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
676 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
681 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
683 ir
->lhs
->accept(this);
684 nir_deref_var
*lhs_deref
= this->deref_head
;
685 nir_src src
= evaluate_rvalue(ir
->rhs
);
687 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
689 * We have no good way to update only part of a variable, so just load
690 * the LHS and do a vec operation to combine the old with the new, and
692 * back into the LHS. Copy propagation should get rid of the mess.
695 nir_intrinsic_instr
*load
=
696 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
697 load
->num_components
= ir
->lhs
->type
->vector_elements
;
698 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
699 load
->variables
[0] = lhs_deref
;
700 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
703 switch (ir
->lhs
->type
->vector_elements
) {
704 case 1: vec_op
= nir_op_imov
; break;
705 case 2: vec_op
= nir_op_vec2
; break;
706 case 3: vec_op
= nir_op_vec3
; break;
707 case 4: vec_op
= nir_op_vec4
; break;
708 default: unreachable("Invalid number of components"); break;
710 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
711 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
712 vec
->dest
.write_mask
= (1 << num_components
) - 1;
714 unsigned component
= 0;
715 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
716 if (ir
->write_mask
& (1 << i
)) {
717 vec
->src
[i
].src
= src
;
719 /* GLSL IR will give us the input to the write-masked assignment
720 * in a single packed vector. So, for example, if the
721 * writemask is xzw, then we have to swizzle x -> x, y -> z,
722 * and z -> w and get the y component from the load.
724 vec
->src
[i
].swizzle
[0] = component
++;
726 vec
->src
[i
].src
.is_ssa
= true;
727 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
728 vec
->src
[i
].swizzle
[0] = i
;
732 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
735 src
.ssa
= &vec
->dest
.dest
.ssa
;
738 nir_intrinsic_instr
*store
=
739 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
740 store
->num_components
= ir
->lhs
->type
->vector_elements
;
741 nir_deref
*store_deref
= nir_copy_deref(this->shader
, &lhs_deref
->deref
);
742 store
->variables
[0] = nir_deref_as_var(store_deref
);
746 nir_if
*if_stmt
= nir_if_create(this->shader
);
747 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
748 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
749 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
751 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
756 * Given an instruction, returns a pointer to its destination or NULL if there
759 * Note that this only handles instructions we generate at this level.
762 get_instr_dest(nir_instr
*instr
)
764 nir_alu_instr
*alu_instr
;
765 nir_intrinsic_instr
*intrinsic_instr
;
766 nir_tex_instr
*tex_instr
;
768 switch (instr
->type
) {
769 case nir_instr_type_alu
:
770 alu_instr
= nir_instr_as_alu(instr
);
771 return &alu_instr
->dest
.dest
;
773 case nir_instr_type_intrinsic
:
774 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
775 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
776 return &intrinsic_instr
->dest
;
780 case nir_instr_type_tex
:
781 tex_instr
= nir_instr_as_tex(instr
);
782 return &tex_instr
->dest
;
785 unreachable("not reached");
792 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
794 nir_dest
*dest
= get_instr_dest(instr
);
796 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
798 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
799 this->result
= instr
;
803 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
806 if (ir
->as_dereference() || ir
->as_constant()) {
808 * A dereference is being used on the right hand side, which means we
809 * must emit a variable load.
812 nir_intrinsic_instr
*load_instr
=
813 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
814 load_instr
->num_components
= ir
->type
->vector_elements
;
815 load_instr
->variables
[0] = this->deref_head
;
816 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
819 nir_dest
*dest
= get_instr_dest(this->result
);
821 assert(dest
->is_ssa
);
824 src
.ssa
= &dest
->ssa
;
830 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
832 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
833 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
834 instr
->src
[i
].src
= srcs
[i
];
835 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
836 add_instr(&instr
->instr
, dest_size
);
841 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
843 assert(nir_op_infos
[op
].num_inputs
== 1);
844 return emit(op
, dest_size
, &src1
);
848 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
851 assert(nir_op_infos
[op
].num_inputs
== 2);
852 nir_src srcs
[] = { src1
, src2
};
853 return emit(op
, dest_size
, srcs
);
857 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
858 nir_src src2
, nir_src src3
)
860 assert(nir_op_infos
[op
].num_inputs
== 3);
861 nir_src srcs
[] = { src1
, src2
, src3
};
862 return emit(op
, dest_size
, srcs
);
866 nir_visitor::visit(ir_expression
*ir
)
868 /* Some special cases */
869 switch (ir
->operation
) {
870 case ir_binop_ubo_load
: {
871 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
875 op
= nir_intrinsic_load_ubo
;
877 op
= nir_intrinsic_load_ubo_indirect
;
879 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
880 load
->num_components
= ir
->type
->vector_elements
;
881 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
882 load
->const_index
[1] = 1; /* number of vec4's */
883 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
885 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
886 add_instr(&load
->instr
, ir
->type
->vector_elements
);
889 * In UBO's, a true boolean value is any non-zero value, but we consider
890 * a true boolean to be ~0. Fix this up with a != 0 comparison.
893 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
894 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
895 const_zero
->value
.u
[0] = 0;
896 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
898 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
899 compare
->src
[0].src
.is_ssa
= true;
900 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
901 compare
->src
[1].src
.is_ssa
= true;
902 compare
->src
[1].src
.ssa
= &const_zero
->def
;
903 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
904 compare
->src
[1].swizzle
[i
] = 0;
905 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
907 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
913 case ir_unop_interpolate_at_centroid
:
914 case ir_binop_interpolate_at_offset
:
915 case ir_binop_interpolate_at_sample
: {
916 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
917 ir_swizzle
*swizzle
= NULL
;
919 /* the api does not allow a swizzle here, but the varying packing code
920 * may have pushed one into here.
922 swizzle
= ir
->operands
[0]->as_swizzle();
924 deref
= swizzle
->val
->as_dereference();
931 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
932 switch (ir
->operation
) {
933 case ir_unop_interpolate_at_centroid
:
934 op
= nir_intrinsic_interp_var_at_centroid
;
936 case ir_binop_interpolate_at_offset
:
937 op
= nir_intrinsic_interp_var_at_offset
;
939 case ir_binop_interpolate_at_sample
:
940 op
= nir_intrinsic_interp_var_at_sample
;
943 unreachable("Invalid interpolation intrinsic");
946 /* This case can happen if the vertex shader does not write the
947 * given varying. In this case, the linker will lower it to a
948 * global variable. Since interpolating a variable makes no
949 * sense, we'll just turn it into a load which will probably
950 * eventually end up as an SSA definition.
952 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
953 op
= nir_intrinsic_load_var
;
956 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
957 intrin
->num_components
= deref
->type
->vector_elements
;
958 intrin
->variables
[0] = this->deref_head
;
960 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
961 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
962 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
964 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
967 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
968 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
969 mov
->src
[0].src
.is_ssa
= true;
970 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
972 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
973 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
974 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
975 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
976 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
977 mov
->src
[0].swizzle
[i
] = 0;
979 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
990 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
991 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
993 glsl_base_type types
[4];
994 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
996 types
[i
] = ir
->operands
[i
]->type
->base_type
;
998 types
[i
] = GLSL_TYPE_FLOAT
;
1000 glsl_base_type out_type
;
1002 out_type
= ir
->type
->base_type
;
1004 out_type
= GLSL_TYPE_FLOAT
;
1006 unsigned dest_size
= ir
->type
->vector_elements
;
1008 nir_alu_instr
*instr
;
1011 switch (ir
->operation
) {
1012 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1013 case ir_unop_logic_not
:
1014 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1017 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1021 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1024 case ir_unop_saturate
:
1025 assert(types
[0] == GLSL_TYPE_FLOAT
);
1026 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1029 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1032 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1033 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1034 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1035 case ir_unop_exp
: emit(nir_op_fexp
, dest_size
, srcs
); break;
1036 case ir_unop_log
: emit(nir_op_flog
, dest_size
, srcs
); break;
1037 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1038 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1040 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1043 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1046 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1048 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1049 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1050 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1051 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1052 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1055 case ir_unop_bitcast_i2f
:
1056 case ir_unop_bitcast_f2i
:
1057 case ir_unop_bitcast_u2f
:
1058 case ir_unop_bitcast_f2u
:
1060 emit(nir_op_imov
, dest_size
, srcs
);
1063 switch (ir
->operands
[0]->type
->vector_elements
) {
1065 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1069 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1073 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1077 unreachable("not reached");
1080 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1081 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1082 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1083 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1084 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1085 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1086 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1087 case ir_unop_sin_reduced
:
1088 emit(nir_op_fsin_reduced
, dest_size
, srcs
);
1090 case ir_unop_cos_reduced
:
1091 emit(nir_op_fcos_reduced
, dest_size
, srcs
);
1093 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1094 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1095 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1096 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1097 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1098 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1099 case ir_unop_pack_snorm_2x16
:
1100 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1102 case ir_unop_pack_snorm_4x8
:
1103 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1105 case ir_unop_pack_unorm_2x16
:
1106 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1108 case ir_unop_pack_unorm_4x8
:
1109 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1111 case ir_unop_pack_half_2x16
:
1112 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1114 case ir_unop_unpack_snorm_2x16
:
1115 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1117 case ir_unop_unpack_snorm_4x8
:
1118 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1120 case ir_unop_unpack_unorm_2x16
:
1121 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1123 case ir_unop_unpack_unorm_4x8
:
1124 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1126 case ir_unop_unpack_half_2x16
:
1127 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1129 case ir_unop_unpack_half_2x16_split_x
:
1130 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1132 case ir_unop_unpack_half_2x16_split_y
:
1133 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1135 case ir_unop_bitfield_reverse
:
1136 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1138 case ir_unop_bit_count
:
1139 emit(nir_op_bit_count
, dest_size
, srcs
);
1141 case ir_unop_find_msb
:
1143 case GLSL_TYPE_UINT
:
1144 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1147 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1150 unreachable("Invalid type for findMSB()");
1153 case ir_unop_find_lsb
:
1154 emit(nir_op_find_lsb
, dest_size
, srcs
);
1158 switch (ir
->type
->vector_elements
) {
1160 switch (ir
->operands
[0]->type
->vector_elements
) {
1161 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1162 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1163 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1164 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1165 default: assert(0); break;
1169 switch (ir
->operands
[0]->type
->vector_elements
) {
1170 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1171 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1172 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1173 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1174 default: assert(0); break;
1178 switch (ir
->operands
[0]->type
->vector_elements
) {
1179 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1180 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1181 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1182 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1183 default: assert(0); break;
1187 switch (ir
->operands
[0]->type
->vector_elements
) {
1188 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1189 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1190 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1191 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1192 default: assert(0); break;
1196 unreachable("not reached");
1207 case ir_binop_bit_and
:
1208 case ir_binop_bit_or
:
1209 case ir_binop_bit_xor
:
1210 case ir_binop_lshift
:
1211 case ir_binop_rshift
:
1212 switch (ir
->operation
) {
1214 if (out_type
== GLSL_TYPE_FLOAT
)
1220 if (out_type
== GLSL_TYPE_FLOAT
)
1226 if (out_type
== GLSL_TYPE_FLOAT
)
1232 if (out_type
== GLSL_TYPE_FLOAT
)
1234 else if (out_type
== GLSL_TYPE_INT
)
1240 if (out_type
== GLSL_TYPE_FLOAT
)
1246 if (out_type
== GLSL_TYPE_FLOAT
)
1248 else if (out_type
== GLSL_TYPE_INT
)
1254 if (out_type
== GLSL_TYPE_FLOAT
)
1256 else if (out_type
== GLSL_TYPE_INT
)
1261 case ir_binop_bit_and
:
1264 case ir_binop_bit_or
:
1267 case ir_binop_bit_xor
:
1270 case ir_binop_lshift
:
1273 case ir_binop_rshift
:
1274 if (out_type
== GLSL_TYPE_INT
)
1284 unreachable("not reached");
1287 instr
= emit(op
, dest_size
, srcs
);
1289 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1290 ir
->operands
[1]->type
->vector_elements
== 1) {
1291 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1293 instr
->src
[1].swizzle
[i
] = 0;
1297 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1298 ir
->operands
[0]->type
->vector_elements
== 1) {
1299 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1301 instr
->src
[0].swizzle
[i
] = 0;
1306 case ir_binop_imul_high
:
1307 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1310 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1311 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1313 if (supports_ints
) {
1314 if (types
[0] == GLSL_TYPE_FLOAT
)
1315 emit(nir_op_flt
, dest_size
, srcs
);
1316 else if (types
[0] == GLSL_TYPE_INT
)
1317 emit(nir_op_ilt
, dest_size
, srcs
);
1319 emit(nir_op_ult
, dest_size
, srcs
);
1321 emit(nir_op_slt
, dest_size
, srcs
);
1324 case ir_binop_greater
:
1325 if (supports_ints
) {
1326 if (types
[0] == GLSL_TYPE_FLOAT
)
1327 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1328 else if (types
[0] == GLSL_TYPE_INT
)
1329 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1331 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1333 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1336 case ir_binop_lequal
:
1337 if (supports_ints
) {
1338 if (types
[0] == GLSL_TYPE_FLOAT
)
1339 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1340 else if (types
[0] == GLSL_TYPE_INT
)
1341 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1343 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1345 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1348 case ir_binop_gequal
:
1349 if (supports_ints
) {
1350 if (types
[0] == GLSL_TYPE_FLOAT
)
1351 emit(nir_op_fge
, dest_size
, srcs
);
1352 else if (types
[0] == GLSL_TYPE_INT
)
1353 emit(nir_op_ige
, dest_size
, srcs
);
1355 emit(nir_op_uge
, dest_size
, srcs
);
1357 emit(nir_op_slt
, dest_size
, srcs
);
1360 case ir_binop_equal
:
1361 if (supports_ints
) {
1362 if (types
[0] == GLSL_TYPE_FLOAT
)
1363 emit(nir_op_feq
, dest_size
, srcs
);
1365 emit(nir_op_ieq
, dest_size
, srcs
);
1367 emit(nir_op_seq
, dest_size
, srcs
);
1370 case ir_binop_nequal
:
1371 if (supports_ints
) {
1372 if (types
[0] == GLSL_TYPE_FLOAT
)
1373 emit(nir_op_fne
, dest_size
, srcs
);
1375 emit(nir_op_ine
, dest_size
, srcs
);
1377 emit(nir_op_sne
, dest_size
, srcs
);
1380 case ir_binop_all_equal
:
1381 if (supports_ints
) {
1382 if (types
[0] == GLSL_TYPE_FLOAT
) {
1383 switch (ir
->operands
[0]->type
->vector_elements
) {
1384 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1385 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1386 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1387 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1389 unreachable("not reached");
1392 switch (ir
->operands
[0]->type
->vector_elements
) {
1393 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1394 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1395 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1396 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1398 unreachable("not reached");
1402 switch (ir
->operands
[0]->type
->vector_elements
) {
1403 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1404 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1405 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1406 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1408 unreachable("not reached");
1412 case ir_binop_any_nequal
:
1413 if (supports_ints
) {
1414 if (types
[0] == GLSL_TYPE_FLOAT
) {
1415 switch (ir
->operands
[0]->type
->vector_elements
) {
1416 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1417 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1418 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1419 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1421 unreachable("not reached");
1424 switch (ir
->operands
[0]->type
->vector_elements
) {
1425 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1426 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1427 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1428 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1430 unreachable("not reached");
1434 switch (ir
->operands
[0]->type
->vector_elements
) {
1435 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1436 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1437 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1438 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1440 unreachable("not reached");
1444 case ir_binop_logic_and
:
1446 emit(nir_op_iand
, dest_size
, srcs
);
1448 emit(nir_op_fand
, dest_size
, srcs
);
1450 case ir_binop_logic_or
:
1452 emit(nir_op_ior
, dest_size
, srcs
);
1454 emit(nir_op_for
, dest_size
, srcs
);
1456 case ir_binop_logic_xor
:
1458 emit(nir_op_ixor
, dest_size
, srcs
);
1460 emit(nir_op_fxor
, dest_size
, srcs
);
1463 switch (ir
->operands
[0]->type
->vector_elements
) {
1464 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1465 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1466 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1468 unreachable("not reached");
1472 case ir_binop_pack_half_2x16_split
:
1473 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1475 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1476 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1477 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1479 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1480 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1481 ir
->operands
[2]->type
->vector_elements
== 1) {
1482 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1484 instr
->src
[2].swizzle
[i
] = 0;
1490 emit(nir_op_bcsel
, dest_size
, srcs
);
1492 emit(nir_op_fcsel
, dest_size
, srcs
);
1495 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1496 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1497 instr
->src
[0].swizzle
[i
] = 0;
1500 case ir_triop_bitfield_extract
:
1501 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1502 nir_op_ubitfield_extract
, dest_size
, srcs
);
1503 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1504 instr
->src
[1].swizzle
[i
] = 0;
1505 instr
->src
[2].swizzle
[i
] = 0;
1508 case ir_quadop_bitfield_insert
:
1509 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1510 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1511 instr
->src
[2].swizzle
[i
] = 0;
1512 instr
->src
[3].swizzle
[i
] = 0;
1515 case ir_quadop_vector
:
1516 switch (ir
->type
->vector_elements
) {
1517 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1518 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1519 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1520 default: assert(0); break;
1525 unreachable("not reached");
1530 nir_visitor::visit(ir_swizzle
*ir
)
1532 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1533 ir
->type
->vector_elements
,
1534 evaluate_rvalue(ir
->val
));
1536 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1537 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1538 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1542 nir_visitor::visit(ir_texture
*ir
)
1549 num_srcs
= 1; /* coordinate */
1554 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1555 num_srcs
= 2; /* coordinate, bias/lod */
1559 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1565 if (ir
->lod_info
.lod
!= NULL
)
1566 num_srcs
= 2; /* coordinate, lod */
1568 num_srcs
= 1; /* coordinate */
1572 op
= nir_texop_txf_ms
;
1573 num_srcs
= 2; /* coordinate, sample_index */
1578 if (ir
->lod_info
.lod
!= NULL
)
1579 num_srcs
= 1; /* lod */
1586 num_srcs
= 1; /* coordinate */
1591 num_srcs
= 1; /* coordinate */
1594 case ir_query_levels
:
1595 op
= nir_texop_query_levels
;
1600 unreachable("not reached");
1603 if (ir
->projector
!= NULL
)
1605 if (ir
->shadow_comparitor
!= NULL
)
1607 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1610 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1613 instr
->sampler_dim
=
1614 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1615 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1616 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1617 if (instr
->is_shadow
)
1618 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1619 switch (ir
->type
->base_type
) {
1620 case GLSL_TYPE_FLOAT
:
1621 instr
->dest_type
= nir_type_float
;
1624 instr
->dest_type
= nir_type_int
;
1626 case GLSL_TYPE_UINT
:
1627 instr
->dest_type
= nir_type_unsigned
;
1633 ir
->sampler
->accept(this);
1634 instr
->sampler
= this->deref_head
;
1636 unsigned src_number
= 0;
1638 if (ir
->coordinate
!= NULL
) {
1639 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1640 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1641 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1645 if (ir
->projector
!= NULL
) {
1646 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1647 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1651 if (ir
->shadow_comparitor
!= NULL
) {
1652 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1653 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1657 if (ir
->offset
!= NULL
) {
1658 /* we don't support multiple offsets yet */
1659 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1661 ir_constant
*const_offset
= ir
->offset
->as_constant();
1662 if (const_offset
!= NULL
) {
1663 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1664 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1666 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1667 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1674 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1675 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1682 if (ir
->lod_info
.lod
!= NULL
) {
1683 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1684 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1690 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1691 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1693 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1694 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1699 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
1700 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1705 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1712 assert(src_number
== num_srcs
);
1714 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1718 nir_visitor::visit(ir_constant
*ir
)
1721 * We don't know if this variable is an an array or struct that gets
1722 * dereferenced, so do the safe thing an make it a variable with a
1723 * constant initializer and return a dereference.
1726 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1727 var
->name
= ralloc_strdup(var
, "const_temp");
1728 var
->type
= ir
->type
;
1729 var
->data
.mode
= nir_var_local
;
1730 var
->data
.read_only
= true;
1731 var
->constant_initializer
= constant_copy(ir
, var
);
1732 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1734 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1735 this->deref_tail
= &this->deref_head
->deref
;
1739 nir_visitor::visit(ir_dereference_variable
*ir
)
1741 struct hash_entry
*entry
=
1742 _mesa_hash_table_search(this->var_table
, ir
->var
);
1744 nir_variable
*var
= (nir_variable
*) entry
->data
;
1746 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1747 this->deref_head
= deref
;
1748 this->deref_tail
= &deref
->deref
;
1752 nir_visitor::visit(ir_dereference_record
*ir
)
1754 ir
->record
->accept(this);
1756 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1757 assert(field_index
>= 0);
1759 nir_deref_struct
*deref
= nir_deref_struct_create(this->shader
, field_index
);
1760 deref
->deref
.type
= ir
->type
;
1761 this->deref_tail
->child
= &deref
->deref
;
1762 this->deref_tail
= &deref
->deref
;
1766 nir_visitor::visit(ir_dereference_array
*ir
)
1768 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1769 deref
->deref
.type
= ir
->type
;
1771 ir_constant
*const_index
= ir
->array_index
->as_constant();
1772 if (const_index
!= NULL
) {
1773 deref
->deref_array_type
= nir_deref_array_type_direct
;
1774 deref
->base_offset
= const_index
->value
.u
[0];
1776 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1777 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1780 ir
->array
->accept(this);
1782 this->deref_tail
->child
= &deref
->deref
;
1783 this->deref_tail
= &deref
->deref
;