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_value
= constant_copy(ir
->constant_value
, var
);
348 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
350 var
->interface_type
= ir
->get_interface_type();
352 switch (var
->data
.mode
) {
354 exec_list_push_tail(&impl
->locals
, &var
->node
);
358 exec_list_push_tail(&shader
->globals
, &var
->node
);
361 case nir_var_shader_in
:
362 _mesa_hash_table_insert(shader
->inputs
, var
->name
, var
);
365 case nir_var_shader_out
:
366 _mesa_hash_table_insert(shader
->outputs
, var
->name
, var
);
369 case nir_var_uniform
:
370 _mesa_hash_table_insert(shader
->uniforms
, var
->name
, var
);
373 case nir_var_system_value
:
374 exec_list_push_tail(&shader
->system_values
, &var
->node
);
382 _mesa_hash_table_insert(var_table
, ir
, var
);
387 nir_function_visitor::visit_enter(ir_function
*ir
)
389 visitor
->create_function(ir
);
390 return visit_continue_with_parent
;
395 nir_visitor::create_function(ir_function
*ir
)
397 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
398 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
399 create_overload(sig
, func
);
406 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
408 if (ir
->is_intrinsic
)
411 nir_function_overload
*overload
= nir_function_overload_create(function
);
413 unsigned num_params
= ir
->parameters
.length();
414 overload
->num_params
= num_params
;
415 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
418 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
419 switch (param
->data
.mode
) {
420 case ir_var_function_in
:
421 overload
->params
[i
].param_type
= nir_parameter_in
;
424 case ir_var_function_out
:
425 overload
->params
[i
].param_type
= nir_parameter_out
;
428 case ir_var_function_inout
:
429 overload
->params
[i
].param_type
= nir_parameter_inout
;
437 overload
->params
[i
].type
= param
->type
;
441 overload
->return_type
= ir
->return_type
;
443 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
447 nir_visitor::visit(ir_function
*ir
)
449 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
454 nir_visitor::visit(ir_function_signature
*ir
)
456 if (ir
->is_intrinsic
)
459 struct hash_entry
*entry
=
460 _mesa_hash_table_search(this->overload_table
, ir
);
463 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
465 if (ir
->is_defined
) {
466 nir_function_impl
*impl
= nir_function_impl_create(overload
);
469 unsigned num_params
= overload
->num_params
;
470 impl
->num_params
= num_params
;
471 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
473 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
475 impl
->params
[i
] = this->var
;
479 if (overload
->return_type
== glsl_type::void_type
) {
480 impl
->return_var
= NULL
;
482 impl
->return_var
= ralloc(this->shader
, nir_variable
);
483 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
485 impl
->return_var
->type
= overload
->return_type
;
488 this->is_global
= false;
490 this->cf_node_list
= &impl
->body
;
491 visit_exec_list(&ir
->body
, this);
493 this->is_global
= true;
495 overload
->impl
= NULL
;
500 nir_visitor::visit(ir_loop
*ir
)
502 exec_list
*old_list
= this->cf_node_list
;
504 nir_loop
*loop
= nir_loop_create(this->shader
);
505 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
506 this->cf_node_list
= &loop
->body
;
507 visit_exec_list(&ir
->body_instructions
, this);
509 this->cf_node_list
= old_list
;
513 nir_visitor::visit(ir_if
*ir
)
515 nir_src condition
= evaluate_rvalue(ir
->condition
);
517 exec_list
*old_list
= this->cf_node_list
;
519 nir_if
*if_stmt
= nir_if_create(this->shader
);
520 if_stmt
->condition
= condition
;
521 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
523 this->cf_node_list
= &if_stmt
->then_list
;
524 visit_exec_list(&ir
->then_instructions
, this);
526 this->cf_node_list
= &if_stmt
->else_list
;
527 visit_exec_list(&ir
->else_instructions
, this);
529 this->cf_node_list
= old_list
;
533 nir_visitor::visit(ir_discard
*ir
)
536 * discards aren't treated as control flow, because before we lower them
537 * they can appear anywhere in the shader and the stuff after them may still
538 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
539 * discards will be immediately followed by a return.
542 nir_intrinsic_instr
*discard
=
543 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
544 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
548 nir_visitor::visit(ir_emit_vertex
*ir
)
550 nir_intrinsic_instr
*instr
=
551 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
552 instr
->const_index
[0] = ir
->stream_id();
553 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
557 nir_visitor::visit(ir_end_primitive
*ir
)
559 nir_intrinsic_instr
*instr
=
560 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
561 instr
->const_index
[0] = ir
->stream_id();
562 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
566 nir_visitor::visit(ir_loop_jump
*ir
)
570 case ir_loop_jump::jump_break
:
571 type
= nir_jump_break
;
573 case ir_loop_jump::jump_continue
:
574 type
= nir_jump_continue
;
581 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
582 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
586 nir_visitor::visit(ir_return
*ir
)
588 if (ir
->value
!= NULL
) {
589 ir
->value
->accept(this);
590 nir_intrinsic_instr
*copy
=
591 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
593 copy
->variables
[0] = nir_deref_var_create(this->shader
,
594 this->impl
->return_var
);
595 copy
->variables
[1] = this->deref_head
;
598 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
599 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
603 nir_visitor::visit(ir_call
*ir
)
605 if (ir
->callee
->is_intrinsic
) {
607 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
608 op
= nir_intrinsic_atomic_counter_read_var
;
609 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
610 op
= nir_intrinsic_atomic_counter_inc_var
;
611 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
612 op
= nir_intrinsic_atomic_counter_dec_var
;
617 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
618 ir_dereference
*param
=
619 (ir_dereference
*) ir
->actual_parameters
.get_head();
621 instr
->variables
[0] = this->deref_head
;
622 instr
->dest
.is_ssa
= true;
623 nir_ssa_def_init(&instr
->instr
, &instr
->dest
.ssa
, 1, NULL
);
625 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
627 nir_intrinsic_instr
*store_instr
=
628 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
629 store_instr
->num_components
= 1;
631 ir
->return_deref
->accept(this);
632 store_instr
->variables
[0] = this->deref_head
;
633 store_instr
->src
[0].is_ssa
= true;
634 store_instr
->src
[0].ssa
= &instr
->dest
.ssa
;
636 nir_instr_insert_after_cf_list(this->cf_node_list
, &store_instr
->instr
);
641 struct hash_entry
*entry
=
642 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
644 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
646 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
649 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
651 instr
->params
[i
] = this->deref_head
;
655 ir
->return_deref
->accept(this);
656 instr
->return_deref
= this->deref_head
;
657 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
661 nir_visitor::visit(ir_assignment
*ir
)
663 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
665 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
666 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
667 /* We're doing a plain-as-can-be copy, so emit a copy_var */
668 nir_intrinsic_instr
*copy
=
669 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
671 ir
->lhs
->accept(this);
672 copy
->variables
[0] = this->deref_head
;
674 ir
->rhs
->accept(this);
675 copy
->variables
[1] = this->deref_head
;
679 nir_if
*if_stmt
= nir_if_create(this->shader
);
680 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
681 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
682 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
684 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
689 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
691 ir
->lhs
->accept(this);
692 nir_deref_var
*lhs_deref
= this->deref_head
;
693 nir_src src
= evaluate_rvalue(ir
->rhs
);
695 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
697 * We have no good way to update only part of a variable, so just load
698 * the LHS and do a vec operation to combine the old with the new, and
700 * back into the LHS. Copy propagation should get rid of the mess.
703 nir_intrinsic_instr
*load
=
704 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
705 load
->num_components
= ir
->lhs
->type
->vector_elements
;
706 load
->dest
.is_ssa
= true;
707 nir_ssa_def_init(&load
->instr
, &load
->dest
.ssa
,
708 num_components
, NULL
);
709 load
->variables
[0] = lhs_deref
;
710 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
713 switch (ir
->lhs
->type
->vector_elements
) {
714 case 1: vec_op
= nir_op_imov
; break;
715 case 2: vec_op
= nir_op_vec2
; break;
716 case 3: vec_op
= nir_op_vec3
; break;
717 case 4: vec_op
= nir_op_vec4
; break;
718 default: unreachable("Invalid number of components"); break;
720 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
721 vec
->dest
.dest
.is_ssa
= true;
722 nir_ssa_def_init(&vec
->instr
, &vec
->dest
.dest
.ssa
,
723 num_components
, NULL
);
724 vec
->dest
.write_mask
= (1 << num_components
) - 1;
726 unsigned component
= 0;
727 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
728 if (ir
->write_mask
& (1 << i
)) {
729 vec
->src
[i
].src
= src
;
731 /* GLSL IR will give us the input to the write-masked assignment
732 * in a single packed vector. So, for example, if the
733 * writemask is xzw, then we have to swizzle x -> x, y -> z,
734 * and z -> w and get the y component from the load.
736 vec
->src
[i
].swizzle
[0] = component
++;
738 vec
->src
[i
].src
.is_ssa
= true;
739 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
740 vec
->src
[i
].swizzle
[0] = i
;
744 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
747 src
.ssa
= &vec
->dest
.dest
.ssa
;
750 nir_intrinsic_instr
*store
=
751 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
752 store
->num_components
= ir
->lhs
->type
->vector_elements
;
753 nir_deref
*store_deref
= nir_copy_deref(this->shader
, &lhs_deref
->deref
);
754 store
->variables
[0] = nir_deref_as_var(store_deref
);
758 nir_if
*if_stmt
= nir_if_create(this->shader
);
759 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
760 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
761 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
763 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
768 * Given an instruction, returns a pointer to its destination or NULL if there
771 * Note that this only handles instructions we generate at this level.
774 get_instr_dest(nir_instr
*instr
)
776 nir_alu_instr
*alu_instr
;
777 nir_intrinsic_instr
*intrinsic_instr
;
778 nir_tex_instr
*tex_instr
;
779 nir_load_const_instr
*load_const_instr
;
781 switch (instr
->type
) {
782 case nir_instr_type_alu
:
783 alu_instr
= nir_instr_as_alu(instr
);
784 return &alu_instr
->dest
.dest
;
786 case nir_instr_type_intrinsic
:
787 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
788 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
789 return &intrinsic_instr
->dest
;
793 case nir_instr_type_tex
:
794 tex_instr
= nir_instr_as_tex(instr
);
795 return &tex_instr
->dest
;
797 case nir_instr_type_load_const
:
798 load_const_instr
= nir_instr_as_load_const(instr
);
799 return &load_const_instr
->dest
;
810 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
812 nir_dest
*dest
= get_instr_dest(instr
);
815 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, NULL
);
817 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
818 this->result
= instr
;
822 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
825 if (ir
->as_dereference() || ir
->as_constant()) {
827 * A dereference is being used on the right hand side, which means we
828 * must emit a variable load.
831 nir_intrinsic_instr
*load_instr
=
832 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
833 load_instr
->num_components
= ir
->type
->vector_elements
;
834 load_instr
->variables
[0] = this->deref_head
;
835 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
838 nir_dest
*dest
= get_instr_dest(this->result
);
840 assert(dest
->is_ssa
);
843 src
.ssa
= &dest
->ssa
;
849 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
851 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
852 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
853 instr
->src
[i
].src
= srcs
[i
];
854 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
855 add_instr(&instr
->instr
, dest_size
);
860 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
862 assert(nir_op_infos
[op
].num_inputs
== 1);
863 return emit(op
, dest_size
, &src1
);
867 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
870 assert(nir_op_infos
[op
].num_inputs
== 2);
871 nir_src srcs
[] = { src1
, src2
};
872 return emit(op
, dest_size
, srcs
);
876 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
877 nir_src src2
, nir_src src3
)
879 assert(nir_op_infos
[op
].num_inputs
== 3);
880 nir_src srcs
[] = { src1
, src2
, src3
};
881 return emit(op
, dest_size
, srcs
);
885 nir_visitor::visit(ir_expression
*ir
)
887 /* Some special cases */
888 switch (ir
->operation
) {
889 case ir_binop_ubo_load
: {
890 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
894 op
= nir_intrinsic_load_ubo
;
896 op
= nir_intrinsic_load_ubo_indirect
;
898 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
899 load
->num_components
= ir
->type
->vector_elements
;
900 load
->const_index
[0] = ir
->operands
[0]->as_constant()->value
.u
[0];
901 load
->const_index
[1] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
902 load
->const_index
[2] = 1; /* number of vec4's */
904 load
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
905 add_instr(&load
->instr
, ir
->type
->vector_elements
);
908 * In UBO's, a true boolean value is any non-zero value, but we consider
909 * a true boolean to be ~0. Fix this up with a != 0 comparison.
912 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
913 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
);
914 const_zero
->num_components
= 1;
915 const_zero
->value
.u
[0] = 0;
916 const_zero
->dest
.is_ssa
= true;
917 nir_ssa_def_init(&const_zero
->instr
, &const_zero
->dest
.ssa
, 1, NULL
);
918 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
920 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
921 compare
->src
[0].src
.is_ssa
= true;
922 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
923 compare
->src
[1].src
.is_ssa
= true;
924 compare
->src
[1].src
.ssa
= &const_zero
->dest
.ssa
;
925 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
926 compare
->src
[1].swizzle
[i
] = 0;
927 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
929 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
935 case ir_unop_interpolate_at_centroid
:
936 case ir_binop_interpolate_at_offset
:
937 case ir_binop_interpolate_at_sample
: {
938 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
939 ir_swizzle
*swizzle
= NULL
;
941 /* the api does not allow a swizzle here, but the varying packing code
942 * may have pushed one into here.
944 swizzle
= ir
->operands
[0]->as_swizzle();
946 deref
= swizzle
->val
->as_dereference();
953 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
954 switch (ir
->operation
) {
955 case ir_unop_interpolate_at_centroid
:
956 op
= nir_intrinsic_interp_var_at_centroid
;
958 case ir_binop_interpolate_at_offset
:
959 op
= nir_intrinsic_interp_var_at_offset
;
961 case ir_binop_interpolate_at_sample
:
962 op
= nir_intrinsic_interp_var_at_sample
;
965 unreachable("Invalid interpolation intrinsic");
968 /* This case can happen if the vertex shader does not write the
969 * given varying. In this case, the linker will lower it to a
970 * global variable. Since interpolating a variable makes no
971 * sense, we'll just turn it into a load which will probably
972 * eventually end up as an SSA definition.
974 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
975 op
= nir_intrinsic_load_var
;
978 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
979 intrin
->num_components
= deref
->type
->vector_elements
;
980 intrin
->variables
[0] = this->deref_head
;
982 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
983 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
984 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
986 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
989 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
990 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
991 mov
->src
[0].src
.is_ssa
= true;
992 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
994 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
995 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
996 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
997 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
998 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
999 mov
->src
[0].swizzle
[i
] = 0;
1001 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1012 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1013 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1015 glsl_base_type types
[4];
1016 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1018 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1020 types
[i
] = GLSL_TYPE_FLOAT
;
1022 glsl_base_type out_type
;
1024 out_type
= ir
->type
->base_type
;
1026 out_type
= GLSL_TYPE_FLOAT
;
1028 unsigned dest_size
= ir
->type
->vector_elements
;
1030 nir_alu_instr
*instr
;
1033 switch (ir
->operation
) {
1034 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1035 case ir_unop_logic_not
:
1036 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1039 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1043 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1046 case ir_unop_saturate
:
1047 assert(types
[0] == GLSL_TYPE_FLOAT
);
1048 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1051 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1054 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1055 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1056 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1057 case ir_unop_exp
: emit(nir_op_fexp
, dest_size
, srcs
); break;
1058 case ir_unop_log
: emit(nir_op_flog
, dest_size
, srcs
); break;
1059 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1060 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1062 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1065 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1068 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1070 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1071 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1072 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1073 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1074 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1077 case ir_unop_bitcast_i2f
:
1078 case ir_unop_bitcast_f2i
:
1079 case ir_unop_bitcast_u2f
:
1080 case ir_unop_bitcast_f2u
:
1082 emit(nir_op_imov
, dest_size
, srcs
);
1085 switch (ir
->operands
[0]->type
->vector_elements
) {
1087 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1091 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1095 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1103 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1104 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1105 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1106 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1107 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1108 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1109 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1110 case ir_unop_sin_reduced
:
1111 emit(nir_op_fsin_reduced
, dest_size
, srcs
);
1113 case ir_unop_cos_reduced
:
1114 emit(nir_op_fcos_reduced
, dest_size
, srcs
);
1116 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1117 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1118 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1119 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1120 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1121 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1122 case ir_unop_pack_snorm_2x16
:
1123 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1125 case ir_unop_pack_snorm_4x8
:
1126 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1128 case ir_unop_pack_unorm_2x16
:
1129 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1131 case ir_unop_pack_unorm_4x8
:
1132 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1134 case ir_unop_pack_half_2x16
:
1135 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1137 case ir_unop_unpack_snorm_2x16
:
1138 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1140 case ir_unop_unpack_snorm_4x8
:
1141 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1143 case ir_unop_unpack_unorm_2x16
:
1144 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1146 case ir_unop_unpack_unorm_4x8
:
1147 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1149 case ir_unop_unpack_half_2x16
:
1150 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1152 case ir_unop_unpack_half_2x16_split_x
:
1153 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1155 case ir_unop_unpack_half_2x16_split_y
:
1156 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1158 case ir_unop_bitfield_reverse
:
1159 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1161 case ir_unop_bit_count
:
1162 emit(nir_op_bit_count
, dest_size
, srcs
);
1164 case ir_unop_find_msb
:
1166 case GLSL_TYPE_UINT
:
1167 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1170 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1173 unreachable("Invalid type for findMSB()");
1176 case ir_unop_find_lsb
:
1177 emit(nir_op_find_lsb
, dest_size
, srcs
);
1181 switch (ir
->type
->vector_elements
) {
1183 switch (ir
->operands
[0]->type
->vector_elements
) {
1184 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1185 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1186 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1187 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1188 default: assert(0); break;
1192 switch (ir
->operands
[0]->type
->vector_elements
) {
1193 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1194 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1195 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1196 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1197 default: assert(0); break;
1201 switch (ir
->operands
[0]->type
->vector_elements
) {
1202 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1203 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1204 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1205 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1206 default: assert(0); break;
1210 switch (ir
->operands
[0]->type
->vector_elements
) {
1211 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1212 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1213 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1214 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1215 default: assert(0); break;
1231 case ir_binop_bit_and
:
1232 case ir_binop_bit_or
:
1233 case ir_binop_bit_xor
:
1234 case ir_binop_lshift
:
1235 case ir_binop_rshift
:
1236 switch (ir
->operation
) {
1238 if (out_type
== GLSL_TYPE_FLOAT
)
1244 if (out_type
== GLSL_TYPE_FLOAT
)
1250 if (out_type
== GLSL_TYPE_FLOAT
)
1256 if (out_type
== GLSL_TYPE_FLOAT
)
1258 else if (out_type
== GLSL_TYPE_INT
)
1264 if (out_type
== GLSL_TYPE_FLOAT
)
1270 if (out_type
== GLSL_TYPE_FLOAT
)
1272 else if (out_type
== GLSL_TYPE_INT
)
1278 if (out_type
== GLSL_TYPE_FLOAT
)
1280 else if (out_type
== GLSL_TYPE_INT
)
1285 case ir_binop_bit_and
:
1288 case ir_binop_bit_or
:
1291 case ir_binop_bit_xor
:
1294 case ir_binop_lshift
:
1297 case ir_binop_rshift
:
1298 if (out_type
== GLSL_TYPE_INT
)
1312 instr
= emit(op
, dest_size
, srcs
);
1314 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1315 ir
->operands
[1]->type
->vector_elements
== 1) {
1316 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1318 instr
->src
[1].swizzle
[i
] = 0;
1322 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1323 ir
->operands
[0]->type
->vector_elements
== 1) {
1324 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1326 instr
->src
[0].swizzle
[i
] = 0;
1331 case ir_binop_imul_high
:
1332 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1335 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1336 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1338 if (supports_ints
) {
1339 if (types
[0] == GLSL_TYPE_FLOAT
)
1340 emit(nir_op_flt
, dest_size
, srcs
);
1341 else if (types
[0] == GLSL_TYPE_INT
)
1342 emit(nir_op_ilt
, dest_size
, srcs
);
1344 emit(nir_op_ult
, dest_size
, srcs
);
1346 emit(nir_op_slt
, dest_size
, srcs
);
1349 case ir_binop_greater
:
1350 if (supports_ints
) {
1351 if (types
[0] == GLSL_TYPE_FLOAT
)
1352 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1353 else if (types
[0] == GLSL_TYPE_INT
)
1354 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1356 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1358 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1361 case ir_binop_lequal
:
1362 if (supports_ints
) {
1363 if (types
[0] == GLSL_TYPE_FLOAT
)
1364 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1365 else if (types
[0] == GLSL_TYPE_INT
)
1366 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1368 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1370 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1373 case ir_binop_gequal
:
1374 if (supports_ints
) {
1375 if (types
[0] == GLSL_TYPE_FLOAT
)
1376 emit(nir_op_fge
, dest_size
, srcs
);
1377 else if (types
[0] == GLSL_TYPE_INT
)
1378 emit(nir_op_ige
, dest_size
, srcs
);
1380 emit(nir_op_uge
, dest_size
, srcs
);
1382 emit(nir_op_slt
, dest_size
, srcs
);
1385 case ir_binop_equal
:
1386 if (supports_ints
) {
1387 if (types
[0] == GLSL_TYPE_FLOAT
)
1388 emit(nir_op_feq
, dest_size
, srcs
);
1390 emit(nir_op_ieq
, dest_size
, srcs
);
1392 emit(nir_op_seq
, dest_size
, srcs
);
1395 case ir_binop_nequal
:
1396 if (supports_ints
) {
1397 if (types
[0] == GLSL_TYPE_FLOAT
)
1398 emit(nir_op_fne
, dest_size
, srcs
);
1400 emit(nir_op_ine
, dest_size
, srcs
);
1402 emit(nir_op_sne
, dest_size
, srcs
);
1405 case ir_binop_all_equal
:
1406 if (supports_ints
) {
1407 if (types
[0] == GLSL_TYPE_FLOAT
) {
1408 switch (ir
->operands
[0]->type
->vector_elements
) {
1409 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1410 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1411 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1412 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1418 switch (ir
->operands
[0]->type
->vector_elements
) {
1419 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1420 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1421 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1422 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1429 switch (ir
->operands
[0]->type
->vector_elements
) {
1430 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1431 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1432 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1433 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1440 case ir_binop_any_nequal
:
1441 if (supports_ints
) {
1442 if (types
[0] == GLSL_TYPE_FLOAT
) {
1443 switch (ir
->operands
[0]->type
->vector_elements
) {
1444 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1445 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1446 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1447 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1453 switch (ir
->operands
[0]->type
->vector_elements
) {
1454 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1455 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1456 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1457 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1464 switch (ir
->operands
[0]->type
->vector_elements
) {
1465 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1466 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1467 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1468 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1475 case ir_binop_logic_and
:
1477 emit(nir_op_iand
, dest_size
, srcs
);
1479 emit(nir_op_fand
, dest_size
, srcs
);
1481 case ir_binop_logic_or
:
1483 emit(nir_op_ior
, dest_size
, srcs
);
1485 emit(nir_op_for
, dest_size
, srcs
);
1487 case ir_binop_logic_xor
:
1489 emit(nir_op_ixor
, dest_size
, srcs
);
1491 emit(nir_op_fxor
, dest_size
, srcs
);
1494 switch (ir
->operands
[0]->type
->vector_elements
) {
1495 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1496 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1497 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1504 case ir_binop_pack_half_2x16_split
:
1505 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1507 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1508 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1509 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1511 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1512 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1513 ir
->operands
[2]->type
->vector_elements
== 1) {
1514 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1516 instr
->src
[2].swizzle
[i
] = 0;
1522 emit(nir_op_bcsel
, dest_size
, srcs
);
1524 emit(nir_op_fcsel
, dest_size
, srcs
);
1527 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1528 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1529 instr
->src
[0].swizzle
[i
] = 0;
1532 case ir_triop_bitfield_extract
:
1533 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1534 nir_op_ubitfield_extract
, dest_size
, srcs
);
1535 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1536 instr
->src
[1].swizzle
[i
] = 0;
1537 instr
->src
[2].swizzle
[i
] = 0;
1540 case ir_quadop_bitfield_insert
:
1541 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1542 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1543 instr
->src
[2].swizzle
[i
] = 0;
1544 instr
->src
[3].swizzle
[i
] = 0;
1547 case ir_quadop_vector
:
1548 switch (ir
->type
->vector_elements
) {
1549 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1550 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1551 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1552 default: assert(0); break;
1563 nir_visitor::visit(ir_swizzle
*ir
)
1565 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1566 ir
->type
->vector_elements
,
1567 evaluate_rvalue(ir
->val
));
1569 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1570 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1571 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1575 nir_visitor::visit(ir_texture
*ir
)
1582 num_srcs
= 1; /* coordinate */
1587 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1588 num_srcs
= 2; /* coordinate, bias/lod */
1592 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1598 if (ir
->lod_info
.lod
!= NULL
)
1599 num_srcs
= 2; /* coordinate, lod */
1601 num_srcs
= 1; /* coordinate */
1605 op
= nir_texop_txf_ms
;
1606 num_srcs
= 2; /* coordinate, sample_index */
1611 if (ir
->lod_info
.lod
!= NULL
)
1612 num_srcs
= 1; /* lod */
1619 num_srcs
= 1; /* coordinate */
1624 num_srcs
= 1; /* coordinate */
1627 case ir_query_levels
:
1628 op
= nir_texop_query_levels
;
1637 if (ir
->projector
!= NULL
)
1639 if (ir
->shadow_comparitor
!= NULL
)
1641 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1644 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1647 instr
->sampler_dim
=
1648 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1649 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1650 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1651 if (instr
->is_shadow
)
1652 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1653 switch (ir
->type
->base_type
) {
1654 case GLSL_TYPE_FLOAT
:
1655 instr
->dest_type
= nir_type_float
;
1658 instr
->dest_type
= nir_type_int
;
1660 case GLSL_TYPE_UINT
:
1661 instr
->dest_type
= nir_type_unsigned
;
1667 ir
->sampler
->accept(this);
1668 instr
->sampler
= this->deref_head
;
1670 unsigned src_number
= 0;
1672 if (ir
->coordinate
!= NULL
) {
1673 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1674 instr
->src
[src_number
] = evaluate_rvalue(ir
->coordinate
);
1675 instr
->src_type
[src_number
] = nir_tex_src_coord
;
1679 if (ir
->projector
!= NULL
) {
1680 instr
->src
[src_number
] = evaluate_rvalue(ir
->projector
);
1681 instr
->src_type
[src_number
] = nir_tex_src_projector
;
1685 if (ir
->shadow_comparitor
!= NULL
) {
1686 instr
->src
[src_number
] = evaluate_rvalue(ir
->shadow_comparitor
);
1687 instr
->src_type
[src_number
] = nir_tex_src_comparitor
;
1691 if (ir
->offset
!= NULL
) {
1692 /* we don't support multiple offsets yet */
1693 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1695 ir_constant
*const_offset
= ir
->offset
->as_constant();
1696 if (const_offset
!= NULL
) {
1697 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1698 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1700 instr
->src
[src_number
] = evaluate_rvalue(ir
->offset
);
1701 instr
->src_type
[src_number
] = nir_tex_src_offset
;
1708 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.bias
);
1709 instr
->src_type
[src_number
] = nir_tex_src_bias
;
1716 if (ir
->lod_info
.lod
!= NULL
) {
1717 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.lod
);
1718 instr
->src_type
[src_number
] = nir_tex_src_lod
;
1724 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
1725 instr
->src_type
[src_number
] = nir_tex_src_ddx
;
1727 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
1728 instr
->src_type
[src_number
] = nir_tex_src_ddy
;
1733 instr
->src
[src_number
] = evaluate_rvalue(ir
->lod_info
.sample_index
);
1734 instr
->src_type
[src_number
] = nir_tex_src_ms_index
;
1739 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1746 assert(src_number
== num_srcs
);
1748 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1752 nir_visitor::visit(ir_constant
*ir
)
1755 * We don't know if this variable is an an array or struct that gets
1756 * dereferenced, so do the safe thing an make it a variable and return a
1760 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
1761 var
->name
= ralloc_strdup(var
, "const_temp");
1762 var
->type
= ir
->type
;
1763 var
->data
.mode
= nir_var_local
;
1764 var
->data
.read_only
= true;
1765 var
->constant_value
= constant_copy(ir
, var
);
1766 var
->constant_initializer
= constant_copy(ir
, var
);
1767 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
1769 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1770 this->deref_tail
= &this->deref_head
->deref
;
1774 nir_visitor::visit(ir_dereference_variable
*ir
)
1776 struct hash_entry
*entry
=
1777 _mesa_hash_table_search(this->var_table
, ir
->var
);
1779 nir_variable
*var
= (nir_variable
*) entry
->data
;
1781 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1782 this->deref_head
= deref
;
1783 this->deref_tail
= &deref
->deref
;
1787 nir_visitor::visit(ir_dereference_record
*ir
)
1789 ir
->record
->accept(this);
1791 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1792 assert(field_index
>= 0);
1794 nir_deref_struct
*deref
= nir_deref_struct_create(this->shader
, field_index
);
1795 deref
->deref
.type
= ir
->type
;
1796 this->deref_tail
->child
= &deref
->deref
;
1797 this->deref_tail
= &deref
->deref
;
1801 nir_visitor::visit(ir_dereference_array
*ir
)
1803 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1804 deref
->deref
.type
= ir
->type
;
1806 ir_constant
*const_index
= ir
->array_index
->as_constant();
1807 if (const_index
!= NULL
) {
1808 deref
->deref_array_type
= nir_deref_array_type_direct
;
1809 deref
->base_offset
= const_index
->value
.u
[0];
1811 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1812 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
1815 ir
->array
->accept(this);
1817 this->deref_tail
->child
= &deref
->deref
;
1818 this->deref_tail
= &deref
->deref
;