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 "nir_control_flow.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
35 * pass to lower GLSL IR to NIR
37 * This will lower variable dereferences to loads/stores of corresponding
38 * variables in NIR - the variables will be converted to registers in a later
44 class nir_visitor
: public ir_visitor
47 nir_visitor(nir_shader
*shader
);
50 virtual void visit(ir_variable
*);
51 virtual void visit(ir_function
*);
52 virtual void visit(ir_function_signature
*);
53 virtual void visit(ir_loop
*);
54 virtual void visit(ir_if
*);
55 virtual void visit(ir_discard
*);
56 virtual void visit(ir_loop_jump
*);
57 virtual void visit(ir_return
*);
58 virtual void visit(ir_call
*);
59 virtual void visit(ir_assignment
*);
60 virtual void visit(ir_emit_vertex
*);
61 virtual void visit(ir_end_primitive
*);
62 virtual void visit(ir_expression
*);
63 virtual void visit(ir_swizzle
*);
64 virtual void visit(ir_texture
*);
65 virtual void visit(ir_constant
*);
66 virtual void visit(ir_dereference_variable
*);
67 virtual void visit(ir_dereference_record
*);
68 virtual void visit(ir_dereference_array
*);
69 virtual void visit(ir_barrier
*);
71 void create_function(ir_function
*ir
);
74 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
75 void add_instr(nir_instr
*instr
, unsigned num_components
);
76 nir_src
evaluate_rvalue(ir_rvalue
*ir
);
78 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
);
79 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
,
82 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
83 nir_src src2
, nir_src src3
);
88 nir_function_impl
*impl
;
89 exec_list
*cf_node_list
;
90 nir_instr
*result
; /* result of the expression tree last visited */
92 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
94 /* the head of the dereference chain we're creating */
95 nir_deref_var
*deref_head
;
96 /* the tail of the dereference chain we're creating */
97 nir_deref
*deref_tail
;
99 nir_variable
*var
; /* variable created by ir_variable visitor */
101 /* whether the IR we're operating on is per-function or global */
104 /* map of ir_variable -> nir_variable */
105 struct hash_table
*var_table
;
107 /* map of ir_function_signature -> nir_function_overload */
108 struct hash_table
*overload_table
;
112 * This visitor runs before the main visitor, calling create_function() for
113 * each function so that the main visitor can resolve forward references in
117 class nir_function_visitor
: public ir_hierarchical_visitor
120 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
123 virtual ir_visitor_status
visit_enter(ir_function
*);
126 nir_visitor
*visitor
;
129 }; /* end of anonymous namespace */
132 glsl_to_nir(struct gl_shader
*sh
, const nir_shader_compiler_options
*options
)
134 nir_shader
*shader
= nir_shader_create(NULL
, sh
->Stage
, options
);
136 nir_visitor
v1(shader
);
137 nir_function_visitor
v2(&v1
);
139 visit_exec_list(sh
->ir
, &v1
);
141 nir_lower_outputs_to_temporaries(shader
);
143 shader
->gs
.vertices_out
= sh
->Geom
.VerticesOut
;
144 shader
->gs
.invocations
= sh
->Geom
.Invocations
;
149 nir_visitor::nir_visitor(nir_shader
*shader
)
151 this->supports_ints
= shader
->options
->native_integers
;
152 this->shader
= shader
;
153 this->is_global
= true;
154 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
155 _mesa_key_pointer_equal
);
156 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
157 _mesa_key_pointer_equal
);
160 nir_visitor::~nir_visitor()
162 _mesa_hash_table_destroy(this->var_table
, NULL
);
163 _mesa_hash_table_destroy(this->overload_table
, NULL
);
167 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
170 ralloc_steal(mem_ctx
, this->deref_head
);
171 return this->deref_head
;
174 static nir_constant
*
175 constant_copy(ir_constant
*ir
, void *mem_ctx
)
180 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
182 unsigned total_elems
= ir
->type
->components();
184 switch (ir
->type
->base_type
) {
186 for (i
= 0; i
< total_elems
; i
++)
187 ret
->value
.u
[i
] = ir
->value
.u
[i
];
191 for (i
= 0; i
< total_elems
; i
++)
192 ret
->value
.i
[i
] = ir
->value
.i
[i
];
195 case GLSL_TYPE_FLOAT
:
196 for (i
= 0; i
< total_elems
; i
++)
197 ret
->value
.f
[i
] = ir
->value
.f
[i
];
201 for (i
= 0; i
< total_elems
; i
++)
202 ret
->value
.b
[i
] = ir
->value
.b
[i
];
205 case GLSL_TYPE_STRUCT
:
206 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
209 foreach_in_list(ir_constant
, field
, &ir
->components
) {
210 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
215 case GLSL_TYPE_ARRAY
:
216 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
219 for (i
= 0; i
< ir
->type
->length
; i
++)
220 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
224 unreachable("not reached");
231 nir_visitor::visit(ir_variable
*ir
)
233 nir_variable
*var
= ralloc(shader
, nir_variable
);
234 var
->type
= ir
->type
;
235 var
->name
= ralloc_strdup(var
, ir
->name
);
237 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
238 unsigned size
= ir
->get_interface_type()->length
;
239 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
240 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
241 size
* sizeof(unsigned));
243 var
->max_ifc_array_access
= NULL
;
246 var
->data
.read_only
= ir
->data
.read_only
;
247 var
->data
.centroid
= ir
->data
.centroid
;
248 var
->data
.sample
= ir
->data
.sample
;
249 var
->data
.invariant
= ir
->data
.invariant
;
250 var
->data
.location
= ir
->data
.location
;
252 switch(ir
->data
.mode
) {
254 case ir_var_temporary
:
256 var
->data
.mode
= nir_var_global
;
258 var
->data
.mode
= nir_var_local
;
261 case ir_var_function_in
:
262 case ir_var_function_out
:
263 case ir_var_function_inout
:
264 case ir_var_const_in
:
265 var
->data
.mode
= nir_var_local
;
268 case ir_var_shader_in
:
269 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
270 ir
->data
.location
== VARYING_SLOT_FACE
) {
271 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
272 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
273 var
->data
.mode
= nir_var_system_value
;
274 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
275 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
276 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
277 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
278 var
->data
.mode
= nir_var_system_value
;
280 var
->data
.mode
= nir_var_shader_in
;
284 case ir_var_shader_out
:
285 var
->data
.mode
= nir_var_shader_out
;
289 var
->data
.mode
= nir_var_uniform
;
292 case ir_var_shader_storage
:
293 var
->data
.mode
= nir_var_shader_storage
;
296 case ir_var_system_value
:
297 var
->data
.mode
= nir_var_system_value
;
301 unreachable("not reached");
304 var
->data
.interpolation
= ir
->data
.interpolation
;
305 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
306 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
307 var
->data
.explicit_location
= ir
->data
.explicit_location
;
308 var
->data
.explicit_index
= ir
->data
.explicit_index
;
309 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
310 var
->data
.has_initializer
= ir
->data
.has_initializer
;
311 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
312 var
->data
.location_frac
= ir
->data
.location_frac
;
313 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
314 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
316 switch (ir
->data
.depth_layout
) {
317 case ir_depth_layout_none
:
318 var
->data
.depth_layout
= nir_depth_layout_none
;
320 case ir_depth_layout_any
:
321 var
->data
.depth_layout
= nir_depth_layout_any
;
323 case ir_depth_layout_greater
:
324 var
->data
.depth_layout
= nir_depth_layout_greater
;
326 case ir_depth_layout_less
:
327 var
->data
.depth_layout
= nir_depth_layout_less
;
329 case ir_depth_layout_unchanged
:
330 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
333 unreachable("not reached");
336 var
->data
.index
= ir
->data
.index
;
337 var
->data
.binding
= ir
->data
.binding
;
338 /* XXX Get rid of buffer_index */
339 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
340 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
341 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
342 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
343 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
344 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
345 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
346 var
->data
.image
.format
= ir
->data
.image_format
;
347 var
->data
.max_array_access
= ir
->data
.max_array_access
;
349 var
->num_state_slots
= ir
->get_num_state_slots();
350 if (var
->num_state_slots
> 0) {
351 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
352 var
->num_state_slots
);
354 ir_state_slot
*state_slots
= ir
->get_state_slots();
355 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
356 for (unsigned j
= 0; j
< 5; j
++)
357 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
358 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
361 var
->state_slots
= NULL
;
364 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
366 var
->interface_type
= ir
->get_interface_type();
368 switch (var
->data
.mode
) {
370 exec_list_push_tail(&impl
->locals
, &var
->node
);
374 exec_list_push_tail(&shader
->globals
, &var
->node
);
377 case nir_var_shader_in
:
378 exec_list_push_tail(&shader
->inputs
, &var
->node
);
381 case nir_var_shader_out
:
382 exec_list_push_tail(&shader
->outputs
, &var
->node
);
385 case nir_var_uniform
:
386 case nir_var_shader_storage
:
387 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
390 case nir_var_system_value
:
391 exec_list_push_tail(&shader
->system_values
, &var
->node
);
395 unreachable("not reached");
398 _mesa_hash_table_insert(var_table
, ir
, var
);
403 nir_function_visitor::visit_enter(ir_function
*ir
)
405 visitor
->create_function(ir
);
406 return visit_continue_with_parent
;
411 nir_visitor::create_function(ir_function
*ir
)
413 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
414 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
415 create_overload(sig
, func
);
422 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
424 if (ir
->is_intrinsic
)
427 nir_function_overload
*overload
= nir_function_overload_create(function
);
429 unsigned num_params
= ir
->parameters
.length();
430 overload
->num_params
= num_params
;
431 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
434 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
435 switch (param
->data
.mode
) {
436 case ir_var_function_in
:
437 overload
->params
[i
].param_type
= nir_parameter_in
;
440 case ir_var_function_out
:
441 overload
->params
[i
].param_type
= nir_parameter_out
;
444 case ir_var_function_inout
:
445 overload
->params
[i
].param_type
= nir_parameter_inout
;
449 unreachable("not reached");
452 overload
->params
[i
].type
= param
->type
;
456 overload
->return_type
= ir
->return_type
;
458 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
462 nir_visitor::visit(ir_function
*ir
)
464 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
469 nir_visitor::visit(ir_function_signature
*ir
)
471 if (ir
->is_intrinsic
)
474 struct hash_entry
*entry
=
475 _mesa_hash_table_search(this->overload_table
, ir
);
478 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
480 if (ir
->is_defined
) {
481 nir_function_impl
*impl
= nir_function_impl_create(overload
);
484 unsigned num_params
= overload
->num_params
;
485 impl
->num_params
= num_params
;
486 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
488 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
490 impl
->params
[i
] = this->var
;
494 if (overload
->return_type
== glsl_type::void_type
) {
495 impl
->return_var
= NULL
;
497 impl
->return_var
= ralloc(this->shader
, nir_variable
);
498 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
500 impl
->return_var
->type
= overload
->return_type
;
503 this->is_global
= false;
505 this->cf_node_list
= &impl
->body
;
506 visit_exec_list(&ir
->body
, this);
508 this->is_global
= true;
510 overload
->impl
= NULL
;
515 nir_visitor::visit(ir_loop
*ir
)
517 exec_list
*old_list
= this->cf_node_list
;
519 nir_loop
*loop
= nir_loop_create(this->shader
);
520 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
521 this->cf_node_list
= &loop
->body
;
522 visit_exec_list(&ir
->body_instructions
, this);
524 this->cf_node_list
= old_list
;
528 nir_visitor::visit(ir_if
*ir
)
530 nir_src condition
= evaluate_rvalue(ir
->condition
);
532 exec_list
*old_list
= this->cf_node_list
;
534 nir_if
*if_stmt
= nir_if_create(this->shader
);
535 if_stmt
->condition
= condition
;
536 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
538 this->cf_node_list
= &if_stmt
->then_list
;
539 visit_exec_list(&ir
->then_instructions
, this);
541 this->cf_node_list
= &if_stmt
->else_list
;
542 visit_exec_list(&ir
->else_instructions
, this);
544 this->cf_node_list
= old_list
;
548 nir_visitor::visit(ir_discard
*ir
)
551 * discards aren't treated as control flow, because before we lower them
552 * they can appear anywhere in the shader and the stuff after them may still
553 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
554 * discards will be immediately followed by a return.
557 nir_intrinsic_instr
*discard
;
559 discard
= nir_intrinsic_instr_create(this->shader
,
560 nir_intrinsic_discard_if
);
561 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
563 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
565 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
569 nir_visitor::visit(ir_emit_vertex
*ir
)
571 nir_intrinsic_instr
*instr
=
572 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
573 instr
->const_index
[0] = ir
->stream_id();
574 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
578 nir_visitor::visit(ir_end_primitive
*ir
)
580 nir_intrinsic_instr
*instr
=
581 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
582 instr
->const_index
[0] = ir
->stream_id();
583 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
587 nir_visitor::visit(ir_loop_jump
*ir
)
591 case ir_loop_jump::jump_break
:
592 type
= nir_jump_break
;
594 case ir_loop_jump::jump_continue
:
595 type
= nir_jump_continue
;
598 unreachable("not reached");
601 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
602 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
606 nir_visitor::visit(ir_return
*ir
)
608 if (ir
->value
!= NULL
) {
609 nir_intrinsic_instr
*copy
=
610 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
612 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
613 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
616 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
617 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
621 nir_visitor::visit(ir_call
*ir
)
623 if (ir
->callee
->is_intrinsic
) {
625 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
626 op
= nir_intrinsic_atomic_counter_read_var
;
627 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
628 op
= nir_intrinsic_atomic_counter_inc_var
;
629 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
630 op
= nir_intrinsic_atomic_counter_dec_var
;
631 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
632 op
= nir_intrinsic_image_load
;
633 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
634 op
= nir_intrinsic_image_store
;
635 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
636 op
= nir_intrinsic_image_atomic_add
;
637 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
638 op
= nir_intrinsic_image_atomic_min
;
639 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
640 op
= nir_intrinsic_image_atomic_max
;
641 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
642 op
= nir_intrinsic_image_atomic_and
;
643 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
644 op
= nir_intrinsic_image_atomic_or
;
645 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
646 op
= nir_intrinsic_image_atomic_xor
;
647 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
648 op
= nir_intrinsic_image_atomic_exchange
;
649 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
650 op
= nir_intrinsic_image_atomic_comp_swap
;
651 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
652 op
= nir_intrinsic_memory_barrier
;
653 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
654 op
= nir_intrinsic_image_size
;
655 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
656 op
= nir_intrinsic_image_samples
;
657 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
658 op
= nir_intrinsic_store_ssbo
;
659 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
660 op
= nir_intrinsic_load_ssbo
;
661 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
662 op
= nir_intrinsic_ssbo_atomic_add
;
663 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
664 op
= nir_intrinsic_ssbo_atomic_and
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
666 op
= nir_intrinsic_ssbo_atomic_or
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
668 op
= nir_intrinsic_ssbo_atomic_xor
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
670 op
= nir_intrinsic_ssbo_atomic_min
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
672 op
= nir_intrinsic_ssbo_atomic_max
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
674 op
= nir_intrinsic_ssbo_atomic_exchange
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
676 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
678 unreachable("not reached");
681 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
682 nir_dest
*dest
= &instr
->dest
;
685 case nir_intrinsic_atomic_counter_read_var
:
686 case nir_intrinsic_atomic_counter_inc_var
:
687 case nir_intrinsic_atomic_counter_dec_var
: {
688 ir_dereference
*param
=
689 (ir_dereference
*) ir
->actual_parameters
.get_head();
690 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
691 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
692 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
695 case nir_intrinsic_image_load
:
696 case nir_intrinsic_image_store
:
697 case nir_intrinsic_image_atomic_add
:
698 case nir_intrinsic_image_atomic_min
:
699 case nir_intrinsic_image_atomic_max
:
700 case nir_intrinsic_image_atomic_and
:
701 case nir_intrinsic_image_atomic_or
:
702 case nir_intrinsic_image_atomic_xor
:
703 case nir_intrinsic_image_atomic_exchange
:
704 case nir_intrinsic_image_atomic_comp_swap
:
705 case nir_intrinsic_image_samples
:
706 case nir_intrinsic_image_size
: {
707 nir_ssa_undef_instr
*instr_undef
=
708 nir_ssa_undef_instr_create(shader
, 1);
709 nir_instr_insert_after_cf_list(this->cf_node_list
,
710 &instr_undef
->instr
);
712 /* Set the image variable dereference. */
713 exec_node
*param
= ir
->actual_parameters
.get_head();
714 ir_dereference
*image
= (ir_dereference
*)param
;
715 const glsl_type
*type
=
716 image
->variable_referenced()->type
->without_array();
718 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
719 param
= param
->get_next();
721 /* Set the intrinsic destination. */
722 if (ir
->return_deref
) {
723 const nir_intrinsic_info
*info
=
724 &nir_intrinsic_infos
[instr
->intrinsic
];
725 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
726 info
->dest_components
, NULL
);
729 if (op
== nir_intrinsic_image_size
||
730 op
== nir_intrinsic_image_samples
) {
731 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
735 /* Set the address argument, extending the coordinate vector to four
738 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
739 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
740 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
742 for (int i
= 0; i
< 4; i
++) {
743 if (i
< type
->coordinate_components()) {
744 instr_addr
->src
[i
].src
= src_addr
;
745 instr_addr
->src
[i
].swizzle
[0] = i
;
747 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
751 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
752 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
753 param
= param
->get_next();
755 /* Set the sample argument, which is undefined for single-sample
758 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
759 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
760 param
= param
->get_next();
762 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
765 /* Set the intrinsic parameters. */
766 if (!param
->is_tail_sentinel()) {
767 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
768 param
= param
->get_next();
771 if (!param
->is_tail_sentinel()) {
772 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
773 param
= param
->get_next();
775 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
778 case nir_intrinsic_memory_barrier
:
779 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
781 case nir_intrinsic_store_ssbo
: {
782 exec_node
*param
= ir
->actual_parameters
.get_head();
783 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
785 param
= param
->get_next();
786 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
788 param
= param
->get_next();
789 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
791 param
= param
->get_next();
792 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
795 /* Check if we need the indirect version */
796 ir_constant
*const_offset
= offset
->as_constant();
798 op
= nir_intrinsic_store_ssbo_indirect
;
800 instr
= nir_intrinsic_instr_create(shader
, op
);
801 instr
->src
[2] = evaluate_rvalue(offset
);
802 instr
->const_index
[0] = 0;
804 instr
->const_index
[0] = const_offset
->value
.u
[0];
807 instr
->const_index
[1] = write_mask
->value
.u
[0];
809 instr
->src
[0] = evaluate_rvalue(val
);
810 instr
->num_components
= val
->type
->vector_elements
;
812 instr
->src
[1] = evaluate_rvalue(block
);
813 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
816 case nir_intrinsic_load_ssbo
: {
817 exec_node
*param
= ir
->actual_parameters
.get_head();
818 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
820 param
= param
->get_next();
821 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
823 /* Check if we need the indirect version */
824 ir_constant
*const_offset
= offset
->as_constant();
826 op
= nir_intrinsic_load_ssbo_indirect
;
828 instr
= nir_intrinsic_instr_create(shader
, op
);
829 instr
->src
[1] = evaluate_rvalue(offset
);
830 instr
->const_index
[0] = 0;
833 instr
->const_index
[0] = const_offset
->value
.u
[0];
836 instr
->src
[0] = evaluate_rvalue(block
);
838 const glsl_type
*type
= ir
->return_deref
->var
->type
;
839 instr
->num_components
= type
->vector_elements
;
841 /* Setup destination register */
842 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
843 type
->vector_elements
, NULL
);
845 /* Insert the created nir instruction now since in the case of boolean
846 * result we will need to emit another instruction after it
848 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
851 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
852 * consider a true boolean to be ~0. Fix this up with a != 0
855 if (type
->base_type
== GLSL_TYPE_BOOL
) {
856 nir_load_const_instr
*const_zero
=
857 nir_load_const_instr_create(shader
, 1);
858 const_zero
->value
.u
[0] = 0;
859 nir_instr_insert_after_cf_list(this->cf_node_list
,
862 nir_alu_instr
*load_ssbo_compare
=
863 nir_alu_instr_create(shader
, nir_op_ine
);
864 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
865 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
866 load_ssbo_compare
->src
[1].src
.is_ssa
= true;
867 load_ssbo_compare
->src
[1].src
.ssa
= &const_zero
->def
;
868 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
869 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
870 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
871 &load_ssbo_compare
->dest
.dest
,
872 type
->vector_elements
, NULL
);
873 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
874 nir_instr_insert_after_cf_list(this->cf_node_list
,
875 &load_ssbo_compare
->instr
);
876 dest
= &load_ssbo_compare
->dest
.dest
;
880 case nir_intrinsic_ssbo_atomic_add
:
881 case nir_intrinsic_ssbo_atomic_min
:
882 case nir_intrinsic_ssbo_atomic_max
:
883 case nir_intrinsic_ssbo_atomic_and
:
884 case nir_intrinsic_ssbo_atomic_or
:
885 case nir_intrinsic_ssbo_atomic_xor
:
886 case nir_intrinsic_ssbo_atomic_exchange
:
887 case nir_intrinsic_ssbo_atomic_comp_swap
: {
888 int param_count
= ir
->actual_parameters
.length();
889 assert(param_count
== 3 || param_count
== 4);
892 exec_node
*param
= ir
->actual_parameters
.get_head();
893 ir_instruction
*inst
= (ir_instruction
*) param
;
894 instr
->src
[0] = evaluate_rvalue(inst
->as_rvalue());
897 param
= param
->get_next();
898 inst
= (ir_instruction
*) param
;
899 instr
->src
[1] = evaluate_rvalue(inst
->as_rvalue());
901 /* data1 parameter (this is always present) */
902 param
= param
->get_next();
903 inst
= (ir_instruction
*) param
;
904 instr
->src
[2] = evaluate_rvalue(inst
->as_rvalue());
906 /* data2 parameter (only with atomic_comp_swap) */
907 if (param_count
== 4) {
908 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
909 param
= param
->get_next();
910 inst
= (ir_instruction
*) param
;
911 instr
->src
[3] = evaluate_rvalue(inst
->as_rvalue());
915 assert(ir
->return_deref
);
916 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
917 ir
->return_deref
->type
->vector_elements
, NULL
);
918 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
922 unreachable("not reached");
925 if (ir
->return_deref
) {
926 nir_intrinsic_instr
*store_instr
=
927 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
928 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
930 store_instr
->variables
[0] =
931 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
932 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
934 nir_instr_insert_after_cf_list(this->cf_node_list
,
935 &store_instr
->instr
);
941 struct hash_entry
*entry
=
942 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
944 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
946 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
949 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
950 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
954 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
955 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
959 nir_visitor::visit(ir_assignment
*ir
)
961 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
963 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
964 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
965 /* We're doing a plain-as-can-be copy, so emit a copy_var */
966 nir_intrinsic_instr
*copy
=
967 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
969 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
970 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
973 nir_if
*if_stmt
= nir_if_create(this->shader
);
974 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
975 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
976 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
978 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
983 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
985 ir
->lhs
->accept(this);
986 nir_deref_var
*lhs_deref
= this->deref_head
;
987 nir_src src
= evaluate_rvalue(ir
->rhs
);
989 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
991 * We have no good way to update only part of a variable, so just load
992 * the LHS and do a vec operation to combine the old with the new, and
994 * back into the LHS. Copy propagation should get rid of the mess.
997 nir_intrinsic_instr
*load
=
998 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
999 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1000 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1001 load
->variables
[0] = lhs_deref
;
1002 ralloc_steal(load
, load
->variables
[0]);
1003 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
1006 switch (ir
->lhs
->type
->vector_elements
) {
1007 case 1: vec_op
= nir_op_imov
; break;
1008 case 2: vec_op
= nir_op_vec2
; break;
1009 case 3: vec_op
= nir_op_vec3
; break;
1010 case 4: vec_op
= nir_op_vec4
; break;
1011 default: unreachable("Invalid number of components"); break;
1013 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
1014 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
1015 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1017 unsigned component
= 0;
1018 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1019 if (ir
->write_mask
& (1 << i
)) {
1020 vec
->src
[i
].src
= src
;
1022 /* GLSL IR will give us the input to the write-masked assignment
1023 * in a single packed vector. So, for example, if the
1024 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1025 * and z -> w and get the y component from the load.
1027 vec
->src
[i
].swizzle
[0] = component
++;
1029 vec
->src
[i
].src
.is_ssa
= true;
1030 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
1031 vec
->src
[i
].swizzle
[0] = i
;
1035 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
1038 src
.ssa
= &vec
->dest
.dest
.ssa
;
1041 nir_intrinsic_instr
*store
=
1042 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1043 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1044 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1045 store
->variables
[0] = nir_deref_as_var(store_deref
);
1046 store
->src
[0] = src
;
1048 if (ir
->condition
) {
1049 nir_if
*if_stmt
= nir_if_create(this->shader
);
1050 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
1051 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
1052 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1054 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
1059 * Given an instruction, returns a pointer to its destination or NULL if there
1060 * is no destination.
1062 * Note that this only handles instructions we generate at this level.
1065 get_instr_dest(nir_instr
*instr
)
1067 nir_alu_instr
*alu_instr
;
1068 nir_intrinsic_instr
*intrinsic_instr
;
1069 nir_tex_instr
*tex_instr
;
1071 switch (instr
->type
) {
1072 case nir_instr_type_alu
:
1073 alu_instr
= nir_instr_as_alu(instr
);
1074 return &alu_instr
->dest
.dest
;
1076 case nir_instr_type_intrinsic
:
1077 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1078 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1079 return &intrinsic_instr
->dest
;
1083 case nir_instr_type_tex
:
1084 tex_instr
= nir_instr_as_tex(instr
);
1085 return &tex_instr
->dest
;
1088 unreachable("not reached");
1095 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1097 nir_dest
*dest
= get_instr_dest(instr
);
1100 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1102 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
1103 this->result
= instr
;
1107 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1110 if (ir
->as_dereference() || ir
->as_constant()) {
1112 * A dereference is being used on the right hand side, which means we
1113 * must emit a variable load.
1116 nir_intrinsic_instr
*load_instr
=
1117 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1118 load_instr
->num_components
= ir
->type
->vector_elements
;
1119 load_instr
->variables
[0] = this->deref_head
;
1120 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1121 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1124 nir_dest
*dest
= get_instr_dest(this->result
);
1125 assert(dest
->is_ssa
);
1127 return nir_src_for_ssa(&dest
->ssa
);
1131 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
1133 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
1134 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
1135 instr
->src
[i
].src
= srcs
[i
];
1136 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
1137 add_instr(&instr
->instr
, dest_size
);
1142 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
1144 assert(nir_op_infos
[op
].num_inputs
== 1);
1145 return emit(op
, dest_size
, &src1
);
1149 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
1152 assert(nir_op_infos
[op
].num_inputs
== 2);
1153 nir_src srcs
[] = { src1
, src2
};
1154 return emit(op
, dest_size
, srcs
);
1158 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
1159 nir_src src2
, nir_src src3
)
1161 assert(nir_op_infos
[op
].num_inputs
== 3);
1162 nir_src srcs
[] = { src1
, src2
, src3
};
1163 return emit(op
, dest_size
, srcs
);
1167 nir_visitor::visit(ir_expression
*ir
)
1169 /* Some special cases */
1170 switch (ir
->operation
) {
1171 case ir_binop_ubo_load
: {
1172 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1174 nir_intrinsic_op op
;
1176 op
= nir_intrinsic_load_ubo
;
1178 op
= nir_intrinsic_load_ubo_indirect
;
1180 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1181 load
->num_components
= ir
->type
->vector_elements
;
1182 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1183 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
1185 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1186 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1189 * In UBO's, a true boolean value is any non-zero value, but we consider
1190 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1193 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1194 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1195 const_zero
->value
.u
[0] = 0;
1196 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1198 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1199 compare
->src
[0].src
.is_ssa
= true;
1200 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1201 compare
->src
[1].src
.is_ssa
= true;
1202 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1203 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1204 compare
->src
[1].swizzle
[i
] = 0;
1205 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1207 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1213 case ir_unop_interpolate_at_centroid
:
1214 case ir_binop_interpolate_at_offset
:
1215 case ir_binop_interpolate_at_sample
: {
1216 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1217 ir_swizzle
*swizzle
= NULL
;
1219 /* the api does not allow a swizzle here, but the varying packing code
1220 * may have pushed one into here.
1222 swizzle
= ir
->operands
[0]->as_swizzle();
1224 deref
= swizzle
->val
->as_dereference();
1228 deref
->accept(this);
1230 nir_intrinsic_op op
;
1231 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1232 switch (ir
->operation
) {
1233 case ir_unop_interpolate_at_centroid
:
1234 op
= nir_intrinsic_interp_var_at_centroid
;
1236 case ir_binop_interpolate_at_offset
:
1237 op
= nir_intrinsic_interp_var_at_offset
;
1239 case ir_binop_interpolate_at_sample
:
1240 op
= nir_intrinsic_interp_var_at_sample
;
1243 unreachable("Invalid interpolation intrinsic");
1246 /* This case can happen if the vertex shader does not write the
1247 * given varying. In this case, the linker will lower it to a
1248 * global variable. Since interpolating a variable makes no
1249 * sense, we'll just turn it into a load which will probably
1250 * eventually end up as an SSA definition.
1252 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1253 op
= nir_intrinsic_load_var
;
1256 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1257 intrin
->num_components
= deref
->type
->vector_elements
;
1258 intrin
->variables
[0] = this->deref_head
;
1259 ralloc_steal(intrin
, intrin
->variables
[0]);
1261 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1262 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1263 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1265 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1268 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1269 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1270 mov
->src
[0].src
.is_ssa
= true;
1271 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1273 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1274 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1275 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1276 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1277 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1278 mov
->src
[0].swizzle
[i
] = 0;
1280 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1291 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1292 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1294 glsl_base_type types
[4];
1295 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1297 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1299 types
[i
] = GLSL_TYPE_FLOAT
;
1301 glsl_base_type out_type
;
1303 out_type
= ir
->type
->base_type
;
1305 out_type
= GLSL_TYPE_FLOAT
;
1307 unsigned dest_size
= ir
->type
->vector_elements
;
1309 nir_alu_instr
*instr
;
1312 switch (ir
->operation
) {
1313 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1314 case ir_unop_logic_not
:
1315 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1318 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1322 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1325 case ir_unop_saturate
:
1326 assert(types
[0] == GLSL_TYPE_FLOAT
);
1327 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1330 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1333 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1334 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1335 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1336 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1337 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1338 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1339 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1341 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1344 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1347 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1349 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1350 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1351 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1352 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1353 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1356 case ir_unop_bitcast_i2f
:
1357 case ir_unop_bitcast_f2i
:
1358 case ir_unop_bitcast_u2f
:
1359 case ir_unop_bitcast_f2u
:
1360 case ir_unop_subroutine_to_int
:
1362 emit(nir_op_imov
, dest_size
, srcs
);
1365 switch (ir
->operands
[0]->type
->vector_elements
) {
1367 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1371 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1375 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1379 unreachable("not reached");
1382 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1383 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1384 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1385 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1386 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1387 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1388 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1389 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1390 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1391 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1392 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1393 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1394 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1395 case ir_unop_pack_snorm_2x16
:
1396 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1398 case ir_unop_pack_snorm_4x8
:
1399 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1401 case ir_unop_pack_unorm_2x16
:
1402 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1404 case ir_unop_pack_unorm_4x8
:
1405 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1407 case ir_unop_pack_half_2x16
:
1408 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1410 case ir_unop_unpack_snorm_2x16
:
1411 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1413 case ir_unop_unpack_snorm_4x8
:
1414 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1416 case ir_unop_unpack_unorm_2x16
:
1417 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1419 case ir_unop_unpack_unorm_4x8
:
1420 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1422 case ir_unop_unpack_half_2x16
:
1423 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1425 case ir_unop_unpack_half_2x16_split_x
:
1426 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1428 case ir_unop_unpack_half_2x16_split_y
:
1429 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1431 case ir_unop_bitfield_reverse
:
1432 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1434 case ir_unop_bit_count
:
1435 emit(nir_op_bit_count
, dest_size
, srcs
);
1437 case ir_unop_find_msb
:
1439 case GLSL_TYPE_UINT
:
1440 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1443 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1446 unreachable("Invalid type for findMSB()");
1449 case ir_unop_find_lsb
:
1450 emit(nir_op_find_lsb
, dest_size
, srcs
);
1454 switch (ir
->type
->vector_elements
) {
1456 switch (ir
->operands
[0]->type
->vector_elements
) {
1457 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1458 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1459 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1460 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1461 default: unreachable("not reached");
1465 switch (ir
->operands
[0]->type
->vector_elements
) {
1466 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1467 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1468 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1469 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1470 default: unreachable("not reached");
1474 switch (ir
->operands
[0]->type
->vector_elements
) {
1475 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1476 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1477 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1478 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1479 default: unreachable("not reached");
1483 switch (ir
->operands
[0]->type
->vector_elements
) {
1484 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1485 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1486 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1487 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1488 default: unreachable("not reached");
1492 unreachable("not reached");
1495 case ir_unop_get_buffer_size
: {
1496 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1498 nir_intrinsic_get_buffer_size
);
1499 load
->num_components
= ir
->type
->vector_elements
;
1500 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
1501 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1513 case ir_binop_bit_and
:
1514 case ir_binop_bit_or
:
1515 case ir_binop_bit_xor
:
1516 case ir_binop_logic_and
:
1517 case ir_binop_logic_or
:
1518 case ir_binop_logic_xor
:
1519 case ir_binop_lshift
:
1520 case ir_binop_rshift
:
1521 switch (ir
->operation
) {
1523 if (out_type
== GLSL_TYPE_FLOAT
)
1529 if (out_type
== GLSL_TYPE_FLOAT
)
1535 if (out_type
== GLSL_TYPE_FLOAT
)
1541 if (out_type
== GLSL_TYPE_FLOAT
)
1543 else if (out_type
== GLSL_TYPE_INT
)
1549 if (out_type
== GLSL_TYPE_FLOAT
)
1555 if (out_type
== GLSL_TYPE_FLOAT
)
1557 else if (out_type
== GLSL_TYPE_INT
)
1563 if (out_type
== GLSL_TYPE_FLOAT
)
1565 else if (out_type
== GLSL_TYPE_INT
)
1570 case ir_binop_bit_and
:
1573 case ir_binop_bit_or
:
1576 case ir_binop_bit_xor
:
1579 case ir_binop_logic_and
:
1585 case ir_binop_logic_or
:
1591 case ir_binop_logic_xor
:
1597 case ir_binop_lshift
:
1600 case ir_binop_rshift
:
1601 if (out_type
== GLSL_TYPE_INT
)
1611 unreachable("not reached");
1614 instr
= emit(op
, dest_size
, srcs
);
1616 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1617 ir
->operands
[1]->type
->vector_elements
== 1) {
1618 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1620 instr
->src
[1].swizzle
[i
] = 0;
1624 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1625 ir
->operands
[0]->type
->vector_elements
== 1) {
1626 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1628 instr
->src
[0].swizzle
[i
] = 0;
1633 case ir_binop_imul_high
:
1634 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1637 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1638 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1640 if (supports_ints
) {
1641 if (types
[0] == GLSL_TYPE_FLOAT
)
1642 emit(nir_op_flt
, dest_size
, srcs
);
1643 else if (types
[0] == GLSL_TYPE_INT
)
1644 emit(nir_op_ilt
, dest_size
, srcs
);
1646 emit(nir_op_ult
, dest_size
, srcs
);
1648 emit(nir_op_slt
, dest_size
, srcs
);
1651 case ir_binop_greater
:
1652 if (supports_ints
) {
1653 if (types
[0] == GLSL_TYPE_FLOAT
)
1654 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1655 else if (types
[0] == GLSL_TYPE_INT
)
1656 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1658 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1660 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1663 case ir_binop_lequal
:
1664 if (supports_ints
) {
1665 if (types
[0] == GLSL_TYPE_FLOAT
)
1666 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1667 else if (types
[0] == GLSL_TYPE_INT
)
1668 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1670 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1672 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1675 case ir_binop_gequal
:
1676 if (supports_ints
) {
1677 if (types
[0] == GLSL_TYPE_FLOAT
)
1678 emit(nir_op_fge
, dest_size
, srcs
);
1679 else if (types
[0] == GLSL_TYPE_INT
)
1680 emit(nir_op_ige
, dest_size
, srcs
);
1682 emit(nir_op_uge
, dest_size
, srcs
);
1684 emit(nir_op_slt
, dest_size
, srcs
);
1687 case ir_binop_equal
:
1688 if (supports_ints
) {
1689 if (types
[0] == GLSL_TYPE_FLOAT
)
1690 emit(nir_op_feq
, dest_size
, srcs
);
1692 emit(nir_op_ieq
, dest_size
, srcs
);
1694 emit(nir_op_seq
, dest_size
, srcs
);
1697 case ir_binop_nequal
:
1698 if (supports_ints
) {
1699 if (types
[0] == GLSL_TYPE_FLOAT
)
1700 emit(nir_op_fne
, dest_size
, srcs
);
1702 emit(nir_op_ine
, dest_size
, srcs
);
1704 emit(nir_op_sne
, dest_size
, srcs
);
1707 case ir_binop_all_equal
:
1708 if (supports_ints
) {
1709 if (types
[0] == GLSL_TYPE_FLOAT
) {
1710 switch (ir
->operands
[0]->type
->vector_elements
) {
1711 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1712 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1713 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1714 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1716 unreachable("not reached");
1719 switch (ir
->operands
[0]->type
->vector_elements
) {
1720 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1721 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1722 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1723 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1725 unreachable("not reached");
1729 switch (ir
->operands
[0]->type
->vector_elements
) {
1730 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1731 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1732 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1733 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1735 unreachable("not reached");
1739 case ir_binop_any_nequal
:
1740 if (supports_ints
) {
1741 if (types
[0] == GLSL_TYPE_FLOAT
) {
1742 switch (ir
->operands
[0]->type
->vector_elements
) {
1743 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1744 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1745 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1746 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1748 unreachable("not reached");
1751 switch (ir
->operands
[0]->type
->vector_elements
) {
1752 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1753 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1754 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1755 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1757 unreachable("not reached");
1761 switch (ir
->operands
[0]->type
->vector_elements
) {
1762 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1763 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1764 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1765 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1767 unreachable("not reached");
1772 switch (ir
->operands
[0]->type
->vector_elements
) {
1773 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1774 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1775 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1777 unreachable("not reached");
1781 case ir_binop_pack_half_2x16_split
:
1782 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1784 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1785 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1786 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1788 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1789 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1790 ir
->operands
[2]->type
->vector_elements
== 1) {
1791 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1793 instr
->src
[2].swizzle
[i
] = 0;
1799 emit(nir_op_bcsel
, dest_size
, srcs
);
1801 emit(nir_op_fcsel
, dest_size
, srcs
);
1804 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1805 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1806 instr
->src
[0].swizzle
[i
] = 0;
1809 case ir_triop_bitfield_extract
:
1810 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1811 nir_op_ubitfield_extract
, dest_size
, srcs
);
1812 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1813 instr
->src
[1].swizzle
[i
] = 0;
1814 instr
->src
[2].swizzle
[i
] = 0;
1817 case ir_quadop_bitfield_insert
:
1818 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1819 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1820 instr
->src
[2].swizzle
[i
] = 0;
1821 instr
->src
[3].swizzle
[i
] = 0;
1824 case ir_quadop_vector
:
1825 switch (ir
->type
->vector_elements
) {
1826 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1827 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1828 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1829 default: unreachable("not reached");
1834 unreachable("not reached");
1839 nir_visitor::visit(ir_swizzle
*ir
)
1841 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1842 ir
->type
->vector_elements
,
1843 evaluate_rvalue(ir
->val
));
1845 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1846 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1847 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1851 nir_visitor::visit(ir_texture
*ir
)
1858 num_srcs
= 1; /* coordinate */
1863 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1864 num_srcs
= 2; /* coordinate, bias/lod */
1868 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1874 if (ir
->lod_info
.lod
!= NULL
)
1875 num_srcs
= 2; /* coordinate, lod */
1877 num_srcs
= 1; /* coordinate */
1881 op
= nir_texop_txf_ms
;
1882 num_srcs
= 2; /* coordinate, sample_index */
1887 if (ir
->lod_info
.lod
!= NULL
)
1888 num_srcs
= 1; /* lod */
1895 num_srcs
= 1; /* coordinate */
1900 num_srcs
= 1; /* coordinate */
1903 case ir_query_levels
:
1904 op
= nir_texop_query_levels
;
1908 case ir_texture_samples
:
1909 op
= nir_texop_texture_samples
;
1914 unreachable("not reached");
1917 if (ir
->projector
!= NULL
)
1919 if (ir
->shadow_comparitor
!= NULL
)
1921 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1924 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1927 instr
->sampler_dim
=
1928 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1929 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1930 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1931 if (instr
->is_shadow
)
1932 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1933 switch (ir
->type
->base_type
) {
1934 case GLSL_TYPE_FLOAT
:
1935 instr
->dest_type
= nir_type_float
;
1938 instr
->dest_type
= nir_type_int
;
1940 case GLSL_TYPE_UINT
:
1941 instr
->dest_type
= nir_type_unsigned
;
1944 unreachable("not reached");
1947 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1949 unsigned src_number
= 0;
1951 if (ir
->coordinate
!= NULL
) {
1952 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1953 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1954 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1958 if (ir
->projector
!= NULL
) {
1959 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1960 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1964 if (ir
->shadow_comparitor
!= NULL
) {
1965 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1966 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1970 if (ir
->offset
!= NULL
) {
1971 /* we don't support multiple offsets yet */
1972 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1974 ir_constant
*const_offset
= ir
->offset
->as_constant();
1975 if (const_offset
!= NULL
) {
1976 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1977 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1979 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
1980 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1987 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
1988 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1995 if (ir
->lod_info
.lod
!= NULL
) {
1996 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
1997 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
2003 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
2004 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
2006 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
2007 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
2012 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
2013 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
2018 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
2025 assert(src_number
== num_srcs
);
2027 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
2031 nir_visitor::visit(ir_constant
*ir
)
2034 * We don't know if this variable is an an array or struct that gets
2035 * dereferenced, so do the safe thing an make it a variable with a
2036 * constant initializer and return a dereference.
2039 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
2040 var
->name
= ralloc_strdup(var
, "const_temp");
2041 var
->type
= ir
->type
;
2042 var
->data
.mode
= nir_var_local
;
2043 var
->data
.read_only
= true;
2044 var
->constant_initializer
= constant_copy(ir
, var
);
2045 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
2047 this->deref_head
= nir_deref_var_create(this->shader
, var
);
2048 this->deref_tail
= &this->deref_head
->deref
;
2052 nir_visitor::visit(ir_dereference_variable
*ir
)
2054 struct hash_entry
*entry
=
2055 _mesa_hash_table_search(this->var_table
, ir
->var
);
2057 nir_variable
*var
= (nir_variable
*) entry
->data
;
2059 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
2060 this->deref_head
= deref
;
2061 this->deref_tail
= &deref
->deref
;
2065 nir_visitor::visit(ir_dereference_record
*ir
)
2067 ir
->record
->accept(this);
2069 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
2070 assert(field_index
>= 0);
2072 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
2073 deref
->deref
.type
= ir
->type
;
2074 this->deref_tail
->child
= &deref
->deref
;
2075 this->deref_tail
= &deref
->deref
;
2079 nir_visitor::visit(ir_dereference_array
*ir
)
2081 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
2082 deref
->deref
.type
= ir
->type
;
2084 ir_constant
*const_index
= ir
->array_index
->as_constant();
2085 if (const_index
!= NULL
) {
2086 deref
->deref_array_type
= nir_deref_array_type_direct
;
2087 deref
->base_offset
= const_index
->value
.u
[0];
2089 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2090 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
2093 ir
->array
->accept(this);
2095 this->deref_tail
->child
= &deref
->deref
;
2096 ralloc_steal(this->deref_tail
, deref
);
2097 this->deref_tail
= &deref
->deref
;
2101 nir_visitor::visit(ir_barrier
*ir
)
2103 nir_intrinsic_instr
*instr
=
2104 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2105 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);