2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "glsl_to_nir.h"
29 #include "ir_visitor.h"
30 #include "ir_hierarchical_visitor.h"
32 #include "compiler/nir/nir_control_flow.h"
33 #include "compiler/nir/nir_builder.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_signature
*ir
);
76 void add_instr(nir_instr
*instr
, unsigned num_components
, unsigned bit_size
);
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
);
89 nir_function_impl
*impl
;
91 nir_ssa_def
*result
; /* result of the expression tree last visited */
93 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
95 /* the head of the dereference chain we're creating */
96 nir_deref_var
*deref_head
;
97 /* the tail of the dereference chain we're creating */
98 nir_deref
*deref_tail
;
100 nir_variable
*var
; /* variable created by ir_variable visitor */
102 /* whether the IR we're operating on is per-function or global */
105 /* map of ir_variable -> nir_variable */
106 struct hash_table
*var_table
;
108 /* map of ir_function_signature -> nir_function_overload */
109 struct hash_table
*overload_table
;
113 * This visitor runs before the main visitor, calling create_function() for
114 * each function so that the main visitor can resolve forward references in
118 class nir_function_visitor
: public ir_hierarchical_visitor
121 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
124 virtual ir_visitor_status
visit_enter(ir_function
*);
127 nir_visitor
*visitor
;
130 } /* end of anonymous namespace */
133 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
134 gl_shader_stage stage
,
135 const nir_shader_compiler_options
*options
)
137 struct gl_linked_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
139 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
141 nir_visitor
v1(shader
);
142 nir_function_visitor
v2(&v1
);
144 visit_exec_list(sh
->ir
, &v1
);
146 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
147 if (shader_prog
->Label
)
148 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
149 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
150 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
151 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
152 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
153 shader
->info
.num_images
= sh
->NumImages
;
154 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
155 shader
->info
.double_inputs_read
= sh
->Program
->DoubleInputsRead
;
156 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
157 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
158 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
159 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
160 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
161 shader
->info
.uses_clip_distance_out
=
162 sh
->Program
->ClipDistanceArraySize
!= 0;
163 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
164 shader
->info
.has_transform_feedback_varyings
=
165 shader_prog
->TransformFeedback
.NumVarying
> 0;
168 case MESA_SHADER_TESS_CTRL
:
169 shader
->info
.tcs
.vertices_out
= sh
->TessCtrl
.VerticesOut
;
172 case MESA_SHADER_GEOMETRY
:
173 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
174 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
175 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
176 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
177 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
178 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
181 case MESA_SHADER_FRAGMENT
: {
182 struct gl_fragment_program
*fp
=
183 (struct gl_fragment_program
*)sh
->Program
;
185 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
186 shader
->info
.fs
.uses_sample_qualifier
= fp
->IsSample
!= 0;
187 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
188 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
192 case MESA_SHADER_COMPUTE
: {
193 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
194 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
195 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
196 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
201 break; /* No stage-specific info */
207 nir_visitor::nir_visitor(nir_shader
*shader
)
209 this->supports_ints
= shader
->options
->native_integers
;
210 this->shader
= shader
;
211 this->is_global
= true;
212 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
213 _mesa_key_pointer_equal
);
214 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
215 _mesa_key_pointer_equal
);
219 this->deref_head
= NULL
;
220 this->deref_tail
= NULL
;
221 memset(&this->b
, 0, sizeof(this->b
));
224 nir_visitor::~nir_visitor()
226 _mesa_hash_table_destroy(this->var_table
, NULL
);
227 _mesa_hash_table_destroy(this->overload_table
, NULL
);
231 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
234 ralloc_steal(mem_ctx
, this->deref_head
);
235 return this->deref_head
;
238 static nir_constant
*
239 constant_copy(ir_constant
*ir
, void *mem_ctx
)
244 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
246 unsigned total_elems
= ir
->type
->components();
249 ret
->num_elements
= 0;
250 switch (ir
->type
->base_type
) {
252 for (i
= 0; i
< total_elems
; i
++)
253 ret
->value
.u
[i
] = ir
->value
.u
[i
];
257 for (i
= 0; i
< total_elems
; i
++)
258 ret
->value
.i
[i
] = ir
->value
.i
[i
];
261 case GLSL_TYPE_FLOAT
:
262 for (i
= 0; i
< total_elems
; i
++)
263 ret
->value
.f
[i
] = ir
->value
.f
[i
];
266 case GLSL_TYPE_DOUBLE
:
267 for (i
= 0; i
< total_elems
; i
++)
268 ret
->value
.d
[i
] = ir
->value
.d
[i
];
272 for (i
= 0; i
< total_elems
; i
++)
273 ret
->value
.b
[i
] = ir
->value
.b
[i
];
276 case GLSL_TYPE_STRUCT
:
277 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
279 ret
->num_elements
= ir
->type
->length
;
282 foreach_in_list(ir_constant
, field
, &ir
->components
) {
283 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
288 case GLSL_TYPE_ARRAY
:
289 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
291 ret
->num_elements
= ir
->type
->length
;
293 for (i
= 0; i
< ir
->type
->length
; i
++)
294 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
298 unreachable("not reached");
305 nir_visitor::visit(ir_variable
*ir
)
307 nir_variable
*var
= ralloc(shader
, nir_variable
);
308 var
->type
= ir
->type
;
309 var
->name
= ralloc_strdup(var
, ir
->name
);
311 var
->data
.read_only
= ir
->data
.read_only
;
312 var
->data
.centroid
= ir
->data
.centroid
;
313 var
->data
.sample
= ir
->data
.sample
;
314 var
->data
.patch
= ir
->data
.patch
;
315 var
->data
.invariant
= ir
->data
.invariant
;
316 var
->data
.location
= ir
->data
.location
;
318 switch(ir
->data
.mode
) {
320 case ir_var_temporary
:
322 var
->data
.mode
= nir_var_global
;
324 var
->data
.mode
= nir_var_local
;
327 case ir_var_function_in
:
328 case ir_var_function_out
:
329 case ir_var_function_inout
:
330 case ir_var_const_in
:
331 var
->data
.mode
= nir_var_local
;
334 case ir_var_shader_in
:
335 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
336 ir
->data
.location
== VARYING_SLOT_FACE
) {
337 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
338 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
339 var
->data
.mode
= nir_var_system_value
;
340 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
341 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
342 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
343 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
344 var
->data
.mode
= nir_var_system_value
;
346 var
->data
.mode
= nir_var_shader_in
;
350 case ir_var_shader_out
:
351 var
->data
.mode
= nir_var_shader_out
;
355 var
->data
.mode
= nir_var_uniform
;
358 case ir_var_shader_storage
:
359 var
->data
.mode
= nir_var_shader_storage
;
362 case ir_var_system_value
:
363 var
->data
.mode
= nir_var_system_value
;
367 unreachable("not reached");
370 var
->data
.interpolation
= ir
->data
.interpolation
;
371 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
372 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
373 var
->data
.explicit_location
= ir
->data
.explicit_location
;
374 var
->data
.explicit_index
= ir
->data
.explicit_index
;
375 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
376 var
->data
.has_initializer
= ir
->data
.has_initializer
;
377 var
->data
.location_frac
= ir
->data
.location_frac
;
379 switch (ir
->data
.depth_layout
) {
380 case ir_depth_layout_none
:
381 var
->data
.depth_layout
= nir_depth_layout_none
;
383 case ir_depth_layout_any
:
384 var
->data
.depth_layout
= nir_depth_layout_any
;
386 case ir_depth_layout_greater
:
387 var
->data
.depth_layout
= nir_depth_layout_greater
;
389 case ir_depth_layout_less
:
390 var
->data
.depth_layout
= nir_depth_layout_less
;
392 case ir_depth_layout_unchanged
:
393 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
396 unreachable("not reached");
399 var
->data
.index
= ir
->data
.index
;
400 var
->data
.binding
= ir
->data
.binding
;
401 var
->data
.offset
= ir
->data
.offset
;
402 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
403 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
404 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
405 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
406 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
407 var
->data
.image
.format
= ir
->data
.image_format
;
408 var
->data
.max_array_access
= ir
->data
.max_array_access
;
410 var
->num_state_slots
= ir
->get_num_state_slots();
411 if (var
->num_state_slots
> 0) {
412 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
413 var
->num_state_slots
);
415 ir_state_slot
*state_slots
= ir
->get_state_slots();
416 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
417 for (unsigned j
= 0; j
< 5; j
++)
418 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
419 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
422 var
->state_slots
= NULL
;
425 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
427 var
->interface_type
= ir
->get_interface_type();
429 if (var
->data
.mode
== nir_var_local
)
430 nir_function_impl_add_variable(impl
, var
);
432 nir_shader_add_variable(shader
, var
);
434 _mesa_hash_table_insert(var_table
, ir
, var
);
439 nir_function_visitor::visit_enter(ir_function
*ir
)
441 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
442 visitor
->create_function(sig
);
444 return visit_continue_with_parent
;
448 nir_visitor::create_function(ir_function_signature
*ir
)
450 if (ir
->is_intrinsic
)
453 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
455 assert(ir
->parameters
.is_empty());
456 assert(ir
->return_type
== glsl_type::void_type
);
458 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
462 nir_visitor::visit(ir_function
*ir
)
464 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
469 nir_visitor::visit(ir_function_signature
*ir
)
471 if (ir
->is_intrinsic
)
474 struct hash_entry
*entry
=
475 _mesa_hash_table_search(this->overload_table
, ir
);
478 nir_function
*func
= (nir_function
*) entry
->data
;
480 if (ir
->is_defined
) {
481 nir_function_impl
*impl
= nir_function_impl_create(func
);
484 assert(strcmp(func
->name
, "main") == 0);
485 assert(ir
->parameters
.is_empty());
486 assert(func
->return_type
== glsl_type::void_type
);
488 this->is_global
= false;
490 nir_builder_init(&b
, impl
);
491 b
.cursor
= nir_after_cf_list(&impl
->body
);
492 visit_exec_list(&ir
->body
, this);
494 this->is_global
= true;
501 nir_visitor::visit(ir_loop
*ir
)
503 nir_loop
*loop
= nir_loop_create(this->shader
);
504 nir_builder_cf_insert(&b
, &loop
->cf_node
);
506 b
.cursor
= nir_after_cf_list(&loop
->body
);
507 visit_exec_list(&ir
->body_instructions
, this);
508 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
512 nir_visitor::visit(ir_if
*ir
)
515 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
517 nir_if
*if_stmt
= nir_if_create(this->shader
);
518 if_stmt
->condition
= condition
;
519 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
521 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
522 visit_exec_list(&ir
->then_instructions
, this);
524 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
525 visit_exec_list(&ir
->else_instructions
, this);
527 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
531 nir_visitor::visit(ir_discard
*ir
)
534 * discards aren't treated as control flow, because before we lower them
535 * they can appear anywhere in the shader and the stuff after them may still
536 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
537 * discards will be immediately followed by a return.
540 nir_intrinsic_instr
*discard
;
542 discard
= nir_intrinsic_instr_create(this->shader
,
543 nir_intrinsic_discard_if
);
545 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
547 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
550 nir_builder_instr_insert(&b
, &discard
->instr
);
554 nir_visitor::visit(ir_emit_vertex
*ir
)
556 nir_intrinsic_instr
*instr
=
557 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
558 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
559 nir_builder_instr_insert(&b
, &instr
->instr
);
563 nir_visitor::visit(ir_end_primitive
*ir
)
565 nir_intrinsic_instr
*instr
=
566 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
567 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
568 nir_builder_instr_insert(&b
, &instr
->instr
);
572 nir_visitor::visit(ir_loop_jump
*ir
)
576 case ir_loop_jump::jump_break
:
577 type
= nir_jump_break
;
579 case ir_loop_jump::jump_continue
:
580 type
= nir_jump_continue
;
583 unreachable("not reached");
586 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
587 nir_builder_instr_insert(&b
, &instr
->instr
);
591 nir_visitor::visit(ir_return
*ir
)
593 if (ir
->value
!= NULL
) {
594 nir_intrinsic_instr
*copy
=
595 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
597 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
598 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
601 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
602 nir_builder_instr_insert(&b
, &instr
->instr
);
606 nir_visitor::visit(ir_call
*ir
)
608 if (ir
->callee
->is_intrinsic
) {
610 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
611 op
= nir_intrinsic_atomic_counter_read_var
;
612 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
613 op
= nir_intrinsic_atomic_counter_inc_var
;
614 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
615 op
= nir_intrinsic_atomic_counter_dec_var
;
616 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
617 op
= nir_intrinsic_image_load
;
618 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
619 op
= nir_intrinsic_image_store
;
620 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
621 op
= nir_intrinsic_image_atomic_add
;
622 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
623 op
= nir_intrinsic_image_atomic_min
;
624 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
625 op
= nir_intrinsic_image_atomic_max
;
626 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
627 op
= nir_intrinsic_image_atomic_and
;
628 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
629 op
= nir_intrinsic_image_atomic_or
;
630 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
631 op
= nir_intrinsic_image_atomic_xor
;
632 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
633 op
= nir_intrinsic_image_atomic_exchange
;
634 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
635 op
= nir_intrinsic_image_atomic_comp_swap
;
636 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
637 op
= nir_intrinsic_memory_barrier
;
638 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
639 op
= nir_intrinsic_image_size
;
640 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
641 op
= nir_intrinsic_image_samples
;
642 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
643 op
= nir_intrinsic_store_ssbo
;
644 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
645 op
= nir_intrinsic_load_ssbo
;
646 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
647 op
= nir_intrinsic_ssbo_atomic_add
;
648 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
649 op
= nir_intrinsic_ssbo_atomic_and
;
650 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
651 op
= nir_intrinsic_ssbo_atomic_or
;
652 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
653 op
= nir_intrinsic_ssbo_atomic_xor
;
654 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
655 assert(ir
->return_deref
);
656 if (ir
->return_deref
->type
== glsl_type::int_type
)
657 op
= nir_intrinsic_ssbo_atomic_imin
;
658 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
659 op
= nir_intrinsic_ssbo_atomic_umin
;
661 unreachable("Invalid type");
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
663 assert(ir
->return_deref
);
664 if (ir
->return_deref
->type
== glsl_type::int_type
)
665 op
= nir_intrinsic_ssbo_atomic_imax
;
666 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
667 op
= nir_intrinsic_ssbo_atomic_umax
;
669 unreachable("Invalid type");
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
671 op
= nir_intrinsic_ssbo_atomic_exchange
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
673 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
675 op
= nir_intrinsic_shader_clock
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
677 op
= nir_intrinsic_group_memory_barrier
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
679 op
= nir_intrinsic_memory_barrier_atomic_counter
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
681 op
= nir_intrinsic_memory_barrier_buffer
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
683 op
= nir_intrinsic_memory_barrier_image
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
685 op
= nir_intrinsic_memory_barrier_shared
;
686 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
687 op
= nir_intrinsic_load_shared
;
688 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
689 op
= nir_intrinsic_store_shared
;
690 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
691 op
= nir_intrinsic_shared_atomic_add
;
692 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
693 op
= nir_intrinsic_shared_atomic_and
;
694 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
695 op
= nir_intrinsic_shared_atomic_or
;
696 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
697 op
= nir_intrinsic_shared_atomic_xor
;
698 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
699 assert(ir
->return_deref
);
700 if (ir
->return_deref
->type
== glsl_type::int_type
)
701 op
= nir_intrinsic_shared_atomic_imin
;
702 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
703 op
= nir_intrinsic_shared_atomic_umin
;
705 unreachable("Invalid type");
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
707 assert(ir
->return_deref
);
708 if (ir
->return_deref
->type
== glsl_type::int_type
)
709 op
= nir_intrinsic_shared_atomic_imax
;
710 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
711 op
= nir_intrinsic_shared_atomic_umax
;
713 unreachable("Invalid type");
714 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
715 op
= nir_intrinsic_shared_atomic_exchange
;
716 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
717 op
= nir_intrinsic_shared_atomic_comp_swap
;
719 unreachable("not reached");
722 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
723 nir_dest
*dest
= &instr
->dest
;
726 case nir_intrinsic_atomic_counter_read_var
:
727 case nir_intrinsic_atomic_counter_inc_var
:
728 case nir_intrinsic_atomic_counter_dec_var
: {
729 ir_dereference
*param
=
730 (ir_dereference
*) ir
->actual_parameters
.get_head();
731 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
732 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
733 nir_builder_instr_insert(&b
, &instr
->instr
);
736 case nir_intrinsic_image_load
:
737 case nir_intrinsic_image_store
:
738 case nir_intrinsic_image_atomic_add
:
739 case nir_intrinsic_image_atomic_min
:
740 case nir_intrinsic_image_atomic_max
:
741 case nir_intrinsic_image_atomic_and
:
742 case nir_intrinsic_image_atomic_or
:
743 case nir_intrinsic_image_atomic_xor
:
744 case nir_intrinsic_image_atomic_exchange
:
745 case nir_intrinsic_image_atomic_comp_swap
:
746 case nir_intrinsic_image_samples
:
747 case nir_intrinsic_image_size
: {
748 nir_ssa_undef_instr
*instr_undef
=
749 nir_ssa_undef_instr_create(shader
, 1, 32);
750 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
752 /* Set the image variable dereference. */
753 exec_node
*param
= ir
->actual_parameters
.get_head();
754 ir_dereference
*image
= (ir_dereference
*)param
;
755 const glsl_type
*type
=
756 image
->variable_referenced()->type
->without_array();
758 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
759 param
= param
->get_next();
761 /* Set the intrinsic destination. */
762 if (ir
->return_deref
) {
763 const nir_intrinsic_info
*info
=
764 &nir_intrinsic_infos
[instr
->intrinsic
];
765 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
766 info
->dest_components
, 32, NULL
);
769 if (op
== nir_intrinsic_image_size
||
770 op
== nir_intrinsic_image_samples
) {
771 nir_builder_instr_insert(&b
, &instr
->instr
);
775 /* Set the address argument, extending the coordinate vector to four
778 nir_ssa_def
*src_addr
=
779 evaluate_rvalue((ir_dereference
*)param
);
780 nir_ssa_def
*srcs
[4];
782 for (int i
= 0; i
< 4; i
++) {
783 if (i
< type
->coordinate_components())
784 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
786 srcs
[i
] = &instr_undef
->def
;
789 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
790 param
= param
->get_next();
792 /* Set the sample argument, which is undefined for single-sample
795 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
797 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
798 param
= param
->get_next();
800 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
803 /* Set the intrinsic parameters. */
804 if (!param
->is_tail_sentinel()) {
806 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
807 param
= param
->get_next();
810 if (!param
->is_tail_sentinel()) {
812 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
813 param
= param
->get_next();
815 nir_builder_instr_insert(&b
, &instr
->instr
);
818 case nir_intrinsic_memory_barrier
:
819 case nir_intrinsic_group_memory_barrier
:
820 case nir_intrinsic_memory_barrier_atomic_counter
:
821 case nir_intrinsic_memory_barrier_buffer
:
822 case nir_intrinsic_memory_barrier_image
:
823 case nir_intrinsic_memory_barrier_shared
:
824 nir_builder_instr_insert(&b
, &instr
->instr
);
826 case nir_intrinsic_shader_clock
:
827 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
828 nir_builder_instr_insert(&b
, &instr
->instr
);
830 case nir_intrinsic_store_ssbo
: {
831 exec_node
*param
= ir
->actual_parameters
.get_head();
832 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
834 param
= param
->get_next();
835 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
837 param
= param
->get_next();
838 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
840 param
= param
->get_next();
841 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
844 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
845 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
846 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
847 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
848 instr
->num_components
= val
->type
->vector_elements
;
850 nir_builder_instr_insert(&b
, &instr
->instr
);
853 case nir_intrinsic_load_ssbo
: {
854 exec_node
*param
= ir
->actual_parameters
.get_head();
855 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
857 param
= param
->get_next();
858 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
860 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
861 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
863 const glsl_type
*type
= ir
->return_deref
->var
->type
;
864 instr
->num_components
= type
->vector_elements
;
866 /* Setup destination register */
867 unsigned bit_size
= glsl_get_bit_size(type
);
868 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
869 type
->vector_elements
, bit_size
, NULL
);
871 /* Insert the created nir instruction now since in the case of boolean
872 * result we will need to emit another instruction after it
874 nir_builder_instr_insert(&b
, &instr
->instr
);
877 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
878 * consider a true boolean to be ~0. Fix this up with a != 0
881 if (type
->base_type
== GLSL_TYPE_BOOL
) {
882 nir_alu_instr
*load_ssbo_compare
=
883 nir_alu_instr_create(shader
, nir_op_ine
);
884 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
885 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
886 load_ssbo_compare
->src
[1].src
=
887 nir_src_for_ssa(nir_imm_int(&b
, 0));
888 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
889 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
890 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
891 &load_ssbo_compare
->dest
.dest
,
892 type
->vector_elements
, bit_size
, NULL
);
893 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
894 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
895 dest
= &load_ssbo_compare
->dest
.dest
;
899 case nir_intrinsic_ssbo_atomic_add
:
900 case nir_intrinsic_ssbo_atomic_imin
:
901 case nir_intrinsic_ssbo_atomic_umin
:
902 case nir_intrinsic_ssbo_atomic_imax
:
903 case nir_intrinsic_ssbo_atomic_umax
:
904 case nir_intrinsic_ssbo_atomic_and
:
905 case nir_intrinsic_ssbo_atomic_or
:
906 case nir_intrinsic_ssbo_atomic_xor
:
907 case nir_intrinsic_ssbo_atomic_exchange
:
908 case nir_intrinsic_ssbo_atomic_comp_swap
: {
909 int param_count
= ir
->actual_parameters
.length();
910 assert(param_count
== 3 || param_count
== 4);
913 exec_node
*param
= ir
->actual_parameters
.get_head();
914 ir_instruction
*inst
= (ir_instruction
*) param
;
915 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
918 param
= param
->get_next();
919 inst
= (ir_instruction
*) param
;
920 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
922 /* data1 parameter (this is always present) */
923 param
= param
->get_next();
924 inst
= (ir_instruction
*) param
;
925 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
927 /* data2 parameter (only with atomic_comp_swap) */
928 if (param_count
== 4) {
929 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
930 param
= param
->get_next();
931 inst
= (ir_instruction
*) param
;
932 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
936 assert(ir
->return_deref
);
937 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
938 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
939 nir_builder_instr_insert(&b
, &instr
->instr
);
942 case nir_intrinsic_load_shared
: {
943 exec_node
*param
= ir
->actual_parameters
.get_head();
944 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
946 nir_intrinsic_set_base(instr
, 0);
947 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
949 const glsl_type
*type
= ir
->return_deref
->var
->type
;
950 instr
->num_components
= type
->vector_elements
;
952 /* Setup destination register */
953 unsigned bit_size
= glsl_get_bit_size(type
);
954 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
955 type
->vector_elements
, bit_size
, NULL
);
957 nir_builder_instr_insert(&b
, &instr
->instr
);
960 case nir_intrinsic_store_shared
: {
961 exec_node
*param
= ir
->actual_parameters
.get_head();
962 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
964 param
= param
->get_next();
965 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
967 param
= param
->get_next();
968 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
971 nir_intrinsic_set_base(instr
, 0);
972 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
974 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
976 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
977 instr
->num_components
= val
->type
->vector_elements
;
979 nir_builder_instr_insert(&b
, &instr
->instr
);
982 case nir_intrinsic_shared_atomic_add
:
983 case nir_intrinsic_shared_atomic_imin
:
984 case nir_intrinsic_shared_atomic_umin
:
985 case nir_intrinsic_shared_atomic_imax
:
986 case nir_intrinsic_shared_atomic_umax
:
987 case nir_intrinsic_shared_atomic_and
:
988 case nir_intrinsic_shared_atomic_or
:
989 case nir_intrinsic_shared_atomic_xor
:
990 case nir_intrinsic_shared_atomic_exchange
:
991 case nir_intrinsic_shared_atomic_comp_swap
: {
992 int param_count
= ir
->actual_parameters
.length();
993 assert(param_count
== 2 || param_count
== 3);
996 exec_node
*param
= ir
->actual_parameters
.get_head();
997 ir_instruction
*inst
= (ir_instruction
*) param
;
998 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1000 /* data1 parameter (this is always present) */
1001 param
= param
->get_next();
1002 inst
= (ir_instruction
*) param
;
1003 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1005 /* data2 parameter (only with atomic_comp_swap) */
1006 if (param_count
== 3) {
1007 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1008 param
= param
->get_next();
1009 inst
= (ir_instruction
*) param
;
1011 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1015 assert(ir
->return_deref
);
1016 unsigned bit_size
= glsl_get_bit_size(ir
->return_deref
->type
);
1017 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1018 ir
->return_deref
->type
->vector_elements
,
1020 nir_builder_instr_insert(&b
, &instr
->instr
);
1024 unreachable("not reached");
1027 if (ir
->return_deref
) {
1028 nir_intrinsic_instr
*store_instr
=
1029 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1030 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1031 nir_intrinsic_set_write_mask(store_instr
,
1032 (1 << store_instr
->num_components
) - 1);
1034 store_instr
->variables
[0] =
1035 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1036 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1038 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1044 struct hash_entry
*entry
=
1045 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1047 nir_function
*callee
= (nir_function
*) entry
->data
;
1049 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1052 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1053 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1057 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1058 nir_builder_instr_insert(&b
, &instr
->instr
);
1062 nir_visitor::visit(ir_assignment
*ir
)
1064 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1066 b
.exact
= ir
->lhs
->variable_referenced()->data
.invariant
||
1067 ir
->lhs
->variable_referenced()->data
.precise
;
1069 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1070 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1071 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1072 nir_intrinsic_instr
*copy
=
1073 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1075 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1076 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1078 if (ir
->condition
) {
1079 nir_if
*if_stmt
= nir_if_create(this->shader
);
1080 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1081 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1082 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1083 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1085 nir_builder_instr_insert(&b
, ©
->instr
);
1090 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1092 ir
->lhs
->accept(this);
1093 nir_deref_var
*lhs_deref
= this->deref_head
;
1094 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1096 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1097 /* GLSL IR will give us the input to the write-masked assignment in a
1098 * single packed vector. So, for example, if the writemask is xzw, then
1099 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1103 unsigned component
= 0;
1104 for (unsigned i
= 0; i
< 4; i
++) {
1105 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1107 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1110 nir_intrinsic_instr
*store
=
1111 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1112 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1113 nir_intrinsic_set_write_mask(store
, ir
->write_mask
);
1114 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1115 store
->variables
[0] = nir_deref_as_var(store_deref
);
1116 store
->src
[0] = nir_src_for_ssa(src
);
1118 if (ir
->condition
) {
1119 nir_if
*if_stmt
= nir_if_create(this->shader
);
1120 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1121 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1122 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1123 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1125 nir_builder_instr_insert(&b
, &store
->instr
);
1130 * Given an instruction, returns a pointer to its destination or NULL if there
1131 * is no destination.
1133 * Note that this only handles instructions we generate at this level.
1136 get_instr_dest(nir_instr
*instr
)
1138 nir_alu_instr
*alu_instr
;
1139 nir_intrinsic_instr
*intrinsic_instr
;
1140 nir_tex_instr
*tex_instr
;
1142 switch (instr
->type
) {
1143 case nir_instr_type_alu
:
1144 alu_instr
= nir_instr_as_alu(instr
);
1145 return &alu_instr
->dest
.dest
;
1147 case nir_instr_type_intrinsic
:
1148 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1149 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1150 return &intrinsic_instr
->dest
;
1154 case nir_instr_type_tex
:
1155 tex_instr
= nir_instr_as_tex(instr
);
1156 return &tex_instr
->dest
;
1159 unreachable("not reached");
1166 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
,
1169 nir_dest
*dest
= get_instr_dest(instr
);
1172 nir_ssa_dest_init(instr
, dest
, num_components
, bit_size
, NULL
);
1174 nir_builder_instr_insert(&b
, instr
);
1177 assert(dest
->is_ssa
);
1178 this->result
= &dest
->ssa
;
1183 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1186 if (ir
->as_dereference() || ir
->as_constant()) {
1188 * A dereference is being used on the right hand side, which means we
1189 * must emit a variable load.
1192 nir_intrinsic_instr
*load_instr
=
1193 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1194 load_instr
->num_components
= ir
->type
->vector_elements
;
1195 load_instr
->variables
[0] = this->deref_head
;
1196 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1197 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1198 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
, bit_size
);
1201 return this->result
;
1205 type_is_float(glsl_base_type type
)
1207 return type
== GLSL_TYPE_FLOAT
|| type
== GLSL_TYPE_DOUBLE
;
1211 nir_visitor::visit(ir_expression
*ir
)
1213 /* Some special cases */
1214 switch (ir
->operation
) {
1215 case ir_binop_ubo_load
: {
1216 nir_intrinsic_instr
*load
=
1217 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1218 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1219 load
->num_components
= ir
->type
->vector_elements
;
1220 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1221 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1222 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1225 * In UBO's, a true boolean value is any non-zero value, but we consider
1226 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1229 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1230 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1235 case ir_unop_interpolate_at_centroid
:
1236 case ir_binop_interpolate_at_offset
:
1237 case ir_binop_interpolate_at_sample
: {
1238 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1239 ir_swizzle
*swizzle
= NULL
;
1241 /* the api does not allow a swizzle here, but the varying packing code
1242 * may have pushed one into here.
1244 swizzle
= ir
->operands
[0]->as_swizzle();
1246 deref
= swizzle
->val
->as_dereference();
1250 deref
->accept(this);
1252 nir_intrinsic_op op
;
1253 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1254 switch (ir
->operation
) {
1255 case ir_unop_interpolate_at_centroid
:
1256 op
= nir_intrinsic_interp_var_at_centroid
;
1258 case ir_binop_interpolate_at_offset
:
1259 op
= nir_intrinsic_interp_var_at_offset
;
1261 case ir_binop_interpolate_at_sample
:
1262 op
= nir_intrinsic_interp_var_at_sample
;
1265 unreachable("Invalid interpolation intrinsic");
1268 /* This case can happen if the vertex shader does not write the
1269 * given varying. In this case, the linker will lower it to a
1270 * global variable. Since interpolating a variable makes no
1271 * sense, we'll just turn it into a load which will probably
1272 * eventually end up as an SSA definition.
1274 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1275 op
= nir_intrinsic_load_var
;
1278 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1279 intrin
->num_components
= deref
->type
->vector_elements
;
1280 intrin
->variables
[0] = this->deref_head
;
1281 ralloc_steal(intrin
, intrin
->variables
[0]);
1283 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1284 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1285 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1287 unsigned bit_size
= glsl_get_bit_size(deref
->type
);
1288 add_instr(&intrin
->instr
, deref
->type
->vector_elements
, bit_size
);
1291 unsigned swiz
[4] = {
1292 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1295 result
= nir_swizzle(&b
, result
, swiz
,
1296 swizzle
->type
->vector_elements
, false);
1306 nir_ssa_def
*srcs
[4];
1307 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1308 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1310 glsl_base_type types
[4];
1311 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1313 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1315 types
[i
] = GLSL_TYPE_FLOAT
;
1317 glsl_base_type out_type
;
1319 out_type
= ir
->type
->base_type
;
1321 out_type
= GLSL_TYPE_FLOAT
;
1323 switch (ir
->operation
) {
1324 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1325 case ir_unop_logic_not
:
1326 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1329 result
= type_is_float(types
[0]) ? nir_fneg(&b
, srcs
[0])
1330 : nir_ineg(&b
, srcs
[0]);
1333 result
= type_is_float(types
[0]) ? nir_fabs(&b
, srcs
[0])
1334 : nir_iabs(&b
, srcs
[0]);
1336 case ir_unop_saturate
:
1337 assert(type_is_float(types
[0]));
1338 result
= nir_fsat(&b
, srcs
[0]);
1341 result
= type_is_float(types
[0]) ? nir_fsign(&b
, srcs
[0])
1342 : nir_isign(&b
, srcs
[0]);
1344 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1345 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1346 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1347 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1348 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1349 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1350 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1352 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1355 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1358 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1360 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1361 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1362 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1363 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1364 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1365 case ir_unop_d2f
: result
= nir_d2f(&b
, srcs
[0]); break;
1366 case ir_unop_f2d
: result
= nir_f2d(&b
, srcs
[0]); break;
1367 case ir_unop_d2i
: result
= nir_d2i(&b
, srcs
[0]); break;
1368 case ir_unop_d2u
: result
= nir_d2u(&b
, srcs
[0]); break;
1369 case ir_unop_d2b
: result
= nir_d2b(&b
, srcs
[0]); break;
1371 assert(supports_ints
);
1372 result
= nir_i2d(&b
, srcs
[0]);
1375 assert(supports_ints
);
1376 result
= nir_u2d(&b
, srcs
[0]);
1380 case ir_unop_bitcast_i2f
:
1381 case ir_unop_bitcast_f2i
:
1382 case ir_unop_bitcast_u2f
:
1383 case ir_unop_bitcast_f2u
:
1384 case ir_unop_subroutine_to_int
:
1386 result
= nir_imov(&b
, srcs
[0]);
1388 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1389 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1390 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1391 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1392 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1393 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1394 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1395 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1396 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1397 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1398 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1399 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1400 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1401 case ir_unop_pack_snorm_2x16
:
1402 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1404 case ir_unop_pack_snorm_4x8
:
1405 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1407 case ir_unop_pack_unorm_2x16
:
1408 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1410 case ir_unop_pack_unorm_4x8
:
1411 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1413 case ir_unop_pack_half_2x16
:
1414 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1416 case ir_unop_unpack_snorm_2x16
:
1417 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1419 case ir_unop_unpack_snorm_4x8
:
1420 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1422 case ir_unop_unpack_unorm_2x16
:
1423 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1425 case ir_unop_unpack_unorm_4x8
:
1426 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1428 case ir_unop_unpack_half_2x16
:
1429 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1431 case ir_unop_pack_double_2x32
:
1432 result
= nir_pack_double_2x32(&b
, srcs
[0]);
1434 case ir_unop_unpack_double_2x32
:
1435 result
= nir_unpack_double_2x32(&b
, srcs
[0]);
1437 case ir_unop_bitfield_reverse
:
1438 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1440 case ir_unop_bit_count
:
1441 result
= nir_bit_count(&b
, srcs
[0]);
1443 case ir_unop_find_msb
:
1445 case GLSL_TYPE_UINT
:
1446 result
= nir_ufind_msb(&b
, srcs
[0]);
1449 result
= nir_ifind_msb(&b
, srcs
[0]);
1452 unreachable("Invalid type for findMSB()");
1455 case ir_unop_find_lsb
:
1456 result
= nir_find_lsb(&b
, srcs
[0]);
1460 switch (ir
->type
->vector_elements
) {
1462 switch (ir
->operands
[0]->type
->vector_elements
) {
1463 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1464 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1465 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1466 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1467 default: unreachable("not reached");
1471 switch (ir
->operands
[0]->type
->vector_elements
) {
1472 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1473 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1474 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1475 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1476 default: unreachable("not reached");
1480 switch (ir
->operands
[0]->type
->vector_elements
) {
1481 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1482 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1483 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1484 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1485 default: unreachable("not reached");
1489 switch (ir
->operands
[0]->type
->vector_elements
) {
1490 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1491 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1492 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1493 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1494 default: unreachable("not reached");
1498 unreachable("not reached");
1501 case ir_unop_get_buffer_size
: {
1502 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1504 nir_intrinsic_get_buffer_size
);
1505 load
->num_components
= ir
->type
->vector_elements
;
1506 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1507 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1508 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1513 result
= type_is_float(out_type
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1514 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1517 result
= type_is_float(out_type
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1518 : nir_isub(&b
, srcs
[0], srcs
[1]);
1521 result
= type_is_float(out_type
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1522 : nir_imul(&b
, srcs
[0], srcs
[1]);
1525 if (type_is_float(out_type
))
1526 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1527 else if (out_type
== GLSL_TYPE_INT
)
1528 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1530 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1533 result
= type_is_float(out_type
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1534 : nir_umod(&b
, srcs
[0], srcs
[1]);
1537 if (type_is_float(out_type
))
1538 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1539 else if (out_type
== GLSL_TYPE_INT
)
1540 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1542 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1545 if (type_is_float(out_type
))
1546 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1547 else if (out_type
== GLSL_TYPE_INT
)
1548 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1550 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1552 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1553 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1554 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1555 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1556 case ir_binop_logic_and
:
1557 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1558 : nir_fand(&b
, srcs
[0], srcs
[1]);
1560 case ir_binop_logic_or
:
1561 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1562 : nir_for(&b
, srcs
[0], srcs
[1]);
1564 case ir_binop_logic_xor
:
1565 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1566 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1568 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1569 case ir_binop_rshift
:
1570 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1571 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1573 case ir_binop_imul_high
:
1574 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1575 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1577 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1578 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1580 if (supports_ints
) {
1581 if (type_is_float(types
[0]))
1582 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1583 else if (types
[0] == GLSL_TYPE_INT
)
1584 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1586 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1588 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1591 case ir_binop_greater
:
1592 if (supports_ints
) {
1593 if (type_is_float(types
[0]))
1594 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1595 else if (types
[0] == GLSL_TYPE_INT
)
1596 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1598 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1600 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1603 case ir_binop_lequal
:
1604 if (supports_ints
) {
1605 if (type_is_float(types
[0]))
1606 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1607 else if (types
[0] == GLSL_TYPE_INT
)
1608 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1610 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1612 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1615 case ir_binop_gequal
:
1616 if (supports_ints
) {
1617 if (type_is_float(types
[0]))
1618 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1619 else if (types
[0] == GLSL_TYPE_INT
)
1620 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1622 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1624 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1627 case ir_binop_equal
:
1628 if (supports_ints
) {
1629 if (type_is_float(types
[0]))
1630 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1632 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1634 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1637 case ir_binop_nequal
:
1638 if (supports_ints
) {
1639 if (type_is_float(types
[0]))
1640 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1642 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1644 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1647 case ir_binop_all_equal
:
1648 if (supports_ints
) {
1649 if (type_is_float(types
[0])) {
1650 switch (ir
->operands
[0]->type
->vector_elements
) {
1651 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1652 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1653 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1654 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1656 unreachable("not reached");
1659 switch (ir
->operands
[0]->type
->vector_elements
) {
1660 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1661 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1662 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1663 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1665 unreachable("not reached");
1669 switch (ir
->operands
[0]->type
->vector_elements
) {
1670 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1671 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1672 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1673 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1675 unreachable("not reached");
1679 case ir_binop_any_nequal
:
1680 if (supports_ints
) {
1681 if (type_is_float(types
[0])) {
1682 switch (ir
->operands
[0]->type
->vector_elements
) {
1683 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1684 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1685 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1686 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1688 unreachable("not reached");
1691 switch (ir
->operands
[0]->type
->vector_elements
) {
1692 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1693 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1694 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1695 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1697 unreachable("not reached");
1701 switch (ir
->operands
[0]->type
->vector_elements
) {
1702 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1703 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1704 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1705 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1707 unreachable("not reached");
1712 switch (ir
->operands
[0]->type
->vector_elements
) {
1713 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1714 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1715 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1717 unreachable("not reached");
1721 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1723 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1726 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1730 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1732 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1734 case ir_triop_bitfield_extract
:
1735 result
= (out_type
== GLSL_TYPE_INT
) ?
1736 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1737 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1739 case ir_quadop_bitfield_insert
:
1740 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1742 case ir_quadop_vector
:
1743 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1747 unreachable("not reached");
1752 nir_visitor::visit(ir_swizzle
*ir
)
1754 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1755 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1756 ir
->type
->vector_elements
, !supports_ints
);
1760 nir_visitor::visit(ir_texture
*ir
)
1767 num_srcs
= 1; /* coordinate */
1772 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1773 num_srcs
= 2; /* coordinate, bias/lod */
1777 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1783 if (ir
->lod_info
.lod
!= NULL
)
1784 num_srcs
= 2; /* coordinate, lod */
1786 num_srcs
= 1; /* coordinate */
1790 op
= nir_texop_txf_ms
;
1791 num_srcs
= 2; /* coordinate, sample_index */
1796 if (ir
->lod_info
.lod
!= NULL
)
1797 num_srcs
= 1; /* lod */
1804 num_srcs
= 1; /* coordinate */
1809 num_srcs
= 1; /* coordinate */
1812 case ir_query_levels
:
1813 op
= nir_texop_query_levels
;
1817 case ir_texture_samples
:
1818 op
= nir_texop_texture_samples
;
1822 case ir_samples_identical
:
1823 op
= nir_texop_samples_identical
;
1824 num_srcs
= 1; /* coordinate */
1828 unreachable("not reached");
1831 if (ir
->projector
!= NULL
)
1833 if (ir
->shadow_comparitor
!= NULL
)
1835 if (ir
->offset
!= NULL
)
1838 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1841 instr
->sampler_dim
=
1842 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1843 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1844 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1845 if (instr
->is_shadow
)
1846 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1847 switch (ir
->type
->base_type
) {
1848 case GLSL_TYPE_FLOAT
:
1849 instr
->dest_type
= nir_type_float
;
1852 instr
->dest_type
= nir_type_int
;
1854 case GLSL_TYPE_BOOL
:
1855 case GLSL_TYPE_UINT
:
1856 instr
->dest_type
= nir_type_uint
;
1859 unreachable("not reached");
1862 instr
->texture
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1864 unsigned src_number
= 0;
1866 if (ir
->coordinate
!= NULL
) {
1867 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1868 instr
->src
[src_number
].src
=
1869 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1870 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1874 if (ir
->projector
!= NULL
) {
1875 instr
->src
[src_number
].src
=
1876 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1877 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1881 if (ir
->shadow_comparitor
!= NULL
) {
1882 instr
->src
[src_number
].src
=
1883 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1884 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1888 if (ir
->offset
!= NULL
) {
1889 /* we don't support multiple offsets yet */
1890 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1892 instr
->src
[src_number
].src
=
1893 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1894 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1900 instr
->src
[src_number
].src
=
1901 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1902 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1909 if (ir
->lod_info
.lod
!= NULL
) {
1910 instr
->src
[src_number
].src
=
1911 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1912 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1918 instr
->src
[src_number
].src
=
1919 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1920 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1922 instr
->src
[src_number
].src
=
1923 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1924 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1929 instr
->src
[src_number
].src
=
1930 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1931 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1936 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1943 assert(src_number
== num_srcs
);
1945 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1946 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
), bit_size
);
1950 nir_visitor::visit(ir_constant
*ir
)
1953 * We don't know if this variable is an array or struct that gets
1954 * dereferenced, so do the safe thing an make it a variable with a
1955 * constant initializer and return a dereference.
1959 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1960 var
->data
.read_only
= true;
1961 var
->constant_initializer
= constant_copy(ir
, var
);
1963 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1964 this->deref_tail
= &this->deref_head
->deref
;
1968 nir_visitor::visit(ir_dereference_variable
*ir
)
1970 struct hash_entry
*entry
=
1971 _mesa_hash_table_search(this->var_table
, ir
->var
);
1973 nir_variable
*var
= (nir_variable
*) entry
->data
;
1975 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1976 this->deref_head
= deref
;
1977 this->deref_tail
= &deref
->deref
;
1981 nir_visitor::visit(ir_dereference_record
*ir
)
1983 ir
->record
->accept(this);
1985 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1986 assert(field_index
>= 0);
1988 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1989 deref
->deref
.type
= ir
->type
;
1990 this->deref_tail
->child
= &deref
->deref
;
1991 this->deref_tail
= &deref
->deref
;
1995 nir_visitor::visit(ir_dereference_array
*ir
)
1997 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1998 deref
->deref
.type
= ir
->type
;
2000 ir_constant
*const_index
= ir
->array_index
->as_constant();
2001 if (const_index
!= NULL
) {
2002 deref
->deref_array_type
= nir_deref_array_type_direct
;
2003 deref
->base_offset
= const_index
->value
.u
[0];
2005 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2007 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2010 ir
->array
->accept(this);
2012 this->deref_tail
->child
= &deref
->deref
;
2013 ralloc_steal(this->deref_tail
, deref
);
2014 this->deref_tail
= &deref
->deref
;
2018 nir_visitor::visit(ir_barrier
*)
2020 nir_intrinsic_instr
*instr
=
2021 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2022 nir_builder_instr_insert(&b
, &instr
->instr
);