2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "glsl_to_nir.h"
29 #include "nir_control_flow.h"
30 #include "nir_builder.h"
31 #include "compiler/glsl/ir_visitor.h"
32 #include "compiler/glsl/ir_hierarchical_visitor.h"
33 #include "compiler/glsl/ir.h"
34 #include "main/imports.h"
37 * pass to lower GLSL IR to NIR
39 * This will lower variable dereferences to loads/stores of corresponding
40 * variables in NIR - the variables will be converted to registers in a later
46 class nir_visitor
: public ir_visitor
49 nir_visitor(nir_shader
*shader
);
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_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
.outputs_written
= sh
->Program
->OutputsWritten
;
156 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
157 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
158 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
159 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
160 shader
->info
.uses_clip_distance_out
=
161 sh
->Program
->ClipDistanceArraySize
!= 0;
162 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
163 shader
->info
.has_transform_feedback_varyings
=
164 shader_prog
->TransformFeedback
.NumVarying
> 0;
167 case MESA_SHADER_TESS_CTRL
:
168 shader
->info
.tcs
.vertices_out
= shader_prog
->TessCtrl
.VerticesOut
;
171 case MESA_SHADER_GEOMETRY
:
172 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
173 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
174 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
175 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
176 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
177 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
180 case MESA_SHADER_FRAGMENT
: {
181 struct gl_fragment_program
*fp
=
182 (struct gl_fragment_program
*)sh
->Program
;
184 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
185 shader
->info
.fs
.uses_sample_qualifier
= fp
->IsSample
!= 0;
186 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
187 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
191 case MESA_SHADER_COMPUTE
: {
192 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
193 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
194 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
195 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
200 break; /* No stage-specific info */
206 nir_visitor::nir_visitor(nir_shader
*shader
)
208 this->supports_ints
= shader
->options
->native_integers
;
209 this->shader
= shader
;
210 this->is_global
= true;
211 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
212 _mesa_key_pointer_equal
);
213 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
214 _mesa_key_pointer_equal
);
217 nir_visitor::~nir_visitor()
219 _mesa_hash_table_destroy(this->var_table
, NULL
);
220 _mesa_hash_table_destroy(this->overload_table
, NULL
);
224 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
227 ralloc_steal(mem_ctx
, this->deref_head
);
228 return this->deref_head
;
231 static nir_constant
*
232 constant_copy(ir_constant
*ir
, void *mem_ctx
)
237 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
239 unsigned total_elems
= ir
->type
->components();
242 ret
->num_elements
= 0;
243 switch (ir
->type
->base_type
) {
245 for (i
= 0; i
< total_elems
; i
++)
246 ret
->value
.u
[i
] = ir
->value
.u
[i
];
250 for (i
= 0; i
< total_elems
; i
++)
251 ret
->value
.i
[i
] = ir
->value
.i
[i
];
254 case GLSL_TYPE_FLOAT
:
255 for (i
= 0; i
< total_elems
; i
++)
256 ret
->value
.f
[i
] = ir
->value
.f
[i
];
259 case GLSL_TYPE_DOUBLE
:
260 for (i
= 0; i
< total_elems
; i
++)
261 ret
->value
.d
[i
] = ir
->value
.d
[i
];
265 for (i
= 0; i
< total_elems
; i
++)
266 ret
->value
.b
[i
] = ir
->value
.b
[i
];
269 case GLSL_TYPE_STRUCT
:
270 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
272 ret
->num_elements
= ir
->type
->length
;
275 foreach_in_list(ir_constant
, field
, &ir
->components
) {
276 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
281 case GLSL_TYPE_ARRAY
:
282 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
284 ret
->num_elements
= ir
->type
->length
;
286 for (i
= 0; i
< ir
->type
->length
; i
++)
287 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
291 unreachable("not reached");
298 nir_visitor::visit(ir_variable
*ir
)
300 nir_variable
*var
= ralloc(shader
, nir_variable
);
301 var
->type
= ir
->type
;
302 var
->name
= ralloc_strdup(var
, ir
->name
);
304 var
->data
.read_only
= ir
->data
.read_only
;
305 var
->data
.centroid
= ir
->data
.centroid
;
306 var
->data
.sample
= ir
->data
.sample
;
307 var
->data
.patch
= ir
->data
.patch
;
308 var
->data
.invariant
= ir
->data
.invariant
;
309 var
->data
.location
= ir
->data
.location
;
311 switch(ir
->data
.mode
) {
313 case ir_var_temporary
:
315 var
->data
.mode
= nir_var_global
;
317 var
->data
.mode
= nir_var_local
;
320 case ir_var_function_in
:
321 case ir_var_function_out
:
322 case ir_var_function_inout
:
323 case ir_var_const_in
:
324 var
->data
.mode
= nir_var_local
;
327 case ir_var_shader_in
:
328 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
329 ir
->data
.location
== VARYING_SLOT_FACE
) {
330 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
331 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
332 var
->data
.mode
= nir_var_system_value
;
333 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
334 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
335 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
336 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
337 var
->data
.mode
= nir_var_system_value
;
339 var
->data
.mode
= nir_var_shader_in
;
343 case ir_var_shader_out
:
344 var
->data
.mode
= nir_var_shader_out
;
348 var
->data
.mode
= nir_var_uniform
;
351 case ir_var_shader_storage
:
352 var
->data
.mode
= nir_var_shader_storage
;
355 case ir_var_system_value
:
356 var
->data
.mode
= nir_var_system_value
;
360 unreachable("not reached");
363 var
->data
.interpolation
= ir
->data
.interpolation
;
364 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
365 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
366 var
->data
.explicit_location
= ir
->data
.explicit_location
;
367 var
->data
.explicit_index
= ir
->data
.explicit_index
;
368 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
369 var
->data
.has_initializer
= ir
->data
.has_initializer
;
370 var
->data
.location_frac
= ir
->data
.location_frac
;
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
.offset
= ir
->data
.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 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
435 visitor
->create_function(sig
);
437 return visit_continue_with_parent
;
441 nir_visitor::create_function(ir_function_signature
*ir
)
443 if (ir
->is_intrinsic
)
446 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
448 assert(ir
->parameters
.is_empty());
449 assert(ir
->return_type
== glsl_type::void_type
);
451 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
455 nir_visitor::visit(ir_function
*ir
)
457 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
462 nir_visitor::visit(ir_function_signature
*ir
)
464 if (ir
->is_intrinsic
)
467 struct hash_entry
*entry
=
468 _mesa_hash_table_search(this->overload_table
, ir
);
471 nir_function
*func
= (nir_function
*) entry
->data
;
473 if (ir
->is_defined
) {
474 nir_function_impl
*impl
= nir_function_impl_create(func
);
477 assert(strcmp(func
->name
, "main") == 0);
478 assert(ir
->parameters
.is_empty());
479 assert(func
->return_type
== glsl_type::void_type
);
481 this->is_global
= false;
483 nir_builder_init(&b
, impl
);
484 b
.cursor
= nir_after_cf_list(&impl
->body
);
485 visit_exec_list(&ir
->body
, this);
487 this->is_global
= true;
494 nir_visitor::visit(ir_loop
*ir
)
496 nir_loop
*loop
= nir_loop_create(this->shader
);
497 nir_builder_cf_insert(&b
, &loop
->cf_node
);
499 b
.cursor
= nir_after_cf_list(&loop
->body
);
500 visit_exec_list(&ir
->body_instructions
, this);
501 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
505 nir_visitor::visit(ir_if
*ir
)
508 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
510 nir_if
*if_stmt
= nir_if_create(this->shader
);
511 if_stmt
->condition
= condition
;
512 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
514 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
515 visit_exec_list(&ir
->then_instructions
, this);
517 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
518 visit_exec_list(&ir
->else_instructions
, this);
520 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
524 nir_visitor::visit(ir_discard
*ir
)
527 * discards aren't treated as control flow, because before we lower them
528 * they can appear anywhere in the shader and the stuff after them may still
529 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
530 * discards will be immediately followed by a return.
533 nir_intrinsic_instr
*discard
;
535 discard
= nir_intrinsic_instr_create(this->shader
,
536 nir_intrinsic_discard_if
);
538 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
540 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
543 nir_builder_instr_insert(&b
, &discard
->instr
);
547 nir_visitor::visit(ir_emit_vertex
*ir
)
549 nir_intrinsic_instr
*instr
=
550 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
551 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
552 nir_builder_instr_insert(&b
, &instr
->instr
);
556 nir_visitor::visit(ir_end_primitive
*ir
)
558 nir_intrinsic_instr
*instr
=
559 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
560 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
561 nir_builder_instr_insert(&b
, &instr
->instr
);
565 nir_visitor::visit(ir_loop_jump
*ir
)
569 case ir_loop_jump::jump_break
:
570 type
= nir_jump_break
;
572 case ir_loop_jump::jump_continue
:
573 type
= nir_jump_continue
;
576 unreachable("not reached");
579 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
580 nir_builder_instr_insert(&b
, &instr
->instr
);
584 nir_visitor::visit(ir_return
*ir
)
586 if (ir
->value
!= NULL
) {
587 nir_intrinsic_instr
*copy
=
588 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
590 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
591 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
594 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
595 nir_builder_instr_insert(&b
, &instr
->instr
);
599 nir_visitor::visit(ir_call
*ir
)
601 if (ir
->callee
->is_intrinsic
) {
603 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
604 op
= nir_intrinsic_atomic_counter_read_var
;
605 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
606 op
= nir_intrinsic_atomic_counter_inc_var
;
607 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
608 op
= nir_intrinsic_atomic_counter_dec_var
;
609 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
610 op
= nir_intrinsic_image_load
;
611 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
612 op
= nir_intrinsic_image_store
;
613 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
614 op
= nir_intrinsic_image_atomic_add
;
615 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
616 op
= nir_intrinsic_image_atomic_min
;
617 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
618 op
= nir_intrinsic_image_atomic_max
;
619 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
620 op
= nir_intrinsic_image_atomic_and
;
621 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
622 op
= nir_intrinsic_image_atomic_or
;
623 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
624 op
= nir_intrinsic_image_atomic_xor
;
625 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
626 op
= nir_intrinsic_image_atomic_exchange
;
627 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
628 op
= nir_intrinsic_image_atomic_comp_swap
;
629 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
630 op
= nir_intrinsic_memory_barrier
;
631 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
632 op
= nir_intrinsic_image_size
;
633 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
634 op
= nir_intrinsic_image_samples
;
635 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
636 op
= nir_intrinsic_store_ssbo
;
637 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
638 op
= nir_intrinsic_load_ssbo
;
639 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
640 op
= nir_intrinsic_ssbo_atomic_add
;
641 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
642 op
= nir_intrinsic_ssbo_atomic_and
;
643 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
644 op
= nir_intrinsic_ssbo_atomic_or
;
645 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
646 op
= nir_intrinsic_ssbo_atomic_xor
;
647 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
648 assert(ir
->return_deref
);
649 if (ir
->return_deref
->type
== glsl_type::int_type
)
650 op
= nir_intrinsic_ssbo_atomic_imin
;
651 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
652 op
= nir_intrinsic_ssbo_atomic_umin
;
654 unreachable("Invalid type");
655 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
656 assert(ir
->return_deref
);
657 if (ir
->return_deref
->type
== glsl_type::int_type
)
658 op
= nir_intrinsic_ssbo_atomic_imax
;
659 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
660 op
= nir_intrinsic_ssbo_atomic_umax
;
662 unreachable("Invalid type");
663 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
664 op
= nir_intrinsic_ssbo_atomic_exchange
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
666 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
668 op
= nir_intrinsic_shader_clock
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
670 op
= nir_intrinsic_group_memory_barrier
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
672 op
= nir_intrinsic_memory_barrier_atomic_counter
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
674 op
= nir_intrinsic_memory_barrier_buffer
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
676 op
= nir_intrinsic_memory_barrier_image
;
677 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
678 op
= nir_intrinsic_memory_barrier_shared
;
679 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
680 op
= nir_intrinsic_load_shared
;
681 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
682 op
= nir_intrinsic_store_shared
;
683 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
684 op
= nir_intrinsic_shared_atomic_add
;
685 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
686 op
= nir_intrinsic_shared_atomic_and
;
687 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
688 op
= nir_intrinsic_shared_atomic_or
;
689 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
690 op
= nir_intrinsic_shared_atomic_xor
;
691 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
692 assert(ir
->return_deref
);
693 if (ir
->return_deref
->type
== glsl_type::int_type
)
694 op
= nir_intrinsic_shared_atomic_imin
;
695 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
696 op
= nir_intrinsic_shared_atomic_umin
;
698 unreachable("Invalid type");
699 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
700 assert(ir
->return_deref
);
701 if (ir
->return_deref
->type
== glsl_type::int_type
)
702 op
= nir_intrinsic_shared_atomic_imax
;
703 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
704 op
= nir_intrinsic_shared_atomic_umax
;
706 unreachable("Invalid type");
707 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
708 op
= nir_intrinsic_shared_atomic_exchange
;
709 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
710 op
= nir_intrinsic_shared_atomic_comp_swap
;
712 unreachable("not reached");
715 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
716 nir_dest
*dest
= &instr
->dest
;
719 case nir_intrinsic_atomic_counter_read_var
:
720 case nir_intrinsic_atomic_counter_inc_var
:
721 case nir_intrinsic_atomic_counter_dec_var
: {
722 ir_dereference
*param
=
723 (ir_dereference
*) ir
->actual_parameters
.get_head();
724 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
725 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
726 nir_builder_instr_insert(&b
, &instr
->instr
);
729 case nir_intrinsic_image_load
:
730 case nir_intrinsic_image_store
:
731 case nir_intrinsic_image_atomic_add
:
732 case nir_intrinsic_image_atomic_min
:
733 case nir_intrinsic_image_atomic_max
:
734 case nir_intrinsic_image_atomic_and
:
735 case nir_intrinsic_image_atomic_or
:
736 case nir_intrinsic_image_atomic_xor
:
737 case nir_intrinsic_image_atomic_exchange
:
738 case nir_intrinsic_image_atomic_comp_swap
:
739 case nir_intrinsic_image_samples
:
740 case nir_intrinsic_image_size
: {
741 nir_ssa_undef_instr
*instr_undef
=
742 nir_ssa_undef_instr_create(shader
, 1, 32);
743 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
745 /* Set the image variable dereference. */
746 exec_node
*param
= ir
->actual_parameters
.get_head();
747 ir_dereference
*image
= (ir_dereference
*)param
;
748 const glsl_type
*type
=
749 image
->variable_referenced()->type
->without_array();
751 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
752 param
= param
->get_next();
754 /* Set the intrinsic destination. */
755 if (ir
->return_deref
) {
756 const nir_intrinsic_info
*info
=
757 &nir_intrinsic_infos
[instr
->intrinsic
];
758 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
759 info
->dest_components
, 32, NULL
);
762 if (op
== nir_intrinsic_image_size
||
763 op
== nir_intrinsic_image_samples
) {
764 nir_builder_instr_insert(&b
, &instr
->instr
);
768 /* Set the address argument, extending the coordinate vector to four
771 nir_ssa_def
*src_addr
=
772 evaluate_rvalue((ir_dereference
*)param
);
773 nir_ssa_def
*srcs
[4];
775 for (int i
= 0; i
< 4; i
++) {
776 if (i
< type
->coordinate_components())
777 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
779 srcs
[i
] = &instr_undef
->def
;
782 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
783 param
= param
->get_next();
785 /* Set the sample argument, which is undefined for single-sample
788 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
790 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
791 param
= param
->get_next();
793 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
796 /* Set the intrinsic parameters. */
797 if (!param
->is_tail_sentinel()) {
799 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
800 param
= param
->get_next();
803 if (!param
->is_tail_sentinel()) {
805 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
806 param
= param
->get_next();
808 nir_builder_instr_insert(&b
, &instr
->instr
);
811 case nir_intrinsic_memory_barrier
:
812 case nir_intrinsic_group_memory_barrier
:
813 case nir_intrinsic_memory_barrier_atomic_counter
:
814 case nir_intrinsic_memory_barrier_buffer
:
815 case nir_intrinsic_memory_barrier_image
:
816 case nir_intrinsic_memory_barrier_shared
:
817 nir_builder_instr_insert(&b
, &instr
->instr
);
819 case nir_intrinsic_shader_clock
:
820 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
821 nir_builder_instr_insert(&b
, &instr
->instr
);
823 case nir_intrinsic_store_ssbo
: {
824 exec_node
*param
= ir
->actual_parameters
.get_head();
825 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
827 param
= param
->get_next();
828 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
830 param
= param
->get_next();
831 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
833 param
= param
->get_next();
834 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
837 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
838 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
839 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
840 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
841 instr
->num_components
= val
->type
->vector_elements
;
843 nir_builder_instr_insert(&b
, &instr
->instr
);
846 case nir_intrinsic_load_ssbo
: {
847 exec_node
*param
= ir
->actual_parameters
.get_head();
848 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
850 param
= param
->get_next();
851 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
853 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
854 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
856 const glsl_type
*type
= ir
->return_deref
->var
->type
;
857 instr
->num_components
= type
->vector_elements
;
859 /* Setup destination register */
860 unsigned bit_size
= glsl_get_bit_size(type
);
861 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
862 type
->vector_elements
, bit_size
, NULL
);
864 /* Insert the created nir instruction now since in the case of boolean
865 * result we will need to emit another instruction after it
867 nir_builder_instr_insert(&b
, &instr
->instr
);
870 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
871 * consider a true boolean to be ~0. Fix this up with a != 0
874 if (type
->base_type
== GLSL_TYPE_BOOL
) {
875 nir_alu_instr
*load_ssbo_compare
=
876 nir_alu_instr_create(shader
, nir_op_ine
);
877 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
878 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
879 load_ssbo_compare
->src
[1].src
=
880 nir_src_for_ssa(nir_imm_int(&b
, 0));
881 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
882 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
883 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
884 &load_ssbo_compare
->dest
.dest
,
885 type
->vector_elements
, bit_size
, NULL
);
886 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
887 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
888 dest
= &load_ssbo_compare
->dest
.dest
;
892 case nir_intrinsic_ssbo_atomic_add
:
893 case nir_intrinsic_ssbo_atomic_imin
:
894 case nir_intrinsic_ssbo_atomic_umin
:
895 case nir_intrinsic_ssbo_atomic_imax
:
896 case nir_intrinsic_ssbo_atomic_umax
:
897 case nir_intrinsic_ssbo_atomic_and
:
898 case nir_intrinsic_ssbo_atomic_or
:
899 case nir_intrinsic_ssbo_atomic_xor
:
900 case nir_intrinsic_ssbo_atomic_exchange
:
901 case nir_intrinsic_ssbo_atomic_comp_swap
: {
902 int param_count
= ir
->actual_parameters
.length();
903 assert(param_count
== 3 || param_count
== 4);
906 exec_node
*param
= ir
->actual_parameters
.get_head();
907 ir_instruction
*inst
= (ir_instruction
*) param
;
908 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
911 param
= param
->get_next();
912 inst
= (ir_instruction
*) param
;
913 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
915 /* data1 parameter (this is always present) */
916 param
= param
->get_next();
917 inst
= (ir_instruction
*) param
;
918 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
920 /* data2 parameter (only with atomic_comp_swap) */
921 if (param_count
== 4) {
922 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
923 param
= param
->get_next();
924 inst
= (ir_instruction
*) param
;
925 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
929 assert(ir
->return_deref
);
930 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
931 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
932 nir_builder_instr_insert(&b
, &instr
->instr
);
935 case nir_intrinsic_load_shared
: {
936 exec_node
*param
= ir
->actual_parameters
.get_head();
937 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
939 nir_intrinsic_set_base(instr
, 0);
940 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
942 const glsl_type
*type
= ir
->return_deref
->var
->type
;
943 instr
->num_components
= type
->vector_elements
;
945 /* Setup destination register */
946 unsigned bit_size
= glsl_get_bit_size(type
);
947 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
948 type
->vector_elements
, bit_size
, NULL
);
950 nir_builder_instr_insert(&b
, &instr
->instr
);
953 case nir_intrinsic_store_shared
: {
954 exec_node
*param
= ir
->actual_parameters
.get_head();
955 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
957 param
= param
->get_next();
958 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
960 param
= param
->get_next();
961 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
964 nir_intrinsic_set_base(instr
, 0);
965 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
967 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
969 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
970 instr
->num_components
= val
->type
->vector_elements
;
972 nir_builder_instr_insert(&b
, &instr
->instr
);
975 case nir_intrinsic_shared_atomic_add
:
976 case nir_intrinsic_shared_atomic_imin
:
977 case nir_intrinsic_shared_atomic_umin
:
978 case nir_intrinsic_shared_atomic_imax
:
979 case nir_intrinsic_shared_atomic_umax
:
980 case nir_intrinsic_shared_atomic_and
:
981 case nir_intrinsic_shared_atomic_or
:
982 case nir_intrinsic_shared_atomic_xor
:
983 case nir_intrinsic_shared_atomic_exchange
:
984 case nir_intrinsic_shared_atomic_comp_swap
: {
985 int param_count
= ir
->actual_parameters
.length();
986 assert(param_count
== 2 || param_count
== 3);
989 exec_node
*param
= ir
->actual_parameters
.get_head();
990 ir_instruction
*inst
= (ir_instruction
*) param
;
991 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
993 /* data1 parameter (this is always present) */
994 param
= param
->get_next();
995 inst
= (ir_instruction
*) param
;
996 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
998 /* data2 parameter (only with atomic_comp_swap) */
999 if (param_count
== 3) {
1000 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1001 param
= param
->get_next();
1002 inst
= (ir_instruction
*) param
;
1004 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1008 assert(ir
->return_deref
);
1009 unsigned bit_size
= glsl_get_bit_size(ir
->return_deref
->type
);
1010 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1011 ir
->return_deref
->type
->vector_elements
,
1013 nir_builder_instr_insert(&b
, &instr
->instr
);
1017 unreachable("not reached");
1020 if (ir
->return_deref
) {
1021 nir_intrinsic_instr
*store_instr
=
1022 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1023 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1024 nir_intrinsic_set_write_mask(store_instr
,
1025 (1 << store_instr
->num_components
) - 1);
1027 store_instr
->variables
[0] =
1028 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1029 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1031 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1037 struct hash_entry
*entry
=
1038 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1040 nir_function
*callee
= (nir_function
*) entry
->data
;
1042 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1045 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1046 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1050 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1051 nir_builder_instr_insert(&b
, &instr
->instr
);
1055 nir_visitor::visit(ir_assignment
*ir
)
1057 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1059 b
.exact
= ir
->lhs
->variable_referenced()->data
.invariant
||
1060 ir
->lhs
->variable_referenced()->data
.precise
;
1062 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1063 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1064 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1065 nir_intrinsic_instr
*copy
=
1066 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1068 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1069 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1071 if (ir
->condition
) {
1072 nir_if
*if_stmt
= nir_if_create(this->shader
);
1073 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1074 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1075 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1076 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1078 nir_builder_instr_insert(&b
, ©
->instr
);
1083 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1085 ir
->lhs
->accept(this);
1086 nir_deref_var
*lhs_deref
= this->deref_head
;
1087 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1089 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1090 /* GLSL IR will give us the input to the write-masked assignment in a
1091 * single packed vector. So, for example, if the writemask is xzw, then
1092 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1096 unsigned component
= 0;
1097 for (unsigned i
= 0; i
< 4; i
++) {
1098 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1100 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1103 nir_intrinsic_instr
*store
=
1104 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1105 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1106 nir_intrinsic_set_write_mask(store
, ir
->write_mask
);
1107 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1108 store
->variables
[0] = nir_deref_as_var(store_deref
);
1109 store
->src
[0] = nir_src_for_ssa(src
);
1111 if (ir
->condition
) {
1112 nir_if
*if_stmt
= nir_if_create(this->shader
);
1113 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1114 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1115 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1116 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1118 nir_builder_instr_insert(&b
, &store
->instr
);
1123 * Given an instruction, returns a pointer to its destination or NULL if there
1124 * is no destination.
1126 * Note that this only handles instructions we generate at this level.
1129 get_instr_dest(nir_instr
*instr
)
1131 nir_alu_instr
*alu_instr
;
1132 nir_intrinsic_instr
*intrinsic_instr
;
1133 nir_tex_instr
*tex_instr
;
1135 switch (instr
->type
) {
1136 case nir_instr_type_alu
:
1137 alu_instr
= nir_instr_as_alu(instr
);
1138 return &alu_instr
->dest
.dest
;
1140 case nir_instr_type_intrinsic
:
1141 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1142 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1143 return &intrinsic_instr
->dest
;
1147 case nir_instr_type_tex
:
1148 tex_instr
= nir_instr_as_tex(instr
);
1149 return &tex_instr
->dest
;
1152 unreachable("not reached");
1159 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
,
1162 nir_dest
*dest
= get_instr_dest(instr
);
1165 nir_ssa_dest_init(instr
, dest
, num_components
, bit_size
, NULL
);
1167 nir_builder_instr_insert(&b
, instr
);
1170 assert(dest
->is_ssa
);
1171 this->result
= &dest
->ssa
;
1176 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1179 if (ir
->as_dereference() || ir
->as_constant()) {
1181 * A dereference is being used on the right hand side, which means we
1182 * must emit a variable load.
1185 nir_intrinsic_instr
*load_instr
=
1186 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1187 load_instr
->num_components
= ir
->type
->vector_elements
;
1188 load_instr
->variables
[0] = this->deref_head
;
1189 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1190 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1191 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
, bit_size
);
1194 return this->result
;
1198 type_is_float(glsl_base_type type
)
1200 return type
== GLSL_TYPE_FLOAT
|| type
== GLSL_TYPE_DOUBLE
;
1204 nir_visitor::visit(ir_expression
*ir
)
1206 /* Some special cases */
1207 switch (ir
->operation
) {
1208 case ir_binop_ubo_load
: {
1209 nir_intrinsic_instr
*load
=
1210 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1211 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1212 load
->num_components
= ir
->type
->vector_elements
;
1213 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1214 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1215 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1218 * In UBO's, a true boolean value is any non-zero value, but we consider
1219 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1222 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1223 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1228 case ir_unop_interpolate_at_centroid
:
1229 case ir_binop_interpolate_at_offset
:
1230 case ir_binop_interpolate_at_sample
: {
1231 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1232 ir_swizzle
*swizzle
= NULL
;
1234 /* the api does not allow a swizzle here, but the varying packing code
1235 * may have pushed one into here.
1237 swizzle
= ir
->operands
[0]->as_swizzle();
1239 deref
= swizzle
->val
->as_dereference();
1243 deref
->accept(this);
1245 nir_intrinsic_op op
;
1246 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1247 switch (ir
->operation
) {
1248 case ir_unop_interpolate_at_centroid
:
1249 op
= nir_intrinsic_interp_var_at_centroid
;
1251 case ir_binop_interpolate_at_offset
:
1252 op
= nir_intrinsic_interp_var_at_offset
;
1254 case ir_binop_interpolate_at_sample
:
1255 op
= nir_intrinsic_interp_var_at_sample
;
1258 unreachable("Invalid interpolation intrinsic");
1261 /* This case can happen if the vertex shader does not write the
1262 * given varying. In this case, the linker will lower it to a
1263 * global variable. Since interpolating a variable makes no
1264 * sense, we'll just turn it into a load which will probably
1265 * eventually end up as an SSA definition.
1267 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1268 op
= nir_intrinsic_load_var
;
1271 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1272 intrin
->num_components
= deref
->type
->vector_elements
;
1273 intrin
->variables
[0] = this->deref_head
;
1274 ralloc_steal(intrin
, intrin
->variables
[0]);
1276 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1277 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1278 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1280 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
)
1281 shader
->info
.uses_interp_var_at_offset
= true;
1283 unsigned bit_size
= glsl_get_bit_size(deref
->type
);
1284 add_instr(&intrin
->instr
, deref
->type
->vector_elements
, bit_size
);
1287 unsigned swiz
[4] = {
1288 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1291 result
= nir_swizzle(&b
, result
, swiz
,
1292 swizzle
->type
->vector_elements
, false);
1302 nir_ssa_def
*srcs
[4];
1303 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1304 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1306 glsl_base_type types
[4];
1307 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1309 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1311 types
[i
] = GLSL_TYPE_FLOAT
;
1313 glsl_base_type out_type
;
1315 out_type
= ir
->type
->base_type
;
1317 out_type
= GLSL_TYPE_FLOAT
;
1319 switch (ir
->operation
) {
1320 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1321 case ir_unop_logic_not
:
1322 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1325 result
= type_is_float(types
[0]) ? nir_fneg(&b
, srcs
[0])
1326 : nir_ineg(&b
, srcs
[0]);
1329 result
= type_is_float(types
[0]) ? nir_fabs(&b
, srcs
[0])
1330 : nir_iabs(&b
, srcs
[0]);
1332 case ir_unop_saturate
:
1333 assert(type_is_float(types
[0]));
1334 result
= nir_fsat(&b
, srcs
[0]);
1337 result
= type_is_float(types
[0]) ? nir_fsign(&b
, srcs
[0])
1338 : nir_isign(&b
, srcs
[0]);
1340 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1341 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1342 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1343 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1344 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1345 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1346 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1348 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1351 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1354 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1356 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1357 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1358 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1359 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1360 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1361 case ir_unop_d2f
: result
= nir_d2f(&b
, srcs
[0]); break;
1362 case ir_unop_f2d
: result
= nir_f2d(&b
, srcs
[0]); break;
1363 case ir_unop_d2i
: result
= nir_d2i(&b
, srcs
[0]); break;
1364 case ir_unop_d2u
: result
= nir_d2u(&b
, srcs
[0]); break;
1365 case ir_unop_d2b
: result
= nir_d2b(&b
, srcs
[0]); break;
1367 assert(supports_ints
);
1368 result
= nir_i2d(&b
, srcs
[0]);
1371 assert(supports_ints
);
1372 result
= nir_u2d(&b
, srcs
[0]);
1376 case ir_unop_bitcast_i2f
:
1377 case ir_unop_bitcast_f2i
:
1378 case ir_unop_bitcast_u2f
:
1379 case ir_unop_bitcast_f2u
:
1380 case ir_unop_subroutine_to_int
:
1382 result
= nir_imov(&b
, srcs
[0]);
1384 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1385 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1386 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1387 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1388 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1389 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1390 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1391 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1392 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1393 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1394 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1395 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1396 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1397 case ir_unop_pack_snorm_2x16
:
1398 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1400 case ir_unop_pack_snorm_4x8
:
1401 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1403 case ir_unop_pack_unorm_2x16
:
1404 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1406 case ir_unop_pack_unorm_4x8
:
1407 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1409 case ir_unop_pack_half_2x16
:
1410 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1412 case ir_unop_unpack_snorm_2x16
:
1413 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1415 case ir_unop_unpack_snorm_4x8
:
1416 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1418 case ir_unop_unpack_unorm_2x16
:
1419 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1421 case ir_unop_unpack_unorm_4x8
:
1422 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1424 case ir_unop_unpack_half_2x16
:
1425 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1427 case ir_unop_pack_double_2x32
:
1428 result
= nir_pack_double_2x32(&b
, srcs
[0]);
1430 case ir_unop_unpack_double_2x32
:
1431 result
= nir_unpack_double_2x32(&b
, srcs
[0]);
1433 case ir_unop_bitfield_reverse
:
1434 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1436 case ir_unop_bit_count
:
1437 result
= nir_bit_count(&b
, srcs
[0]);
1439 case ir_unop_find_msb
:
1441 case GLSL_TYPE_UINT
:
1442 result
= nir_ufind_msb(&b
, srcs
[0]);
1445 result
= nir_ifind_msb(&b
, srcs
[0]);
1448 unreachable("Invalid type for findMSB()");
1451 case ir_unop_find_lsb
:
1452 result
= nir_find_lsb(&b
, srcs
[0]);
1456 switch (ir
->type
->vector_elements
) {
1458 switch (ir
->operands
[0]->type
->vector_elements
) {
1459 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1460 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1461 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1462 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1463 default: unreachable("not reached");
1467 switch (ir
->operands
[0]->type
->vector_elements
) {
1468 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1469 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1470 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1471 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1472 default: unreachable("not reached");
1476 switch (ir
->operands
[0]->type
->vector_elements
) {
1477 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1478 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1479 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1480 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1481 default: unreachable("not reached");
1485 switch (ir
->operands
[0]->type
->vector_elements
) {
1486 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1487 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1488 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1489 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1490 default: unreachable("not reached");
1494 unreachable("not reached");
1497 case ir_unop_get_buffer_size
: {
1498 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1500 nir_intrinsic_get_buffer_size
);
1501 load
->num_components
= ir
->type
->vector_elements
;
1502 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1503 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1504 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1509 result
= type_is_float(out_type
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1510 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1513 result
= type_is_float(out_type
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1514 : nir_isub(&b
, srcs
[0], srcs
[1]);
1517 result
= type_is_float(out_type
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1518 : nir_imul(&b
, srcs
[0], srcs
[1]);
1521 if (type_is_float(out_type
))
1522 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1523 else if (out_type
== GLSL_TYPE_INT
)
1524 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1526 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1529 result
= type_is_float(out_type
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1530 : nir_umod(&b
, srcs
[0], srcs
[1]);
1533 if (type_is_float(out_type
))
1534 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1535 else if (out_type
== GLSL_TYPE_INT
)
1536 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1538 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1541 if (type_is_float(out_type
))
1542 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1543 else if (out_type
== GLSL_TYPE_INT
)
1544 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1546 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1548 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1549 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1550 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1551 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1552 case ir_binop_logic_and
:
1553 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1554 : nir_fand(&b
, srcs
[0], srcs
[1]);
1556 case ir_binop_logic_or
:
1557 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1558 : nir_for(&b
, srcs
[0], srcs
[1]);
1560 case ir_binop_logic_xor
:
1561 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1562 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1564 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1565 case ir_binop_rshift
:
1566 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1567 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1569 case ir_binop_imul_high
:
1570 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1571 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1573 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1574 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1576 if (supports_ints
) {
1577 if (type_is_float(types
[0]))
1578 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1579 else if (types
[0] == GLSL_TYPE_INT
)
1580 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1582 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1584 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1587 case ir_binop_greater
:
1588 if (supports_ints
) {
1589 if (type_is_float(types
[0]))
1590 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1591 else if (types
[0] == GLSL_TYPE_INT
)
1592 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1594 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1596 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1599 case ir_binop_lequal
:
1600 if (supports_ints
) {
1601 if (type_is_float(types
[0]))
1602 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1603 else if (types
[0] == GLSL_TYPE_INT
)
1604 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1606 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1608 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1611 case ir_binop_gequal
:
1612 if (supports_ints
) {
1613 if (type_is_float(types
[0]))
1614 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1615 else if (types
[0] == GLSL_TYPE_INT
)
1616 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1618 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1620 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1623 case ir_binop_equal
:
1624 if (supports_ints
) {
1625 if (type_is_float(types
[0]))
1626 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1628 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1630 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1633 case ir_binop_nequal
:
1634 if (supports_ints
) {
1635 if (type_is_float(types
[0]))
1636 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1638 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1640 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1643 case ir_binop_all_equal
:
1644 if (supports_ints
) {
1645 if (type_is_float(types
[0])) {
1646 switch (ir
->operands
[0]->type
->vector_elements
) {
1647 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1648 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1649 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1650 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1652 unreachable("not reached");
1655 switch (ir
->operands
[0]->type
->vector_elements
) {
1656 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1657 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1658 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1659 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1661 unreachable("not reached");
1665 switch (ir
->operands
[0]->type
->vector_elements
) {
1666 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1667 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1668 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1669 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1671 unreachable("not reached");
1675 case ir_binop_any_nequal
:
1676 if (supports_ints
) {
1677 if (type_is_float(types
[0])) {
1678 switch (ir
->operands
[0]->type
->vector_elements
) {
1679 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1680 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1681 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1682 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1684 unreachable("not reached");
1687 switch (ir
->operands
[0]->type
->vector_elements
) {
1688 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1689 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1690 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1691 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1693 unreachable("not reached");
1697 switch (ir
->operands
[0]->type
->vector_elements
) {
1698 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1699 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1700 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1701 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1703 unreachable("not reached");
1708 switch (ir
->operands
[0]->type
->vector_elements
) {
1709 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1710 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1711 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1713 unreachable("not reached");
1717 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1719 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1722 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1726 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1728 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1730 case ir_triop_bitfield_extract
:
1731 result
= (out_type
== GLSL_TYPE_INT
) ?
1732 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1733 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1735 case ir_quadop_bitfield_insert
:
1736 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1738 case ir_quadop_vector
:
1739 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1743 unreachable("not reached");
1748 nir_visitor::visit(ir_swizzle
*ir
)
1750 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1751 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1752 ir
->type
->vector_elements
, !supports_ints
);
1756 nir_visitor::visit(ir_texture
*ir
)
1763 num_srcs
= 1; /* coordinate */
1768 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1769 num_srcs
= 2; /* coordinate, bias/lod */
1773 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1779 if (ir
->lod_info
.lod
!= NULL
)
1780 num_srcs
= 2; /* coordinate, lod */
1782 num_srcs
= 1; /* coordinate */
1786 op
= nir_texop_txf_ms
;
1787 num_srcs
= 2; /* coordinate, sample_index */
1792 if (ir
->lod_info
.lod
!= NULL
)
1793 num_srcs
= 1; /* lod */
1800 num_srcs
= 1; /* coordinate */
1805 num_srcs
= 1; /* coordinate */
1808 case ir_query_levels
:
1809 op
= nir_texop_query_levels
;
1813 case ir_texture_samples
:
1814 op
= nir_texop_texture_samples
;
1818 case ir_samples_identical
:
1819 op
= nir_texop_samples_identical
;
1820 num_srcs
= 1; /* coordinate */
1824 unreachable("not reached");
1827 if (ir
->projector
!= NULL
)
1829 if (ir
->shadow_comparitor
!= NULL
)
1831 if (ir
->offset
!= NULL
)
1834 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1837 instr
->sampler_dim
=
1838 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1839 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1840 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1841 if (instr
->is_shadow
)
1842 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1843 switch (ir
->type
->base_type
) {
1844 case GLSL_TYPE_FLOAT
:
1845 instr
->dest_type
= nir_type_float
;
1848 instr
->dest_type
= nir_type_int
;
1850 case GLSL_TYPE_BOOL
:
1851 case GLSL_TYPE_UINT
:
1852 instr
->dest_type
= nir_type_uint
;
1855 unreachable("not reached");
1858 instr
->texture
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1860 unsigned src_number
= 0;
1862 if (ir
->coordinate
!= NULL
) {
1863 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1864 instr
->src
[src_number
].src
=
1865 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1866 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1870 if (ir
->projector
!= NULL
) {
1871 instr
->src
[src_number
].src
=
1872 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1873 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1877 if (ir
->shadow_comparitor
!= NULL
) {
1878 instr
->src
[src_number
].src
=
1879 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1880 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1884 if (ir
->offset
!= NULL
) {
1885 /* we don't support multiple offsets yet */
1886 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1888 instr
->src
[src_number
].src
=
1889 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1890 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1896 instr
->src
[src_number
].src
=
1897 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1898 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1905 if (ir
->lod_info
.lod
!= NULL
) {
1906 instr
->src
[src_number
].src
=
1907 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1908 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1914 instr
->src
[src_number
].src
=
1915 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1916 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1918 instr
->src
[src_number
].src
=
1919 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1920 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1925 instr
->src
[src_number
].src
=
1926 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1927 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1932 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1939 assert(src_number
== num_srcs
);
1941 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1942 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
), bit_size
);
1946 nir_visitor::visit(ir_constant
*ir
)
1949 * We don't know if this variable is an an array or struct that gets
1950 * dereferenced, so do the safe thing an make it a variable with a
1951 * constant initializer and return a dereference.
1955 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1956 var
->data
.read_only
= true;
1957 var
->constant_initializer
= constant_copy(ir
, var
);
1959 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1960 this->deref_tail
= &this->deref_head
->deref
;
1964 nir_visitor::visit(ir_dereference_variable
*ir
)
1966 struct hash_entry
*entry
=
1967 _mesa_hash_table_search(this->var_table
, ir
->var
);
1969 nir_variable
*var
= (nir_variable
*) entry
->data
;
1971 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1972 this->deref_head
= deref
;
1973 this->deref_tail
= &deref
->deref
;
1977 nir_visitor::visit(ir_dereference_record
*ir
)
1979 ir
->record
->accept(this);
1981 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1982 assert(field_index
>= 0);
1984 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1985 deref
->deref
.type
= ir
->type
;
1986 this->deref_tail
->child
= &deref
->deref
;
1987 this->deref_tail
= &deref
->deref
;
1991 nir_visitor::visit(ir_dereference_array
*ir
)
1993 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1994 deref
->deref
.type
= ir
->type
;
1996 ir_constant
*const_index
= ir
->array_index
->as_constant();
1997 if (const_index
!= NULL
) {
1998 deref
->deref_array_type
= nir_deref_array_type_direct
;
1999 deref
->base_offset
= const_index
->value
.u
[0];
2001 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2003 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2006 ir
->array
->accept(this);
2008 this->deref_tail
->child
= &deref
->deref
;
2009 ralloc_steal(this->deref_tail
, deref
);
2010 this->deref_tail
= &deref
->deref
;
2014 nir_visitor::visit(ir_barrier
*)
2016 nir_intrinsic_instr
*instr
=
2017 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2018 nir_builder_instr_insert(&b
, &instr
->instr
);