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
);
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
;
247 switch(ir
->data
.mode
) {
249 case ir_var_temporary
:
251 var
->data
.mode
= nir_var_global
;
253 var
->data
.mode
= nir_var_local
;
256 case ir_var_function_in
:
257 case ir_var_function_out
:
258 case ir_var_function_inout
:
259 case ir_var_const_in
:
260 var
->data
.mode
= nir_var_local
;
263 case ir_var_shader_in
:
264 var
->data
.mode
= nir_var_shader_in
;
267 case ir_var_shader_out
:
268 var
->data
.mode
= nir_var_shader_out
;
272 var
->data
.mode
= nir_var_uniform
;
276 case ir_var_system_value
:
277 var
->data
.mode
= nir_var_system_value
;
285 var
->data
.interpolation
= ir
->data
.interpolation
;
286 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
287 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
288 var
->data
.explicit_location
= ir
->data
.explicit_location
;
289 var
->data
.explicit_index
= ir
->data
.explicit_index
;
290 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
291 var
->data
.has_initializer
= ir
->data
.has_initializer
;
292 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
293 var
->data
.location_frac
= ir
->data
.location_frac
;
294 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
295 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
297 switch (ir
->data
.depth_layout
) {
298 case ir_depth_layout_none
:
299 var
->data
.depth_layout
= nir_depth_layout_none
;
301 case ir_depth_layout_any
:
302 var
->data
.depth_layout
= nir_depth_layout_any
;
304 case ir_depth_layout_greater
:
305 var
->data
.depth_layout
= nir_depth_layout_greater
;
307 case ir_depth_layout_less
:
308 var
->data
.depth_layout
= nir_depth_layout_less
;
310 case ir_depth_layout_unchanged
:
311 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
318 var
->data
.location
= ir
->data
.location
;
319 var
->data
.index
= ir
->data
.index
;
320 var
->data
.binding
= ir
->data
.binding
;
321 /* XXX Get rid of buffer_index */
322 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
323 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
324 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
325 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
326 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
327 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
328 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
329 var
->data
.image
.format
= ir
->data
.image_format
;
330 var
->data
.max_array_access
= ir
->data
.max_array_access
;
332 var
->num_state_slots
= ir
->get_num_state_slots();
333 if (var
->num_state_slots
> 0) {
334 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
335 var
->num_state_slots
);
337 ir_state_slot
*state_slots
= ir
->get_state_slots();
338 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
339 for (unsigned j
= 0; j
< 5; j
++)
340 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
341 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
344 var
->state_slots
= NULL
;
347 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
349 var
->interface_type
= ir
->get_interface_type();
351 switch (var
->data
.mode
) {
353 exec_list_push_tail(&impl
->locals
, &var
->node
);
357 exec_list_push_tail(&shader
->globals
, &var
->node
);
360 case nir_var_shader_in
:
361 _mesa_hash_table_insert(shader
->inputs
, var
->name
, var
);
364 case nir_var_shader_out
:
365 _mesa_hash_table_insert(shader
->outputs
, var
->name
, var
);
368 case nir_var_uniform
:
369 _mesa_hash_table_insert(shader
->uniforms
, var
->name
, var
);
372 case nir_var_system_value
:
373 exec_list_push_tail(&shader
->system_values
, &var
->node
);
381 _mesa_hash_table_insert(var_table
, ir
, var
);
386 nir_function_visitor::visit_enter(ir_function
*ir
)
388 visitor
->create_function(ir
);
389 return visit_continue_with_parent
;
394 nir_visitor::create_function(ir_function
*ir
)
396 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
397 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
398 create_overload(sig
, func
);
405 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
407 if (ir
->is_intrinsic
)
410 nir_function_overload
*overload
= nir_function_overload_create(function
);
412 unsigned num_params
= ir
->parameters
.length();
413 overload
->num_params
= num_params
;
414 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
417 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
418 switch (param
->data
.mode
) {
419 case ir_var_function_in
:
420 overload
->params
[i
].param_type
= nir_parameter_in
;
423 case ir_var_function_out
:
424 overload
->params
[i
].param_type
= nir_parameter_out
;
427 case ir_var_function_inout
:
428 overload
->params
[i
].param_type
= nir_parameter_inout
;
436 overload
->params
[i
].type
= param
->type
;
440 overload
->return_type
= ir
->return_type
;
442 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
446 nir_visitor::visit(ir_function
*ir
)
448 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
453 nir_visitor::visit(ir_function_signature
*ir
)
455 if (ir
->is_intrinsic
)
458 struct hash_entry
*entry
=
459 _mesa_hash_table_search(this->overload_table
, ir
);
462 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
464 if (ir
->is_defined
) {
465 nir_function_impl
*impl
= nir_function_impl_create(overload
);
468 unsigned num_params
= overload
->num_params
;
469 impl
->num_params
= num_params
;
470 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
472 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
474 impl
->params
[i
] = this->var
;
478 if (overload
->return_type
== glsl_type::void_type
) {
479 impl
->return_var
= NULL
;
481 impl
->return_var
= ralloc(this->shader
, nir_variable
);
482 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
484 impl
->return_var
->type
= overload
->return_type
;
487 this->is_global
= false;
489 this->cf_node_list
= &impl
->body
;
490 visit_exec_list(&ir
->body
, this);
492 this->is_global
= true;
494 overload
->impl
= NULL
;
499 nir_visitor::visit(ir_loop
*ir
)
501 exec_list
*old_list
= this->cf_node_list
;
503 nir_loop
*loop
= nir_loop_create(this->shader
);
504 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
505 this->cf_node_list
= &loop
->body
;
506 visit_exec_list(&ir
->body_instructions
, this);
508 this->cf_node_list
= old_list
;
512 nir_visitor::visit(ir_if
*ir
)
514 nir_src condition
= evaluate_rvalue(ir
->condition
);
516 exec_list
*old_list
= this->cf_node_list
;
518 nir_if
*if_stmt
= nir_if_create(this->shader
);
519 if_stmt
->condition
= condition
;
520 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
522 this->cf_node_list
= &if_stmt
->then_list
;
523 visit_exec_list(&ir
->then_instructions
, this);
525 this->cf_node_list
= &if_stmt
->else_list
;
526 visit_exec_list(&ir
->else_instructions
, this);
528 this->cf_node_list
= old_list
;
532 nir_visitor::visit(ir_discard
*ir
)
535 * discards aren't treated as control flow, because before we lower them
536 * they can appear anywhere in the shader and the stuff after them may still
537 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
538 * discards will be immediately followed by a return.
541 nir_intrinsic_instr
*discard
=
542 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
543 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
547 nir_visitor::visit(ir_emit_vertex
*ir
)
549 nir_intrinsic_instr
*instr
=
550 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
551 instr
->const_index
[0] = ir
->stream_id();
552 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
556 nir_visitor::visit(ir_end_primitive
*ir
)
558 nir_intrinsic_instr
*instr
=
559 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
560 instr
->const_index
[0] = ir
->stream_id();
561 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
565 nir_visitor::visit(ir_loop_jump
*ir
)
569 case ir_loop_jump::jump_break
:
570 type
= nir_jump_break
;
572 case ir_loop_jump::jump_continue
:
573 type
= nir_jump_continue
;
580 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
581 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
585 nir_visitor::visit(ir_return
*ir
)
587 if (ir
->value
!= NULL
) {
588 ir
->value
->accept(this);
589 nir_intrinsic_instr
*copy
=
590 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
592 copy
->variables
[0] = nir_deref_var_create(this->shader
,
593 this->impl
->return_var
);
594 copy
->variables
[1] = this->deref_head
;
597 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
598 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
602 nir_visitor::visit(ir_call
*ir
)
604 if (ir
->callee
->is_intrinsic
) {
606 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
607 op
= nir_intrinsic_atomic_counter_read_var
;
608 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
609 op
= nir_intrinsic_atomic_counter_inc_var
;
610 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
611 op
= nir_intrinsic_atomic_counter_dec_var
;
616 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
617 ir_dereference
*param
=
618 (ir_dereference
*) ir
->actual_parameters
.get_head();
620 instr
->variables
[0] = this->deref_head
;
621 instr
->dest
.is_ssa
= true;
622 nir_ssa_def_init(&instr
->instr
, &instr
->dest
.ssa
, 1, NULL
);
624 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
626 nir_intrinsic_instr
*store_instr
=
627 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
628 store_instr
->num_components
= 1;
630 ir
->return_deref
->accept(this);
631 store_instr
->variables
[0] = this->deref_head
;
632 store_instr
->src
[0].is_ssa
= true;
633 store_instr
->src
[0].ssa
= &instr
->dest
.ssa
;
635 nir_instr_insert_after_cf_list(this->cf_node_list
, &store_instr
->instr
);
640 struct hash_entry
*entry
=
641 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
643 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
645 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
648 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
650 instr
->params
[i
] = this->deref_head
;
654 ir
->return_deref
->accept(this);
655 instr
->return_deref
= this->deref_head
;
656 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
660 nir_visitor::visit(ir_assignment
*ir
)
662 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
664 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
665 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
666 /* We're doing a plain-as-can-be copy, so emit a copy_var */
667 nir_intrinsic_instr
*copy
=
668 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
670 ir
->lhs
->accept(this);
671 copy
->variables
[0] = this->deref_head
;
673 ir
->rhs
->accept(this);
674 copy
->variables
[1] = this->deref_head
;
678 nir_if
*if_stmt
= nir_if_create(this->shader
);
679 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
680 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
681 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
683 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
688 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
690 ir
->lhs
->accept(this);
691 nir_deref_var
*lhs_deref
= this->deref_head
;
692 nir_src src
= evaluate_rvalue(ir
->rhs
);
694 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
696 * We have no good way to update only part of a variable, so just load
697 * the LHS and do a vec operation to combine the old with the new, and
699 * back into the LHS. Copy propagation should get rid of the mess.
702 nir_intrinsic_instr
*load
=
703 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
704 load
->num_components
= ir
->lhs
->type
->vector_elements
;
705 load
->dest
.is_ssa
= true;
706 nir_ssa_def_init(&load
->instr
, &load
->dest
.ssa
,
707 num_components
, NULL
);
708 load
->variables
[0] = lhs_deref
;
709 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
712 switch (ir
->lhs
->type
->vector_elements
) {
713 case 1: vec_op
= nir_op_imov
; break;
714 case 2: vec_op
= nir_op_vec2
; break;
715 case 3: vec_op
= nir_op_vec3
; break;
716 case 4: vec_op
= nir_op_vec4
; break;
717 default: unreachable("Invalid number of components"); break;
719 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
720 vec
->dest
.dest
.is_ssa
= true;
721 nir_ssa_def_init(&vec
->instr
, &vec
->dest
.dest
.ssa
,
722 num_components
, NULL
);
723 vec
->dest
.write_mask
= (1 << num_components
) - 1;
725 unsigned component
= 0;
726 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
727 if (ir
->write_mask
& (1 << i
)) {
728 vec
->src
[i
].src
= src
;
730 /* GLSL IR will give us the input to the write-masked assignment
731 * in a single packed vector. So, for example, if the
732 * writemask is xzw, then we have to swizzle x -> x, y -> z,
733 * and z -> w and get the y component from the load.
735 vec
->src
[i
].swizzle
[0] = component
++;
737 vec
->src
[i
].src
.is_ssa
= true;
738 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
739 vec
->src
[i
].swizzle
[0] = i
;
743 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
746 src
.ssa
= &vec
->dest
.dest
.ssa
;
749 nir_intrinsic_instr
*store
=
750 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
751 store
->num_components
= ir
->lhs
->type
->vector_elements
;
752 nir_deref
*store_deref
= nir_copy_deref(this->shader
, &lhs_deref
->deref
);
753 store
->variables
[0] = nir_deref_as_var(store_deref
);
757 nir_if
*if_stmt
= nir_if_create(this->shader
);
758 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
759 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
760 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
762 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
767 * Given an instruction, returns a pointer to its destination or NULL if there
770 * Note that this only handles instructions we generate at this level.
773 get_instr_dest(nir_instr
*instr
)
775 nir_alu_instr
*alu_instr
;
776 nir_intrinsic_instr
*intrinsic_instr
;
777 nir_tex_instr
*tex_instr
;
779 switch (instr
->type
) {
780 case nir_instr_type_alu
:
781 alu_instr
= nir_instr_as_alu(instr
);
782 return &alu_instr
->dest
.dest
;
784 case nir_instr_type_intrinsic
:
785 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
786 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
787 return &intrinsic_instr
->dest
;
791 case nir_instr_type_tex
:
792 tex_instr
= nir_instr_as_tex(instr
);
793 return &tex_instr
->dest
;
804 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
806 nir_dest
*dest
= get_instr_dest(instr
);
809 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, NULL
);
811 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
812 this->result
= instr
;
816 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
819 if (ir
->as_dereference() || ir
->as_constant()) {
821 * A dereference is being used on the right hand side, which means we
822 * must emit a variable load.
825 nir_intrinsic_instr
*load_instr
=
826 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
827 load_instr
->num_components
= ir
->type
->vector_elements
;
828 load_instr
->variables
[0] = this->deref_head
;
829 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
832 nir_dest
*dest
= get_instr_dest(this->result
);
834 assert(dest
->is_ssa
);
837 src
.ssa
= &dest
->ssa
;
843 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
845 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
846 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
847 instr
->src
[i
].src
= srcs
[i
];
848 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
849 add_instr(&instr
->instr
, dest_size
);
854 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
856 assert(nir_op_infos
[op
].num_inputs
== 1);
857 return emit(op
, dest_size
, &src1
);
861 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
864 assert(nir_op_infos
[op
].num_inputs
== 2);
865 nir_src srcs
[] = { src1
, src2
};
866 return emit(op
, dest_size
, srcs
);
870 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
871 nir_src src2
, nir_src src3
)
873 assert(nir_op_infos
[op
].num_inputs
== 3);
874 nir_src srcs
[] = { src1
, src2
, src3
};
875 return emit(op
, dest_size
, srcs
);
879 nir_visitor::visit(ir_expression
*ir
)
881 /* Some special cases */
882 switch (ir
->operation
) {
883 case ir_binop_ubo_load
: {
884 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
888 op
= nir_intrinsic_load_ubo
;
890 op
= nir_intrinsic_load_ubo_indirect
;
892 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
893 load
->num_components
= ir
->type
->vector_elements
;
894 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
895 load
->const_index
[1] = 1; /* number of vec4's */
896 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
898 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
899 add_instr(&load
->instr
, ir
->type
->vector_elements
);
902 * In UBO's, a true boolean value is any non-zero value, but we consider
903 * a true boolean to be ~0. Fix this up with a != 0 comparison.
906 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
907 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
908 const_zero
->value
.u
[0] = 0;
909 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
911 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
912 compare
->src
[0].src
.is_ssa
= true;
913 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
914 compare
->src
[1].src
.is_ssa
= true;
915 compare
->src
[1].src
.ssa
= &const_zero
->def
;
916 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
917 compare
->src
[1].swizzle
[i
] = 0;
918 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
920 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
926 case ir_unop_interpolate_at_centroid
:
927 case ir_binop_interpolate_at_offset
:
928 case ir_binop_interpolate_at_sample
: {
929 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
930 ir_swizzle
*swizzle
= NULL
;
932 /* the api does not allow a swizzle here, but the varying packing code
933 * may have pushed one into here.
935 swizzle
= ir
->operands
[0]->as_swizzle();
937 deref
= swizzle
->val
->as_dereference();
944 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
945 switch (ir
->operation
) {
946 case ir_unop_interpolate_at_centroid
:
947 op
= nir_intrinsic_interp_var_at_centroid
;
949 case ir_binop_interpolate_at_offset
:
950 op
= nir_intrinsic_interp_var_at_offset
;
952 case ir_binop_interpolate_at_sample
:
953 op
= nir_intrinsic_interp_var_at_sample
;
956 unreachable("Invalid interpolation intrinsic");
959 /* This case can happen if the vertex shader does not write the
960 * given varying. In this case, the linker will lower it to a
961 * global variable. Since interpolating a variable makes no
962 * sense, we'll just turn it into a load which will probably
963 * eventually end up as an SSA definition.
965 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
966 op
= nir_intrinsic_load_var
;
969 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
970 intrin
->num_components
= deref
->type
->vector_elements
;
971 intrin
->variables
[0] = this->deref_head
;
973 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
974 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
975 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
977 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
980 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
981 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
982 mov
->src
[0].src
.is_ssa
= true;
983 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
985 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
986 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
987 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
988 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
989 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
990 mov
->src
[0].swizzle
[i
] = 0;
992 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1003 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1004 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1006 glsl_base_type types
[4];
1007 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1009 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1011 types
[i
] = GLSL_TYPE_FLOAT
;
1013 glsl_base_type out_type
;
1015 out_type
= ir
->type
->base_type
;
1017 out_type
= GLSL_TYPE_FLOAT
;
1019 unsigned dest_size
= ir
->type
->vector_elements
;
1021 nir_alu_instr
*instr
;
1024 switch (ir
->operation
) {
1025 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1026 case ir_unop_logic_not
:
1027 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1030 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1034 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1037 case ir_unop_saturate
:
1038 assert(types
[0] == GLSL_TYPE_FLOAT
);
1039 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1042 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1045 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1046 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1047 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1048 case ir_unop_exp
: emit(nir_op_fexp
, dest_size
, srcs
); break;
1049 case ir_unop_log
: emit(nir_op_flog
, dest_size
, srcs
); break;
1050 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1051 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1053 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1056 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1059 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1061 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1062 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1063 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1064 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1065 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1068 case ir_unop_bitcast_i2f
:
1069 case ir_unop_bitcast_f2i
:
1070 case ir_unop_bitcast_u2f
:
1071 case ir_unop_bitcast_f2u
:
1073 emit(nir_op_imov
, dest_size
, srcs
);
1076 switch (ir
->operands
[0]->type
->vector_elements
) {
1078 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1082 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1086 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1094 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1095 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1096 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1097 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1098 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1099 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1100 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1101 case ir_unop_sin_reduced
:
1102 emit(nir_op_fsin_reduced
, dest_size
, srcs
);
1104 case ir_unop_cos_reduced
:
1105 emit(nir_op_fcos_reduced
, dest_size
, srcs
);
1107 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1108 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1109 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1110 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1111 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1112 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1113 case ir_unop_pack_snorm_2x16
:
1114 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1116 case ir_unop_pack_snorm_4x8
:
1117 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1119 case ir_unop_pack_unorm_2x16
:
1120 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1122 case ir_unop_pack_unorm_4x8
:
1123 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1125 case ir_unop_pack_half_2x16
:
1126 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1128 case ir_unop_unpack_snorm_2x16
:
1129 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1131 case ir_unop_unpack_snorm_4x8
:
1132 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1134 case ir_unop_unpack_unorm_2x16
:
1135 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1137 case ir_unop_unpack_unorm_4x8
:
1138 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1140 case ir_unop_unpack_half_2x16
:
1141 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1143 case ir_unop_unpack_half_2x16_split_x
:
1144 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1146 case ir_unop_unpack_half_2x16_split_y
:
1147 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1149 case ir_unop_bitfield_reverse
:
1150 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1152 case ir_unop_bit_count
:
1153 emit(nir_op_bit_count
, dest_size
, srcs
);
1155 case ir_unop_find_msb
:
1157 case GLSL_TYPE_UINT
:
1158 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1161 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1164 unreachable("Invalid type for findMSB()");
1167 case ir_unop_find_lsb
:
1168 emit(nir_op_find_lsb
, dest_size
, srcs
);
1172 switch (ir
->type
->vector_elements
) {
1174 switch (ir
->operands
[0]->type
->vector_elements
) {
1175 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1176 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1177 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1178 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1179 default: assert(0); break;
1183 switch (ir
->operands
[0]->type
->vector_elements
) {
1184 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1185 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1186 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1187 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1188 default: assert(0); break;
1192 switch (ir
->operands
[0]->type
->vector_elements
) {
1193 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1194 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1195 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1196 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1197 default: assert(0); break;
1201 switch (ir
->operands
[0]->type
->vector_elements
) {
1202 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1203 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1204 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1205 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1206 default: assert(0); break;
1222 case ir_binop_bit_and
:
1223 case ir_binop_bit_or
:
1224 case ir_binop_bit_xor
:
1225 case ir_binop_lshift
:
1226 case ir_binop_rshift
:
1227 switch (ir
->operation
) {
1229 if (out_type
== GLSL_TYPE_FLOAT
)
1235 if (out_type
== GLSL_TYPE_FLOAT
)
1241 if (out_type
== GLSL_TYPE_FLOAT
)
1247 if (out_type
== GLSL_TYPE_FLOAT
)
1249 else if (out_type
== GLSL_TYPE_INT
)
1255 if (out_type
== GLSL_TYPE_FLOAT
)
1261 if (out_type
== GLSL_TYPE_FLOAT
)
1263 else if (out_type
== GLSL_TYPE_INT
)
1269 if (out_type
== GLSL_TYPE_FLOAT
)
1271 else if (out_type
== GLSL_TYPE_INT
)
1276 case ir_binop_bit_and
:
1279 case ir_binop_bit_or
:
1282 case ir_binop_bit_xor
:
1285 case ir_binop_lshift
:
1288 case ir_binop_rshift
:
1289 if (out_type
== GLSL_TYPE_INT
)
1303 instr
= emit(op
, dest_size
, srcs
);
1305 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1306 ir
->operands
[1]->type
->vector_elements
== 1) {
1307 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1309 instr
->src
[1].swizzle
[i
] = 0;
1313 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1314 ir
->operands
[0]->type
->vector_elements
== 1) {
1315 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1317 instr
->src
[0].swizzle
[i
] = 0;
1322 case ir_binop_imul_high
:
1323 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1326 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1327 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1329 if (supports_ints
) {
1330 if (types
[0] == GLSL_TYPE_FLOAT
)
1331 emit(nir_op_flt
, dest_size
, srcs
);
1332 else if (types
[0] == GLSL_TYPE_INT
)
1333 emit(nir_op_ilt
, dest_size
, srcs
);
1335 emit(nir_op_ult
, dest_size
, srcs
);
1337 emit(nir_op_slt
, dest_size
, srcs
);
1340 case ir_binop_greater
:
1341 if (supports_ints
) {
1342 if (types
[0] == GLSL_TYPE_FLOAT
)
1343 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1344 else if (types
[0] == GLSL_TYPE_INT
)
1345 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1347 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1349 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1352 case ir_binop_lequal
:
1353 if (supports_ints
) {
1354 if (types
[0] == GLSL_TYPE_FLOAT
)
1355 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1356 else if (types
[0] == GLSL_TYPE_INT
)
1357 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1359 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1361 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1364 case ir_binop_gequal
:
1365 if (supports_ints
) {
1366 if (types
[0] == GLSL_TYPE_FLOAT
)
1367 emit(nir_op_fge
, dest_size
, srcs
);
1368 else if (types
[0] == GLSL_TYPE_INT
)
1369 emit(nir_op_ige
, dest_size
, srcs
);
1371 emit(nir_op_uge
, dest_size
, srcs
);
1373 emit(nir_op_slt
, dest_size
, srcs
);
1376 case ir_binop_equal
:
1377 if (supports_ints
) {
1378 if (types
[0] == GLSL_TYPE_FLOAT
)
1379 emit(nir_op_feq
, dest_size
, srcs
);
1381 emit(nir_op_ieq
, dest_size
, srcs
);
1383 emit(nir_op_seq
, dest_size
, srcs
);
1386 case ir_binop_nequal
:
1387 if (supports_ints
) {
1388 if (types
[0] == GLSL_TYPE_FLOAT
)
1389 emit(nir_op_fne
, dest_size
, srcs
);
1391 emit(nir_op_ine
, dest_size
, srcs
);
1393 emit(nir_op_sne
, dest_size
, srcs
);
1396 case ir_binop_all_equal
:
1397 if (supports_ints
) {
1398 if (types
[0] == GLSL_TYPE_FLOAT
) {
1399 switch (ir
->operands
[0]->type
->vector_elements
) {
1400 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1401 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1402 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1403 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1409 switch (ir
->operands
[0]->type
->vector_elements
) {
1410 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1411 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1412 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1413 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1420 switch (ir
->operands
[0]->type
->vector_elements
) {
1421 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1422 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1423 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1424 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1431 case ir_binop_any_nequal
:
1432 if (supports_ints
) {
1433 if (types
[0] == GLSL_TYPE_FLOAT
) {
1434 switch (ir
->operands
[0]->type
->vector_elements
) {
1435 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1436 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1437 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1438 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1444 switch (ir
->operands
[0]->type
->vector_elements
) {
1445 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1446 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1447 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1448 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1455 switch (ir
->operands
[0]->type
->vector_elements
) {
1456 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1457 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1458 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1459 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1466 case ir_binop_logic_and
:
1468 emit(nir_op_iand
, dest_size
, srcs
);
1470 emit(nir_op_fand
, dest_size
, srcs
);
1472 case ir_binop_logic_or
:
1474 emit(nir_op_ior
, dest_size
, srcs
);
1476 emit(nir_op_for
, dest_size
, srcs
);
1478 case ir_binop_logic_xor
:
1480 emit(nir_op_ixor
, dest_size
, srcs
);
1482 emit(nir_op_fxor
, dest_size
, srcs
);
1485 switch (ir
->operands
[0]->type
->vector_elements
) {
1486 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1487 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1488 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1495 case ir_binop_pack_half_2x16_split
:
1496 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1498 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1499 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1500 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1502 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1503 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1504 ir
->operands
[2]->type
->vector_elements
== 1) {
1505 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1507 instr
->src
[2].swizzle
[i
] = 0;
1513 emit(nir_op_bcsel
, dest_size
, srcs
);
1515 emit(nir_op_fcsel
, dest_size
, srcs
);
1518 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1519 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1520 instr
->src
[0].swizzle
[i
] = 0;
1523 case ir_triop_bitfield_extract
:
1524 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1525 nir_op_ubitfield_extract
, dest_size
, srcs
);
1526 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1527 instr
->src
[1].swizzle
[i
] = 0;
1528 instr
->src
[2].swizzle
[i
] = 0;
1531 case ir_quadop_bitfield_insert
:
1532 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1533 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1534 instr
->src
[2].swizzle
[i
] = 0;
1535 instr
->src
[3].swizzle
[i
] = 0;
1538 case ir_quadop_vector
:
1539 switch (ir
->type
->vector_elements
) {
1540 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1541 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1542 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1543 default: assert(0); break;
1554 nir_visitor::visit(ir_swizzle
*ir
)
1556 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1557 ir
->type
->vector_elements
,
1558 evaluate_rvalue(ir
->val
));
1560 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1561 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1562 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1566 nir_visitor::visit(ir_texture
*ir
)
1573 num_srcs
= 1; /* coordinate */
1578 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1579 num_srcs
= 2; /* coordinate, bias/lod */
1583 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1589 if (ir
->lod_info
.lod
!= NULL
)
1590 num_srcs
= 2; /* coordinate, lod */
1592 num_srcs
= 1; /* coordinate */
1596 op
= nir_texop_txf_ms
;
1597 num_srcs
= 2; /* coordinate, sample_index */
1602 if (ir
->lod_info
.lod
!= NULL
)
1603 num_srcs
= 1; /* lod */
1610 num_srcs
= 1; /* coordinate */
1615 num_srcs
= 1; /* coordinate */
1618 case ir_query_levels
:
1619 op
= nir_texop_query_levels
;
1628 if (ir
->projector
!= NULL
)
1630 if (ir
->shadow_comparitor
!= NULL
)
1632 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1635 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1638 instr
->sampler_dim
=
1639 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1640 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1641 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1642 if (instr
->is_shadow
)
1643 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1644 switch (ir
->type
->base_type
) {
1645 case GLSL_TYPE_FLOAT
:
1646 instr
->dest_type
= nir_type_float
;
1649 instr
->dest_type
= nir_type_int
;
1651 case GLSL_TYPE_UINT
:
1652 instr
->dest_type
= nir_type_unsigned
;
1658 ir
->sampler
->accept(this);
1659 instr
->sampler
= this->deref_head
;
1661 unsigned src_number
= 0;
1663 if (ir
->coordinate
!= NULL
) {
1664 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1665 instr
->src
[src_number
] = evaluate_rvalue(ir
->coordinate
);
1666 instr
->src_type
[src_number
] = nir_tex_src_coord
;
1670 if (ir
->projector
!= NULL
) {
1671 instr
->src
[src_number
] = evaluate_rvalue(ir
->projector
);
1672 instr
->src_type
[src_number
] = nir_tex_src_projector
;
1676 if (ir
->shadow_comparitor
!= NULL
) {
1677 instr
->src
[src_number
] = evaluate_rvalue(ir
->shadow_comparitor
);
1678 instr
->src_type
[src_number
] = nir_tex_src_comparitor
;
1682 if (ir
->offset
!= NULL
) {
1683 /* we don't support multiple offsets yet */
1684 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1686 ir_constant
*const_offset
= ir
->offset
->as_constant();
1687 if (const_offset
!= NULL
) {
1688 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1689 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1691 instr
->src
[src_number
] = evaluate_rvalue(ir
->offset
);
1692 instr
->src_type
[src_number
] = nir_tex_src_offset
;
1699 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.bias
);
1700 instr
->src_type
[src_number
] = nir_tex_src_bias
;
1707 if (ir
->lod_info
.lod
!= NULL
) {
1708 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.lod
);
1709 instr
->src_type
[src_number
] = nir_tex_src_lod
;
1715 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1716 instr
->src_type
[src_number
] = nir_tex_src_ddx
;
1718 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1719 instr
->src_type
[src_number
] = nir_tex_src_ddy
;
1724 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.sample_index
);
1725 instr
->src_type
[src_number
] = nir_tex_src_ms_index
;
1730 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1737 assert(src_number
== num_srcs
);
1739 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1743 nir_visitor::visit(ir_constant
*ir
)
1746 * We don't know if this variable is an an array or struct that gets
1747 * dereferenced, so do the safe thing an make it a variable with a
1748 * constant initializer and return a dereference.
1751 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1752 var
->name
= ralloc_strdup(var
, "const_temp");
1753 var
->type
= ir
->type
;
1754 var
->data
.mode
= nir_var_local
;
1755 var
->data
.read_only
= true;
1756 var
->constant_initializer
= constant_copy(ir
, var
);
1757 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1759 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1760 this->deref_tail
= &this->deref_head
->deref
;
1764 nir_visitor::visit(ir_dereference_variable
*ir
)
1766 struct hash_entry
*entry
=
1767 _mesa_hash_table_search(this->var_table
, ir
->var
);
1769 nir_variable
*var
= (nir_variable
*) entry
->data
;
1771 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1772 this->deref_head
= deref
;
1773 this->deref_tail
= &deref
->deref
;
1777 nir_visitor::visit(ir_dereference_record
*ir
)
1779 ir
->record
->accept(this);
1781 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1782 assert(field_index
>= 0);
1784 nir_deref_struct
*deref
= nir_deref_struct_create(this->shader
, field_index
);
1785 deref
->deref
.type
= ir
->type
;
1786 this->deref_tail
->child
= &deref
->deref
;
1787 this->deref_tail
= &deref
->deref
;
1791 nir_visitor::visit(ir_dereference_array
*ir
)
1793 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1794 deref
->deref
.type
= ir
->type
;
1796 ir_constant
*const_index
= ir
->array_index
->as_constant();
1797 if (const_index
!= NULL
) {
1798 deref
->deref_array_type
= nir_deref_array_type_direct
;
1799 deref
->base_offset
= const_index
->value
.u
[0];
1801 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1802 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1805 ir
->array
->accept(this);
1807 this->deref_tail
->child
= &deref
->deref
;
1808 this->deref_tail
= &deref
->deref
;