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
);
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
*ir
);
76 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
77 void add_instr(nir_instr
*instr
, unsigned num_components
);
78 nir_ssa_def
*evaluate_rvalue(ir_rvalue
*ir
);
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
**srcs
);
81 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
);
82 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
84 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
85 nir_ssa_def
*src2
, nir_ssa_def
*src3
);
90 nir_function_impl
*impl
;
92 nir_ssa_def
*result
; /* result of the expression tree last visited */
94 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
96 /* the head of the dereference chain we're creating */
97 nir_deref_var
*deref_head
;
98 /* the tail of the dereference chain we're creating */
99 nir_deref
*deref_tail
;
101 nir_variable
*var
; /* variable created by ir_variable visitor */
103 /* whether the IR we're operating on is per-function or global */
106 /* map of ir_variable -> nir_variable */
107 struct hash_table
*var_table
;
109 /* map of ir_function_signature -> nir_function_overload */
110 struct hash_table
*overload_table
;
114 * This visitor runs before the main visitor, calling create_function() for
115 * each function so that the main visitor can resolve forward references in
119 class nir_function_visitor
: public ir_hierarchical_visitor
122 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
125 virtual ir_visitor_status
visit_enter(ir_function
*);
128 nir_visitor
*visitor
;
131 }; /* end of anonymous namespace */
134 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
135 gl_shader_stage stage
,
136 const nir_shader_compiler_options
*options
)
138 struct gl_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
140 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
142 nir_visitor
v1(shader
);
143 nir_function_visitor
v2(&v1
);
145 visit_exec_list(sh
->ir
, &v1
);
147 nir_lower_outputs_to_temporaries(shader
);
149 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
150 if (shader_prog
->Label
)
151 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
152 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
153 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
154 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
155 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
156 shader
->info
.num_images
= sh
->NumImages
;
157 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
158 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
159 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
160 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
161 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
162 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
163 shader
->info
.uses_clip_distance_out
=
164 sh
->Program
->ClipDistanceArraySize
!= 0;
165 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
166 shader
->info
.has_transform_feedback_varyings
=
167 shader_prog
->TransformFeedback
.NumVarying
> 0;
170 case MESA_SHADER_TESS_CTRL
:
171 shader
->info
.tcs
.vertices_out
= shader_prog
->TessCtrl
.VerticesOut
;
174 case MESA_SHADER_GEOMETRY
:
175 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
176 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
177 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
178 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
179 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
180 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
183 case MESA_SHADER_FRAGMENT
: {
184 struct gl_fragment_program
*fp
=
185 (struct gl_fragment_program
*)sh
->Program
;
187 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
188 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
189 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
193 case MESA_SHADER_COMPUTE
: {
194 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
195 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
196 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
197 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
202 break; /* No stage-specific info */
208 nir_visitor::nir_visitor(nir_shader
*shader
)
210 this->supports_ints
= shader
->options
->native_integers
;
211 this->shader
= shader
;
212 this->is_global
= true;
213 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
214 _mesa_key_pointer_equal
);
215 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
216 _mesa_key_pointer_equal
);
219 nir_visitor::~nir_visitor()
221 _mesa_hash_table_destroy(this->var_table
, NULL
);
222 _mesa_hash_table_destroy(this->overload_table
, NULL
);
226 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
229 ralloc_steal(mem_ctx
, this->deref_head
);
230 return this->deref_head
;
233 static nir_constant
*
234 constant_copy(ir_constant
*ir
, void *mem_ctx
)
239 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
241 unsigned total_elems
= ir
->type
->components();
244 ret
->num_elements
= 0;
245 switch (ir
->type
->base_type
) {
247 for (i
= 0; i
< total_elems
; i
++)
248 ret
->value
.u
[i
] = ir
->value
.u
[i
];
252 for (i
= 0; i
< total_elems
; i
++)
253 ret
->value
.i
[i
] = ir
->value
.i
[i
];
256 case GLSL_TYPE_FLOAT
:
257 for (i
= 0; i
< total_elems
; i
++)
258 ret
->value
.f
[i
] = ir
->value
.f
[i
];
262 for (i
= 0; i
< total_elems
; i
++)
263 ret
->value
.b
[i
] = ir
->value
.b
[i
];
266 case GLSL_TYPE_STRUCT
:
267 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
269 ret
->num_elements
= ir
->type
->length
;
272 foreach_in_list(ir_constant
, field
, &ir
->components
) {
273 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
278 case GLSL_TYPE_ARRAY
:
279 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
281 ret
->num_elements
= ir
->type
->length
;
283 for (i
= 0; i
< ir
->type
->length
; i
++)
284 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
288 unreachable("not reached");
295 nir_visitor::visit(ir_variable
*ir
)
297 nir_variable
*var
= ralloc(shader
, nir_variable
);
298 var
->type
= ir
->type
;
299 var
->name
= ralloc_strdup(var
, ir
->name
);
301 var
->data
.read_only
= ir
->data
.read_only
;
302 var
->data
.centroid
= ir
->data
.centroid
;
303 var
->data
.sample
= ir
->data
.sample
;
304 var
->data
.patch
= ir
->data
.patch
;
305 var
->data
.invariant
= ir
->data
.invariant
;
306 var
->data
.location
= ir
->data
.location
;
308 switch(ir
->data
.mode
) {
310 case ir_var_temporary
:
312 var
->data
.mode
= nir_var_global
;
314 var
->data
.mode
= nir_var_local
;
317 case ir_var_function_in
:
318 case ir_var_function_out
:
319 case ir_var_function_inout
:
320 case ir_var_const_in
:
321 var
->data
.mode
= nir_var_local
;
324 case ir_var_shader_in
:
325 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
326 ir
->data
.location
== VARYING_SLOT_FACE
) {
327 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
328 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
329 var
->data
.mode
= nir_var_system_value
;
330 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
331 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
332 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
333 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
334 var
->data
.mode
= nir_var_system_value
;
336 var
->data
.mode
= nir_var_shader_in
;
340 case ir_var_shader_out
:
341 var
->data
.mode
= nir_var_shader_out
;
345 var
->data
.mode
= nir_var_uniform
;
348 case ir_var_shader_storage
:
349 var
->data
.mode
= nir_var_shader_storage
;
352 case ir_var_system_value
:
353 var
->data
.mode
= nir_var_system_value
;
357 unreachable("not reached");
360 var
->data
.interpolation
= ir
->data
.interpolation
;
361 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
362 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
363 var
->data
.explicit_location
= ir
->data
.explicit_location
;
364 var
->data
.explicit_index
= ir
->data
.explicit_index
;
365 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
366 var
->data
.has_initializer
= ir
->data
.has_initializer
;
367 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
368 var
->data
.location_frac
= ir
->data
.location_frac
;
369 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
370 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
372 switch (ir
->data
.depth_layout
) {
373 case ir_depth_layout_none
:
374 var
->data
.depth_layout
= nir_depth_layout_none
;
376 case ir_depth_layout_any
:
377 var
->data
.depth_layout
= nir_depth_layout_any
;
379 case ir_depth_layout_greater
:
380 var
->data
.depth_layout
= nir_depth_layout_greater
;
382 case ir_depth_layout_less
:
383 var
->data
.depth_layout
= nir_depth_layout_less
;
385 case ir_depth_layout_unchanged
:
386 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
389 unreachable("not reached");
392 var
->data
.index
= ir
->data
.index
;
393 var
->data
.binding
= ir
->data
.binding
;
394 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
395 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
396 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
397 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
398 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
399 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
400 var
->data
.image
.format
= ir
->data
.image_format
;
401 var
->data
.max_array_access
= ir
->data
.max_array_access
;
403 var
->num_state_slots
= ir
->get_num_state_slots();
404 if (var
->num_state_slots
> 0) {
405 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
406 var
->num_state_slots
);
408 ir_state_slot
*state_slots
= ir
->get_state_slots();
409 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
410 for (unsigned j
= 0; j
< 5; j
++)
411 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
412 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
415 var
->state_slots
= NULL
;
418 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
420 var
->interface_type
= ir
->get_interface_type();
422 if (var
->data
.mode
== nir_var_local
)
423 nir_function_impl_add_variable(impl
, var
);
425 nir_shader_add_variable(shader
, var
);
427 _mesa_hash_table_insert(var_table
, ir
, var
);
432 nir_function_visitor::visit_enter(ir_function
*ir
)
434 visitor
->create_function(ir
);
435 return visit_continue_with_parent
;
440 nir_visitor::create_function(ir_function
*ir
)
442 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
443 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
444 create_overload(sig
, func
);
451 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
453 if (ir
->is_intrinsic
)
456 nir_function_overload
*overload
= nir_function_overload_create(function
);
458 unsigned num_params
= ir
->parameters
.length();
459 overload
->num_params
= num_params
;
460 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
463 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
464 switch (param
->data
.mode
) {
465 case ir_var_function_in
:
466 overload
->params
[i
].param_type
= nir_parameter_in
;
469 case ir_var_function_out
:
470 overload
->params
[i
].param_type
= nir_parameter_out
;
473 case ir_var_function_inout
:
474 overload
->params
[i
].param_type
= nir_parameter_inout
;
478 unreachable("not reached");
481 overload
->params
[i
].type
= param
->type
;
485 overload
->return_type
= ir
->return_type
;
487 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
491 nir_visitor::visit(ir_function
*ir
)
493 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
498 nir_visitor::visit(ir_function_signature
*ir
)
500 if (ir
->is_intrinsic
)
503 struct hash_entry
*entry
=
504 _mesa_hash_table_search(this->overload_table
, ir
);
507 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
509 if (ir
->is_defined
) {
510 nir_function_impl
*impl
= nir_function_impl_create(overload
);
513 unsigned num_params
= overload
->num_params
;
514 impl
->num_params
= num_params
;
515 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
517 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
519 impl
->params
[i
] = this->var
;
523 if (overload
->return_type
== glsl_type::void_type
) {
524 impl
->return_var
= NULL
;
526 impl
->return_var
= ralloc(this->shader
, nir_variable
);
527 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
529 impl
->return_var
->type
= overload
->return_type
;
532 this->is_global
= false;
534 nir_builder_init(&b
, impl
);
535 b
.cursor
= nir_after_cf_list(&impl
->body
);
536 visit_exec_list(&ir
->body
, this);
538 this->is_global
= true;
540 overload
->impl
= NULL
;
545 nir_visitor::visit(ir_loop
*ir
)
547 nir_loop
*loop
= nir_loop_create(this->shader
);
548 nir_builder_cf_insert(&b
, &loop
->cf_node
);
550 b
.cursor
= nir_after_cf_list(&loop
->body
);
551 visit_exec_list(&ir
->body_instructions
, this);
552 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
556 nir_visitor::visit(ir_if
*ir
)
559 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
561 nir_if
*if_stmt
= nir_if_create(this->shader
);
562 if_stmt
->condition
= condition
;
563 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
565 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
566 visit_exec_list(&ir
->then_instructions
, this);
568 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
569 visit_exec_list(&ir
->else_instructions
, this);
571 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
575 nir_visitor::visit(ir_discard
*ir
)
578 * discards aren't treated as control flow, because before we lower them
579 * they can appear anywhere in the shader and the stuff after them may still
580 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
581 * discards will be immediately followed by a return.
584 nir_intrinsic_instr
*discard
;
586 discard
= nir_intrinsic_instr_create(this->shader
,
587 nir_intrinsic_discard_if
);
589 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
591 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
594 nir_builder_instr_insert(&b
, &discard
->instr
);
598 nir_visitor::visit(ir_emit_vertex
*ir
)
600 nir_intrinsic_instr
*instr
=
601 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
602 instr
->const_index
[0] = ir
->stream_id();
603 nir_builder_instr_insert(&b
, &instr
->instr
);
607 nir_visitor::visit(ir_end_primitive
*ir
)
609 nir_intrinsic_instr
*instr
=
610 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
611 instr
->const_index
[0] = ir
->stream_id();
612 nir_builder_instr_insert(&b
, &instr
->instr
);
616 nir_visitor::visit(ir_loop_jump
*ir
)
620 case ir_loop_jump::jump_break
:
621 type
= nir_jump_break
;
623 case ir_loop_jump::jump_continue
:
624 type
= nir_jump_continue
;
627 unreachable("not reached");
630 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
631 nir_builder_instr_insert(&b
, &instr
->instr
);
635 nir_visitor::visit(ir_return
*ir
)
637 if (ir
->value
!= NULL
) {
638 nir_intrinsic_instr
*copy
=
639 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
641 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
642 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
645 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
646 nir_builder_instr_insert(&b
, &instr
->instr
);
650 nir_visitor::visit(ir_call
*ir
)
652 if (ir
->callee
->is_intrinsic
) {
654 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
655 op
= nir_intrinsic_atomic_counter_read_var
;
656 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
657 op
= nir_intrinsic_atomic_counter_inc_var
;
658 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
659 op
= nir_intrinsic_atomic_counter_dec_var
;
660 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
661 op
= nir_intrinsic_image_load
;
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
663 op
= nir_intrinsic_image_store
;
664 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
665 op
= nir_intrinsic_image_atomic_add
;
666 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
667 op
= nir_intrinsic_image_atomic_min
;
668 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
669 op
= nir_intrinsic_image_atomic_max
;
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
671 op
= nir_intrinsic_image_atomic_and
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
673 op
= nir_intrinsic_image_atomic_or
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
675 op
= nir_intrinsic_image_atomic_xor
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
677 op
= nir_intrinsic_image_atomic_exchange
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
679 op
= nir_intrinsic_image_atomic_comp_swap
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
681 op
= nir_intrinsic_memory_barrier
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
683 op
= nir_intrinsic_image_size
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
685 op
= nir_intrinsic_image_samples
;
686 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
687 op
= nir_intrinsic_store_ssbo
;
688 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
689 op
= nir_intrinsic_load_ssbo
;
690 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
691 op
= nir_intrinsic_ssbo_atomic_add
;
692 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
693 op
= nir_intrinsic_ssbo_atomic_and
;
694 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
695 op
= nir_intrinsic_ssbo_atomic_or
;
696 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
697 op
= nir_intrinsic_ssbo_atomic_xor
;
698 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
699 assert(ir
->return_deref
);
700 if (ir
->return_deref
->type
== glsl_type::int_type
)
701 op
= nir_intrinsic_ssbo_atomic_imin
;
702 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
703 op
= nir_intrinsic_ssbo_atomic_umin
;
705 unreachable("Invalid type");
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
707 assert(ir
->return_deref
);
708 if (ir
->return_deref
->type
== glsl_type::int_type
)
709 op
= nir_intrinsic_ssbo_atomic_imax
;
710 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
711 op
= nir_intrinsic_ssbo_atomic_umax
;
713 unreachable("Invalid type");
714 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
715 op
= nir_intrinsic_ssbo_atomic_exchange
;
716 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
717 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
718 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
719 op
= nir_intrinsic_shader_clock
;
720 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
721 op
= nir_intrinsic_group_memory_barrier
;
722 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
723 op
= nir_intrinsic_memory_barrier_atomic_counter
;
724 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
725 op
= nir_intrinsic_memory_barrier_buffer
;
726 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
727 op
= nir_intrinsic_memory_barrier_image
;
728 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
729 op
= nir_intrinsic_memory_barrier_shared
;
730 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
731 op
= nir_intrinsic_load_shared
;
732 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
733 op
= nir_intrinsic_store_shared
;
734 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
735 op
= nir_intrinsic_shared_atomic_add
;
736 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
737 op
= nir_intrinsic_shared_atomic_and
;
738 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
739 op
= nir_intrinsic_shared_atomic_or
;
740 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
741 op
= nir_intrinsic_shared_atomic_xor
;
742 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
743 assert(ir
->return_deref
);
744 if (ir
->return_deref
->type
== glsl_type::int_type
)
745 op
= nir_intrinsic_shared_atomic_imin
;
746 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
747 op
= nir_intrinsic_shared_atomic_umin
;
749 unreachable("Invalid type");
750 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
751 assert(ir
->return_deref
);
752 if (ir
->return_deref
->type
== glsl_type::int_type
)
753 op
= nir_intrinsic_shared_atomic_imax
;
754 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
755 op
= nir_intrinsic_shared_atomic_umax
;
757 unreachable("Invalid type");
758 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
759 op
= nir_intrinsic_shared_atomic_exchange
;
760 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
761 op
= nir_intrinsic_shared_atomic_comp_swap
;
763 unreachable("not reached");
766 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
767 nir_dest
*dest
= &instr
->dest
;
770 case nir_intrinsic_atomic_counter_read_var
:
771 case nir_intrinsic_atomic_counter_inc_var
:
772 case nir_intrinsic_atomic_counter_dec_var
: {
773 ir_dereference
*param
=
774 (ir_dereference
*) ir
->actual_parameters
.get_head();
775 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
776 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
777 nir_builder_instr_insert(&b
, &instr
->instr
);
780 case nir_intrinsic_image_load
:
781 case nir_intrinsic_image_store
:
782 case nir_intrinsic_image_atomic_add
:
783 case nir_intrinsic_image_atomic_min
:
784 case nir_intrinsic_image_atomic_max
:
785 case nir_intrinsic_image_atomic_and
:
786 case nir_intrinsic_image_atomic_or
:
787 case nir_intrinsic_image_atomic_xor
:
788 case nir_intrinsic_image_atomic_exchange
:
789 case nir_intrinsic_image_atomic_comp_swap
:
790 case nir_intrinsic_image_samples
:
791 case nir_intrinsic_image_size
: {
792 nir_ssa_undef_instr
*instr_undef
=
793 nir_ssa_undef_instr_create(shader
, 1);
794 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
796 /* Set the image variable dereference. */
797 exec_node
*param
= ir
->actual_parameters
.get_head();
798 ir_dereference
*image
= (ir_dereference
*)param
;
799 const glsl_type
*type
=
800 image
->variable_referenced()->type
->without_array();
802 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
803 param
= param
->get_next();
805 /* Set the intrinsic destination. */
806 if (ir
->return_deref
) {
807 const nir_intrinsic_info
*info
=
808 &nir_intrinsic_infos
[instr
->intrinsic
];
809 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
810 info
->dest_components
, NULL
);
813 if (op
== nir_intrinsic_image_size
||
814 op
== nir_intrinsic_image_samples
) {
815 nir_builder_instr_insert(&b
, &instr
->instr
);
819 /* Set the address argument, extending the coordinate vector to four
822 nir_ssa_def
*src_addr
=
823 evaluate_rvalue((ir_dereference
*)param
);
824 nir_ssa_def
*srcs
[4];
826 for (int i
= 0; i
< 4; i
++) {
827 if (i
< type
->coordinate_components())
828 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
830 srcs
[i
] = &instr_undef
->def
;
833 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
834 param
= param
->get_next();
836 /* Set the sample argument, which is undefined for single-sample
839 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
841 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
842 param
= param
->get_next();
844 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
847 /* Set the intrinsic parameters. */
848 if (!param
->is_tail_sentinel()) {
850 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
851 param
= param
->get_next();
854 if (!param
->is_tail_sentinel()) {
856 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
857 param
= param
->get_next();
859 nir_builder_instr_insert(&b
, &instr
->instr
);
862 case nir_intrinsic_memory_barrier
:
863 case nir_intrinsic_group_memory_barrier
:
864 case nir_intrinsic_memory_barrier_atomic_counter
:
865 case nir_intrinsic_memory_barrier_buffer
:
866 case nir_intrinsic_memory_barrier_image
:
867 case nir_intrinsic_memory_barrier_shared
:
868 nir_builder_instr_insert(&b
, &instr
->instr
);
870 case nir_intrinsic_shader_clock
:
871 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
872 nir_builder_instr_insert(&b
, &instr
->instr
);
874 case nir_intrinsic_store_ssbo
: {
875 exec_node
*param
= ir
->actual_parameters
.get_head();
876 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
878 param
= param
->get_next();
879 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
881 param
= param
->get_next();
882 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
884 param
= param
->get_next();
885 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
888 /* Check if we need the indirect version */
889 ir_constant
*const_offset
= offset
->as_constant();
891 op
= nir_intrinsic_store_ssbo_indirect
;
893 instr
= nir_intrinsic_instr_create(shader
, op
);
894 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
895 instr
->const_index
[0] = 0;
897 instr
->const_index
[0] = const_offset
->value
.u
[0];
900 instr
->const_index
[1] = write_mask
->value
.u
[0];
902 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
903 instr
->num_components
= val
->type
->vector_elements
;
905 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
906 nir_builder_instr_insert(&b
, &instr
->instr
);
909 case nir_intrinsic_load_ssbo
: {
910 exec_node
*param
= ir
->actual_parameters
.get_head();
911 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
913 param
= param
->get_next();
914 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
916 /* Check if we need the indirect version */
917 ir_constant
*const_offset
= offset
->as_constant();
919 op
= nir_intrinsic_load_ssbo_indirect
;
921 instr
= nir_intrinsic_instr_create(shader
, op
);
922 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
923 instr
->const_index
[0] = 0;
926 instr
->const_index
[0] = const_offset
->value
.u
[0];
929 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
931 const glsl_type
*type
= ir
->return_deref
->var
->type
;
932 instr
->num_components
= type
->vector_elements
;
934 /* Setup destination register */
935 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
936 type
->vector_elements
, NULL
);
938 /* Insert the created nir instruction now since in the case of boolean
939 * result we will need to emit another instruction after it
941 nir_builder_instr_insert(&b
, &instr
->instr
);
944 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
945 * consider a true boolean to be ~0. Fix this up with a != 0
948 if (type
->base_type
== GLSL_TYPE_BOOL
) {
949 nir_alu_instr
*load_ssbo_compare
=
950 nir_alu_instr_create(shader
, nir_op_ine
);
951 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
952 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
953 load_ssbo_compare
->src
[1].src
=
954 nir_src_for_ssa(nir_imm_int(&b
, 0));
955 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
956 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
957 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
958 &load_ssbo_compare
->dest
.dest
,
959 type
->vector_elements
, NULL
);
960 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
961 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
962 dest
= &load_ssbo_compare
->dest
.dest
;
966 case nir_intrinsic_ssbo_atomic_add
:
967 case nir_intrinsic_ssbo_atomic_imin
:
968 case nir_intrinsic_ssbo_atomic_umin
:
969 case nir_intrinsic_ssbo_atomic_imax
:
970 case nir_intrinsic_ssbo_atomic_umax
:
971 case nir_intrinsic_ssbo_atomic_and
:
972 case nir_intrinsic_ssbo_atomic_or
:
973 case nir_intrinsic_ssbo_atomic_xor
:
974 case nir_intrinsic_ssbo_atomic_exchange
:
975 case nir_intrinsic_ssbo_atomic_comp_swap
: {
976 int param_count
= ir
->actual_parameters
.length();
977 assert(param_count
== 3 || param_count
== 4);
980 exec_node
*param
= ir
->actual_parameters
.get_head();
981 ir_instruction
*inst
= (ir_instruction
*) param
;
982 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
985 param
= param
->get_next();
986 inst
= (ir_instruction
*) param
;
987 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
989 /* data1 parameter (this is always present) */
990 param
= param
->get_next();
991 inst
= (ir_instruction
*) param
;
992 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
994 /* data2 parameter (only with atomic_comp_swap) */
995 if (param_count
== 4) {
996 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
997 param
= param
->get_next();
998 inst
= (ir_instruction
*) param
;
999 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1003 assert(ir
->return_deref
);
1004 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1005 ir
->return_deref
->type
->vector_elements
, NULL
);
1006 nir_builder_instr_insert(&b
, &instr
->instr
);
1009 case nir_intrinsic_load_shared
: {
1010 exec_node
*param
= ir
->actual_parameters
.get_head();
1011 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1013 /* Check if we need the indirect version */
1014 ir_constant
*const_offset
= offset
->as_constant();
1015 if (!const_offset
) {
1016 op
= nir_intrinsic_load_shared_indirect
;
1018 instr
= nir_intrinsic_instr_create(shader
, op
);
1019 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
1020 instr
->const_index
[0] = 0;
1021 dest
= &instr
->dest
;
1023 instr
->const_index
[0] = const_offset
->value
.u
[0];
1026 const glsl_type
*type
= ir
->return_deref
->var
->type
;
1027 instr
->num_components
= type
->vector_elements
;
1029 /* Setup destination register */
1030 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1031 type
->vector_elements
, NULL
);
1033 nir_builder_instr_insert(&b
, &instr
->instr
);
1036 case nir_intrinsic_store_shared
: {
1037 exec_node
*param
= ir
->actual_parameters
.get_head();
1038 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1040 param
= param
->get_next();
1041 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1043 param
= param
->get_next();
1044 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1047 /* Check if we need the indirect version */
1048 ir_constant
*const_offset
= offset
->as_constant();
1049 if (!const_offset
) {
1050 op
= nir_intrinsic_store_shared_indirect
;
1052 instr
= nir_intrinsic_instr_create(shader
, op
);
1053 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1054 instr
->const_index
[0] = 0;
1056 instr
->const_index
[0] = const_offset
->value
.u
[0];
1059 instr
->const_index
[1] = write_mask
->value
.u
[0];
1061 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
1062 instr
->num_components
= val
->type
->vector_elements
;
1064 nir_builder_instr_insert(&b
, &instr
->instr
);
1067 case nir_intrinsic_shared_atomic_add
:
1068 case nir_intrinsic_shared_atomic_imin
:
1069 case nir_intrinsic_shared_atomic_umin
:
1070 case nir_intrinsic_shared_atomic_imax
:
1071 case nir_intrinsic_shared_atomic_umax
:
1072 case nir_intrinsic_shared_atomic_and
:
1073 case nir_intrinsic_shared_atomic_or
:
1074 case nir_intrinsic_shared_atomic_xor
:
1075 case nir_intrinsic_shared_atomic_exchange
:
1076 case nir_intrinsic_shared_atomic_comp_swap
: {
1077 int param_count
= ir
->actual_parameters
.length();
1078 assert(param_count
== 2 || param_count
== 3);
1081 exec_node
*param
= ir
->actual_parameters
.get_head();
1082 ir_instruction
*inst
= (ir_instruction
*) param
;
1083 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1085 /* data1 parameter (this is always present) */
1086 param
= param
->get_next();
1087 inst
= (ir_instruction
*) param
;
1088 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1090 /* data2 parameter (only with atomic_comp_swap) */
1091 if (param_count
== 3) {
1092 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1093 param
= param
->get_next();
1094 inst
= (ir_instruction
*) param
;
1096 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1100 assert(ir
->return_deref
);
1101 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1102 ir
->return_deref
->type
->vector_elements
, NULL
);
1103 nir_builder_instr_insert(&b
, &instr
->instr
);
1107 unreachable("not reached");
1110 if (ir
->return_deref
) {
1111 nir_intrinsic_instr
*store_instr
=
1112 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1113 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1115 store_instr
->variables
[0] =
1116 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1117 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1119 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1125 struct hash_entry
*entry
=
1126 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1128 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
1130 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1133 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1134 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1138 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1139 nir_builder_instr_insert(&b
, &instr
->instr
);
1143 nir_visitor::visit(ir_assignment
*ir
)
1145 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1147 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1148 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1149 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1150 nir_intrinsic_instr
*copy
=
1151 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1153 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1154 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1156 if (ir
->condition
) {
1157 nir_if
*if_stmt
= nir_if_create(this->shader
);
1158 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1159 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1160 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1161 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1163 nir_builder_instr_insert(&b
, ©
->instr
);
1168 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1170 ir
->lhs
->accept(this);
1171 nir_deref_var
*lhs_deref
= this->deref_head
;
1172 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1174 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1176 * We have no good way to update only part of a variable, so just load
1177 * the LHS and do a vec operation to combine the old with the new, and
1179 * back into the LHS. Copy propagation should get rid of the mess.
1182 nir_intrinsic_instr
*load
=
1183 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1184 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1185 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1186 load
->variables
[0] = lhs_deref
;
1187 ralloc_steal(load
, load
->variables
[0]);
1188 nir_builder_instr_insert(&b
, &load
->instr
);
1190 nir_ssa_def
*srcs
[4];
1192 unsigned component
= 0;
1193 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1194 if (ir
->write_mask
& (1 << i
)) {
1195 /* GLSL IR will give us the input to the write-masked assignment
1196 * in a single packed vector. So, for example, if the
1197 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1198 * and z -> w and get the y component from the load.
1200 srcs
[i
] = nir_channel(&b
, src
, component
++);
1202 srcs
[i
] = nir_channel(&b
, &load
->dest
.ssa
, i
);
1206 src
= nir_vec(&b
, srcs
, ir
->lhs
->type
->vector_elements
);
1209 nir_intrinsic_instr
*store
=
1210 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1211 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1212 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1213 store
->variables
[0] = nir_deref_as_var(store_deref
);
1214 store
->src
[0] = nir_src_for_ssa(src
);
1216 if (ir
->condition
) {
1217 nir_if
*if_stmt
= nir_if_create(this->shader
);
1218 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1219 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1220 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1221 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1223 nir_builder_instr_insert(&b
, &store
->instr
);
1228 * Given an instruction, returns a pointer to its destination or NULL if there
1229 * is no destination.
1231 * Note that this only handles instructions we generate at this level.
1234 get_instr_dest(nir_instr
*instr
)
1236 nir_alu_instr
*alu_instr
;
1237 nir_intrinsic_instr
*intrinsic_instr
;
1238 nir_tex_instr
*tex_instr
;
1240 switch (instr
->type
) {
1241 case nir_instr_type_alu
:
1242 alu_instr
= nir_instr_as_alu(instr
);
1243 return &alu_instr
->dest
.dest
;
1245 case nir_instr_type_intrinsic
:
1246 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1247 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1248 return &intrinsic_instr
->dest
;
1252 case nir_instr_type_tex
:
1253 tex_instr
= nir_instr_as_tex(instr
);
1254 return &tex_instr
->dest
;
1257 unreachable("not reached");
1264 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1266 nir_dest
*dest
= get_instr_dest(instr
);
1269 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1271 nir_builder_instr_insert(&b
, instr
);
1274 assert(dest
->is_ssa
);
1275 this->result
= &dest
->ssa
;
1280 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1283 if (ir
->as_dereference() || ir
->as_constant()) {
1285 * A dereference is being used on the right hand side, which means we
1286 * must emit a variable load.
1289 nir_intrinsic_instr
*load_instr
=
1290 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1291 load_instr
->num_components
= ir
->type
->vector_elements
;
1292 load_instr
->variables
[0] = this->deref_head
;
1293 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1294 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1297 return this->result
;
1301 nir_visitor::visit(ir_expression
*ir
)
1303 /* Some special cases */
1304 switch (ir
->operation
) {
1305 case ir_binop_ubo_load
: {
1306 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1308 nir_intrinsic_op op
;
1310 op
= nir_intrinsic_load_ubo
;
1312 op
= nir_intrinsic_load_ubo_indirect
;
1314 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1315 load
->num_components
= ir
->type
->vector_elements
;
1316 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1317 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1319 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1320 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1323 * In UBO's, a true boolean value is any non-zero value, but we consider
1324 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1327 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1328 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1333 case ir_unop_interpolate_at_centroid
:
1334 case ir_binop_interpolate_at_offset
:
1335 case ir_binop_interpolate_at_sample
: {
1336 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1337 ir_swizzle
*swizzle
= NULL
;
1339 /* the api does not allow a swizzle here, but the varying packing code
1340 * may have pushed one into here.
1342 swizzle
= ir
->operands
[0]->as_swizzle();
1344 deref
= swizzle
->val
->as_dereference();
1348 deref
->accept(this);
1350 nir_intrinsic_op op
;
1351 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1352 switch (ir
->operation
) {
1353 case ir_unop_interpolate_at_centroid
:
1354 op
= nir_intrinsic_interp_var_at_centroid
;
1356 case ir_binop_interpolate_at_offset
:
1357 op
= nir_intrinsic_interp_var_at_offset
;
1359 case ir_binop_interpolate_at_sample
:
1360 op
= nir_intrinsic_interp_var_at_sample
;
1363 unreachable("Invalid interpolation intrinsic");
1366 /* This case can happen if the vertex shader does not write the
1367 * given varying. In this case, the linker will lower it to a
1368 * global variable. Since interpolating a variable makes no
1369 * sense, we'll just turn it into a load which will probably
1370 * eventually end up as an SSA definition.
1372 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1373 op
= nir_intrinsic_load_var
;
1376 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1377 intrin
->num_components
= deref
->type
->vector_elements
;
1378 intrin
->variables
[0] = this->deref_head
;
1379 ralloc_steal(intrin
, intrin
->variables
[0]);
1381 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1382 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1383 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1385 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1388 unsigned swiz
[4] = {
1389 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1392 result
= nir_swizzle(&b
, result
, swiz
,
1393 swizzle
->type
->vector_elements
, false);
1403 nir_ssa_def
*srcs
[4];
1404 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1405 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1407 glsl_base_type types
[4];
1408 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1410 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1412 types
[i
] = GLSL_TYPE_FLOAT
;
1414 glsl_base_type out_type
;
1416 out_type
= ir
->type
->base_type
;
1418 out_type
= GLSL_TYPE_FLOAT
;
1420 switch (ir
->operation
) {
1421 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1422 case ir_unop_logic_not
:
1423 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1426 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1427 : nir_ineg(&b
, srcs
[0]);
1430 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1431 : nir_iabs(&b
, srcs
[0]);
1433 case ir_unop_saturate
:
1434 assert(types
[0] == GLSL_TYPE_FLOAT
);
1435 result
= nir_fsat(&b
, srcs
[0]);
1438 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1439 : nir_isign(&b
, srcs
[0]);
1441 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1442 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1443 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1444 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1445 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1446 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1447 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1449 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1452 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1455 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1457 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1458 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1459 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1460 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1461 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1464 case ir_unop_bitcast_i2f
:
1465 case ir_unop_bitcast_f2i
:
1466 case ir_unop_bitcast_u2f
:
1467 case ir_unop_bitcast_f2u
:
1468 case ir_unop_subroutine_to_int
:
1470 result
= nir_imov(&b
, srcs
[0]);
1473 switch (ir
->operands
[0]->type
->vector_elements
) {
1475 result
= supports_ints
? nir_bany2(&b
, srcs
[0])
1476 : nir_fany2(&b
, srcs
[0]);
1479 result
= supports_ints
? nir_bany3(&b
, srcs
[0])
1480 : nir_fany3(&b
, srcs
[0]);
1483 result
= supports_ints
? nir_bany4(&b
, srcs
[0])
1484 : nir_fany4(&b
, srcs
[0]);
1487 unreachable("not reached");
1490 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1491 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1492 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1493 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1494 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1495 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1496 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1497 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1498 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1499 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1500 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1501 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1502 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1503 case ir_unop_pack_snorm_2x16
:
1504 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1506 case ir_unop_pack_snorm_4x8
:
1507 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1509 case ir_unop_pack_unorm_2x16
:
1510 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1512 case ir_unop_pack_unorm_4x8
:
1513 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1515 case ir_unop_pack_half_2x16
:
1516 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1518 case ir_unop_unpack_snorm_2x16
:
1519 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1521 case ir_unop_unpack_snorm_4x8
:
1522 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1524 case ir_unop_unpack_unorm_2x16
:
1525 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1527 case ir_unop_unpack_unorm_4x8
:
1528 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1530 case ir_unop_unpack_half_2x16
:
1531 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1533 case ir_unop_unpack_half_2x16_split_x
:
1534 result
= nir_unpack_half_2x16_split_x(&b
, srcs
[0]);
1536 case ir_unop_unpack_half_2x16_split_y
:
1537 result
= nir_unpack_half_2x16_split_y(&b
, srcs
[0]);
1539 case ir_unop_bitfield_reverse
:
1540 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1542 case ir_unop_bit_count
:
1543 result
= nir_bit_count(&b
, srcs
[0]);
1545 case ir_unop_find_msb
:
1547 case GLSL_TYPE_UINT
:
1548 result
= nir_ufind_msb(&b
, srcs
[0]);
1551 result
= nir_ifind_msb(&b
, srcs
[0]);
1554 unreachable("Invalid type for findMSB()");
1557 case ir_unop_find_lsb
:
1558 result
= nir_find_lsb(&b
, srcs
[0]);
1562 switch (ir
->type
->vector_elements
) {
1564 switch (ir
->operands
[0]->type
->vector_elements
) {
1565 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1566 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1567 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1568 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1569 default: unreachable("not reached");
1573 switch (ir
->operands
[0]->type
->vector_elements
) {
1574 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1575 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1576 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1577 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1578 default: unreachable("not reached");
1582 switch (ir
->operands
[0]->type
->vector_elements
) {
1583 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1584 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1585 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1586 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1587 default: unreachable("not reached");
1591 switch (ir
->operands
[0]->type
->vector_elements
) {
1592 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1593 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1594 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1595 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1596 default: unreachable("not reached");
1600 unreachable("not reached");
1603 case ir_unop_get_buffer_size
: {
1604 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1606 nir_intrinsic_get_buffer_size
);
1607 load
->num_components
= ir
->type
->vector_elements
;
1608 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1609 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1614 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1615 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1618 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1619 : nir_isub(&b
, srcs
[0], srcs
[1]);
1622 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1623 : nir_imul(&b
, srcs
[0], srcs
[1]);
1626 if (out_type
== GLSL_TYPE_FLOAT
)
1627 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1628 else if (out_type
== GLSL_TYPE_INT
)
1629 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1631 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1634 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1635 : nir_umod(&b
, srcs
[0], srcs
[1]);
1638 if (out_type
== GLSL_TYPE_FLOAT
)
1639 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1640 else if (out_type
== GLSL_TYPE_INT
)
1641 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1643 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1646 if (out_type
== GLSL_TYPE_FLOAT
)
1647 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1648 else if (out_type
== GLSL_TYPE_INT
)
1649 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1651 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1653 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1654 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1655 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1656 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1657 case ir_binop_logic_and
:
1658 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1659 : nir_fand(&b
, srcs
[0], srcs
[1]);
1661 case ir_binop_logic_or
:
1662 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1663 : nir_for(&b
, srcs
[0], srcs
[1]);
1665 case ir_binop_logic_xor
:
1666 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1667 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1669 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1670 case ir_binop_rshift
:
1671 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1672 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1674 case ir_binop_imul_high
:
1675 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1676 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1678 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1679 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1681 if (supports_ints
) {
1682 if (types
[0] == GLSL_TYPE_FLOAT
)
1683 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1684 else if (types
[0] == GLSL_TYPE_INT
)
1685 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1687 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1689 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1692 case ir_binop_greater
:
1693 if (supports_ints
) {
1694 if (types
[0] == GLSL_TYPE_FLOAT
)
1695 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1696 else if (types
[0] == GLSL_TYPE_INT
)
1697 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1699 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1701 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1704 case ir_binop_lequal
:
1705 if (supports_ints
) {
1706 if (types
[0] == GLSL_TYPE_FLOAT
)
1707 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1708 else if (types
[0] == GLSL_TYPE_INT
)
1709 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1711 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1713 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1716 case ir_binop_gequal
:
1717 if (supports_ints
) {
1718 if (types
[0] == GLSL_TYPE_FLOAT
)
1719 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1720 else if (types
[0] == GLSL_TYPE_INT
)
1721 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1723 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1725 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1728 case ir_binop_equal
:
1729 if (supports_ints
) {
1730 if (types
[0] == GLSL_TYPE_FLOAT
)
1731 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1733 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1735 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1738 case ir_binop_nequal
:
1739 if (supports_ints
) {
1740 if (types
[0] == GLSL_TYPE_FLOAT
)
1741 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1743 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1745 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1748 case ir_binop_all_equal
:
1749 if (supports_ints
) {
1750 if (types
[0] == GLSL_TYPE_FLOAT
) {
1751 switch (ir
->operands
[0]->type
->vector_elements
) {
1752 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1753 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1754 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1755 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1757 unreachable("not reached");
1760 switch (ir
->operands
[0]->type
->vector_elements
) {
1761 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1762 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1763 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1764 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1766 unreachable("not reached");
1770 switch (ir
->operands
[0]->type
->vector_elements
) {
1771 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1772 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1773 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1774 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1776 unreachable("not reached");
1780 case ir_binop_any_nequal
:
1781 if (supports_ints
) {
1782 if (types
[0] == GLSL_TYPE_FLOAT
) {
1783 switch (ir
->operands
[0]->type
->vector_elements
) {
1784 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1785 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1786 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1787 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1789 unreachable("not reached");
1792 switch (ir
->operands
[0]->type
->vector_elements
) {
1793 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1794 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1795 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1796 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1798 unreachable("not reached");
1802 switch (ir
->operands
[0]->type
->vector_elements
) {
1803 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1804 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1805 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1806 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1808 unreachable("not reached");
1813 switch (ir
->operands
[0]->type
->vector_elements
) {
1814 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1815 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1816 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1818 unreachable("not reached");
1822 case ir_binop_pack_half_2x16_split
:
1823 result
= nir_pack_half_2x16_split(&b
, srcs
[0], srcs
[1]);
1825 case ir_binop_bfm
: result
= nir_bfm(&b
, srcs
[0], srcs
[1]); break;
1826 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1828 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1831 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1835 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1837 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1840 result
= nir_bfi(&b
, srcs
[0], srcs
[1], srcs
[2]);
1842 case ir_triop_bitfield_extract
:
1843 result
= (out_type
== GLSL_TYPE_INT
) ?
1844 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1845 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1847 case ir_quadop_bitfield_insert
:
1848 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1850 case ir_quadop_vector
:
1851 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1855 unreachable("not reached");
1860 nir_visitor::visit(ir_swizzle
*ir
)
1862 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1863 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1864 ir
->type
->vector_elements
, !supports_ints
);
1868 nir_visitor::visit(ir_texture
*ir
)
1875 num_srcs
= 1; /* coordinate */
1880 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1881 num_srcs
= 2; /* coordinate, bias/lod */
1885 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1891 if (ir
->lod_info
.lod
!= NULL
)
1892 num_srcs
= 2; /* coordinate, lod */
1894 num_srcs
= 1; /* coordinate */
1898 op
= nir_texop_txf_ms
;
1899 num_srcs
= 2; /* coordinate, sample_index */
1904 if (ir
->lod_info
.lod
!= NULL
)
1905 num_srcs
= 1; /* lod */
1912 num_srcs
= 1; /* coordinate */
1917 num_srcs
= 1; /* coordinate */
1920 case ir_query_levels
:
1921 op
= nir_texop_query_levels
;
1925 case ir_texture_samples
:
1926 op
= nir_texop_texture_samples
;
1930 case ir_samples_identical
:
1931 op
= nir_texop_samples_identical
;
1932 num_srcs
= 1; /* coordinate */
1936 unreachable("not reached");
1939 if (ir
->projector
!= NULL
)
1941 if (ir
->shadow_comparitor
!= NULL
)
1943 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1946 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1949 instr
->sampler_dim
=
1950 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1951 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1952 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1953 if (instr
->is_shadow
)
1954 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1955 switch (ir
->type
->base_type
) {
1956 case GLSL_TYPE_FLOAT
:
1957 instr
->dest_type
= nir_type_float
;
1960 instr
->dest_type
= nir_type_int
;
1962 case GLSL_TYPE_BOOL
:
1963 case GLSL_TYPE_UINT
:
1964 instr
->dest_type
= nir_type_uint
;
1967 unreachable("not reached");
1970 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1972 unsigned src_number
= 0;
1974 if (ir
->coordinate
!= NULL
) {
1975 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1976 instr
->src
[src_number
].src
=
1977 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1978 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1982 if (ir
->projector
!= NULL
) {
1983 instr
->src
[src_number
].src
=
1984 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1985 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1989 if (ir
->shadow_comparitor
!= NULL
) {
1990 instr
->src
[src_number
].src
=
1991 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1992 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1996 if (ir
->offset
!= NULL
) {
1997 /* we don't support multiple offsets yet */
1998 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
2000 ir_constant
*const_offset
= ir
->offset
->as_constant();
2001 if (const_offset
!= NULL
) {
2002 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
2003 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
2005 instr
->src
[src_number
].src
=
2006 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
2007 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
2014 instr
->src
[src_number
].src
=
2015 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
2016 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
2023 if (ir
->lod_info
.lod
!= NULL
) {
2024 instr
->src
[src_number
].src
=
2025 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
2026 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
2032 instr
->src
[src_number
].src
=
2033 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
2034 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
2036 instr
->src
[src_number
].src
=
2037 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
2038 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
2043 instr
->src
[src_number
].src
=
2044 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
2045 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
2050 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
2057 assert(src_number
== num_srcs
);
2059 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
2063 nir_visitor::visit(ir_constant
*ir
)
2066 * We don't know if this variable is an an array or struct that gets
2067 * dereferenced, so do the safe thing an make it a variable with a
2068 * constant initializer and return a dereference.
2072 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
2073 var
->data
.read_only
= true;
2074 var
->constant_initializer
= constant_copy(ir
, var
);
2076 this->deref_head
= nir_deref_var_create(this->shader
, var
);
2077 this->deref_tail
= &this->deref_head
->deref
;
2081 nir_visitor::visit(ir_dereference_variable
*ir
)
2083 struct hash_entry
*entry
=
2084 _mesa_hash_table_search(this->var_table
, ir
->var
);
2086 nir_variable
*var
= (nir_variable
*) entry
->data
;
2088 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
2089 this->deref_head
= deref
;
2090 this->deref_tail
= &deref
->deref
;
2094 nir_visitor::visit(ir_dereference_record
*ir
)
2096 ir
->record
->accept(this);
2098 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
2099 assert(field_index
>= 0);
2101 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
2102 deref
->deref
.type
= ir
->type
;
2103 this->deref_tail
->child
= &deref
->deref
;
2104 this->deref_tail
= &deref
->deref
;
2108 nir_visitor::visit(ir_dereference_array
*ir
)
2110 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
2111 deref
->deref
.type
= ir
->type
;
2113 ir_constant
*const_index
= ir
->array_index
->as_constant();
2114 if (const_index
!= NULL
) {
2115 deref
->deref_array_type
= nir_deref_array_type_direct
;
2116 deref
->base_offset
= const_index
->value
.u
[0];
2118 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2120 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2123 ir
->array
->accept(this);
2125 this->deref_tail
->child
= &deref
->deref
;
2126 ralloc_steal(this->deref_tail
, deref
);
2127 this->deref_tail
= &deref
->deref
;
2131 nir_visitor::visit(ir_barrier
*ir
)
2133 nir_intrinsic_instr
*instr
=
2134 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2135 nir_builder_instr_insert(&b
, &instr
->instr
);