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 "compiler/glsl/ir_visitor.h"
32 #include "compiler/glsl/ir_hierarchical_visitor.h"
33 #include "compiler/glsl/ir.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_function
*main
= NULL
;
149 nir_foreach_function(shader
, func
) {
150 if (strcmp(func
->name
, "main") == 0) {
157 nir_lower_outputs_to_temporaries(shader
, main
);
159 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
160 if (shader_prog
->Label
)
161 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
162 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
163 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
164 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
165 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
166 shader
->info
.num_images
= sh
->NumImages
;
167 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
168 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
169 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
170 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
171 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
172 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
173 shader
->info
.uses_clip_distance_out
=
174 sh
->Program
->ClipDistanceArraySize
!= 0;
175 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
176 shader
->info
.has_transform_feedback_varyings
=
177 shader_prog
->TransformFeedback
.NumVarying
> 0;
180 case MESA_SHADER_TESS_CTRL
:
181 shader
->info
.tcs
.vertices_out
= shader_prog
->TessCtrl
.VerticesOut
;
184 case MESA_SHADER_GEOMETRY
:
185 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
186 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
187 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
188 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
189 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
190 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
193 case MESA_SHADER_FRAGMENT
: {
194 struct gl_fragment_program
*fp
=
195 (struct gl_fragment_program
*)sh
->Program
;
197 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
198 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
199 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
203 case MESA_SHADER_COMPUTE
: {
204 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
205 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
206 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
207 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
212 break; /* No stage-specific info */
218 nir_visitor::nir_visitor(nir_shader
*shader
, gl_shader
*sh
)
220 this->supports_ints
= shader
->options
->native_integers
;
221 this->shader
= shader
;
223 this->is_global
= true;
224 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
225 _mesa_key_pointer_equal
);
226 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
227 _mesa_key_pointer_equal
);
230 nir_visitor::~nir_visitor()
232 _mesa_hash_table_destroy(this->var_table
, NULL
);
233 _mesa_hash_table_destroy(this->overload_table
, NULL
);
237 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
240 ralloc_steal(mem_ctx
, this->deref_head
);
241 return this->deref_head
;
244 static nir_constant
*
245 constant_copy(ir_constant
*ir
, void *mem_ctx
)
250 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
252 unsigned total_elems
= ir
->type
->components();
255 ret
->num_elements
= 0;
256 switch (ir
->type
->base_type
) {
258 for (i
= 0; i
< total_elems
; i
++)
259 ret
->value
.u
[i
] = ir
->value
.u
[i
];
263 for (i
= 0; i
< total_elems
; i
++)
264 ret
->value
.i
[i
] = ir
->value
.i
[i
];
267 case GLSL_TYPE_FLOAT
:
268 for (i
= 0; i
< total_elems
; i
++)
269 ret
->value
.f
[i
] = ir
->value
.f
[i
];
273 for (i
= 0; i
< total_elems
; i
++)
274 ret
->value
.b
[i
] = ir
->value
.b
[i
];
277 case GLSL_TYPE_STRUCT
:
278 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
280 ret
->num_elements
= ir
->type
->length
;
283 foreach_in_list(ir_constant
, field
, &ir
->components
) {
284 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
289 case GLSL_TYPE_ARRAY
:
290 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
292 ret
->num_elements
= ir
->type
->length
;
294 for (i
= 0; i
< ir
->type
->length
; i
++)
295 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
299 unreachable("not reached");
306 nir_visitor::visit(ir_variable
*ir
)
308 nir_variable
*var
= ralloc(shader
, nir_variable
);
309 var
->type
= ir
->type
;
310 var
->name
= ralloc_strdup(var
, ir
->name
);
312 var
->data
.read_only
= ir
->data
.read_only
;
313 var
->data
.centroid
= ir
->data
.centroid
;
314 var
->data
.sample
= ir
->data
.sample
;
315 var
->data
.patch
= ir
->data
.patch
;
316 var
->data
.invariant
= ir
->data
.invariant
;
317 var
->data
.location
= ir
->data
.location
;
319 switch(ir
->data
.mode
) {
321 case ir_var_temporary
:
323 var
->data
.mode
= nir_var_global
;
325 var
->data
.mode
= nir_var_local
;
328 case ir_var_function_in
:
329 case ir_var_function_out
:
330 case ir_var_function_inout
:
331 case ir_var_const_in
:
332 var
->data
.mode
= nir_var_local
;
335 case ir_var_shader_in
:
336 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
337 ir
->data
.location
== VARYING_SLOT_FACE
) {
338 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
339 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
340 var
->data
.mode
= nir_var_system_value
;
341 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
342 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
343 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
344 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
345 var
->data
.mode
= nir_var_system_value
;
347 var
->data
.mode
= nir_var_shader_in
;
351 case ir_var_shader_out
:
352 var
->data
.mode
= nir_var_shader_out
;
356 var
->data
.mode
= nir_var_uniform
;
359 case ir_var_shader_storage
:
360 var
->data
.mode
= nir_var_shader_storage
;
363 case ir_var_system_value
:
364 var
->data
.mode
= nir_var_system_value
;
368 unreachable("not reached");
371 var
->data
.interpolation
= ir
->data
.interpolation
;
372 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
373 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
374 var
->data
.explicit_location
= ir
->data
.explicit_location
;
375 var
->data
.explicit_index
= ir
->data
.explicit_index
;
376 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
377 var
->data
.has_initializer
= ir
->data
.has_initializer
;
378 var
->data
.location_frac
= ir
->data
.location_frac
;
380 switch (ir
->data
.depth_layout
) {
381 case ir_depth_layout_none
:
382 var
->data
.depth_layout
= nir_depth_layout_none
;
384 case ir_depth_layout_any
:
385 var
->data
.depth_layout
= nir_depth_layout_any
;
387 case ir_depth_layout_greater
:
388 var
->data
.depth_layout
= nir_depth_layout_greater
;
390 case ir_depth_layout_less
:
391 var
->data
.depth_layout
= nir_depth_layout_less
;
393 case ir_depth_layout_unchanged
:
394 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
397 unreachable("not reached");
400 var
->data
.index
= ir
->data
.index
;
401 var
->data
.descriptor_set
= 0;
402 var
->data
.binding
= ir
->data
.binding
;
403 var
->data
.offset
= ir
->data
.offset
;
404 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
405 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
406 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
407 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
408 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
409 var
->data
.image
.format
= ir
->data
.image_format
;
410 var
->data
.max_array_access
= ir
->data
.max_array_access
;
412 var
->num_state_slots
= ir
->get_num_state_slots();
413 if (var
->num_state_slots
> 0) {
414 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
415 var
->num_state_slots
);
417 ir_state_slot
*state_slots
= ir
->get_state_slots();
418 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
419 for (unsigned j
= 0; j
< 5; j
++)
420 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
421 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
424 var
->state_slots
= NULL
;
427 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
429 var
->interface_type
= ir
->get_interface_type();
431 if (var
->data
.mode
== nir_var_local
)
432 nir_function_impl_add_variable(impl
, var
);
434 nir_shader_add_variable(shader
, var
);
436 _mesa_hash_table_insert(var_table
, ir
, var
);
441 nir_function_visitor::visit_enter(ir_function
*ir
)
443 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
444 visitor
->create_function(sig
);
446 return visit_continue_with_parent
;
450 nir_visitor::create_function(ir_function_signature
*ir
)
452 if (ir
->is_intrinsic
)
455 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
457 unsigned num_params
= ir
->parameters
.length();
458 func
->num_params
= num_params
;
459 func
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
462 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
463 switch (param
->data
.mode
) {
464 case ir_var_function_in
:
465 func
->params
[i
].param_type
= nir_parameter_in
;
468 case ir_var_function_out
:
469 func
->params
[i
].param_type
= nir_parameter_out
;
472 case ir_var_function_inout
:
473 func
->params
[i
].param_type
= nir_parameter_inout
;
477 unreachable("not reached");
480 func
->params
[i
].type
= param
->type
;
484 func
->return_type
= ir
->return_type
;
486 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
490 nir_visitor::visit(ir_function
*ir
)
492 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
497 nir_visitor::visit(ir_function_signature
*ir
)
499 if (ir
->is_intrinsic
)
502 struct hash_entry
*entry
=
503 _mesa_hash_table_search(this->overload_table
, ir
);
506 nir_function
*func
= (nir_function
*) entry
->data
;
508 if (ir
->is_defined
) {
509 nir_function_impl
*impl
= nir_function_impl_create(func
);
512 unsigned num_params
= func
->num_params
;
513 impl
->num_params
= num_params
;
514 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
516 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
518 impl
->params
[i
] = this->var
;
522 if (func
->return_type
== glsl_type::void_type
) {
523 impl
->return_var
= NULL
;
525 impl
->return_var
= ralloc(this->shader
, nir_variable
);
526 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
528 impl
->return_var
->type
= func
->return_type
;
531 this->is_global
= false;
533 nir_builder_init(&b
, impl
);
534 b
.cursor
= nir_after_cf_list(&impl
->body
);
535 visit_exec_list(&ir
->body
, this);
537 this->is_global
= true;
544 nir_visitor::visit(ir_loop
*ir
)
546 nir_loop
*loop
= nir_loop_create(this->shader
);
547 nir_builder_cf_insert(&b
, &loop
->cf_node
);
549 b
.cursor
= nir_after_cf_list(&loop
->body
);
550 visit_exec_list(&ir
->body_instructions
, this);
551 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
555 nir_visitor::visit(ir_if
*ir
)
558 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
560 nir_if
*if_stmt
= nir_if_create(this->shader
);
561 if_stmt
->condition
= condition
;
562 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
564 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
565 visit_exec_list(&ir
->then_instructions
, this);
567 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
568 visit_exec_list(&ir
->else_instructions
, this);
570 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
574 nir_visitor::visit(ir_discard
*ir
)
577 * discards aren't treated as control flow, because before we lower them
578 * they can appear anywhere in the shader and the stuff after them may still
579 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
580 * discards will be immediately followed by a return.
583 nir_intrinsic_instr
*discard
;
585 discard
= nir_intrinsic_instr_create(this->shader
,
586 nir_intrinsic_discard_if
);
588 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
590 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
593 nir_builder_instr_insert(&b
, &discard
->instr
);
597 nir_visitor::visit(ir_emit_vertex
*ir
)
599 nir_intrinsic_instr
*instr
=
600 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
601 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
602 nir_builder_instr_insert(&b
, &instr
->instr
);
606 nir_visitor::visit(ir_end_primitive
*ir
)
608 nir_intrinsic_instr
*instr
=
609 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
610 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
611 nir_builder_instr_insert(&b
, &instr
->instr
);
615 nir_visitor::visit(ir_loop_jump
*ir
)
619 case ir_loop_jump::jump_break
:
620 type
= nir_jump_break
;
622 case ir_loop_jump::jump_continue
:
623 type
= nir_jump_continue
;
626 unreachable("not reached");
629 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
630 nir_builder_instr_insert(&b
, &instr
->instr
);
634 nir_visitor::visit(ir_return
*ir
)
636 if (ir
->value
!= NULL
) {
637 nir_intrinsic_instr
*copy
=
638 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
640 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
641 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
644 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
645 nir_builder_instr_insert(&b
, &instr
->instr
);
649 nir_visitor::visit(ir_call
*ir
)
651 if (ir
->callee
->is_intrinsic
) {
653 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
654 op
= nir_intrinsic_atomic_counter_read_var
;
655 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
656 op
= nir_intrinsic_atomic_counter_inc_var
;
657 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
658 op
= nir_intrinsic_atomic_counter_dec_var
;
659 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
660 op
= nir_intrinsic_image_load
;
661 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
662 op
= nir_intrinsic_image_store
;
663 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
664 op
= nir_intrinsic_image_atomic_add
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
666 op
= nir_intrinsic_image_atomic_min
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
668 op
= nir_intrinsic_image_atomic_max
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
670 op
= nir_intrinsic_image_atomic_and
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
672 op
= nir_intrinsic_image_atomic_or
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
674 op
= nir_intrinsic_image_atomic_xor
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
676 op
= nir_intrinsic_image_atomic_exchange
;
677 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
678 op
= nir_intrinsic_image_atomic_comp_swap
;
679 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
680 op
= nir_intrinsic_memory_barrier
;
681 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
682 op
= nir_intrinsic_image_size
;
683 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
684 op
= nir_intrinsic_image_samples
;
685 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
686 op
= nir_intrinsic_store_ssbo
;
687 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
688 op
= nir_intrinsic_load_ssbo
;
689 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
690 op
= nir_intrinsic_ssbo_atomic_add
;
691 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
692 op
= nir_intrinsic_ssbo_atomic_and
;
693 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
694 op
= nir_intrinsic_ssbo_atomic_or
;
695 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
696 op
= nir_intrinsic_ssbo_atomic_xor
;
697 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
698 assert(ir
->return_deref
);
699 if (ir
->return_deref
->type
== glsl_type::int_type
)
700 op
= nir_intrinsic_ssbo_atomic_imin
;
701 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
702 op
= nir_intrinsic_ssbo_atomic_umin
;
704 unreachable("Invalid type");
705 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
706 assert(ir
->return_deref
);
707 if (ir
->return_deref
->type
== glsl_type::int_type
)
708 op
= nir_intrinsic_ssbo_atomic_imax
;
709 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
710 op
= nir_intrinsic_ssbo_atomic_umax
;
712 unreachable("Invalid type");
713 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
714 op
= nir_intrinsic_ssbo_atomic_exchange
;
715 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
716 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
717 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
718 op
= nir_intrinsic_shader_clock
;
719 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
720 op
= nir_intrinsic_group_memory_barrier
;
721 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
722 op
= nir_intrinsic_memory_barrier_atomic_counter
;
723 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
724 op
= nir_intrinsic_memory_barrier_buffer
;
725 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
726 op
= nir_intrinsic_memory_barrier_image
;
727 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
728 op
= nir_intrinsic_memory_barrier_shared
;
729 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
730 op
= nir_intrinsic_load_shared
;
731 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
732 op
= nir_intrinsic_store_shared
;
733 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
734 op
= nir_intrinsic_shared_atomic_add
;
735 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
736 op
= nir_intrinsic_shared_atomic_and
;
737 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
738 op
= nir_intrinsic_shared_atomic_or
;
739 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
740 op
= nir_intrinsic_shared_atomic_xor
;
741 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
742 assert(ir
->return_deref
);
743 if (ir
->return_deref
->type
== glsl_type::int_type
)
744 op
= nir_intrinsic_shared_atomic_imin
;
745 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
746 op
= nir_intrinsic_shared_atomic_umin
;
748 unreachable("Invalid type");
749 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
750 assert(ir
->return_deref
);
751 if (ir
->return_deref
->type
== glsl_type::int_type
)
752 op
= nir_intrinsic_shared_atomic_imax
;
753 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
754 op
= nir_intrinsic_shared_atomic_umax
;
756 unreachable("Invalid type");
757 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
758 op
= nir_intrinsic_shared_atomic_exchange
;
759 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
760 op
= nir_intrinsic_shared_atomic_comp_swap
;
762 unreachable("not reached");
765 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
766 nir_dest
*dest
= &instr
->dest
;
769 case nir_intrinsic_atomic_counter_read_var
:
770 case nir_intrinsic_atomic_counter_inc_var
:
771 case nir_intrinsic_atomic_counter_dec_var
: {
772 ir_dereference
*param
=
773 (ir_dereference
*) ir
->actual_parameters
.get_head();
774 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
775 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
776 nir_builder_instr_insert(&b
, &instr
->instr
);
779 case nir_intrinsic_image_load
:
780 case nir_intrinsic_image_store
:
781 case nir_intrinsic_image_atomic_add
:
782 case nir_intrinsic_image_atomic_min
:
783 case nir_intrinsic_image_atomic_max
:
784 case nir_intrinsic_image_atomic_and
:
785 case nir_intrinsic_image_atomic_or
:
786 case nir_intrinsic_image_atomic_xor
:
787 case nir_intrinsic_image_atomic_exchange
:
788 case nir_intrinsic_image_atomic_comp_swap
:
789 case nir_intrinsic_image_samples
:
790 case nir_intrinsic_image_size
: {
791 nir_ssa_undef_instr
*instr_undef
=
792 nir_ssa_undef_instr_create(shader
, 1);
793 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
795 /* Set the image variable dereference. */
796 exec_node
*param
= ir
->actual_parameters
.get_head();
797 ir_dereference
*image
= (ir_dereference
*)param
;
798 const glsl_type
*type
=
799 image
->variable_referenced()->type
->without_array();
801 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
802 param
= param
->get_next();
804 /* Set the intrinsic destination. */
805 if (ir
->return_deref
) {
806 const nir_intrinsic_info
*info
=
807 &nir_intrinsic_infos
[instr
->intrinsic
];
808 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
809 info
->dest_components
, NULL
);
812 if (op
== nir_intrinsic_image_size
||
813 op
== nir_intrinsic_image_samples
) {
814 nir_builder_instr_insert(&b
, &instr
->instr
);
818 /* Set the address argument, extending the coordinate vector to four
821 nir_ssa_def
*src_addr
=
822 evaluate_rvalue((ir_dereference
*)param
);
823 nir_ssa_def
*srcs
[4];
825 for (int i
= 0; i
< 4; i
++) {
826 if (i
< type
->coordinate_components())
827 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
829 srcs
[i
] = &instr_undef
->def
;
832 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
833 param
= param
->get_next();
835 /* Set the sample argument, which is undefined for single-sample
838 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
840 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
841 param
= param
->get_next();
843 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
846 /* Set the intrinsic parameters. */
847 if (!param
->is_tail_sentinel()) {
849 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
850 param
= param
->get_next();
853 if (!param
->is_tail_sentinel()) {
855 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
856 param
= param
->get_next();
858 nir_builder_instr_insert(&b
, &instr
->instr
);
861 case nir_intrinsic_memory_barrier
:
862 case nir_intrinsic_group_memory_barrier
:
863 case nir_intrinsic_memory_barrier_atomic_counter
:
864 case nir_intrinsic_memory_barrier_buffer
:
865 case nir_intrinsic_memory_barrier_image
:
866 case nir_intrinsic_memory_barrier_shared
:
867 nir_builder_instr_insert(&b
, &instr
->instr
);
869 case nir_intrinsic_shader_clock
:
870 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
871 nir_builder_instr_insert(&b
, &instr
->instr
);
873 case nir_intrinsic_store_ssbo
: {
874 exec_node
*param
= ir
->actual_parameters
.get_head();
875 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
877 param
= param
->get_next();
878 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
880 param
= param
->get_next();
881 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
883 param
= param
->get_next();
884 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
887 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
888 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
889 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
890 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
891 instr
->num_components
= val
->type
->vector_elements
;
893 nir_builder_instr_insert(&b
, &instr
->instr
);
896 case nir_intrinsic_load_ssbo
: {
897 exec_node
*param
= ir
->actual_parameters
.get_head();
898 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
900 param
= param
->get_next();
901 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
903 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
904 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
906 const glsl_type
*type
= ir
->return_deref
->var
->type
;
907 instr
->num_components
= type
->vector_elements
;
909 /* Setup destination register */
910 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
911 type
->vector_elements
, NULL
);
913 /* Insert the created nir instruction now since in the case of boolean
914 * result we will need to emit another instruction after it
916 nir_builder_instr_insert(&b
, &instr
->instr
);
919 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
920 * consider a true boolean to be ~0. Fix this up with a != 0
923 if (type
->base_type
== GLSL_TYPE_BOOL
) {
924 nir_alu_instr
*load_ssbo_compare
=
925 nir_alu_instr_create(shader
, nir_op_ine
);
926 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
927 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
928 load_ssbo_compare
->src
[1].src
=
929 nir_src_for_ssa(nir_imm_int(&b
, 0));
930 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
931 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
932 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
933 &load_ssbo_compare
->dest
.dest
,
934 type
->vector_elements
, NULL
);
935 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
936 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
937 dest
= &load_ssbo_compare
->dest
.dest
;
941 case nir_intrinsic_ssbo_atomic_add
:
942 case nir_intrinsic_ssbo_atomic_imin
:
943 case nir_intrinsic_ssbo_atomic_umin
:
944 case nir_intrinsic_ssbo_atomic_imax
:
945 case nir_intrinsic_ssbo_atomic_umax
:
946 case nir_intrinsic_ssbo_atomic_and
:
947 case nir_intrinsic_ssbo_atomic_or
:
948 case nir_intrinsic_ssbo_atomic_xor
:
949 case nir_intrinsic_ssbo_atomic_exchange
:
950 case nir_intrinsic_ssbo_atomic_comp_swap
: {
951 int param_count
= ir
->actual_parameters
.length();
952 assert(param_count
== 3 || param_count
== 4);
955 exec_node
*param
= ir
->actual_parameters
.get_head();
956 ir_instruction
*inst
= (ir_instruction
*) param
;
957 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
960 param
= param
->get_next();
961 inst
= (ir_instruction
*) param
;
962 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
964 /* data1 parameter (this is always present) */
965 param
= param
->get_next();
966 inst
= (ir_instruction
*) param
;
967 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
969 /* data2 parameter (only with atomic_comp_swap) */
970 if (param_count
== 4) {
971 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
972 param
= param
->get_next();
973 inst
= (ir_instruction
*) param
;
974 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
978 assert(ir
->return_deref
);
979 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
980 ir
->return_deref
->type
->vector_elements
, NULL
);
981 nir_builder_instr_insert(&b
, &instr
->instr
);
984 case nir_intrinsic_load_shared
: {
985 exec_node
*param
= ir
->actual_parameters
.get_head();
986 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
988 nir_intrinsic_set_base(instr
, 0);
989 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
991 const glsl_type
*type
= ir
->return_deref
->var
->type
;
992 instr
->num_components
= type
->vector_elements
;
994 /* Setup destination register */
995 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
996 type
->vector_elements
, NULL
);
998 nir_builder_instr_insert(&b
, &instr
->instr
);
1001 case nir_intrinsic_store_shared
: {
1002 exec_node
*param
= ir
->actual_parameters
.get_head();
1003 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1005 param
= param
->get_next();
1006 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1008 param
= param
->get_next();
1009 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1012 nir_intrinsic_set_base(instr
, 0);
1013 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1015 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1017 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
1018 instr
->num_components
= val
->type
->vector_elements
;
1020 nir_builder_instr_insert(&b
, &instr
->instr
);
1023 case nir_intrinsic_shared_atomic_add
:
1024 case nir_intrinsic_shared_atomic_imin
:
1025 case nir_intrinsic_shared_atomic_umin
:
1026 case nir_intrinsic_shared_atomic_imax
:
1027 case nir_intrinsic_shared_atomic_umax
:
1028 case nir_intrinsic_shared_atomic_and
:
1029 case nir_intrinsic_shared_atomic_or
:
1030 case nir_intrinsic_shared_atomic_xor
:
1031 case nir_intrinsic_shared_atomic_exchange
:
1032 case nir_intrinsic_shared_atomic_comp_swap
: {
1033 int param_count
= ir
->actual_parameters
.length();
1034 assert(param_count
== 2 || param_count
== 3);
1037 exec_node
*param
= ir
->actual_parameters
.get_head();
1038 ir_instruction
*inst
= (ir_instruction
*) param
;
1039 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1041 /* data1 parameter (this is always present) */
1042 param
= param
->get_next();
1043 inst
= (ir_instruction
*) param
;
1044 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1046 /* data2 parameter (only with atomic_comp_swap) */
1047 if (param_count
== 3) {
1048 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1049 param
= param
->get_next();
1050 inst
= (ir_instruction
*) param
;
1052 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1056 assert(ir
->return_deref
);
1057 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1058 ir
->return_deref
->type
->vector_elements
, NULL
);
1059 nir_builder_instr_insert(&b
, &instr
->instr
);
1063 unreachable("not reached");
1066 if (ir
->return_deref
) {
1067 nir_intrinsic_instr
*store_instr
=
1068 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1069 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1070 nir_intrinsic_set_write_mask(store_instr
,
1071 (1 << store_instr
->num_components
) - 1);
1073 store_instr
->variables
[0] =
1074 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1075 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1077 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1083 struct hash_entry
*entry
=
1084 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1086 nir_function
*callee
= (nir_function
*) entry
->data
;
1088 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1091 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1092 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1096 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1097 nir_builder_instr_insert(&b
, &instr
->instr
);
1101 nir_visitor::visit(ir_assignment
*ir
)
1103 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1105 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1106 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1107 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1108 nir_intrinsic_instr
*copy
=
1109 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1111 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1112 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1114 if (ir
->condition
) {
1115 nir_if
*if_stmt
= nir_if_create(this->shader
);
1116 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1117 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1118 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1119 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1121 nir_builder_instr_insert(&b
, ©
->instr
);
1126 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1128 ir
->lhs
->accept(this);
1129 nir_deref_var
*lhs_deref
= this->deref_head
;
1130 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1132 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1133 /* GLSL IR will give us the input to the write-masked assignment in a
1134 * single packed vector. So, for example, if the writemask is xzw, then
1135 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1139 unsigned component
= 0;
1140 for (unsigned i
= 0; i
< 4; i
++) {
1141 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1143 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1146 nir_intrinsic_instr
*store
=
1147 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1148 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1149 nir_intrinsic_set_write_mask(store
, ir
->write_mask
);
1150 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1151 store
->variables
[0] = nir_deref_as_var(store_deref
);
1152 store
->src
[0] = nir_src_for_ssa(src
);
1154 if (ir
->condition
) {
1155 nir_if
*if_stmt
= nir_if_create(this->shader
);
1156 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1157 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1158 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1159 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1161 nir_builder_instr_insert(&b
, &store
->instr
);
1166 * Given an instruction, returns a pointer to its destination or NULL if there
1167 * is no destination.
1169 * Note that this only handles instructions we generate at this level.
1172 get_instr_dest(nir_instr
*instr
)
1174 nir_alu_instr
*alu_instr
;
1175 nir_intrinsic_instr
*intrinsic_instr
;
1176 nir_tex_instr
*tex_instr
;
1178 switch (instr
->type
) {
1179 case nir_instr_type_alu
:
1180 alu_instr
= nir_instr_as_alu(instr
);
1181 return &alu_instr
->dest
.dest
;
1183 case nir_instr_type_intrinsic
:
1184 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1185 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1186 return &intrinsic_instr
->dest
;
1190 case nir_instr_type_tex
:
1191 tex_instr
= nir_instr_as_tex(instr
);
1192 return &tex_instr
->dest
;
1195 unreachable("not reached");
1202 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1204 nir_dest
*dest
= get_instr_dest(instr
);
1207 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1209 nir_builder_instr_insert(&b
, instr
);
1212 assert(dest
->is_ssa
);
1213 this->result
= &dest
->ssa
;
1218 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1221 if (ir
->as_dereference() || ir
->as_constant()) {
1223 * A dereference is being used on the right hand side, which means we
1224 * must emit a variable load.
1227 nir_intrinsic_instr
*load_instr
=
1228 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1229 load_instr
->num_components
= ir
->type
->vector_elements
;
1230 load_instr
->variables
[0] = this->deref_head
;
1231 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1232 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1235 return this->result
;
1239 nir_visitor::visit(ir_expression
*ir
)
1241 /* Some special cases */
1242 switch (ir
->operation
) {
1243 case ir_binop_ubo_load
: {
1244 nir_intrinsic_instr
*load
=
1245 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1246 load
->num_components
= ir
->type
->vector_elements
;
1247 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1248 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1249 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1252 * In UBO's, a true boolean value is any non-zero value, but we consider
1253 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1256 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1257 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1262 case ir_unop_interpolate_at_centroid
:
1263 case ir_binop_interpolate_at_offset
:
1264 case ir_binop_interpolate_at_sample
: {
1265 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1266 ir_swizzle
*swizzle
= NULL
;
1268 /* the api does not allow a swizzle here, but the varying packing code
1269 * may have pushed one into here.
1271 swizzle
= ir
->operands
[0]->as_swizzle();
1273 deref
= swizzle
->val
->as_dereference();
1277 deref
->accept(this);
1279 nir_intrinsic_op op
;
1280 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1281 switch (ir
->operation
) {
1282 case ir_unop_interpolate_at_centroid
:
1283 op
= nir_intrinsic_interp_var_at_centroid
;
1285 case ir_binop_interpolate_at_offset
:
1286 op
= nir_intrinsic_interp_var_at_offset
;
1288 case ir_binop_interpolate_at_sample
:
1289 op
= nir_intrinsic_interp_var_at_sample
;
1292 unreachable("Invalid interpolation intrinsic");
1295 /* This case can happen if the vertex shader does not write the
1296 * given varying. In this case, the linker will lower it to a
1297 * global variable. Since interpolating a variable makes no
1298 * sense, we'll just turn it into a load which will probably
1299 * eventually end up as an SSA definition.
1301 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1302 op
= nir_intrinsic_load_var
;
1305 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1306 intrin
->num_components
= deref
->type
->vector_elements
;
1307 intrin
->variables
[0] = this->deref_head
;
1308 ralloc_steal(intrin
, intrin
->variables
[0]);
1310 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1311 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1312 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1314 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1317 unsigned swiz
[4] = {
1318 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1321 result
= nir_swizzle(&b
, result
, swiz
,
1322 swizzle
->type
->vector_elements
, false);
1332 nir_ssa_def
*srcs
[4];
1333 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1334 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1336 glsl_base_type types
[4];
1337 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1339 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1341 types
[i
] = GLSL_TYPE_FLOAT
;
1343 glsl_base_type out_type
;
1345 out_type
= ir
->type
->base_type
;
1347 out_type
= GLSL_TYPE_FLOAT
;
1349 switch (ir
->operation
) {
1350 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1351 case ir_unop_logic_not
:
1352 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1355 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1356 : nir_ineg(&b
, srcs
[0]);
1359 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1360 : nir_iabs(&b
, srcs
[0]);
1362 case ir_unop_saturate
:
1363 assert(types
[0] == GLSL_TYPE_FLOAT
);
1364 result
= nir_fsat(&b
, srcs
[0]);
1367 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1368 : nir_isign(&b
, srcs
[0]);
1370 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1371 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1372 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1373 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1374 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1375 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1376 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1378 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1381 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1384 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1386 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1387 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1388 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1389 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1390 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1393 case ir_unop_bitcast_i2f
:
1394 case ir_unop_bitcast_f2i
:
1395 case ir_unop_bitcast_u2f
:
1396 case ir_unop_bitcast_f2u
:
1397 case ir_unop_subroutine_to_int
:
1399 result
= nir_imov(&b
, srcs
[0]);
1401 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1402 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1403 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1404 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1405 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1406 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1407 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1408 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1409 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1410 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1411 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1412 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1413 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1414 case ir_unop_pack_snorm_2x16
:
1415 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1417 case ir_unop_pack_snorm_4x8
:
1418 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1420 case ir_unop_pack_unorm_2x16
:
1421 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1423 case ir_unop_pack_unorm_4x8
:
1424 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1426 case ir_unop_pack_half_2x16
:
1427 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1429 case ir_unop_unpack_snorm_2x16
:
1430 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1432 case ir_unop_unpack_snorm_4x8
:
1433 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1435 case ir_unop_unpack_unorm_2x16
:
1436 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1438 case ir_unop_unpack_unorm_4x8
:
1439 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1441 case ir_unop_unpack_half_2x16
:
1442 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1444 case ir_unop_bitfield_reverse
:
1445 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1447 case ir_unop_bit_count
:
1448 result
= nir_bit_count(&b
, srcs
[0]);
1450 case ir_unop_find_msb
:
1452 case GLSL_TYPE_UINT
:
1453 result
= nir_ufind_msb(&b
, srcs
[0]);
1456 result
= nir_ifind_msb(&b
, srcs
[0]);
1459 unreachable("Invalid type for findMSB()");
1462 case ir_unop_find_lsb
:
1463 result
= nir_find_lsb(&b
, srcs
[0]);
1467 switch (ir
->type
->vector_elements
) {
1469 switch (ir
->operands
[0]->type
->vector_elements
) {
1470 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1471 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1472 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1473 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1474 default: unreachable("not reached");
1478 switch (ir
->operands
[0]->type
->vector_elements
) {
1479 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1480 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1481 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1482 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1483 default: unreachable("not reached");
1487 switch (ir
->operands
[0]->type
->vector_elements
) {
1488 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1489 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1490 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1491 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1492 default: unreachable("not reached");
1496 switch (ir
->operands
[0]->type
->vector_elements
) {
1497 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1498 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1499 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1500 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1501 default: unreachable("not reached");
1505 unreachable("not reached");
1508 case ir_unop_get_buffer_size
: {
1509 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1511 nir_intrinsic_get_buffer_size
);
1512 load
->num_components
= ir
->type
->vector_elements
;
1513 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1514 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1519 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1520 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1523 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1524 : nir_isub(&b
, srcs
[0], srcs
[1]);
1527 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1528 : nir_imul(&b
, srcs
[0], srcs
[1]);
1531 if (out_type
== GLSL_TYPE_FLOAT
)
1532 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1533 else if (out_type
== GLSL_TYPE_INT
)
1534 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1536 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1539 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1540 : nir_umod(&b
, srcs
[0], srcs
[1]);
1543 if (out_type
== GLSL_TYPE_FLOAT
)
1544 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1545 else if (out_type
== GLSL_TYPE_INT
)
1546 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1548 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1551 if (out_type
== GLSL_TYPE_FLOAT
)
1552 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1553 else if (out_type
== GLSL_TYPE_INT
)
1554 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1556 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1558 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1559 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1560 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1561 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1562 case ir_binop_logic_and
:
1563 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1564 : nir_fand(&b
, srcs
[0], srcs
[1]);
1566 case ir_binop_logic_or
:
1567 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1568 : nir_for(&b
, srcs
[0], srcs
[1]);
1570 case ir_binop_logic_xor
:
1571 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1572 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1574 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1575 case ir_binop_rshift
:
1576 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1577 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1579 case ir_binop_imul_high
:
1580 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1581 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1583 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1584 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1586 if (supports_ints
) {
1587 if (types
[0] == GLSL_TYPE_FLOAT
)
1588 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1589 else if (types
[0] == GLSL_TYPE_INT
)
1590 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1592 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1594 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1597 case ir_binop_greater
:
1598 if (supports_ints
) {
1599 if (types
[0] == GLSL_TYPE_FLOAT
)
1600 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1601 else if (types
[0] == GLSL_TYPE_INT
)
1602 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1604 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1606 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1609 case ir_binop_lequal
:
1610 if (supports_ints
) {
1611 if (types
[0] == GLSL_TYPE_FLOAT
)
1612 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1613 else if (types
[0] == GLSL_TYPE_INT
)
1614 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1616 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1618 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1621 case ir_binop_gequal
:
1622 if (supports_ints
) {
1623 if (types
[0] == GLSL_TYPE_FLOAT
)
1624 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1625 else if (types
[0] == GLSL_TYPE_INT
)
1626 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1628 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1630 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1633 case ir_binop_equal
:
1634 if (supports_ints
) {
1635 if (types
[0] == GLSL_TYPE_FLOAT
)
1636 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1638 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1640 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1643 case ir_binop_nequal
:
1644 if (supports_ints
) {
1645 if (types
[0] == GLSL_TYPE_FLOAT
)
1646 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1648 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1650 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1653 case ir_binop_all_equal
:
1654 if (supports_ints
) {
1655 if (types
[0] == GLSL_TYPE_FLOAT
) {
1656 switch (ir
->operands
[0]->type
->vector_elements
) {
1657 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1658 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1659 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1660 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1662 unreachable("not reached");
1665 switch (ir
->operands
[0]->type
->vector_elements
) {
1666 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1667 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1668 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1669 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1671 unreachable("not reached");
1675 switch (ir
->operands
[0]->type
->vector_elements
) {
1676 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1677 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1678 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1679 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1681 unreachable("not reached");
1685 case ir_binop_any_nequal
:
1686 if (supports_ints
) {
1687 if (types
[0] == GLSL_TYPE_FLOAT
) {
1688 switch (ir
->operands
[0]->type
->vector_elements
) {
1689 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1690 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1691 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1692 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1694 unreachable("not reached");
1697 switch (ir
->operands
[0]->type
->vector_elements
) {
1698 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1699 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1700 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1701 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1703 unreachable("not reached");
1707 switch (ir
->operands
[0]->type
->vector_elements
) {
1708 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1709 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1710 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1711 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1713 unreachable("not reached");
1718 switch (ir
->operands
[0]->type
->vector_elements
) {
1719 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1720 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1721 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1723 unreachable("not reached");
1727 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1729 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1732 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1736 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1738 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1740 case ir_triop_bitfield_extract
:
1741 result
= (out_type
== GLSL_TYPE_INT
) ?
1742 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1743 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1745 case ir_quadop_bitfield_insert
:
1746 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1748 case ir_quadop_vector
:
1749 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1753 unreachable("not reached");
1758 nir_visitor::visit(ir_swizzle
*ir
)
1760 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1761 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1762 ir
->type
->vector_elements
, !supports_ints
);
1766 nir_visitor::visit(ir_texture
*ir
)
1773 num_srcs
= 1; /* coordinate */
1778 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1779 num_srcs
= 2; /* coordinate, bias/lod */
1783 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1789 if (ir
->lod_info
.lod
!= NULL
)
1790 num_srcs
= 2; /* coordinate, lod */
1792 num_srcs
= 1; /* coordinate */
1796 op
= nir_texop_txf_ms
;
1797 num_srcs
= 2; /* coordinate, sample_index */
1802 if (ir
->lod_info
.lod
!= NULL
)
1803 num_srcs
= 1; /* lod */
1810 num_srcs
= 1; /* coordinate */
1815 num_srcs
= 1; /* coordinate */
1818 case ir_query_levels
:
1819 op
= nir_texop_query_levels
;
1823 case ir_texture_samples
:
1824 op
= nir_texop_texture_samples
;
1828 case ir_samples_identical
:
1829 op
= nir_texop_samples_identical
;
1830 num_srcs
= 1; /* coordinate */
1834 unreachable("not reached");
1837 if (ir
->projector
!= NULL
)
1839 if (ir
->shadow_comparitor
!= NULL
)
1841 if (ir
->offset
!= NULL
)
1844 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1847 instr
->sampler_dim
=
1848 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1849 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1850 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1851 if (instr
->is_shadow
)
1852 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1853 switch (ir
->type
->base_type
) {
1854 case GLSL_TYPE_FLOAT
:
1855 instr
->dest_type
= nir_type_float
;
1858 instr
->dest_type
= nir_type_int
;
1860 case GLSL_TYPE_BOOL
:
1861 case GLSL_TYPE_UINT
:
1862 instr
->dest_type
= nir_type_uint
;
1865 unreachable("not reached");
1868 instr
->texture
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1870 unsigned src_number
= 0;
1872 if (ir
->coordinate
!= NULL
) {
1873 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1874 instr
->src
[src_number
].src
=
1875 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1876 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1880 if (ir
->projector
!= NULL
) {
1881 instr
->src
[src_number
].src
=
1882 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1883 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1887 if (ir
->shadow_comparitor
!= NULL
) {
1888 instr
->src
[src_number
].src
=
1889 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1890 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1894 if (ir
->offset
!= NULL
) {
1895 /* we don't support multiple offsets yet */
1896 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1898 instr
->src
[src_number
].src
=
1899 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1900 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1906 instr
->src
[src_number
].src
=
1907 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1908 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1915 if (ir
->lod_info
.lod
!= NULL
) {
1916 instr
->src
[src_number
].src
=
1917 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1918 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1924 instr
->src
[src_number
].src
=
1925 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1926 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1928 instr
->src
[src_number
].src
=
1929 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1930 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1935 instr
->src
[src_number
].src
=
1936 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1937 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1942 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1949 assert(src_number
== num_srcs
);
1951 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1955 nir_visitor::visit(ir_constant
*ir
)
1958 * We don't know if this variable is an an array or struct that gets
1959 * dereferenced, so do the safe thing an make it a variable with a
1960 * constant initializer and return a dereference.
1964 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1965 var
->data
.read_only
= true;
1966 var
->constant_initializer
= constant_copy(ir
, var
);
1968 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1969 this->deref_tail
= &this->deref_head
->deref
;
1973 nir_visitor::visit(ir_dereference_variable
*ir
)
1975 struct hash_entry
*entry
=
1976 _mesa_hash_table_search(this->var_table
, ir
->var
);
1978 nir_variable
*var
= (nir_variable
*) entry
->data
;
1980 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1981 this->deref_head
= deref
;
1982 this->deref_tail
= &deref
->deref
;
1986 nir_visitor::visit(ir_dereference_record
*ir
)
1988 ir
->record
->accept(this);
1990 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1991 assert(field_index
>= 0);
1993 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1994 deref
->deref
.type
= ir
->type
;
1995 this->deref_tail
->child
= &deref
->deref
;
1996 this->deref_tail
= &deref
->deref
;
2000 nir_visitor::visit(ir_dereference_array
*ir
)
2002 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
2003 deref
->deref
.type
= ir
->type
;
2005 ir_constant
*const_index
= ir
->array_index
->as_constant();
2006 if (const_index
!= NULL
) {
2007 deref
->deref_array_type
= nir_deref_array_type_direct
;
2008 deref
->base_offset
= const_index
->value
.u
[0];
2010 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2012 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2015 ir
->array
->accept(this);
2017 this->deref_tail
->child
= &deref
->deref
;
2018 ralloc_steal(this->deref_tail
, deref
);
2019 this->deref_tail
= &deref
->deref
;
2023 nir_visitor::visit(ir_barrier
*ir
)
2025 nir_intrinsic_instr
*instr
=
2026 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2027 nir_builder_instr_insert(&b
, &instr
->instr
);