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
);
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
.binding
= ir
->data
.binding
;
390 var
->data
.offset
= ir
->data
.offset
;
391 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
392 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
393 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
394 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
395 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
396 var
->data
.image
.format
= ir
->data
.image_format
;
397 var
->data
.max_array_access
= ir
->data
.max_array_access
;
399 var
->num_state_slots
= ir
->get_num_state_slots();
400 if (var
->num_state_slots
> 0) {
401 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
402 var
->num_state_slots
);
404 ir_state_slot
*state_slots
= ir
->get_state_slots();
405 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
406 for (unsigned j
= 0; j
< 5; j
++)
407 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
408 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
411 var
->state_slots
= NULL
;
414 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
416 var
->interface_type
= ir
->get_interface_type();
418 if (var
->data
.mode
== nir_var_local
)
419 nir_function_impl_add_variable(impl
, var
);
421 nir_shader_add_variable(shader
, var
);
423 _mesa_hash_table_insert(var_table
, ir
, var
);
428 nir_function_visitor::visit_enter(ir_function
*ir
)
430 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
431 visitor
->create_function(sig
);
433 return visit_continue_with_parent
;
437 nir_visitor::create_function(ir_function_signature
*ir
)
439 if (ir
->is_intrinsic
)
442 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
444 unsigned num_params
= ir
->parameters
.length();
445 func
->num_params
= num_params
;
446 func
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
449 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
450 switch (param
->data
.mode
) {
451 case ir_var_function_in
:
452 func
->params
[i
].param_type
= nir_parameter_in
;
455 case ir_var_function_out
:
456 func
->params
[i
].param_type
= nir_parameter_out
;
459 case ir_var_function_inout
:
460 func
->params
[i
].param_type
= nir_parameter_inout
;
464 unreachable("not reached");
467 func
->params
[i
].type
= param
->type
;
471 func
->return_type
= ir
->return_type
;
473 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
477 nir_visitor::visit(ir_function
*ir
)
479 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
484 nir_visitor::visit(ir_function_signature
*ir
)
486 if (ir
->is_intrinsic
)
489 struct hash_entry
*entry
=
490 _mesa_hash_table_search(this->overload_table
, ir
);
493 nir_function
*func
= (nir_function
*) entry
->data
;
495 if (ir
->is_defined
) {
496 nir_function_impl
*impl
= nir_function_impl_create(func
);
499 unsigned num_params
= func
->num_params
;
500 impl
->num_params
= num_params
;
501 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
503 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
505 impl
->params
[i
] = this->var
;
509 if (func
->return_type
== glsl_type::void_type
) {
510 impl
->return_var
= NULL
;
512 impl
->return_var
= ralloc(this->shader
, nir_variable
);
513 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
515 impl
->return_var
->type
= func
->return_type
;
518 this->is_global
= false;
520 nir_builder_init(&b
, impl
);
521 b
.cursor
= nir_after_cf_list(&impl
->body
);
522 visit_exec_list(&ir
->body
, this);
524 this->is_global
= true;
531 nir_visitor::visit(ir_loop
*ir
)
533 nir_loop
*loop
= nir_loop_create(this->shader
);
534 nir_builder_cf_insert(&b
, &loop
->cf_node
);
536 b
.cursor
= nir_after_cf_list(&loop
->body
);
537 visit_exec_list(&ir
->body_instructions
, this);
538 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
542 nir_visitor::visit(ir_if
*ir
)
545 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
547 nir_if
*if_stmt
= nir_if_create(this->shader
);
548 if_stmt
->condition
= condition
;
549 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
551 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
552 visit_exec_list(&ir
->then_instructions
, this);
554 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
555 visit_exec_list(&ir
->else_instructions
, this);
557 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
561 nir_visitor::visit(ir_discard
*ir
)
564 * discards aren't treated as control flow, because before we lower them
565 * they can appear anywhere in the shader and the stuff after them may still
566 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
567 * discards will be immediately followed by a return.
570 nir_intrinsic_instr
*discard
;
572 discard
= nir_intrinsic_instr_create(this->shader
,
573 nir_intrinsic_discard_if
);
575 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
577 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
580 nir_builder_instr_insert(&b
, &discard
->instr
);
584 nir_visitor::visit(ir_emit_vertex
*ir
)
586 nir_intrinsic_instr
*instr
=
587 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
588 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
589 nir_builder_instr_insert(&b
, &instr
->instr
);
593 nir_visitor::visit(ir_end_primitive
*ir
)
595 nir_intrinsic_instr
*instr
=
596 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
597 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
598 nir_builder_instr_insert(&b
, &instr
->instr
);
602 nir_visitor::visit(ir_loop_jump
*ir
)
606 case ir_loop_jump::jump_break
:
607 type
= nir_jump_break
;
609 case ir_loop_jump::jump_continue
:
610 type
= nir_jump_continue
;
613 unreachable("not reached");
616 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
617 nir_builder_instr_insert(&b
, &instr
->instr
);
621 nir_visitor::visit(ir_return
*ir
)
623 if (ir
->value
!= NULL
) {
624 nir_intrinsic_instr
*copy
=
625 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
627 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
628 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
631 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
632 nir_builder_instr_insert(&b
, &instr
->instr
);
636 nir_visitor::visit(ir_call
*ir
)
638 if (ir
->callee
->is_intrinsic
) {
640 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
641 op
= nir_intrinsic_atomic_counter_read_var
;
642 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
643 op
= nir_intrinsic_atomic_counter_inc_var
;
644 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
645 op
= nir_intrinsic_atomic_counter_dec_var
;
646 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
647 op
= nir_intrinsic_image_load
;
648 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
649 op
= nir_intrinsic_image_store
;
650 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
651 op
= nir_intrinsic_image_atomic_add
;
652 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
653 op
= nir_intrinsic_image_atomic_min
;
654 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
655 op
= nir_intrinsic_image_atomic_max
;
656 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
657 op
= nir_intrinsic_image_atomic_and
;
658 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
659 op
= nir_intrinsic_image_atomic_or
;
660 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
661 op
= nir_intrinsic_image_atomic_xor
;
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
663 op
= nir_intrinsic_image_atomic_exchange
;
664 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
665 op
= nir_intrinsic_image_atomic_comp_swap
;
666 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
667 op
= nir_intrinsic_memory_barrier
;
668 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
669 op
= nir_intrinsic_image_size
;
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
671 op
= nir_intrinsic_image_samples
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
673 op
= nir_intrinsic_store_ssbo
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
675 op
= nir_intrinsic_load_ssbo
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
677 op
= nir_intrinsic_ssbo_atomic_add
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
679 op
= nir_intrinsic_ssbo_atomic_and
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
681 op
= nir_intrinsic_ssbo_atomic_or
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
683 op
= nir_intrinsic_ssbo_atomic_xor
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
685 assert(ir
->return_deref
);
686 if (ir
->return_deref
->type
== glsl_type::int_type
)
687 op
= nir_intrinsic_ssbo_atomic_imin
;
688 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
689 op
= nir_intrinsic_ssbo_atomic_umin
;
691 unreachable("Invalid type");
692 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
693 assert(ir
->return_deref
);
694 if (ir
->return_deref
->type
== glsl_type::int_type
)
695 op
= nir_intrinsic_ssbo_atomic_imax
;
696 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
697 op
= nir_intrinsic_ssbo_atomic_umax
;
699 unreachable("Invalid type");
700 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
701 op
= nir_intrinsic_ssbo_atomic_exchange
;
702 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
703 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
704 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
705 op
= nir_intrinsic_shader_clock
;
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
707 op
= nir_intrinsic_group_memory_barrier
;
708 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
709 op
= nir_intrinsic_memory_barrier_atomic_counter
;
710 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
711 op
= nir_intrinsic_memory_barrier_buffer
;
712 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
713 op
= nir_intrinsic_memory_barrier_image
;
714 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
715 op
= nir_intrinsic_memory_barrier_shared
;
716 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_shared") == 0) {
717 op
= nir_intrinsic_load_shared
;
718 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_shared") == 0) {
719 op
= nir_intrinsic_store_shared
;
720 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
721 op
= nir_intrinsic_shared_atomic_add
;
722 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
723 op
= nir_intrinsic_shared_atomic_and
;
724 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
725 op
= nir_intrinsic_shared_atomic_or
;
726 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
727 op
= nir_intrinsic_shared_atomic_xor
;
728 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
729 assert(ir
->return_deref
);
730 if (ir
->return_deref
->type
== glsl_type::int_type
)
731 op
= nir_intrinsic_shared_atomic_imin
;
732 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
733 op
= nir_intrinsic_shared_atomic_umin
;
735 unreachable("Invalid type");
736 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
737 assert(ir
->return_deref
);
738 if (ir
->return_deref
->type
== glsl_type::int_type
)
739 op
= nir_intrinsic_shared_atomic_imax
;
740 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
741 op
= nir_intrinsic_shared_atomic_umax
;
743 unreachable("Invalid type");
744 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
745 op
= nir_intrinsic_shared_atomic_exchange
;
746 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
747 op
= nir_intrinsic_shared_atomic_comp_swap
;
749 unreachable("not reached");
752 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
753 nir_dest
*dest
= &instr
->dest
;
756 case nir_intrinsic_atomic_counter_read_var
:
757 case nir_intrinsic_atomic_counter_inc_var
:
758 case nir_intrinsic_atomic_counter_dec_var
: {
759 ir_dereference
*param
=
760 (ir_dereference
*) ir
->actual_parameters
.get_head();
761 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
762 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
763 nir_builder_instr_insert(&b
, &instr
->instr
);
766 case nir_intrinsic_image_load
:
767 case nir_intrinsic_image_store
:
768 case nir_intrinsic_image_atomic_add
:
769 case nir_intrinsic_image_atomic_min
:
770 case nir_intrinsic_image_atomic_max
:
771 case nir_intrinsic_image_atomic_and
:
772 case nir_intrinsic_image_atomic_or
:
773 case nir_intrinsic_image_atomic_xor
:
774 case nir_intrinsic_image_atomic_exchange
:
775 case nir_intrinsic_image_atomic_comp_swap
:
776 case nir_intrinsic_image_samples
:
777 case nir_intrinsic_image_size
: {
778 nir_ssa_undef_instr
*instr_undef
=
779 nir_ssa_undef_instr_create(shader
, 1);
780 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
782 /* Set the image variable dereference. */
783 exec_node
*param
= ir
->actual_parameters
.get_head();
784 ir_dereference
*image
= (ir_dereference
*)param
;
785 const glsl_type
*type
=
786 image
->variable_referenced()->type
->without_array();
788 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
789 param
= param
->get_next();
791 /* Set the intrinsic destination. */
792 if (ir
->return_deref
) {
793 const nir_intrinsic_info
*info
=
794 &nir_intrinsic_infos
[instr
->intrinsic
];
795 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
796 info
->dest_components
, NULL
);
799 if (op
== nir_intrinsic_image_size
||
800 op
== nir_intrinsic_image_samples
) {
801 nir_builder_instr_insert(&b
, &instr
->instr
);
805 /* Set the address argument, extending the coordinate vector to four
808 nir_ssa_def
*src_addr
=
809 evaluate_rvalue((ir_dereference
*)param
);
810 nir_ssa_def
*srcs
[4];
812 for (int i
= 0; i
< 4; i
++) {
813 if (i
< type
->coordinate_components())
814 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
816 srcs
[i
] = &instr_undef
->def
;
819 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
820 param
= param
->get_next();
822 /* Set the sample argument, which is undefined for single-sample
825 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
827 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
828 param
= param
->get_next();
830 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
833 /* Set the intrinsic parameters. */
834 if (!param
->is_tail_sentinel()) {
836 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
837 param
= param
->get_next();
840 if (!param
->is_tail_sentinel()) {
842 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
843 param
= param
->get_next();
845 nir_builder_instr_insert(&b
, &instr
->instr
);
848 case nir_intrinsic_memory_barrier
:
849 case nir_intrinsic_group_memory_barrier
:
850 case nir_intrinsic_memory_barrier_atomic_counter
:
851 case nir_intrinsic_memory_barrier_buffer
:
852 case nir_intrinsic_memory_barrier_image
:
853 case nir_intrinsic_memory_barrier_shared
:
854 nir_builder_instr_insert(&b
, &instr
->instr
);
856 case nir_intrinsic_shader_clock
:
857 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
858 nir_builder_instr_insert(&b
, &instr
->instr
);
860 case nir_intrinsic_store_ssbo
: {
861 exec_node
*param
= ir
->actual_parameters
.get_head();
862 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
864 param
= param
->get_next();
865 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
867 param
= param
->get_next();
868 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
870 param
= param
->get_next();
871 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
874 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
875 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
876 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
877 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
878 instr
->num_components
= val
->type
->vector_elements
;
880 nir_builder_instr_insert(&b
, &instr
->instr
);
883 case nir_intrinsic_load_ssbo
: {
884 exec_node
*param
= ir
->actual_parameters
.get_head();
885 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
887 param
= param
->get_next();
888 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
890 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
891 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
893 const glsl_type
*type
= ir
->return_deref
->var
->type
;
894 instr
->num_components
= type
->vector_elements
;
896 /* Setup destination register */
897 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
898 type
->vector_elements
, NULL
);
900 /* Insert the created nir instruction now since in the case of boolean
901 * result we will need to emit another instruction after it
903 nir_builder_instr_insert(&b
, &instr
->instr
);
906 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
907 * consider a true boolean to be ~0. Fix this up with a != 0
910 if (type
->base_type
== GLSL_TYPE_BOOL
) {
911 nir_alu_instr
*load_ssbo_compare
=
912 nir_alu_instr_create(shader
, nir_op_ine
);
913 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
914 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
915 load_ssbo_compare
->src
[1].src
=
916 nir_src_for_ssa(nir_imm_int(&b
, 0));
917 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
918 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
919 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
920 &load_ssbo_compare
->dest
.dest
,
921 type
->vector_elements
, NULL
);
922 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
923 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
924 dest
= &load_ssbo_compare
->dest
.dest
;
928 case nir_intrinsic_ssbo_atomic_add
:
929 case nir_intrinsic_ssbo_atomic_imin
:
930 case nir_intrinsic_ssbo_atomic_umin
:
931 case nir_intrinsic_ssbo_atomic_imax
:
932 case nir_intrinsic_ssbo_atomic_umax
:
933 case nir_intrinsic_ssbo_atomic_and
:
934 case nir_intrinsic_ssbo_atomic_or
:
935 case nir_intrinsic_ssbo_atomic_xor
:
936 case nir_intrinsic_ssbo_atomic_exchange
:
937 case nir_intrinsic_ssbo_atomic_comp_swap
: {
938 int param_count
= ir
->actual_parameters
.length();
939 assert(param_count
== 3 || param_count
== 4);
942 exec_node
*param
= ir
->actual_parameters
.get_head();
943 ir_instruction
*inst
= (ir_instruction
*) param
;
944 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
947 param
= param
->get_next();
948 inst
= (ir_instruction
*) param
;
949 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
951 /* data1 parameter (this is always present) */
952 param
= param
->get_next();
953 inst
= (ir_instruction
*) param
;
954 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
956 /* data2 parameter (only with atomic_comp_swap) */
957 if (param_count
== 4) {
958 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
959 param
= param
->get_next();
960 inst
= (ir_instruction
*) param
;
961 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
965 assert(ir
->return_deref
);
966 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
967 ir
->return_deref
->type
->vector_elements
, NULL
);
968 nir_builder_instr_insert(&b
, &instr
->instr
);
971 case nir_intrinsic_load_shared
: {
972 exec_node
*param
= ir
->actual_parameters
.get_head();
973 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
975 nir_intrinsic_set_base(instr
, 0);
976 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
978 const glsl_type
*type
= ir
->return_deref
->var
->type
;
979 instr
->num_components
= type
->vector_elements
;
981 /* Setup destination register */
982 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
983 type
->vector_elements
, NULL
);
985 nir_builder_instr_insert(&b
, &instr
->instr
);
988 case nir_intrinsic_store_shared
: {
989 exec_node
*param
= ir
->actual_parameters
.get_head();
990 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
992 param
= param
->get_next();
993 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
995 param
= param
->get_next();
996 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
999 nir_intrinsic_set_base(instr
, 0);
1000 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1002 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1004 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
1005 instr
->num_components
= val
->type
->vector_elements
;
1007 nir_builder_instr_insert(&b
, &instr
->instr
);
1010 case nir_intrinsic_shared_atomic_add
:
1011 case nir_intrinsic_shared_atomic_imin
:
1012 case nir_intrinsic_shared_atomic_umin
:
1013 case nir_intrinsic_shared_atomic_imax
:
1014 case nir_intrinsic_shared_atomic_umax
:
1015 case nir_intrinsic_shared_atomic_and
:
1016 case nir_intrinsic_shared_atomic_or
:
1017 case nir_intrinsic_shared_atomic_xor
:
1018 case nir_intrinsic_shared_atomic_exchange
:
1019 case nir_intrinsic_shared_atomic_comp_swap
: {
1020 int param_count
= ir
->actual_parameters
.length();
1021 assert(param_count
== 2 || param_count
== 3);
1024 exec_node
*param
= ir
->actual_parameters
.get_head();
1025 ir_instruction
*inst
= (ir_instruction
*) param
;
1026 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1028 /* data1 parameter (this is always present) */
1029 param
= param
->get_next();
1030 inst
= (ir_instruction
*) param
;
1031 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1033 /* data2 parameter (only with atomic_comp_swap) */
1034 if (param_count
== 3) {
1035 assert(op
== nir_intrinsic_shared_atomic_comp_swap
);
1036 param
= param
->get_next();
1037 inst
= (ir_instruction
*) param
;
1039 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1043 assert(ir
->return_deref
);
1044 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1045 ir
->return_deref
->type
->vector_elements
, NULL
);
1046 nir_builder_instr_insert(&b
, &instr
->instr
);
1050 unreachable("not reached");
1053 if (ir
->return_deref
) {
1054 nir_intrinsic_instr
*store_instr
=
1055 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
1056 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1057 nir_intrinsic_set_write_mask(store_instr
,
1058 (1 << store_instr
->num_components
) - 1);
1060 store_instr
->variables
[0] =
1061 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
1062 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
1064 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1070 struct hash_entry
*entry
=
1071 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1073 nir_function
*callee
= (nir_function
*) entry
->data
;
1075 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1078 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1079 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1083 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1084 nir_builder_instr_insert(&b
, &instr
->instr
);
1088 nir_visitor::visit(ir_assignment
*ir
)
1090 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1092 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1093 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1094 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1095 nir_intrinsic_instr
*copy
=
1096 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1098 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1099 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1101 if (ir
->condition
) {
1102 nir_if
*if_stmt
= nir_if_create(this->shader
);
1103 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1104 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1105 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1106 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1108 nir_builder_instr_insert(&b
, ©
->instr
);
1113 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1115 ir
->lhs
->accept(this);
1116 nir_deref_var
*lhs_deref
= this->deref_head
;
1117 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1119 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1120 /* GLSL IR will give us the input to the write-masked assignment in a
1121 * single packed vector. So, for example, if the writemask is xzw, then
1122 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1126 unsigned component
= 0;
1127 for (unsigned i
= 0; i
< 4; i
++) {
1128 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1130 src
= nir_swizzle(&b
, src
, swiz
, num_components
, !supports_ints
);
1133 nir_intrinsic_instr
*store
=
1134 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1135 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1136 nir_intrinsic_set_write_mask(store
, ir
->write_mask
);
1137 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1138 store
->variables
[0] = nir_deref_as_var(store_deref
);
1139 store
->src
[0] = nir_src_for_ssa(src
);
1141 if (ir
->condition
) {
1142 nir_if
*if_stmt
= nir_if_create(this->shader
);
1143 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1144 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1145 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1146 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1148 nir_builder_instr_insert(&b
, &store
->instr
);
1153 * Given an instruction, returns a pointer to its destination or NULL if there
1154 * is no destination.
1156 * Note that this only handles instructions we generate at this level.
1159 get_instr_dest(nir_instr
*instr
)
1161 nir_alu_instr
*alu_instr
;
1162 nir_intrinsic_instr
*intrinsic_instr
;
1163 nir_tex_instr
*tex_instr
;
1165 switch (instr
->type
) {
1166 case nir_instr_type_alu
:
1167 alu_instr
= nir_instr_as_alu(instr
);
1168 return &alu_instr
->dest
.dest
;
1170 case nir_instr_type_intrinsic
:
1171 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1172 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1173 return &intrinsic_instr
->dest
;
1177 case nir_instr_type_tex
:
1178 tex_instr
= nir_instr_as_tex(instr
);
1179 return &tex_instr
->dest
;
1182 unreachable("not reached");
1189 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1191 nir_dest
*dest
= get_instr_dest(instr
);
1194 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1196 nir_builder_instr_insert(&b
, instr
);
1199 assert(dest
->is_ssa
);
1200 this->result
= &dest
->ssa
;
1205 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1208 if (ir
->as_dereference() || ir
->as_constant()) {
1210 * A dereference is being used on the right hand side, which means we
1211 * must emit a variable load.
1214 nir_intrinsic_instr
*load_instr
=
1215 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1216 load_instr
->num_components
= ir
->type
->vector_elements
;
1217 load_instr
->variables
[0] = this->deref_head
;
1218 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1219 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1222 return this->result
;
1226 nir_visitor::visit(ir_expression
*ir
)
1228 /* Some special cases */
1229 switch (ir
->operation
) {
1230 case ir_binop_ubo_load
: {
1231 nir_intrinsic_instr
*load
=
1232 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1233 load
->num_components
= ir
->type
->vector_elements
;
1234 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1235 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1236 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1239 * In UBO's, a true boolean value is any non-zero value, but we consider
1240 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1243 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1244 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1249 case ir_unop_interpolate_at_centroid
:
1250 case ir_binop_interpolate_at_offset
:
1251 case ir_binop_interpolate_at_sample
: {
1252 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1253 ir_swizzle
*swizzle
= NULL
;
1255 /* the api does not allow a swizzle here, but the varying packing code
1256 * may have pushed one into here.
1258 swizzle
= ir
->operands
[0]->as_swizzle();
1260 deref
= swizzle
->val
->as_dereference();
1264 deref
->accept(this);
1266 nir_intrinsic_op op
;
1267 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1268 switch (ir
->operation
) {
1269 case ir_unop_interpolate_at_centroid
:
1270 op
= nir_intrinsic_interp_var_at_centroid
;
1272 case ir_binop_interpolate_at_offset
:
1273 op
= nir_intrinsic_interp_var_at_offset
;
1275 case ir_binop_interpolate_at_sample
:
1276 op
= nir_intrinsic_interp_var_at_sample
;
1279 unreachable("Invalid interpolation intrinsic");
1282 /* This case can happen if the vertex shader does not write the
1283 * given varying. In this case, the linker will lower it to a
1284 * global variable. Since interpolating a variable makes no
1285 * sense, we'll just turn it into a load which will probably
1286 * eventually end up as an SSA definition.
1288 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1289 op
= nir_intrinsic_load_var
;
1292 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1293 intrin
->num_components
= deref
->type
->vector_elements
;
1294 intrin
->variables
[0] = this->deref_head
;
1295 ralloc_steal(intrin
, intrin
->variables
[0]);
1297 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1298 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1299 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1301 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1304 unsigned swiz
[4] = {
1305 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1308 result
= nir_swizzle(&b
, result
, swiz
,
1309 swizzle
->type
->vector_elements
, false);
1319 nir_ssa_def
*srcs
[4];
1320 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1321 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1323 glsl_base_type types
[4];
1324 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1326 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1328 types
[i
] = GLSL_TYPE_FLOAT
;
1330 glsl_base_type out_type
;
1332 out_type
= ir
->type
->base_type
;
1334 out_type
= GLSL_TYPE_FLOAT
;
1336 switch (ir
->operation
) {
1337 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1338 case ir_unop_logic_not
:
1339 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1342 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1343 : nir_ineg(&b
, srcs
[0]);
1346 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1347 : nir_iabs(&b
, srcs
[0]);
1349 case ir_unop_saturate
:
1350 assert(types
[0] == GLSL_TYPE_FLOAT
);
1351 result
= nir_fsat(&b
, srcs
[0]);
1354 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1355 : nir_isign(&b
, srcs
[0]);
1357 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1358 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1359 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1360 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1361 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1362 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1363 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1365 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1368 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1371 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1373 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1374 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1375 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1376 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1377 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1380 case ir_unop_bitcast_i2f
:
1381 case ir_unop_bitcast_f2i
:
1382 case ir_unop_bitcast_u2f
:
1383 case ir_unop_bitcast_f2u
:
1384 case ir_unop_subroutine_to_int
:
1386 result
= nir_imov(&b
, srcs
[0]);
1388 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1389 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1390 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1391 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1392 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1393 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1394 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1395 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1396 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1397 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1398 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1399 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1400 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1401 case ir_unop_pack_snorm_2x16
:
1402 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1404 case ir_unop_pack_snorm_4x8
:
1405 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1407 case ir_unop_pack_unorm_2x16
:
1408 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1410 case ir_unop_pack_unorm_4x8
:
1411 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1413 case ir_unop_pack_half_2x16
:
1414 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1416 case ir_unop_unpack_snorm_2x16
:
1417 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1419 case ir_unop_unpack_snorm_4x8
:
1420 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1422 case ir_unop_unpack_unorm_2x16
:
1423 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1425 case ir_unop_unpack_unorm_4x8
:
1426 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1428 case ir_unop_unpack_half_2x16
:
1429 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1431 case ir_unop_bitfield_reverse
:
1432 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1434 case ir_unop_bit_count
:
1435 result
= nir_bit_count(&b
, srcs
[0]);
1437 case ir_unop_find_msb
:
1439 case GLSL_TYPE_UINT
:
1440 result
= nir_ufind_msb(&b
, srcs
[0]);
1443 result
= nir_ifind_msb(&b
, srcs
[0]);
1446 unreachable("Invalid type for findMSB()");
1449 case ir_unop_find_lsb
:
1450 result
= nir_find_lsb(&b
, srcs
[0]);
1454 switch (ir
->type
->vector_elements
) {
1456 switch (ir
->operands
[0]->type
->vector_elements
) {
1457 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1458 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1459 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1460 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1461 default: unreachable("not reached");
1465 switch (ir
->operands
[0]->type
->vector_elements
) {
1466 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1467 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1468 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1469 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1470 default: unreachable("not reached");
1474 switch (ir
->operands
[0]->type
->vector_elements
) {
1475 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1476 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1477 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1478 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1479 default: unreachable("not reached");
1483 switch (ir
->operands
[0]->type
->vector_elements
) {
1484 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1485 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1486 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1487 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1488 default: unreachable("not reached");
1492 unreachable("not reached");
1495 case ir_unop_get_buffer_size
: {
1496 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1498 nir_intrinsic_get_buffer_size
);
1499 load
->num_components
= ir
->type
->vector_elements
;
1500 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1501 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1506 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1507 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1510 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1511 : nir_isub(&b
, srcs
[0], srcs
[1]);
1514 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1515 : nir_imul(&b
, srcs
[0], srcs
[1]);
1518 if (out_type
== GLSL_TYPE_FLOAT
)
1519 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1520 else if (out_type
== GLSL_TYPE_INT
)
1521 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1523 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1526 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1527 : nir_umod(&b
, srcs
[0], srcs
[1]);
1530 if (out_type
== GLSL_TYPE_FLOAT
)
1531 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1532 else if (out_type
== GLSL_TYPE_INT
)
1533 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1535 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1538 if (out_type
== GLSL_TYPE_FLOAT
)
1539 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1540 else if (out_type
== GLSL_TYPE_INT
)
1541 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1543 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1545 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1546 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1547 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1548 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1549 case ir_binop_logic_and
:
1550 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1551 : nir_fand(&b
, srcs
[0], srcs
[1]);
1553 case ir_binop_logic_or
:
1554 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1555 : nir_for(&b
, srcs
[0], srcs
[1]);
1557 case ir_binop_logic_xor
:
1558 result
= supports_ints
? nir_ixor(&b
, srcs
[0], srcs
[1])
1559 : nir_fxor(&b
, srcs
[0], srcs
[1]);
1561 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1562 case ir_binop_rshift
:
1563 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1564 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1566 case ir_binop_imul_high
:
1567 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1568 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1570 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1571 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1573 if (supports_ints
) {
1574 if (types
[0] == GLSL_TYPE_FLOAT
)
1575 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1576 else if (types
[0] == GLSL_TYPE_INT
)
1577 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1579 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1581 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1584 case ir_binop_greater
:
1585 if (supports_ints
) {
1586 if (types
[0] == GLSL_TYPE_FLOAT
)
1587 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1588 else if (types
[0] == GLSL_TYPE_INT
)
1589 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1591 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1593 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1596 case ir_binop_lequal
:
1597 if (supports_ints
) {
1598 if (types
[0] == GLSL_TYPE_FLOAT
)
1599 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1600 else if (types
[0] == GLSL_TYPE_INT
)
1601 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1603 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1605 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1608 case ir_binop_gequal
:
1609 if (supports_ints
) {
1610 if (types
[0] == GLSL_TYPE_FLOAT
)
1611 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1612 else if (types
[0] == GLSL_TYPE_INT
)
1613 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1615 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1617 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1620 case ir_binop_equal
:
1621 if (supports_ints
) {
1622 if (types
[0] == GLSL_TYPE_FLOAT
)
1623 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1625 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1627 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1630 case ir_binop_nequal
:
1631 if (supports_ints
) {
1632 if (types
[0] == GLSL_TYPE_FLOAT
)
1633 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1635 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1637 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1640 case ir_binop_all_equal
:
1641 if (supports_ints
) {
1642 if (types
[0] == GLSL_TYPE_FLOAT
) {
1643 switch (ir
->operands
[0]->type
->vector_elements
) {
1644 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1645 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1646 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1647 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1649 unreachable("not reached");
1652 switch (ir
->operands
[0]->type
->vector_elements
) {
1653 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1654 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1655 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1656 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1658 unreachable("not reached");
1662 switch (ir
->operands
[0]->type
->vector_elements
) {
1663 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1664 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1665 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1666 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1668 unreachable("not reached");
1672 case ir_binop_any_nequal
:
1673 if (supports_ints
) {
1674 if (types
[0] == GLSL_TYPE_FLOAT
) {
1675 switch (ir
->operands
[0]->type
->vector_elements
) {
1676 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1677 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1678 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1679 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1681 unreachable("not reached");
1684 switch (ir
->operands
[0]->type
->vector_elements
) {
1685 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1686 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1687 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1688 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1690 unreachable("not reached");
1694 switch (ir
->operands
[0]->type
->vector_elements
) {
1695 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1696 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1697 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1698 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1700 unreachable("not reached");
1705 switch (ir
->operands
[0]->type
->vector_elements
) {
1706 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1707 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1708 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1710 unreachable("not reached");
1714 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1716 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1719 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1723 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1725 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1727 case ir_triop_bitfield_extract
:
1728 result
= (out_type
== GLSL_TYPE_INT
) ?
1729 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1730 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1732 case ir_quadop_bitfield_insert
:
1733 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1735 case ir_quadop_vector
:
1736 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1740 unreachable("not reached");
1745 nir_visitor::visit(ir_swizzle
*ir
)
1747 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1748 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1749 ir
->type
->vector_elements
, !supports_ints
);
1753 nir_visitor::visit(ir_texture
*ir
)
1760 num_srcs
= 1; /* coordinate */
1765 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1766 num_srcs
= 2; /* coordinate, bias/lod */
1770 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1776 if (ir
->lod_info
.lod
!= NULL
)
1777 num_srcs
= 2; /* coordinate, lod */
1779 num_srcs
= 1; /* coordinate */
1783 op
= nir_texop_txf_ms
;
1784 num_srcs
= 2; /* coordinate, sample_index */
1789 if (ir
->lod_info
.lod
!= NULL
)
1790 num_srcs
= 1; /* lod */
1797 num_srcs
= 1; /* coordinate */
1802 num_srcs
= 1; /* coordinate */
1805 case ir_query_levels
:
1806 op
= nir_texop_query_levels
;
1810 case ir_texture_samples
:
1811 op
= nir_texop_texture_samples
;
1815 case ir_samples_identical
:
1816 op
= nir_texop_samples_identical
;
1817 num_srcs
= 1; /* coordinate */
1821 unreachable("not reached");
1824 if (ir
->projector
!= NULL
)
1826 if (ir
->shadow_comparitor
!= NULL
)
1828 if (ir
->offset
!= NULL
)
1831 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1834 instr
->sampler_dim
=
1835 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1836 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1837 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1838 if (instr
->is_shadow
)
1839 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1840 switch (ir
->type
->base_type
) {
1841 case GLSL_TYPE_FLOAT
:
1842 instr
->dest_type
= nir_type_float
;
1845 instr
->dest_type
= nir_type_int
;
1847 case GLSL_TYPE_BOOL
:
1848 case GLSL_TYPE_UINT
:
1849 instr
->dest_type
= nir_type_uint
;
1852 unreachable("not reached");
1855 instr
->texture
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1857 unsigned src_number
= 0;
1859 if (ir
->coordinate
!= NULL
) {
1860 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1861 instr
->src
[src_number
].src
=
1862 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1863 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1867 if (ir
->projector
!= NULL
) {
1868 instr
->src
[src_number
].src
=
1869 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1870 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1874 if (ir
->shadow_comparitor
!= NULL
) {
1875 instr
->src
[src_number
].src
=
1876 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1877 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1881 if (ir
->offset
!= NULL
) {
1882 /* we don't support multiple offsets yet */
1883 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1885 instr
->src
[src_number
].src
=
1886 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1887 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1893 instr
->src
[src_number
].src
=
1894 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1895 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1902 if (ir
->lod_info
.lod
!= NULL
) {
1903 instr
->src
[src_number
].src
=
1904 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1905 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1911 instr
->src
[src_number
].src
=
1912 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1913 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1915 instr
->src
[src_number
].src
=
1916 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1917 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1922 instr
->src
[src_number
].src
=
1923 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1924 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1929 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1936 assert(src_number
== num_srcs
);
1938 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1942 nir_visitor::visit(ir_constant
*ir
)
1945 * We don't know if this variable is an an array or struct that gets
1946 * dereferenced, so do the safe thing an make it a variable with a
1947 * constant initializer and return a dereference.
1951 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1952 var
->data
.read_only
= true;
1953 var
->constant_initializer
= constant_copy(ir
, var
);
1955 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1956 this->deref_tail
= &this->deref_head
->deref
;
1960 nir_visitor::visit(ir_dereference_variable
*ir
)
1962 struct hash_entry
*entry
=
1963 _mesa_hash_table_search(this->var_table
, ir
->var
);
1965 nir_variable
*var
= (nir_variable
*) entry
->data
;
1967 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1968 this->deref_head
= deref
;
1969 this->deref_tail
= &deref
->deref
;
1973 nir_visitor::visit(ir_dereference_record
*ir
)
1975 ir
->record
->accept(this);
1977 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1978 assert(field_index
>= 0);
1980 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1981 deref
->deref
.type
= ir
->type
;
1982 this->deref_tail
->child
= &deref
->deref
;
1983 this->deref_tail
= &deref
->deref
;
1987 nir_visitor::visit(ir_dereference_array
*ir
)
1989 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1990 deref
->deref
.type
= ir
->type
;
1992 ir_constant
*const_index
= ir
->array_index
->as_constant();
1993 if (const_index
!= NULL
) {
1994 deref
->deref_array_type
= nir_deref_array_type_direct
;
1995 deref
->base_offset
= const_index
->value
.u
[0];
1997 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1999 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
2002 ir
->array
->accept(this);
2004 this->deref_tail
->child
= &deref
->deref
;
2005 ralloc_steal(this->deref_tail
, deref
);
2006 this->deref_tail
= &deref
->deref
;
2010 nir_visitor::visit(ir_barrier
*ir
)
2012 nir_intrinsic_instr
*instr
=
2013 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2014 nir_builder_instr_insert(&b
, &instr
->instr
);