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 "nir_builder.h"
31 #include "ir_visitor.h"
32 #include "ir_hierarchical_visitor.h"
34 #include "main/imports.h"
37 * pass to lower GLSL IR to NIR
39 * This will lower variable dereferences to loads/stores of corresponding
40 * variables in NIR - the variables will be converted to registers in a later
46 class nir_visitor
: public ir_visitor
49 nir_visitor(nir_shader
*shader
, gl_shader
*sh
);
52 virtual void visit(ir_variable
*);
53 virtual void visit(ir_function
*);
54 virtual void visit(ir_function_signature
*);
55 virtual void visit(ir_loop
*);
56 virtual void visit(ir_if
*);
57 virtual void visit(ir_discard
*);
58 virtual void visit(ir_loop_jump
*);
59 virtual void visit(ir_return
*);
60 virtual void visit(ir_call
*);
61 virtual void visit(ir_assignment
*);
62 virtual void visit(ir_emit_vertex
*);
63 virtual void visit(ir_end_primitive
*);
64 virtual void visit(ir_expression
*);
65 virtual void visit(ir_swizzle
*);
66 virtual void visit(ir_texture
*);
67 virtual void visit(ir_constant
*);
68 virtual void visit(ir_dereference_variable
*);
69 virtual void visit(ir_dereference_record
*);
70 virtual void visit(ir_dereference_array
*);
71 virtual void visit(ir_barrier
*);
73 void create_function(ir_function_signature
*ir
);
76 void add_instr(nir_instr
*instr
, unsigned num_components
);
77 nir_ssa_def
*evaluate_rvalue(ir_rvalue
*ir
);
79 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
**srcs
);
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
);
81 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
83 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
84 nir_ssa_def
*src2
, nir_ssa_def
*src3
);
91 nir_function_impl
*impl
;
93 nir_ssa_def
*result
; /* result of the expression tree last visited */
95 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
97 /* the head of the dereference chain we're creating */
98 nir_deref_var
*deref_head
;
99 /* the tail of the dereference chain we're creating */
100 nir_deref
*deref_tail
;
102 nir_variable
*var
; /* variable created by ir_variable visitor */
104 /* whether the IR we're operating on is per-function or global */
107 /* map of ir_variable -> nir_variable */
108 struct hash_table
*var_table
;
110 /* map of ir_function_signature -> nir_function_overload */
111 struct hash_table
*overload_table
;
115 * This visitor runs before the main visitor, calling create_function() for
116 * each function so that the main visitor can resolve forward references in
120 class nir_function_visitor
: public ir_hierarchical_visitor
123 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
126 virtual ir_visitor_status
visit_enter(ir_function
*);
129 nir_visitor
*visitor
;
132 }; /* end of anonymous namespace */
135 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
136 gl_shader_stage stage
,
137 const nir_shader_compiler_options
*options
)
139 struct gl_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
141 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
143 nir_visitor
v1(shader
, sh
);
144 nir_function_visitor
v2(&v1
);
146 visit_exec_list(sh
->ir
, &v1
);
148 nir_lower_outputs_to_temporaries(shader
);
150 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
151 if (shader_prog
->Label
)
152 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
153 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
154 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
155 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
156 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
157 shader
->info
.num_images
= sh
->NumImages
;
158 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
159 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
160 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
161 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
162 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
163 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
164 shader
->info
.uses_clip_distance_out
=
165 sh
->Program
->ClipDistanceArraySize
!= 0;
166 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
167 shader
->info
.has_transform_feedback_varyings
=
168 shader_prog
->TransformFeedback
.NumVarying
> 0;
171 case MESA_SHADER_TESS_CTRL
:
172 shader
->info
.tcs
.vertices_out
= shader_prog
->TessCtrl
.VerticesOut
;
175 case MESA_SHADER_GEOMETRY
:
176 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
177 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
178 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
179 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
180 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
181 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
184 case MESA_SHADER_FRAGMENT
: {
185 struct gl_fragment_program
*fp
=
186 (struct gl_fragment_program
*)sh
->Program
;
188 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
189 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
190 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
194 case MESA_SHADER_COMPUTE
: {
195 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
196 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
197 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
198 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
203 break; /* No stage-specific info */
209 nir_visitor::nir_visitor(nir_shader
*shader
, gl_shader
*sh
)
211 this->supports_ints
= shader
->options
->native_integers
;
212 this->shader
= shader
;
214 this->is_global
= true;
215 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
216 _mesa_key_pointer_equal
);
217 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
218 _mesa_key_pointer_equal
);
221 nir_visitor::~nir_visitor()
223 _mesa_hash_table_destroy(this->var_table
, NULL
);
224 _mesa_hash_table_destroy(this->overload_table
, NULL
);
228 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
231 ralloc_steal(mem_ctx
, this->deref_head
);
232 return this->deref_head
;
235 static nir_constant
*
236 constant_copy(ir_constant
*ir
, void *mem_ctx
)
241 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
243 unsigned total_elems
= ir
->type
->components();
246 ret
->num_elements
= 0;
247 switch (ir
->type
->base_type
) {
249 for (i
= 0; i
< total_elems
; i
++)
250 ret
->value
.u
[i
] = ir
->value
.u
[i
];
254 for (i
= 0; i
< total_elems
; i
++)
255 ret
->value
.i
[i
] = ir
->value
.i
[i
];
258 case GLSL_TYPE_FLOAT
:
259 for (i
= 0; i
< total_elems
; i
++)
260 ret
->value
.f
[i
] = ir
->value
.f
[i
];
264 for (i
= 0; i
< total_elems
; i
++)
265 ret
->value
.b
[i
] = ir
->value
.b
[i
];
268 case GLSL_TYPE_STRUCT
:
269 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
271 ret
->num_elements
= ir
->type
->length
;
274 foreach_in_list(ir_constant
, field
, &ir
->components
) {
275 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
280 case GLSL_TYPE_ARRAY
:
281 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
283 ret
->num_elements
= ir
->type
->length
;
285 for (i
= 0; i
< ir
->type
->length
; i
++)
286 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
290 unreachable("not reached");
297 nir_visitor::visit(ir_variable
*ir
)
299 nir_variable
*var
= ralloc(shader
, nir_variable
);
300 var
->type
= ir
->type
;
301 var
->name
= ralloc_strdup(var
, ir
->name
);
303 var
->data
.read_only
= ir
->data
.read_only
;
304 var
->data
.centroid
= ir
->data
.centroid
;
305 var
->data
.sample
= ir
->data
.sample
;
306 var
->data
.patch
= ir
->data
.patch
;
307 var
->data
.invariant
= ir
->data
.invariant
;
308 var
->data
.location
= ir
->data
.location
;
310 switch(ir
->data
.mode
) {
312 case ir_var_temporary
:
314 var
->data
.mode
= nir_var_global
;
316 var
->data
.mode
= nir_var_local
;
319 case ir_var_function_in
:
320 case ir_var_function_out
:
321 case ir_var_function_inout
:
322 case ir_var_const_in
:
323 var
->data
.mode
= nir_var_local
;
326 case ir_var_shader_in
:
327 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
328 ir
->data
.location
== VARYING_SLOT_FACE
) {
329 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
330 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
331 var
->data
.mode
= nir_var_system_value
;
332 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
333 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
334 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
335 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
336 var
->data
.mode
= nir_var_system_value
;
338 var
->data
.mode
= nir_var_shader_in
;
342 case ir_var_shader_out
:
343 var
->data
.mode
= nir_var_shader_out
;
347 var
->data
.mode
= nir_var_uniform
;
350 case ir_var_shader_storage
:
351 var
->data
.mode
= nir_var_shader_storage
;
354 case ir_var_system_value
:
355 var
->data
.mode
= nir_var_system_value
;
359 unreachable("not reached");
362 var
->data
.interpolation
= ir
->data
.interpolation
;
363 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
364 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
365 var
->data
.explicit_location
= ir
->data
.explicit_location
;
366 var
->data
.explicit_index
= ir
->data
.explicit_index
;
367 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
368 var
->data
.has_initializer
= ir
->data
.has_initializer
;
369 var
->data
.location_frac
= ir
->data
.location_frac
;
370 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
371 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
373 switch (ir
->data
.depth_layout
) {
374 case ir_depth_layout_none
:
375 var
->data
.depth_layout
= nir_depth_layout_none
;
377 case ir_depth_layout_any
:
378 var
->data
.depth_layout
= nir_depth_layout_any
;
380 case ir_depth_layout_greater
:
381 var
->data
.depth_layout
= nir_depth_layout_greater
;
383 case ir_depth_layout_less
:
384 var
->data
.depth_layout
= nir_depth_layout_less
;
386 case ir_depth_layout_unchanged
:
387 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
390 unreachable("not reached");
393 var
->data
.index
= ir
->data
.index
;
394 var
->data
.descriptor_set
= 0;
395 var
->data
.binding
= ir
->data
.binding
;
396 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
397 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
398 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
399 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
400 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
401 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
402 var
->data
.image
.format
= ir
->data
.image_format
;
403 var
->data
.max_array_access
= ir
->data
.max_array_access
;
405 var
->num_state_slots
= ir
->get_num_state_slots();
406 if (var
->num_state_slots
> 0) {
407 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
408 var
->num_state_slots
);
410 ir_state_slot
*state_slots
= ir
->get_state_slots();
411 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
412 for (unsigned j
= 0; j
< 5; j
++)
413 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
414 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
417 var
->state_slots
= NULL
;
420 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
422 var
->interface_type
= ir
->get_interface_type();
424 if (var
->data
.mode
== nir_var_local
)
425 nir_function_impl_add_variable(impl
, var
);
427 nir_shader_add_variable(shader
, var
);
429 _mesa_hash_table_insert(var_table
, ir
, var
);
434 nir_function_visitor::visit_enter(ir_function
*ir
)
436 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
437 visitor
->create_function(sig
);
439 return visit_continue_with_parent
;
443 nir_visitor::create_function(ir_function_signature
*ir
)
445 if (ir
->is_intrinsic
)
448 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
450 unsigned num_params
= ir
->parameters
.length();
451 func
->num_params
= num_params
;
452 func
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
455 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
456 switch (param
->data
.mode
) {
457 case ir_var_function_in
:
458 func
->params
[i
].param_type
= nir_parameter_in
;
461 case ir_var_function_out
:
462 func
->params
[i
].param_type
= nir_parameter_out
;
465 case ir_var_function_inout
:
466 func
->params
[i
].param_type
= nir_parameter_inout
;
470 unreachable("not reached");
473 func
->params
[i
].type
= param
->type
;
477 func
->return_type
= ir
->return_type
;
479 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
483 nir_visitor::visit(ir_function
*ir
)
485 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
490 nir_visitor::visit(ir_function_signature
*ir
)
492 if (ir
->is_intrinsic
)
495 struct hash_entry
*entry
=
496 _mesa_hash_table_search(this->overload_table
, ir
);
499 nir_function
*func
= (nir_function
*) entry
->data
;
501 if (ir
->is_defined
) {
502 nir_function_impl
*impl
= nir_function_impl_create(func
);
505 unsigned num_params
= func
->num_params
;
506 impl
->num_params
= num_params
;
507 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
509 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
511 impl
->params
[i
] = this->var
;
515 if (func
->return_type
== glsl_type::void_type
) {
516 impl
->return_var
= NULL
;
518 impl
->return_var
= ralloc(this->shader
, nir_variable
);
519 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
521 impl
->return_var
->type
= func
->return_type
;
524 this->is_global
= false;
526 nir_builder_init(&b
, impl
);
527 b
.cursor
= nir_after_cf_list(&impl
->body
);
528 visit_exec_list(&ir
->body
, this);
530 this->is_global
= true;
537 nir_visitor::visit(ir_loop
*ir
)
539 nir_loop
*loop
= nir_loop_create(this->shader
);
540 nir_builder_cf_insert(&b
, &loop
->cf_node
);
542 b
.cursor
= nir_after_cf_list(&loop
->body
);
543 visit_exec_list(&ir
->body_instructions
, this);
544 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
548 nir_visitor::visit(ir_if
*ir
)
551 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
553 nir_if
*if_stmt
= nir_if_create(this->shader
);
554 if_stmt
->condition
= condition
;
555 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
557 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
558 visit_exec_list(&ir
->then_instructions
, this);
560 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
561 visit_exec_list(&ir
->else_instructions
, this);
563 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
567 nir_visitor::visit(ir_discard
*ir
)
570 * discards aren't treated as control flow, because before we lower them
571 * they can appear anywhere in the shader and the stuff after them may still
572 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
573 * discards will be immediately followed by a return.
576 nir_intrinsic_instr
*discard
;
578 discard
= nir_intrinsic_instr_create(this->shader
,
579 nir_intrinsic_discard_if
);
581 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
583 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
586 nir_builder_instr_insert(&b
, &discard
->instr
);
590 nir_visitor::visit(ir_emit_vertex
*ir
)
592 nir_intrinsic_instr
*instr
=
593 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
594 instr
->const_index
[0] = ir
->stream_id();
595 nir_builder_instr_insert(&b
, &instr
->instr
);
599 nir_visitor::visit(ir_end_primitive
*ir
)
601 nir_intrinsic_instr
*instr
=
602 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
603 instr
->const_index
[0] = ir
->stream_id();
604 nir_builder_instr_insert(&b
, &instr
->instr
);
608 nir_visitor::visit(ir_loop_jump
*ir
)
612 case ir_loop_jump::jump_break
:
613 type
= nir_jump_break
;
615 case ir_loop_jump::jump_continue
:
616 type
= nir_jump_continue
;
619 unreachable("not reached");
622 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
623 nir_builder_instr_insert(&b
, &instr
->instr
);
627 nir_visitor::visit(ir_return
*ir
)
629 if (ir
->value
!= NULL
) {
630 nir_intrinsic_instr
*copy
=
631 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
633 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
634 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
637 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
638 nir_builder_instr_insert(&b
, &instr
->instr
);
642 nir_visitor::visit(ir_call
*ir
)
644 if (ir
->callee
->is_intrinsic
) {
646 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
647 op
= nir_intrinsic_atomic_counter_read_var
;
648 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
649 op
= nir_intrinsic_atomic_counter_inc_var
;
650 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
651 op
= nir_intrinsic_atomic_counter_dec_var
;
652 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
653 op
= nir_intrinsic_image_load
;
654 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
655 op
= nir_intrinsic_image_store
;
656 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
657 op
= nir_intrinsic_image_atomic_add
;
658 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
659 op
= nir_intrinsic_image_atomic_min
;
660 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
661 op
= nir_intrinsic_image_atomic_max
;
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
663 op
= nir_intrinsic_image_atomic_and
;
664 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
665 op
= nir_intrinsic_image_atomic_or
;
666 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
667 op
= nir_intrinsic_image_atomic_xor
;
668 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
669 op
= nir_intrinsic_image_atomic_exchange
;
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
671 op
= nir_intrinsic_image_atomic_comp_swap
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
673 op
= nir_intrinsic_memory_barrier
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
675 op
= nir_intrinsic_image_size
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
677 op
= nir_intrinsic_image_samples
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
679 op
= nir_intrinsic_store_ssbo
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
681 op
= nir_intrinsic_load_ssbo
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
683 op
= nir_intrinsic_ssbo_atomic_add
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
685 op
= nir_intrinsic_ssbo_atomic_and
;
686 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
687 op
= nir_intrinsic_ssbo_atomic_or
;
688 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
689 op
= nir_intrinsic_ssbo_atomic_xor
;
690 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
691 assert(ir
->return_deref
);
692 if (ir
->return_deref
->type
== glsl_type::int_type
)
693 op
= nir_intrinsic_ssbo_atomic_imin
;
694 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
695 op
= nir_intrinsic_ssbo_atomic_umin
;
697 unreachable("Invalid type");
698 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
699 assert(ir
->return_deref
);
700 if (ir
->return_deref
->type
== glsl_type::int_type
)
701 op
= nir_intrinsic_ssbo_atomic_imax
;
702 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
703 op
= nir_intrinsic_ssbo_atomic_umax
;
705 unreachable("Invalid type");
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
707 op
= nir_intrinsic_ssbo_atomic_exchange
;
708 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
709 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
710 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
711 op
= nir_intrinsic_shader_clock
;
712 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
713 op
= nir_intrinsic_group_memory_barrier
;
714 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
715 op
= nir_intrinsic_memory_barrier_atomic_counter
;
716 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
717 op
= nir_intrinsic_memory_barrier_buffer
;
718 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
719 op
= nir_intrinsic_memory_barrier_image
;
720 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
721 op
= nir_intrinsic_memory_barrier_shared
;
722 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
723 op
= nir_intrinsic_load_shared
;
724 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
725 op
= nir_intrinsic_store_shared
;
726 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
727 op
= nir_intrinsic_shared_atomic_add
;
728 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
729 op
= nir_intrinsic_shared_atomic_and
;
730 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
731 op
= nir_intrinsic_shared_atomic_or
;
732 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
733 op
= nir_intrinsic_shared_atomic_xor
;
734 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
735 assert(ir
->return_deref
);
736 if (ir
->return_deref
->type
== glsl_type::int_type
)
737 op
= nir_intrinsic_shared_atomic_imin
;
738 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
739 op
= nir_intrinsic_shared_atomic_umin
;
741 unreachable("Invalid type");
742 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
743 assert(ir
->return_deref
);
744 if (ir
->return_deref
->type
== glsl_type::int_type
)
745 op
= nir_intrinsic_shared_atomic_imax
;
746 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
747 op
= nir_intrinsic_shared_atomic_umax
;
749 unreachable("Invalid type");
750 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
751 op
= nir_intrinsic_shared_atomic_exchange
;
752 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
753 op
= nir_intrinsic_shared_atomic_comp_swap
;
755 unreachable("not reached");
758 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
759 nir_dest
*dest
= &instr
->dest
;
762 case nir_intrinsic_atomic_counter_read_var
:
763 case nir_intrinsic_atomic_counter_inc_var
:
764 case nir_intrinsic_atomic_counter_dec_var
: {
765 ir_dereference
*param
=
766 (ir_dereference
*) ir
->actual_parameters
.get_head();
767 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
768 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
769 nir_builder_instr_insert(&b
, &instr
->instr
);
772 case nir_intrinsic_image_load
:
773 case nir_intrinsic_image_store
:
774 case nir_intrinsic_image_atomic_add
:
775 case nir_intrinsic_image_atomic_min
:
776 case nir_intrinsic_image_atomic_max
:
777 case nir_intrinsic_image_atomic_and
:
778 case nir_intrinsic_image_atomic_or
:
779 case nir_intrinsic_image_atomic_xor
:
780 case nir_intrinsic_image_atomic_exchange
:
781 case nir_intrinsic_image_atomic_comp_swap
:
782 case nir_intrinsic_image_samples
:
783 case nir_intrinsic_image_size
: {
784 nir_ssa_undef_instr
*instr_undef
=
785 nir_ssa_undef_instr_create(shader
, 1);
786 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
788 /* Set the image variable dereference. */
789 exec_node
*param
= ir
->actual_parameters
.get_head();
790 ir_dereference
*image
= (ir_dereference
*)param
;
791 const glsl_type
*type
=
792 image
->variable_referenced()->type
->without_array();
794 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
795 param
= param
->get_next();
797 /* Set the intrinsic destination. */
798 if (ir
->return_deref
) {
799 const nir_intrinsic_info
*info
=
800 &nir_intrinsic_infos
[instr
->intrinsic
];
801 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
802 info
->dest_components
, NULL
);
805 if (op
== nir_intrinsic_image_size
||
806 op
== nir_intrinsic_image_samples
) {
807 nir_builder_instr_insert(&b
, &instr
->instr
);
811 /* Set the address argument, extending the coordinate vector to four
814 nir_ssa_def
*src_addr
=
815 evaluate_rvalue((ir_dereference
*)param
);
816 nir_ssa_def
*srcs
[4];
818 for (int i
= 0; i
< 4; i
++) {
819 if (i
< type
->coordinate_components())
820 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
822 srcs
[i
] = &instr_undef
->def
;
825 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
826 param
= param
->get_next();
828 /* Set the sample argument, which is undefined for single-sample
831 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
833 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
834 param
= param
->get_next();
836 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
839 /* Set the intrinsic parameters. */
840 if (!param
->is_tail_sentinel()) {
842 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
843 param
= param
->get_next();
846 if (!param
->is_tail_sentinel()) {
848 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
849 param
= param
->get_next();
851 nir_builder_instr_insert(&b
, &instr
->instr
);
854 case nir_intrinsic_memory_barrier
:
855 case nir_intrinsic_group_memory_barrier
:
856 case nir_intrinsic_memory_barrier_atomic_counter
:
857 case nir_intrinsic_memory_barrier_buffer
:
858 case nir_intrinsic_memory_barrier_image
:
859 case nir_intrinsic_memory_barrier_shared
:
860 nir_builder_instr_insert(&b
, &instr
->instr
);
862 case nir_intrinsic_shader_clock
:
863 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
864 nir_builder_instr_insert(&b
, &instr
->instr
);
866 case nir_intrinsic_store_ssbo
: {
867 exec_node
*param
= ir
->actual_parameters
.get_head();
868 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
870 param
= param
->get_next();
871 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
873 param
= param
->get_next();
874 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
876 param
= param
->get_next();
877 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
880 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
881 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
882 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
883 instr
->const_index
[0] = write_mask
->value
.u
[0];
884 instr
->num_components
= val
->type
->vector_elements
;
886 nir_builder_instr_insert(&b
, &instr
->instr
);
889 case nir_intrinsic_load_ssbo
: {
890 exec_node
*param
= ir
->actual_parameters
.get_head();
891 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
893 param
= param
->get_next();
894 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
896 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
897 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
899 const glsl_type
*type
= ir
->return_deref
->var
->type
;
900 instr
->num_components
= type
->vector_elements
;
902 /* Setup destination register */
903 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
904 type
->vector_elements
, NULL
);
906 /* Insert the created nir instruction now since in the case of boolean
907 * result we will need to emit another instruction after it
909 nir_builder_instr_insert(&b
, &instr
->instr
);
912 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
913 * consider a true boolean to be ~0. Fix this up with a != 0
916 if (type
->base_type
== GLSL_TYPE_BOOL
) {
917 nir_alu_instr
*load_ssbo_compare
=
918 nir_alu_instr_create(shader
, nir_op_ine
);
919 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
920 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
921 load_ssbo_compare
->src
[1].src
=
922 nir_src_for_ssa(nir_imm_int(&b
, 0));
923 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
924 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
925 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
926 &load_ssbo_compare
->dest
.dest
,
927 type
->vector_elements
, NULL
);
928 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
929 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
930 dest
= &load_ssbo_compare
->dest
.dest
;
934 case nir_intrinsic_ssbo_atomic_add
:
935 case nir_intrinsic_ssbo_atomic_imin
:
936 case nir_intrinsic_ssbo_atomic_umin
:
937 case nir_intrinsic_ssbo_atomic_imax
:
938 case nir_intrinsic_ssbo_atomic_umax
:
939 case nir_intrinsic_ssbo_atomic_and
:
940 case nir_intrinsic_ssbo_atomic_or
:
941 case nir_intrinsic_ssbo_atomic_xor
:
942 case nir_intrinsic_ssbo_atomic_exchange
:
943 case nir_intrinsic_ssbo_atomic_comp_swap
: {
944 int param_count
= ir
->actual_parameters
.length();
945 assert(param_count
== 3 || param_count
== 4);
948 exec_node
*param
= ir
->actual_parameters
.get_head();
949 ir_instruction
*inst
= (ir_instruction
*) param
;
950 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
953 param
= param
->get_next();
954 inst
= (ir_instruction
*) param
;
955 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
957 /* data1 parameter (this is always present) */
958 param
= param
->get_next();
959 inst
= (ir_instruction
*) param
;
960 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
962 /* data2 parameter (only with atomic_comp_swap) */
963 if (param_count
== 4) {
964 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
965 param
= param
->get_next();
966 inst
= (ir_instruction
*) param
;
967 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
971 assert(ir
->return_deref
);
972 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
973 ir
->return_deref
->type
->vector_elements
, NULL
);
974 nir_builder_instr_insert(&b
, &instr
->instr
);
977 case nir_intrinsic_load_shared
: {
978 exec_node
*param
= ir
->actual_parameters
.get_head();
979 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
981 instr
->const_index
[0] = 0;
982 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
984 const glsl_type
*type
= ir
->return_deref
->var
->type
;
985 instr
->num_components
= type
->vector_elements
;
987 /* Setup destination register */
988 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
989 type
->vector_elements
, NULL
);
991 nir_builder_instr_insert(&b
, &instr
->instr
);
994 case nir_intrinsic_store_shared
: {
995 exec_node
*param
= ir
->actual_parameters
.get_head();
996 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
998 param
= param
->get_next();
999 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1001 param
= param
->get_next();
1002 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1005 instr
->const_index
[0] = 0;
1006 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1008 instr
->const_index
[1] = write_mask
->value
.u
[0];
1010 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
1011 instr
->num_components
= val
->type
->vector_elements
;
1013 nir_builder_instr_insert(&b
, &instr
->instr
);
1016 case nir_intrinsic_shared_atomic_add
:
1017 case nir_intrinsic_shared_atomic_imin
:
1018 case nir_intrinsic_shared_atomic_umin
:
1019 case nir_intrinsic_shared_atomic_imax
:
1020 case nir_intrinsic_shared_atomic_umax
:
1021 case nir_intrinsic_shared_atomic_and
:
1022 case nir_intrinsic_shared_atomic_or
:
1023 case nir_intrinsic_shared_atomic_xor
:
1024 case nir_intrinsic_shared_atomic_exchange
:
1025 case nir_intrinsic_shared_atomic_comp_swap
: {
1026 int param_count
= ir
->actual_parameters
.length();
1027 assert(param_count
== 2 || param_count
== 3);
1030 exec_node
*param
= ir
->actual_parameters
.get_head();
1031 ir_instruction
*inst
= (ir_instruction
*) param
;
1032 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1034 /* data1 parameter (this is always present) */
1035 param
= param
->get_next();
1036 inst
= (ir_instruction
*) param
;
1037 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1039 /* data2 parameter (only with atomic_comp_swap) */
1040 if (param_count
== 3) {
1041 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1042 param
= param
->get_next();
1043 inst
= (ir_instruction
*) param
;
1045 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1049 assert(ir
->return_deref
);
1050 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1051 ir
->return_deref
->type
->vector_elements
, NULL
);
1052 nir_builder_instr_insert(&b
, &instr
->instr
);
1056 unreachable("not reached");
1059 if (ir
->return_deref
) {
1060 nir_intrinsic_instr
*store_instr
=
1061 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1062 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1063 store_instr
->const_index
[0] = (1 << store_instr
->num_components
) - 1;
1065 store_instr
->variables
[0] =
1066 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1067 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1069 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1075 struct hash_entry
*entry
=
1076 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1078 nir_function
*callee
= (nir_function
*) entry
->data
;
1080 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1083 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1084 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1088 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1089 nir_builder_instr_insert(&b
, &instr
->instr
);
1093 nir_visitor::visit(ir_assignment
*ir
)
1095 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1097 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1098 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1099 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1100 nir_intrinsic_instr
*copy
=
1101 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1103 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1104 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1106 if (ir
->condition
) {
1107 nir_if
*if_stmt
= nir_if_create(this->shader
);
1108 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1109 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1110 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1111 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1113 nir_builder_instr_insert(&b
, ©
->instr
);
1118 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1120 ir
->lhs
->accept(this);
1121 nir_deref_var
*lhs_deref
= this->deref_head
;
1122 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1124 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1125 /* GLSL IR will give us the input to the write-masked assignment in a
1126 * single packed vector. So, for example, if the writemask is xzw, then
1127 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1131 unsigned component
= 0;
1132 for (unsigned i
= 0; i
< 4; i
++) {
1133 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1135 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1138 nir_intrinsic_instr
*store
=
1139 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1140 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1141 store
->const_index
[0] = ir
->write_mask
;
1142 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1143 store
->variables
[0] = nir_deref_as_var(store_deref
);
1144 store
->src
[0] = nir_src_for_ssa(src
);
1146 if (ir
->condition
) {
1147 nir_if
*if_stmt
= nir_if_create(this->shader
);
1148 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1149 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1150 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1151 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1153 nir_builder_instr_insert(&b
, &store
->instr
);
1158 * Given an instruction, returns a pointer to its destination or NULL if there
1159 * is no destination.
1161 * Note that this only handles instructions we generate at this level.
1164 get_instr_dest(nir_instr
*instr
)
1166 nir_alu_instr
*alu_instr
;
1167 nir_intrinsic_instr
*intrinsic_instr
;
1168 nir_tex_instr
*tex_instr
;
1170 switch (instr
->type
) {
1171 case nir_instr_type_alu
:
1172 alu_instr
= nir_instr_as_alu(instr
);
1173 return &alu_instr
->dest
.dest
;
1175 case nir_instr_type_intrinsic
:
1176 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1177 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1178 return &intrinsic_instr
->dest
;
1182 case nir_instr_type_tex
:
1183 tex_instr
= nir_instr_as_tex(instr
);
1184 return &tex_instr
->dest
;
1187 unreachable("not reached");
1194 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1196 nir_dest
*dest
= get_instr_dest(instr
);
1199 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1201 nir_builder_instr_insert(&b
, instr
);
1204 assert(dest
->is_ssa
);
1205 this->result
= &dest
->ssa
;
1210 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1213 if (ir
->as_dereference() || ir
->as_constant()) {
1215 * A dereference is being used on the right hand side, which means we
1216 * must emit a variable load.
1219 nir_intrinsic_instr
*load_instr
=
1220 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1221 load_instr
->num_components
= ir
->type
->vector_elements
;
1222 load_instr
->variables
[0] = this->deref_head
;
1223 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1224 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1227 return this->result
;
1231 nir_visitor::visit(ir_expression
*ir
)
1233 /* Some special cases */
1234 switch (ir
->operation
) {
1235 case ir_binop_ubo_load
: {
1236 nir_intrinsic_instr
*load
=
1237 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1238 load
->num_components
= ir
->type
->vector_elements
;
1239 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1240 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1241 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1244 * In UBO's, a true boolean value is any non-zero value, but we consider
1245 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1248 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1249 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1254 case ir_unop_interpolate_at_centroid
:
1255 case ir_binop_interpolate_at_offset
:
1256 case ir_binop_interpolate_at_sample
: {
1257 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1258 ir_swizzle
*swizzle
= NULL
;
1260 /* the api does not allow a swizzle here, but the varying packing code
1261 * may have pushed one into here.
1263 swizzle
= ir
->operands
[0]->as_swizzle();
1265 deref
= swizzle
->val
->as_dereference();
1269 deref
->accept(this);
1271 nir_intrinsic_op op
;
1272 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1273 switch (ir
->operation
) {
1274 case ir_unop_interpolate_at_centroid
:
1275 op
= nir_intrinsic_interp_var_at_centroid
;
1277 case ir_binop_interpolate_at_offset
:
1278 op
= nir_intrinsic_interp_var_at_offset
;
1280 case ir_binop_interpolate_at_sample
:
1281 op
= nir_intrinsic_interp_var_at_sample
;
1284 unreachable("Invalid interpolation intrinsic");
1287 /* This case can happen if the vertex shader does not write the
1288 * given varying. In this case, the linker will lower it to a
1289 * global variable. Since interpolating a variable makes no
1290 * sense, we'll just turn it into a load which will probably
1291 * eventually end up as an SSA definition.
1293 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1294 op
= nir_intrinsic_load_var
;
1297 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1298 intrin
->num_components
= deref
->type
->vector_elements
;
1299 intrin
->variables
[0] = this->deref_head
;
1300 ralloc_steal(intrin
, intrin
->variables
[0]);
1302 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1303 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1304 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1306 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1309 unsigned swiz
[4] = {
1310 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1313 result
= nir_swizzle(&b
, result
, swiz
,
1314 swizzle
->type
->vector_elements
, false);
1324 nir_ssa_def
*srcs
[4];
1325 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1326 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1328 glsl_base_type types
[4];
1329 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1331 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1333 types
[i
] = GLSL_TYPE_FLOAT
;
1335 glsl_base_type out_type
;
1337 out_type
= ir
->type
->base_type
;
1339 out_type
= GLSL_TYPE_FLOAT
;
1341 switch (ir
->operation
) {
1342 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1343 case ir_unop_logic_not
:
1344 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1347 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1348 : nir_ineg(&b
, srcs
[0]);
1351 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1352 : nir_iabs(&b
, srcs
[0]);
1354 case ir_unop_saturate
:
1355 assert(types
[0] == GLSL_TYPE_FLOAT
);
1356 result
= nir_fsat(&b
, srcs
[0]);
1359 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1360 : nir_isign(&b
, srcs
[0]);
1362 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1363 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1364 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1365 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1366 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1367 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1368 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1370 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1373 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1376 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1378 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1379 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1380 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1381 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1382 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1385 case ir_unop_bitcast_i2f
:
1386 case ir_unop_bitcast_f2i
:
1387 case ir_unop_bitcast_u2f
:
1388 case ir_unop_bitcast_f2u
:
1389 case ir_unop_subroutine_to_int
:
1391 result
= nir_imov(&b
, srcs
[0]);
1393 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1394 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1395 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1396 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1397 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1398 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1399 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1400 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1401 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1402 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1403 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1404 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1405 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1406 case ir_unop_pack_snorm_2x16
:
1407 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1409 case ir_unop_pack_snorm_4x8
:
1410 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1412 case ir_unop_pack_unorm_2x16
:
1413 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1415 case ir_unop_pack_unorm_4x8
:
1416 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1418 case ir_unop_pack_half_2x16
:
1419 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1421 case ir_unop_unpack_snorm_2x16
:
1422 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1424 case ir_unop_unpack_snorm_4x8
:
1425 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1427 case ir_unop_unpack_unorm_2x16
:
1428 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1430 case ir_unop_unpack_unorm_4x8
:
1431 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1433 case ir_unop_unpack_half_2x16
:
1434 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1436 case ir_unop_unpack_half_2x16_split_x
:
1437 result
= nir_unpack_half_2x16_split_x(&b
, srcs
[0]);
1439 case ir_unop_unpack_half_2x16_split_y
:
1440 result
= nir_unpack_half_2x16_split_y(&b
, srcs
[0]);
1442 case ir_unop_bitfield_reverse
:
1443 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1445 case ir_unop_bit_count
:
1446 result
= nir_bit_count(&b
, srcs
[0]);
1448 case ir_unop_find_msb
:
1450 case GLSL_TYPE_UINT
:
1451 result
= nir_ufind_msb(&b
, srcs
[0]);
1454 result
= nir_ifind_msb(&b
, srcs
[0]);
1457 unreachable("Invalid type for findMSB()");
1460 case ir_unop_find_lsb
:
1461 result
= nir_find_lsb(&b
, srcs
[0]);
1465 switch (ir
->type
->vector_elements
) {
1467 switch (ir
->operands
[0]->type
->vector_elements
) {
1468 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1469 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1470 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1471 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1472 default: unreachable("not reached");
1476 switch (ir
->operands
[0]->type
->vector_elements
) {
1477 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1478 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1479 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1480 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1481 default: unreachable("not reached");
1485 switch (ir
->operands
[0]->type
->vector_elements
) {
1486 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1487 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1488 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1489 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1490 default: unreachable("not reached");
1494 switch (ir
->operands
[0]->type
->vector_elements
) {
1495 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1496 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1497 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1498 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1499 default: unreachable("not reached");
1503 unreachable("not reached");
1506 case ir_unop_get_buffer_size
: {
1507 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1509 nir_intrinsic_get_buffer_size
);
1510 load
->num_components
= ir
->type
->vector_elements
;
1511 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1512 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1517 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1518 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1521 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1522 : nir_isub(&b
, srcs
[0], srcs
[1]);
1525 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1526 : nir_imul(&b
, srcs
[0], srcs
[1]);
1529 if (out_type
== GLSL_TYPE_FLOAT
)
1530 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1531 else if (out_type
== GLSL_TYPE_INT
)
1532 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1534 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1537 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1538 : nir_umod(&b
, srcs
[0], srcs
[1]);
1541 if (out_type
== GLSL_TYPE_FLOAT
)
1542 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1543 else if (out_type
== GLSL_TYPE_INT
)
1544 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1546 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1549 if (out_type
== GLSL_TYPE_FLOAT
)
1550 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1551 else if (out_type
== GLSL_TYPE_INT
)
1552 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1554 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1556 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1557 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1558 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1559 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1560 case ir_binop_logic_and
:
1561 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1562 : nir_fand(&b
, srcs
[0], srcs
[1]);
1564 case ir_binop_logic_or
:
1565 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1566 : nir_for(&b
, srcs
[0], srcs
[1]);
1568 case ir_binop_logic_xor
:
1569 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1570 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1572 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1573 case ir_binop_rshift
:
1574 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1575 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1577 case ir_binop_imul_high
:
1578 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1579 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1581 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1582 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1584 if (supports_ints
) {
1585 if (types
[0] == GLSL_TYPE_FLOAT
)
1586 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1587 else if (types
[0] == GLSL_TYPE_INT
)
1588 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1590 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1592 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1595 case ir_binop_greater
:
1596 if (supports_ints
) {
1597 if (types
[0] == GLSL_TYPE_FLOAT
)
1598 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1599 else if (types
[0] == GLSL_TYPE_INT
)
1600 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1602 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1604 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1607 case ir_binop_lequal
:
1608 if (supports_ints
) {
1609 if (types
[0] == GLSL_TYPE_FLOAT
)
1610 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1611 else if (types
[0] == GLSL_TYPE_INT
)
1612 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1614 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1616 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1619 case ir_binop_gequal
:
1620 if (supports_ints
) {
1621 if (types
[0] == GLSL_TYPE_FLOAT
)
1622 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1623 else if (types
[0] == GLSL_TYPE_INT
)
1624 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1626 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1628 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1631 case ir_binop_equal
:
1632 if (supports_ints
) {
1633 if (types
[0] == GLSL_TYPE_FLOAT
)
1634 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1636 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1638 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1641 case ir_binop_nequal
:
1642 if (supports_ints
) {
1643 if (types
[0] == GLSL_TYPE_FLOAT
)
1644 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1646 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1648 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1651 case ir_binop_all_equal
:
1652 if (supports_ints
) {
1653 if (types
[0] == GLSL_TYPE_FLOAT
) {
1654 switch (ir
->operands
[0]->type
->vector_elements
) {
1655 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1656 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1657 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1658 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1660 unreachable("not reached");
1663 switch (ir
->operands
[0]->type
->vector_elements
) {
1664 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1665 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1666 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1667 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1669 unreachable("not reached");
1673 switch (ir
->operands
[0]->type
->vector_elements
) {
1674 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1675 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1676 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1677 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1679 unreachable("not reached");
1683 case ir_binop_any_nequal
:
1684 if (supports_ints
) {
1685 if (types
[0] == GLSL_TYPE_FLOAT
) {
1686 switch (ir
->operands
[0]->type
->vector_elements
) {
1687 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1688 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1689 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1690 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1692 unreachable("not reached");
1695 switch (ir
->operands
[0]->type
->vector_elements
) {
1696 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1697 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1698 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1699 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1701 unreachable("not reached");
1705 switch (ir
->operands
[0]->type
->vector_elements
) {
1706 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1707 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1708 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1709 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1711 unreachable("not reached");
1716 switch (ir
->operands
[0]->type
->vector_elements
) {
1717 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1718 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1719 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1721 unreachable("not reached");
1725 case ir_binop_pack_half_2x16_split
:
1726 result
= nir_pack_half_2x16_split(&b
, srcs
[0], srcs
[1]);
1728 case ir_binop_bfm
: result
= nir_bfm(&b
, srcs
[0], srcs
[1]); break;
1729 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1731 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1734 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1738 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1740 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1743 result
= nir_bfi(&b
, srcs
[0], srcs
[1], srcs
[2]);
1745 case ir_triop_bitfield_extract
:
1746 result
= (out_type
== GLSL_TYPE_INT
) ?
1747 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1748 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1750 case ir_quadop_bitfield_insert
:
1751 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1753 case ir_quadop_vector
:
1754 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1758 unreachable("not reached");
1763 nir_visitor::visit(ir_swizzle
*ir
)
1765 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1766 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1767 ir
->type
->vector_elements
, !supports_ints
);
1771 nir_visitor::visit(ir_texture
*ir
)
1778 num_srcs
= 1; /* coordinate */
1783 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1784 num_srcs
= 2; /* coordinate, bias/lod */
1788 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1794 if (ir
->lod_info
.lod
!= NULL
)
1795 num_srcs
= 2; /* coordinate, lod */
1797 num_srcs
= 1; /* coordinate */
1801 op
= nir_texop_txf_ms
;
1802 num_srcs
= 2; /* coordinate, sample_index */
1807 if (ir
->lod_info
.lod
!= NULL
)
1808 num_srcs
= 1; /* lod */
1815 num_srcs
= 1; /* coordinate */
1820 num_srcs
= 1; /* coordinate */
1823 case ir_query_levels
:
1824 op
= nir_texop_query_levels
;
1828 case ir_texture_samples
:
1829 op
= nir_texop_texture_samples
;
1833 case ir_samples_identical
:
1834 op
= nir_texop_samples_identical
;
1835 num_srcs
= 1; /* coordinate */
1839 unreachable("not reached");
1842 if (ir
->projector
!= NULL
)
1844 if (ir
->shadow_comparitor
!= NULL
)
1846 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1849 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1852 instr
->sampler_dim
=
1853 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1854 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1855 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1856 if (instr
->is_shadow
)
1857 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1858 switch (ir
->type
->base_type
) {
1859 case GLSL_TYPE_FLOAT
:
1860 instr
->dest_type
= nir_type_float
;
1863 instr
->dest_type
= nir_type_int
;
1865 case GLSL_TYPE_BOOL
:
1866 case GLSL_TYPE_UINT
:
1867 instr
->dest_type
= nir_type_uint
;
1870 unreachable("not reached");
1873 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1875 unsigned src_number
= 0;
1877 if (ir
->coordinate
!= NULL
) {
1878 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1879 instr
->src
[src_number
].src
=
1880 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1881 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1885 if (ir
->projector
!= NULL
) {
1886 instr
->src
[src_number
].src
=
1887 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1888 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1892 if (ir
->shadow_comparitor
!= NULL
) {
1893 instr
->src
[src_number
].src
=
1894 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1895 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1899 if (ir
->offset
!= NULL
) {
1900 /* we don't support multiple offsets yet */
1901 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1903 ir_constant
*const_offset
= ir
->offset
->as_constant();
1904 if (const_offset
!= NULL
) {
1905 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1906 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1908 instr
->src
[src_number
].src
=
1909 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1910 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1917 instr
->src
[src_number
].src
=
1918 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1919 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1926 if (ir
->lod_info
.lod
!= NULL
) {
1927 instr
->src
[src_number
].src
=
1928 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1929 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1935 instr
->src
[src_number
].src
=
1936 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1937 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1939 instr
->src
[src_number
].src
=
1940 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1941 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1946 instr
->src
[src_number
].src
=
1947 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1948 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1953 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1960 assert(src_number
== num_srcs
);
1962 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1966 nir_visitor::visit(ir_constant
*ir
)
1969 * We don't know if this variable is an an array or struct that gets
1970 * dereferenced, so do the safe thing an make it a variable with a
1971 * constant initializer and return a dereference.
1975 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1976 var
->data
.read_only
= true;
1977 var
->constant_initializer
= constant_copy(ir
, var
);
1979 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1980 this->deref_tail
= &this->deref_head
->deref
;
1984 nir_visitor::visit(ir_dereference_variable
*ir
)
1986 struct hash_entry
*entry
=
1987 _mesa_hash_table_search(this->var_table
, ir
->var
);
1989 nir_variable
*var
= (nir_variable
*) entry
->data
;
1991 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1992 this->deref_head
= deref
;
1993 this->deref_tail
= &deref
->deref
;
1997 nir_visitor::visit(ir_dereference_record
*ir
)
1999 ir
->record
->accept(this);
2001 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
2002 assert(field_index
>= 0);
2004 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
2005 deref
->deref
.type
= ir
->type
;
2006 this->deref_tail
->child
= &deref
->deref
;
2007 this->deref_tail
= &deref
->deref
;
2011 nir_visitor::visit(ir_dereference_array
*ir
)
2013 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
2014 deref
->deref
.type
= ir
->type
;
2016 ir_constant
*const_index
= ir
->array_index
->as_constant();
2017 if (const_index
!= NULL
) {
2018 deref
->deref_array_type
= nir_deref_array_type_direct
;
2019 deref
->base_offset
= const_index
->value
.u
[0];
2021 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2023 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2026 ir
->array
->accept(this);
2028 this->deref_tail
->child
= &deref
->deref
;
2029 ralloc_steal(this->deref_tail
, deref
);
2030 this->deref_tail
= &deref
->deref
;
2034 nir_visitor::visit(ir_barrier
*ir
)
2036 nir_intrinsic_instr
*instr
=
2037 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2038 nir_builder_instr_insert(&b
, &instr
->instr
);