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
);
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 nir_lower_outputs_to_temporaries(shader
, nir_shader_get_entrypoint(shader
));
148 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
149 if (shader_prog
->Label
)
150 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
151 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
152 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
153 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
154 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
155 shader
->info
.num_images
= sh
->NumImages
;
156 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
157 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
158 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
159 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
160 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
161 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
162 shader
->info
.uses_clip_distance_out
=
163 sh
->Program
->ClipDistanceArraySize
!= 0;
164 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
165 shader
->info
.has_transform_feedback_varyings
=
166 shader_prog
->TransformFeedback
.NumVarying
> 0;
169 case MESA_SHADER_TESS_CTRL
:
170 shader
->info
.tcs
.vertices_out
= shader_prog
->TessCtrl
.VerticesOut
;
173 case MESA_SHADER_GEOMETRY
:
174 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
175 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
176 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
177 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
178 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
179 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
182 case MESA_SHADER_FRAGMENT
: {
183 struct gl_fragment_program
*fp
=
184 (struct gl_fragment_program
*)sh
->Program
;
186 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
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
);
218 nir_visitor::~nir_visitor()
220 _mesa_hash_table_destroy(this->var_table
, NULL
);
221 _mesa_hash_table_destroy(this->overload_table
, NULL
);
225 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
228 ralloc_steal(mem_ctx
, this->deref_head
);
229 return this->deref_head
;
232 static nir_constant
*
233 constant_copy(ir_constant
*ir
, void *mem_ctx
)
238 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
240 unsigned total_elems
= ir
->type
->components();
243 ret
->num_elements
= 0;
244 switch (ir
->type
->base_type
) {
246 for (i
= 0; i
< total_elems
; i
++)
247 ret
->value
.u
[i
] = ir
->value
.u
[i
];
251 for (i
= 0; i
< total_elems
; i
++)
252 ret
->value
.i
[i
] = ir
->value
.i
[i
];
255 case GLSL_TYPE_FLOAT
:
256 for (i
= 0; i
< total_elems
; i
++)
257 ret
->value
.f
[i
] = ir
->value
.f
[i
];
261 for (i
= 0; i
< total_elems
; i
++)
262 ret
->value
.b
[i
] = ir
->value
.b
[i
];
265 case GLSL_TYPE_STRUCT
:
266 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
268 ret
->num_elements
= ir
->type
->length
;
271 foreach_in_list(ir_constant
, field
, &ir
->components
) {
272 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
277 case GLSL_TYPE_ARRAY
:
278 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
280 ret
->num_elements
= ir
->type
->length
;
282 for (i
= 0; i
< ir
->type
->length
; i
++)
283 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
287 unreachable("not reached");
294 nir_visitor::visit(ir_variable
*ir
)
296 nir_variable
*var
= ralloc(shader
, nir_variable
);
297 var
->type
= ir
->type
;
298 var
->name
= ralloc_strdup(var
, ir
->name
);
300 var
->data
.read_only
= ir
->data
.read_only
;
301 var
->data
.centroid
= ir
->data
.centroid
;
302 var
->data
.sample
= ir
->data
.sample
;
303 var
->data
.patch
= ir
->data
.patch
;
304 var
->data
.invariant
= ir
->data
.invariant
;
305 var
->data
.location
= ir
->data
.location
;
307 switch(ir
->data
.mode
) {
309 case ir_var_temporary
:
311 var
->data
.mode
= nir_var_global
;
313 var
->data
.mode
= nir_var_local
;
316 case ir_var_function_in
:
317 case ir_var_function_out
:
318 case ir_var_function_inout
:
319 case ir_var_const_in
:
320 var
->data
.mode
= nir_var_local
;
323 case ir_var_shader_in
:
324 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
325 ir
->data
.location
== VARYING_SLOT_FACE
) {
326 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
327 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
328 var
->data
.mode
= nir_var_system_value
;
329 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
330 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
331 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
332 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
333 var
->data
.mode
= nir_var_system_value
;
335 var
->data
.mode
= nir_var_shader_in
;
339 case ir_var_shader_out
:
340 var
->data
.mode
= nir_var_shader_out
;
344 var
->data
.mode
= nir_var_uniform
;
347 case ir_var_shader_storage
:
348 var
->data
.mode
= nir_var_shader_storage
;
351 case ir_var_system_value
:
352 var
->data
.mode
= nir_var_system_value
;
356 unreachable("not reached");
359 var
->data
.interpolation
= ir
->data
.interpolation
;
360 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
361 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
362 var
->data
.explicit_location
= ir
->data
.explicit_location
;
363 var
->data
.explicit_index
= ir
->data
.explicit_index
;
364 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
365 var
->data
.has_initializer
= ir
->data
.has_initializer
;
366 var
->data
.location_frac
= ir
->data
.location_frac
;
368 switch (ir
->data
.depth_layout
) {
369 case ir_depth_layout_none
:
370 var
->data
.depth_layout
= nir_depth_layout_none
;
372 case ir_depth_layout_any
:
373 var
->data
.depth_layout
= nir_depth_layout_any
;
375 case ir_depth_layout_greater
:
376 var
->data
.depth_layout
= nir_depth_layout_greater
;
378 case ir_depth_layout_less
:
379 var
->data
.depth_layout
= nir_depth_layout_less
;
381 case ir_depth_layout_unchanged
:
382 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
385 unreachable("not reached");
388 var
->data
.index
= ir
->data
.index
;
389 var
->data
.descriptor_set
= 0;
390 var
->data
.binding
= ir
->data
.binding
;
391 var
->data
.offset
= ir
->data
.offset
;
392 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
393 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
394 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
395 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
396 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
397 var
->data
.image
.format
= ir
->data
.image_format
;
398 var
->data
.max_array_access
= ir
->data
.max_array_access
;
400 var
->num_state_slots
= ir
->get_num_state_slots();
401 if (var
->num_state_slots
> 0) {
402 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
403 var
->num_state_slots
);
405 ir_state_slot
*state_slots
= ir
->get_state_slots();
406 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
407 for (unsigned j
= 0; j
< 5; j
++)
408 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
409 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
412 var
->state_slots
= NULL
;
415 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
417 var
->interface_type
= ir
->get_interface_type();
419 if (var
->data
.mode
== nir_var_local
)
420 nir_function_impl_add_variable(impl
, var
);
422 nir_shader_add_variable(shader
, var
);
424 _mesa_hash_table_insert(var_table
, ir
, var
);
429 nir_function_visitor::visit_enter(ir_function
*ir
)
431 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
432 visitor
->create_function(sig
);
434 return visit_continue_with_parent
;
438 nir_visitor::create_function(ir_function_signature
*ir
)
440 if (ir
->is_intrinsic
)
443 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
445 assert(ir
->parameters
.is_empty());
446 assert(ir
->return_type
== glsl_type::void_type
);
448 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
452 nir_visitor::visit(ir_function
*ir
)
454 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
459 nir_visitor::visit(ir_function_signature
*ir
)
461 if (ir
->is_intrinsic
)
464 struct hash_entry
*entry
=
465 _mesa_hash_table_search(this->overload_table
, ir
);
468 nir_function
*func
= (nir_function
*) entry
->data
;
470 if (ir
->is_defined
) {
471 nir_function_impl
*impl
= nir_function_impl_create(func
);
474 assert(strcmp(func
->name
, "main") == 0);
475 assert(ir
->parameters
.is_empty());
476 assert(func
->return_type
== glsl_type::void_type
);
478 this->is_global
= false;
480 nir_builder_init(&b
, impl
);
481 b
.cursor
= nir_after_cf_list(&impl
->body
);
482 visit_exec_list(&ir
->body
, this);
484 this->is_global
= true;
491 nir_visitor::visit(ir_loop
*ir
)
493 nir_loop
*loop
= nir_loop_create(this->shader
);
494 nir_builder_cf_insert(&b
, &loop
->cf_node
);
496 b
.cursor
= nir_after_cf_list(&loop
->body
);
497 visit_exec_list(&ir
->body_instructions
, this);
498 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
502 nir_visitor::visit(ir_if
*ir
)
505 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
507 nir_if
*if_stmt
= nir_if_create(this->shader
);
508 if_stmt
->condition
= condition
;
509 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
511 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
512 visit_exec_list(&ir
->then_instructions
, this);
514 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
515 visit_exec_list(&ir
->else_instructions
, this);
517 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
521 nir_visitor::visit(ir_discard
*ir
)
524 * discards aren't treated as control flow, because before we lower them
525 * they can appear anywhere in the shader and the stuff after them may still
526 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
527 * discards will be immediately followed by a return.
530 nir_intrinsic_instr
*discard
;
532 discard
= nir_intrinsic_instr_create(this->shader
,
533 nir_intrinsic_discard_if
);
535 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
537 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
540 nir_builder_instr_insert(&b
, &discard
->instr
);
544 nir_visitor::visit(ir_emit_vertex
*ir
)
546 nir_intrinsic_instr
*instr
=
547 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
548 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
549 nir_builder_instr_insert(&b
, &instr
->instr
);
553 nir_visitor::visit(ir_end_primitive
*ir
)
555 nir_intrinsic_instr
*instr
=
556 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
557 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
558 nir_builder_instr_insert(&b
, &instr
->instr
);
562 nir_visitor::visit(ir_loop_jump
*ir
)
566 case ir_loop_jump::jump_break
:
567 type
= nir_jump_break
;
569 case ir_loop_jump::jump_continue
:
570 type
= nir_jump_continue
;
573 unreachable("not reached");
576 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
577 nir_builder_instr_insert(&b
, &instr
->instr
);
581 nir_visitor::visit(ir_return
*ir
)
583 if (ir
->value
!= NULL
) {
584 nir_intrinsic_instr
*copy
=
585 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
587 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
588 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
591 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
592 nir_builder_instr_insert(&b
, &instr
->instr
);
596 nir_visitor::visit(ir_call
*ir
)
598 if (ir
->callee
->is_intrinsic
) {
600 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
601 op
= nir_intrinsic_atomic_counter_read_var
;
602 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
603 op
= nir_intrinsic_atomic_counter_inc_var
;
604 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
605 op
= nir_intrinsic_atomic_counter_dec_var
;
606 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
607 op
= nir_intrinsic_image_load
;
608 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
609 op
= nir_intrinsic_image_store
;
610 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
611 op
= nir_intrinsic_image_atomic_add
;
612 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
613 op
= nir_intrinsic_image_atomic_min
;
614 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
615 op
= nir_intrinsic_image_atomic_max
;
616 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
617 op
= nir_intrinsic_image_atomic_and
;
618 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
619 op
= nir_intrinsic_image_atomic_or
;
620 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
621 op
= nir_intrinsic_image_atomic_xor
;
622 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
623 op
= nir_intrinsic_image_atomic_exchange
;
624 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
625 op
= nir_intrinsic_image_atomic_comp_swap
;
626 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
627 op
= nir_intrinsic_memory_barrier
;
628 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
629 op
= nir_intrinsic_image_size
;
630 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
631 op
= nir_intrinsic_image_samples
;
632 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
633 op
= nir_intrinsic_store_ssbo
;
634 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
635 op
= nir_intrinsic_load_ssbo
;
636 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
637 op
= nir_intrinsic_ssbo_atomic_add
;
638 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
639 op
= nir_intrinsic_ssbo_atomic_and
;
640 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
641 op
= nir_intrinsic_ssbo_atomic_or
;
642 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
643 op
= nir_intrinsic_ssbo_atomic_xor
;
644 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
645 assert(ir
->return_deref
);
646 if (ir
->return_deref
->type
== glsl_type::int_type
)
647 op
= nir_intrinsic_ssbo_atomic_imin
;
648 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
649 op
= nir_intrinsic_ssbo_atomic_umin
;
651 unreachable("Invalid type");
652 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
653 assert(ir
->return_deref
);
654 if (ir
->return_deref
->type
== glsl_type::int_type
)
655 op
= nir_intrinsic_ssbo_atomic_imax
;
656 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
657 op
= nir_intrinsic_ssbo_atomic_umax
;
659 unreachable("Invalid type");
660 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
661 op
= nir_intrinsic_ssbo_atomic_exchange
;
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
663 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
664 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
665 op
= nir_intrinsic_shader_clock
;
666 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
667 op
= nir_intrinsic_group_memory_barrier
;
668 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
669 op
= nir_intrinsic_memory_barrier_atomic_counter
;
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
671 op
= nir_intrinsic_memory_barrier_buffer
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
673 op
= nir_intrinsic_memory_barrier_image
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
675 op
= nir_intrinsic_memory_barrier_shared
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
677 op
= nir_intrinsic_load_shared
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
679 op
= nir_intrinsic_store_shared
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
681 op
= nir_intrinsic_shared_atomic_add
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
683 op
= nir_intrinsic_shared_atomic_and
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
685 op
= nir_intrinsic_shared_atomic_or
;
686 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
687 op
= nir_intrinsic_shared_atomic_xor
;
688 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
689 assert(ir
->return_deref
);
690 if (ir
->return_deref
->type
== glsl_type::int_type
)
691 op
= nir_intrinsic_shared_atomic_imin
;
692 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
693 op
= nir_intrinsic_shared_atomic_umin
;
695 unreachable("Invalid type");
696 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
697 assert(ir
->return_deref
);
698 if (ir
->return_deref
->type
== glsl_type::int_type
)
699 op
= nir_intrinsic_shared_atomic_imax
;
700 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
701 op
= nir_intrinsic_shared_atomic_umax
;
703 unreachable("Invalid type");
704 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
705 op
= nir_intrinsic_shared_atomic_exchange
;
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
707 op
= nir_intrinsic_shared_atomic_comp_swap
;
709 unreachable("not reached");
712 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
713 nir_dest
*dest
= &instr
->dest
;
716 case nir_intrinsic_atomic_counter_read_var
:
717 case nir_intrinsic_atomic_counter_inc_var
:
718 case nir_intrinsic_atomic_counter_dec_var
: {
719 ir_dereference
*param
=
720 (ir_dereference
*) ir
->actual_parameters
.get_head();
721 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
722 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
723 nir_builder_instr_insert(&b
, &instr
->instr
);
726 case nir_intrinsic_image_load
:
727 case nir_intrinsic_image_store
:
728 case nir_intrinsic_image_atomic_add
:
729 case nir_intrinsic_image_atomic_min
:
730 case nir_intrinsic_image_atomic_max
:
731 case nir_intrinsic_image_atomic_and
:
732 case nir_intrinsic_image_atomic_or
:
733 case nir_intrinsic_image_atomic_xor
:
734 case nir_intrinsic_image_atomic_exchange
:
735 case nir_intrinsic_image_atomic_comp_swap
:
736 case nir_intrinsic_image_samples
:
737 case nir_intrinsic_image_size
: {
738 nir_ssa_undef_instr
*instr_undef
=
739 nir_ssa_undef_instr_create(shader
, 1);
740 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
742 /* Set the image variable dereference. */
743 exec_node
*param
= ir
->actual_parameters
.get_head();
744 ir_dereference
*image
= (ir_dereference
*)param
;
745 const glsl_type
*type
=
746 image
->variable_referenced()->type
->without_array();
748 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
749 param
= param
->get_next();
751 /* Set the intrinsic destination. */
752 if (ir
->return_deref
) {
753 const nir_intrinsic_info
*info
=
754 &nir_intrinsic_infos
[instr
->intrinsic
];
755 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
756 info
->dest_components
, 32, NULL
);
759 if (op
== nir_intrinsic_image_size
||
760 op
== nir_intrinsic_image_samples
) {
761 nir_builder_instr_insert(&b
, &instr
->instr
);
765 /* Set the address argument, extending the coordinate vector to four
768 nir_ssa_def
*src_addr
=
769 evaluate_rvalue((ir_dereference
*)param
);
770 nir_ssa_def
*srcs
[4];
772 for (int i
= 0; i
< 4; i
++) {
773 if (i
< type
->coordinate_components())
774 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
776 srcs
[i
] = &instr_undef
->def
;
779 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
780 param
= param
->get_next();
782 /* Set the sample argument, which is undefined for single-sample
785 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
787 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
788 param
= param
->get_next();
790 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
793 /* Set the intrinsic parameters. */
794 if (!param
->is_tail_sentinel()) {
796 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
797 param
= param
->get_next();
800 if (!param
->is_tail_sentinel()) {
802 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
803 param
= param
->get_next();
805 nir_builder_instr_insert(&b
, &instr
->instr
);
808 case nir_intrinsic_memory_barrier
:
809 case nir_intrinsic_group_memory_barrier
:
810 case nir_intrinsic_memory_barrier_atomic_counter
:
811 case nir_intrinsic_memory_barrier_buffer
:
812 case nir_intrinsic_memory_barrier_image
:
813 case nir_intrinsic_memory_barrier_shared
:
814 nir_builder_instr_insert(&b
, &instr
->instr
);
816 case nir_intrinsic_shader_clock
:
817 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
818 nir_builder_instr_insert(&b
, &instr
->instr
);
820 case nir_intrinsic_store_ssbo
: {
821 exec_node
*param
= ir
->actual_parameters
.get_head();
822 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
824 param
= param
->get_next();
825 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
827 param
= param
->get_next();
828 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
830 param
= param
->get_next();
831 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
834 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
835 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
836 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
837 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
838 instr
->num_components
= val
->type
->vector_elements
;
840 nir_builder_instr_insert(&b
, &instr
->instr
);
843 case nir_intrinsic_load_ssbo
: {
844 exec_node
*param
= ir
->actual_parameters
.get_head();
845 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
847 param
= param
->get_next();
848 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
850 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
851 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
853 const glsl_type
*type
= ir
->return_deref
->var
->type
;
854 instr
->num_components
= type
->vector_elements
;
856 /* Setup destination register */
857 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
858 type
->vector_elements
, 32, NULL
);
860 /* Insert the created nir instruction now since in the case of boolean
861 * result we will need to emit another instruction after it
863 nir_builder_instr_insert(&b
, &instr
->instr
);
866 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
867 * consider a true boolean to be ~0. Fix this up with a != 0
870 if (type
->base_type
== GLSL_TYPE_BOOL
) {
871 nir_alu_instr
*load_ssbo_compare
=
872 nir_alu_instr_create(shader
, nir_op_ine
);
873 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
874 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
875 load_ssbo_compare
->src
[1].src
=
876 nir_src_for_ssa(nir_imm_int(&b
, 0));
877 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
878 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
879 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
880 &load_ssbo_compare
->dest
.dest
,
881 type
->vector_elements
, 32, NULL
);
882 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
883 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
884 dest
= &load_ssbo_compare
->dest
.dest
;
888 case nir_intrinsic_ssbo_atomic_add
:
889 case nir_intrinsic_ssbo_atomic_imin
:
890 case nir_intrinsic_ssbo_atomic_umin
:
891 case nir_intrinsic_ssbo_atomic_imax
:
892 case nir_intrinsic_ssbo_atomic_umax
:
893 case nir_intrinsic_ssbo_atomic_and
:
894 case nir_intrinsic_ssbo_atomic_or
:
895 case nir_intrinsic_ssbo_atomic_xor
:
896 case nir_intrinsic_ssbo_atomic_exchange
:
897 case nir_intrinsic_ssbo_atomic_comp_swap
: {
898 int param_count
= ir
->actual_parameters
.length();
899 assert(param_count
== 3 || param_count
== 4);
902 exec_node
*param
= ir
->actual_parameters
.get_head();
903 ir_instruction
*inst
= (ir_instruction
*) param
;
904 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
907 param
= param
->get_next();
908 inst
= (ir_instruction
*) param
;
909 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
911 /* data1 parameter (this is always present) */
912 param
= param
->get_next();
913 inst
= (ir_instruction
*) param
;
914 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
916 /* data2 parameter (only with atomic_comp_swap) */
917 if (param_count
== 4) {
918 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
919 param
= param
->get_next();
920 inst
= (ir_instruction
*) param
;
921 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
925 assert(ir
->return_deref
);
926 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
927 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
928 nir_builder_instr_insert(&b
, &instr
->instr
);
931 case nir_intrinsic_load_shared
: {
932 exec_node
*param
= ir
->actual_parameters
.get_head();
933 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
935 nir_intrinsic_set_base(instr
, 0);
936 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
938 const glsl_type
*type
= ir
->return_deref
->var
->type
;
939 instr
->num_components
= type
->vector_elements
;
941 /* Setup destination register */
942 unsigned bit_size
= glsl_get_bit_size(type
->base_type
);
943 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
944 type
->vector_elements
, bit_size
, NULL
);
946 nir_builder_instr_insert(&b
, &instr
->instr
);
949 case nir_intrinsic_store_shared
: {
950 exec_node
*param
= ir
->actual_parameters
.get_head();
951 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
953 param
= param
->get_next();
954 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
956 param
= param
->get_next();
957 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
960 nir_intrinsic_set_base(instr
, 0);
961 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
963 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
965 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
966 instr
->num_components
= val
->type
->vector_elements
;
968 nir_builder_instr_insert(&b
, &instr
->instr
);
971 case nir_intrinsic_shared_atomic_add
:
972 case nir_intrinsic_shared_atomic_imin
:
973 case nir_intrinsic_shared_atomic_umin
:
974 case nir_intrinsic_shared_atomic_imax
:
975 case nir_intrinsic_shared_atomic_umax
:
976 case nir_intrinsic_shared_atomic_and
:
977 case nir_intrinsic_shared_atomic_or
:
978 case nir_intrinsic_shared_atomic_xor
:
979 case nir_intrinsic_shared_atomic_exchange
:
980 case nir_intrinsic_shared_atomic_comp_swap
: {
981 int param_count
= ir
->actual_parameters
.length();
982 assert(param_count
== 2 || param_count
== 3);
985 exec_node
*param
= ir
->actual_parameters
.get_head();
986 ir_instruction
*inst
= (ir_instruction
*) param
;
987 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
989 /* data1 parameter (this is always present) */
990 param
= param
->get_next();
991 inst
= (ir_instruction
*) param
;
992 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
994 /* data2 parameter (only with atomic_comp_swap) */
995 if (param_count
== 3) {
996 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
997 param
= param
->get_next();
998 inst
= (ir_instruction
*) param
;
1000 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1004 assert(ir
->return_deref
);
1005 unsigned bit_size
= glsl_get_bit_size(ir
->return_deref
->type
->base_type
);
1006 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1007 ir
->return_deref
->type
->vector_elements
,
1009 nir_builder_instr_insert(&b
, &instr
->instr
);
1013 unreachable("not reached");
1016 if (ir
->return_deref
) {
1017 nir_intrinsic_instr
*store_instr
=
1018 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1019 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1020 nir_intrinsic_set_write_mask(store_instr
,
1021 (1 << store_instr
->num_components
) - 1);
1023 store_instr
->variables
[0] =
1024 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1025 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1027 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1033 struct hash_entry
*entry
=
1034 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1036 nir_function
*callee
= (nir_function
*) entry
->data
;
1038 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1041 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1042 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1046 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1047 nir_builder_instr_insert(&b
, &instr
->instr
);
1051 nir_visitor::visit(ir_assignment
*ir
)
1053 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1055 b
.exact
= ir
->lhs
->variable_referenced()->data
.invariant
||
1056 ir
->lhs
->variable_referenced()->data
.precise
;
1058 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1059 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1060 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1061 nir_intrinsic_instr
*copy
=
1062 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1064 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1065 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1067 if (ir
->condition
) {
1068 nir_if
*if_stmt
= nir_if_create(this->shader
);
1069 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1070 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1071 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1072 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1074 nir_builder_instr_insert(&b
, ©
->instr
);
1079 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1081 ir
->lhs
->accept(this);
1082 nir_deref_var
*lhs_deref
= this->deref_head
;
1083 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1085 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1086 /* GLSL IR will give us the input to the write-masked assignment in a
1087 * single packed vector. So, for example, if the writemask is xzw, then
1088 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1092 unsigned component
= 0;
1093 for (unsigned i
= 0; i
< 4; i
++) {
1094 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1096 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1099 nir_intrinsic_instr
*store
=
1100 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1101 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1102 nir_intrinsic_set_write_mask(store
, ir
->write_mask
);
1103 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1104 store
->variables
[0] = nir_deref_as_var(store_deref
);
1105 store
->src
[0] = nir_src_for_ssa(src
);
1107 if (ir
->condition
) {
1108 nir_if
*if_stmt
= nir_if_create(this->shader
);
1109 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1110 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1111 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1112 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1114 nir_builder_instr_insert(&b
, &store
->instr
);
1119 * Given an instruction, returns a pointer to its destination or NULL if there
1120 * is no destination.
1122 * Note that this only handles instructions we generate at this level.
1125 get_instr_dest(nir_instr
*instr
)
1127 nir_alu_instr
*alu_instr
;
1128 nir_intrinsic_instr
*intrinsic_instr
;
1129 nir_tex_instr
*tex_instr
;
1131 switch (instr
->type
) {
1132 case nir_instr_type_alu
:
1133 alu_instr
= nir_instr_as_alu(instr
);
1134 return &alu_instr
->dest
.dest
;
1136 case nir_instr_type_intrinsic
:
1137 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1138 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1139 return &intrinsic_instr
->dest
;
1143 case nir_instr_type_tex
:
1144 tex_instr
= nir_instr_as_tex(instr
);
1145 return &tex_instr
->dest
;
1148 unreachable("not reached");
1155 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1157 nir_dest
*dest
= get_instr_dest(instr
);
1160 nir_ssa_dest_init(instr
, dest
, num_components
, 32, NULL
);
1162 nir_builder_instr_insert(&b
, instr
);
1165 assert(dest
->is_ssa
);
1166 this->result
= &dest
->ssa
;
1171 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1174 if (ir
->as_dereference() || ir
->as_constant()) {
1176 * A dereference is being used on the right hand side, which means we
1177 * must emit a variable load.
1180 nir_intrinsic_instr
*load_instr
=
1181 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1182 load_instr
->num_components
= ir
->type
->vector_elements
;
1183 load_instr
->variables
[0] = this->deref_head
;
1184 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1185 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1188 return this->result
;
1192 nir_visitor::visit(ir_expression
*ir
)
1194 /* Some special cases */
1195 switch (ir
->operation
) {
1196 case ir_binop_ubo_load
: {
1197 nir_intrinsic_instr
*load
=
1198 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1199 load
->num_components
= ir
->type
->vector_elements
;
1200 load
->dest
.ssa
.bit_size
= glsl_get_bit_size(ir
->type
->base_type
);
1201 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1202 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1203 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1206 * In UBO's, a true boolean value is any non-zero value, but we consider
1207 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1210 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1211 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1216 case ir_unop_interpolate_at_centroid
:
1217 case ir_binop_interpolate_at_offset
:
1218 case ir_binop_interpolate_at_sample
: {
1219 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1220 ir_swizzle
*swizzle
= NULL
;
1222 /* the api does not allow a swizzle here, but the varying packing code
1223 * may have pushed one into here.
1225 swizzle
= ir
->operands
[0]->as_swizzle();
1227 deref
= swizzle
->val
->as_dereference();
1231 deref
->accept(this);
1233 nir_intrinsic_op op
;
1234 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1235 switch (ir
->operation
) {
1236 case ir_unop_interpolate_at_centroid
:
1237 op
= nir_intrinsic_interp_var_at_centroid
;
1239 case ir_binop_interpolate_at_offset
:
1240 op
= nir_intrinsic_interp_var_at_offset
;
1242 case ir_binop_interpolate_at_sample
:
1243 op
= nir_intrinsic_interp_var_at_sample
;
1246 unreachable("Invalid interpolation intrinsic");
1249 /* This case can happen if the vertex shader does not write the
1250 * given varying. In this case, the linker will lower it to a
1251 * global variable. Since interpolating a variable makes no
1252 * sense, we'll just turn it into a load which will probably
1253 * eventually end up as an SSA definition.
1255 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1256 op
= nir_intrinsic_load_var
;
1259 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1260 intrin
->num_components
= deref
->type
->vector_elements
;
1261 intrin
->variables
[0] = this->deref_head
;
1262 ralloc_steal(intrin
, intrin
->variables
[0]);
1264 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1265 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1266 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1268 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1271 unsigned swiz
[4] = {
1272 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1275 result
= nir_swizzle(&b
, result
, swiz
,
1276 swizzle
->type
->vector_elements
, false);
1286 nir_ssa_def
*srcs
[4];
1287 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1288 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1290 glsl_base_type types
[4];
1291 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1293 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1295 types
[i
] = GLSL_TYPE_FLOAT
;
1297 glsl_base_type out_type
;
1299 out_type
= ir
->type
->base_type
;
1301 out_type
= GLSL_TYPE_FLOAT
;
1303 switch (ir
->operation
) {
1304 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1305 case ir_unop_logic_not
:
1306 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1309 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1310 : nir_ineg(&b
, srcs
[0]);
1313 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1314 : nir_iabs(&b
, srcs
[0]);
1316 case ir_unop_saturate
:
1317 assert(types
[0] == GLSL_TYPE_FLOAT
);
1318 result
= nir_fsat(&b
, srcs
[0]);
1321 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1322 : nir_isign(&b
, srcs
[0]);
1324 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1325 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1326 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1327 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1328 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1329 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1330 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1332 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1335 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1338 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1340 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1341 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1342 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1343 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1344 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1347 case ir_unop_bitcast_i2f
:
1348 case ir_unop_bitcast_f2i
:
1349 case ir_unop_bitcast_u2f
:
1350 case ir_unop_bitcast_f2u
:
1351 case ir_unop_subroutine_to_int
:
1353 result
= nir_imov(&b
, srcs
[0]);
1355 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1356 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1357 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1358 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1359 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1360 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1361 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1362 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1363 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1364 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1365 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1366 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1367 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1368 case ir_unop_pack_snorm_2x16
:
1369 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1371 case ir_unop_pack_snorm_4x8
:
1372 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1374 case ir_unop_pack_unorm_2x16
:
1375 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1377 case ir_unop_pack_unorm_4x8
:
1378 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1380 case ir_unop_pack_half_2x16
:
1381 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1383 case ir_unop_unpack_snorm_2x16
:
1384 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1386 case ir_unop_unpack_snorm_4x8
:
1387 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1389 case ir_unop_unpack_unorm_2x16
:
1390 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1392 case ir_unop_unpack_unorm_4x8
:
1393 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1395 case ir_unop_unpack_half_2x16
:
1396 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1398 case ir_unop_bitfield_reverse
:
1399 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1401 case ir_unop_bit_count
:
1402 result
= nir_bit_count(&b
, srcs
[0]);
1404 case ir_unop_find_msb
:
1406 case GLSL_TYPE_UINT
:
1407 result
= nir_ufind_msb(&b
, srcs
[0]);
1410 result
= nir_ifind_msb(&b
, srcs
[0]);
1413 unreachable("Invalid type for findMSB()");
1416 case ir_unop_find_lsb
:
1417 result
= nir_find_lsb(&b
, srcs
[0]);
1421 switch (ir
->type
->vector_elements
) {
1423 switch (ir
->operands
[0]->type
->vector_elements
) {
1424 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1425 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1426 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1427 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1428 default: unreachable("not reached");
1432 switch (ir
->operands
[0]->type
->vector_elements
) {
1433 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1434 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1435 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1436 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1437 default: unreachable("not reached");
1441 switch (ir
->operands
[0]->type
->vector_elements
) {
1442 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1443 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1444 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1445 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1446 default: unreachable("not reached");
1450 switch (ir
->operands
[0]->type
->vector_elements
) {
1451 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1452 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1453 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1454 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1455 default: unreachable("not reached");
1459 unreachable("not reached");
1462 case ir_unop_get_buffer_size
: {
1463 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1465 nir_intrinsic_get_buffer_size
);
1466 load
->num_components
= ir
->type
->vector_elements
;
1467 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1468 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1473 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1474 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1477 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1478 : nir_isub(&b
, srcs
[0], srcs
[1]);
1481 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1482 : nir_imul(&b
, srcs
[0], srcs
[1]);
1485 if (out_type
== GLSL_TYPE_FLOAT
)
1486 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1487 else if (out_type
== GLSL_TYPE_INT
)
1488 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1490 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1493 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1494 : nir_umod(&b
, srcs
[0], srcs
[1]);
1497 if (out_type
== GLSL_TYPE_FLOAT
)
1498 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1499 else if (out_type
== GLSL_TYPE_INT
)
1500 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1502 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1505 if (out_type
== GLSL_TYPE_FLOAT
)
1506 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1507 else if (out_type
== GLSL_TYPE_INT
)
1508 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1510 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1512 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1513 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1514 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1515 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1516 case ir_binop_logic_and
:
1517 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1518 : nir_fand(&b
, srcs
[0], srcs
[1]);
1520 case ir_binop_logic_or
:
1521 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1522 : nir_for(&b
, srcs
[0], srcs
[1]);
1524 case ir_binop_logic_xor
:
1525 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1526 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1528 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1529 case ir_binop_rshift
:
1530 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1531 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1533 case ir_binop_imul_high
:
1534 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1535 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1537 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1538 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1540 if (supports_ints
) {
1541 if (types
[0] == GLSL_TYPE_FLOAT
)
1542 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1543 else if (types
[0] == GLSL_TYPE_INT
)
1544 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1546 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1548 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1551 case ir_binop_greater
:
1552 if (supports_ints
) {
1553 if (types
[0] == GLSL_TYPE_FLOAT
)
1554 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1555 else if (types
[0] == GLSL_TYPE_INT
)
1556 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1558 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1560 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1563 case ir_binop_lequal
:
1564 if (supports_ints
) {
1565 if (types
[0] == GLSL_TYPE_FLOAT
)
1566 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1567 else if (types
[0] == GLSL_TYPE_INT
)
1568 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1570 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1572 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1575 case ir_binop_gequal
:
1576 if (supports_ints
) {
1577 if (types
[0] == GLSL_TYPE_FLOAT
)
1578 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1579 else if (types
[0] == GLSL_TYPE_INT
)
1580 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1582 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1584 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1587 case ir_binop_equal
:
1588 if (supports_ints
) {
1589 if (types
[0] == GLSL_TYPE_FLOAT
)
1590 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1592 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1594 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1597 case ir_binop_nequal
:
1598 if (supports_ints
) {
1599 if (types
[0] == GLSL_TYPE_FLOAT
)
1600 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1602 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1604 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1607 case ir_binop_all_equal
:
1608 if (supports_ints
) {
1609 if (types
[0] == GLSL_TYPE_FLOAT
) {
1610 switch (ir
->operands
[0]->type
->vector_elements
) {
1611 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1612 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1613 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1614 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1616 unreachable("not reached");
1619 switch (ir
->operands
[0]->type
->vector_elements
) {
1620 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1621 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1622 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1623 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1625 unreachable("not reached");
1629 switch (ir
->operands
[0]->type
->vector_elements
) {
1630 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1631 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1632 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1633 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1635 unreachable("not reached");
1639 case ir_binop_any_nequal
:
1640 if (supports_ints
) {
1641 if (types
[0] == GLSL_TYPE_FLOAT
) {
1642 switch (ir
->operands
[0]->type
->vector_elements
) {
1643 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1644 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1645 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1646 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1648 unreachable("not reached");
1651 switch (ir
->operands
[0]->type
->vector_elements
) {
1652 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1653 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1654 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1655 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1657 unreachable("not reached");
1661 switch (ir
->operands
[0]->type
->vector_elements
) {
1662 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1663 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1664 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1665 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1667 unreachable("not reached");
1672 switch (ir
->operands
[0]->type
->vector_elements
) {
1673 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1674 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1675 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1677 unreachable("not reached");
1681 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1683 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1686 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1690 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1692 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1694 case ir_triop_bitfield_extract
:
1695 result
= (out_type
== GLSL_TYPE_INT
) ?
1696 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1697 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1699 case ir_quadop_bitfield_insert
:
1700 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1702 case ir_quadop_vector
:
1703 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1707 unreachable("not reached");
1712 nir_visitor::visit(ir_swizzle
*ir
)
1714 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1715 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1716 ir
->type
->vector_elements
, !supports_ints
);
1720 nir_visitor::visit(ir_texture
*ir
)
1727 num_srcs
= 1; /* coordinate */
1732 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1733 num_srcs
= 2; /* coordinate, bias/lod */
1737 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1743 if (ir
->lod_info
.lod
!= NULL
)
1744 num_srcs
= 2; /* coordinate, lod */
1746 num_srcs
= 1; /* coordinate */
1750 op
= nir_texop_txf_ms
;
1751 num_srcs
= 2; /* coordinate, sample_index */
1756 if (ir
->lod_info
.lod
!= NULL
)
1757 num_srcs
= 1; /* lod */
1764 num_srcs
= 1; /* coordinate */
1769 num_srcs
= 1; /* coordinate */
1772 case ir_query_levels
:
1773 op
= nir_texop_query_levels
;
1777 case ir_texture_samples
:
1778 op
= nir_texop_texture_samples
;
1782 case ir_samples_identical
:
1783 op
= nir_texop_samples_identical
;
1784 num_srcs
= 1; /* coordinate */
1788 unreachable("not reached");
1791 if (ir
->projector
!= NULL
)
1793 if (ir
->shadow_comparitor
!= NULL
)
1795 if (ir
->offset
!= NULL
)
1798 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1801 instr
->sampler_dim
=
1802 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1803 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1804 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1805 if (instr
->is_shadow
)
1806 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1807 switch (ir
->type
->base_type
) {
1808 case GLSL_TYPE_FLOAT
:
1809 instr
->dest_type
= nir_type_float
;
1812 instr
->dest_type
= nir_type_int
;
1814 case GLSL_TYPE_BOOL
:
1815 case GLSL_TYPE_UINT
:
1816 instr
->dest_type
= nir_type_uint
;
1819 unreachable("not reached");
1822 instr
->texture
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1824 unsigned src_number
= 0;
1826 if (ir
->coordinate
!= NULL
) {
1827 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1828 instr
->src
[src_number
].src
=
1829 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1830 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1834 if (ir
->projector
!= NULL
) {
1835 instr
->src
[src_number
].src
=
1836 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1837 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1841 if (ir
->shadow_comparitor
!= NULL
) {
1842 instr
->src
[src_number
].src
=
1843 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1844 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1848 if (ir
->offset
!= NULL
) {
1849 /* we don't support multiple offsets yet */
1850 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1852 instr
->src
[src_number
].src
=
1853 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1854 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1860 instr
->src
[src_number
].src
=
1861 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1862 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1869 if (ir
->lod_info
.lod
!= NULL
) {
1870 instr
->src
[src_number
].src
=
1871 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1872 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1878 instr
->src
[src_number
].src
=
1879 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1880 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1882 instr
->src
[src_number
].src
=
1883 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1884 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1889 instr
->src
[src_number
].src
=
1890 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1891 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1896 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1903 assert(src_number
== num_srcs
);
1905 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1909 nir_visitor::visit(ir_constant
*ir
)
1912 * We don't know if this variable is an an array or struct that gets
1913 * dereferenced, so do the safe thing an make it a variable with a
1914 * constant initializer and return a dereference.
1918 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1919 var
->data
.read_only
= true;
1920 var
->constant_initializer
= constant_copy(ir
, var
);
1922 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1923 this->deref_tail
= &this->deref_head
->deref
;
1927 nir_visitor::visit(ir_dereference_variable
*ir
)
1929 struct hash_entry
*entry
=
1930 _mesa_hash_table_search(this->var_table
, ir
->var
);
1932 nir_variable
*var
= (nir_variable
*) entry
->data
;
1934 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1935 this->deref_head
= deref
;
1936 this->deref_tail
= &deref
->deref
;
1940 nir_visitor::visit(ir_dereference_record
*ir
)
1942 ir
->record
->accept(this);
1944 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1945 assert(field_index
>= 0);
1947 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1948 deref
->deref
.type
= ir
->type
;
1949 this->deref_tail
->child
= &deref
->deref
;
1950 this->deref_tail
= &deref
->deref
;
1954 nir_visitor::visit(ir_dereference_array
*ir
)
1956 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1957 deref
->deref
.type
= ir
->type
;
1959 ir_constant
*const_index
= ir
->array_index
->as_constant();
1960 if (const_index
!= NULL
) {
1961 deref
->deref_array_type
= nir_deref_array_type_direct
;
1962 deref
->base_offset
= const_index
->value
.u
[0];
1964 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1966 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
1969 ir
->array
->accept(this);
1971 this->deref_tail
->child
= &deref
->deref
;
1972 ralloc_steal(this->deref_tail
, deref
);
1973 this->deref_tail
= &deref
->deref
;
1977 nir_visitor::visit(ir_barrier
*ir
)
1979 nir_intrinsic_instr
*instr
=
1980 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
1981 nir_builder_instr_insert(&b
, &instr
->instr
);