2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "glsl_to_nir.h"
29 #include "nir_control_flow.h"
30 #include "nir_builder.h"
31 #include "ir_visitor.h"
32 #include "ir_hierarchical_visitor.h"
36 * pass to lower GLSL IR to NIR
38 * This will lower variable dereferences to loads/stores of corresponding
39 * variables in NIR - the variables will be converted to registers in a later
45 class nir_visitor
: public ir_visitor
48 nir_visitor(nir_shader
*shader
);
51 virtual void visit(ir_variable
*);
52 virtual void visit(ir_function
*);
53 virtual void visit(ir_function_signature
*);
54 virtual void visit(ir_loop
*);
55 virtual void visit(ir_if
*);
56 virtual void visit(ir_discard
*);
57 virtual void visit(ir_loop_jump
*);
58 virtual void visit(ir_return
*);
59 virtual void visit(ir_call
*);
60 virtual void visit(ir_assignment
*);
61 virtual void visit(ir_emit_vertex
*);
62 virtual void visit(ir_end_primitive
*);
63 virtual void visit(ir_expression
*);
64 virtual void visit(ir_swizzle
*);
65 virtual void visit(ir_texture
*);
66 virtual void visit(ir_constant
*);
67 virtual void visit(ir_dereference_variable
*);
68 virtual void visit(ir_dereference_record
*);
69 virtual void visit(ir_dereference_array
*);
70 virtual void visit(ir_barrier
*);
72 void create_function(ir_function
*ir
);
75 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
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 /* TODO: Use _mesa_fls instead */
149 unsigned num_textures
= 0;
150 for (unsigned i
= 0; i
< 8 * sizeof(sh
->Program
->SamplersUsed
); i
++)
151 if (sh
->Program
->SamplersUsed
& (1 << i
))
154 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
155 if (shader_prog
->Label
)
156 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
157 shader
->info
.num_textures
= num_textures
;
158 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
159 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
160 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
161 shader
->info
.num_images
= sh
->NumImages
;
162 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
163 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
164 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
165 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
166 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
167 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
168 shader
->info
.uses_clip_distance_out
=
169 sh
->Program
->ClipDistanceArraySize
!= 0;
170 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
171 shader
->info
.has_transform_feedback_varyings
=
172 shader_prog
->TransformFeedback
.NumVarying
> 0;
175 case MESA_SHADER_GEOMETRY
:
176 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
177 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
178 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
179 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
180 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
181 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
184 case MESA_SHADER_FRAGMENT
: {
185 struct gl_fragment_program
*fp
=
186 (struct gl_fragment_program
*)sh
->Program
;
188 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
189 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
190 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
194 case MESA_SHADER_COMPUTE
: {
195 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
196 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
197 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
198 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
203 break; /* No stage-specific info */
209 nir_visitor::nir_visitor(nir_shader
*shader
)
211 this->supports_ints
= shader
->options
->native_integers
;
212 this->shader
= shader
;
213 this->is_global
= true;
214 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
215 _mesa_key_pointer_equal
);
216 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
217 _mesa_key_pointer_equal
);
220 nir_visitor::~nir_visitor()
222 _mesa_hash_table_destroy(this->var_table
, NULL
);
223 _mesa_hash_table_destroy(this->overload_table
, NULL
);
227 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
230 ralloc_steal(mem_ctx
, this->deref_head
);
231 return this->deref_head
;
234 static nir_constant
*
235 constant_copy(ir_constant
*ir
, void *mem_ctx
)
240 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
242 unsigned total_elems
= ir
->type
->components();
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
*,
269 foreach_in_list(ir_constant
, field
, &ir
->components
) {
270 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
275 case GLSL_TYPE_ARRAY
:
276 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
279 for (i
= 0; i
< ir
->type
->length
; i
++)
280 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
284 unreachable("not reached");
291 nir_visitor::visit(ir_variable
*ir
)
293 nir_variable
*var
= ralloc(shader
, nir_variable
);
294 var
->type
= ir
->type
;
295 var
->name
= ralloc_strdup(var
, ir
->name
);
297 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
298 unsigned size
= ir
->get_interface_type()->length
;
299 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
300 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
301 size
* sizeof(unsigned));
303 var
->max_ifc_array_access
= NULL
;
306 var
->data
.read_only
= ir
->data
.read_only
;
307 var
->data
.centroid
= ir
->data
.centroid
;
308 var
->data
.sample
= ir
->data
.sample
;
309 var
->data
.patch
= ir
->data
.patch
;
310 var
->data
.invariant
= ir
->data
.invariant
;
311 var
->data
.location
= ir
->data
.location
;
313 switch(ir
->data
.mode
) {
315 case ir_var_temporary
:
317 var
->data
.mode
= nir_var_global
;
319 var
->data
.mode
= nir_var_local
;
322 case ir_var_function_in
:
323 case ir_var_function_out
:
324 case ir_var_function_inout
:
325 case ir_var_const_in
:
326 var
->data
.mode
= nir_var_local
;
329 case ir_var_shader_in
:
330 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
331 ir
->data
.location
== VARYING_SLOT_FACE
) {
332 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
333 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
334 var
->data
.mode
= nir_var_system_value
;
335 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
336 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
337 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
338 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
339 var
->data
.mode
= nir_var_system_value
;
341 var
->data
.mode
= nir_var_shader_in
;
345 case ir_var_shader_out
:
346 var
->data
.mode
= nir_var_shader_out
;
350 var
->data
.mode
= nir_var_uniform
;
353 case ir_var_shader_storage
:
354 var
->data
.mode
= nir_var_shader_storage
;
357 case ir_var_system_value
:
358 var
->data
.mode
= nir_var_system_value
;
362 unreachable("not reached");
365 var
->data
.interpolation
= ir
->data
.interpolation
;
366 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
367 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
368 var
->data
.explicit_location
= ir
->data
.explicit_location
;
369 var
->data
.explicit_index
= ir
->data
.explicit_index
;
370 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
371 var
->data
.has_initializer
= ir
->data
.has_initializer
;
372 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
373 var
->data
.location_frac
= ir
->data
.location_frac
;
374 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
375 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
377 switch (ir
->data
.depth_layout
) {
378 case ir_depth_layout_none
:
379 var
->data
.depth_layout
= nir_depth_layout_none
;
381 case ir_depth_layout_any
:
382 var
->data
.depth_layout
= nir_depth_layout_any
;
384 case ir_depth_layout_greater
:
385 var
->data
.depth_layout
= nir_depth_layout_greater
;
387 case ir_depth_layout_less
:
388 var
->data
.depth_layout
= nir_depth_layout_less
;
390 case ir_depth_layout_unchanged
:
391 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
394 unreachable("not reached");
397 var
->data
.index
= ir
->data
.index
;
398 var
->data
.binding
= ir
->data
.binding
;
399 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
400 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
401 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
402 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
403 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
404 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
405 var
->data
.image
.format
= ir
->data
.image_format
;
406 var
->data
.max_array_access
= ir
->data
.max_array_access
;
408 var
->num_state_slots
= ir
->get_num_state_slots();
409 if (var
->num_state_slots
> 0) {
410 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
411 var
->num_state_slots
);
413 ir_state_slot
*state_slots
= ir
->get_state_slots();
414 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
415 for (unsigned j
= 0; j
< 5; j
++)
416 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
417 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
420 var
->state_slots
= NULL
;
423 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
425 var
->interface_type
= ir
->get_interface_type();
427 if (var
->data
.mode
== nir_var_local
)
428 nir_function_impl_add_variable(impl
, var
);
430 nir_shader_add_variable(shader
, var
);
432 _mesa_hash_table_insert(var_table
, ir
, var
);
437 nir_function_visitor::visit_enter(ir_function
*ir
)
439 visitor
->create_function(ir
);
440 return visit_continue_with_parent
;
445 nir_visitor::create_function(ir_function
*ir
)
447 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
448 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
449 create_overload(sig
, func
);
456 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
458 if (ir
->is_intrinsic
)
461 nir_function_overload
*overload
= nir_function_overload_create(function
);
463 unsigned num_params
= ir
->parameters
.length();
464 overload
->num_params
= num_params
;
465 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
468 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
469 switch (param
->data
.mode
) {
470 case ir_var_function_in
:
471 overload
->params
[i
].param_type
= nir_parameter_in
;
474 case ir_var_function_out
:
475 overload
->params
[i
].param_type
= nir_parameter_out
;
478 case ir_var_function_inout
:
479 overload
->params
[i
].param_type
= nir_parameter_inout
;
483 unreachable("not reached");
486 overload
->params
[i
].type
= param
->type
;
490 overload
->return_type
= ir
->return_type
;
492 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
496 nir_visitor::visit(ir_function
*ir
)
498 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
503 nir_visitor::visit(ir_function_signature
*ir
)
505 if (ir
->is_intrinsic
)
508 struct hash_entry
*entry
=
509 _mesa_hash_table_search(this->overload_table
, ir
);
512 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
514 if (ir
->is_defined
) {
515 nir_function_impl
*impl
= nir_function_impl_create(overload
);
518 unsigned num_params
= overload
->num_params
;
519 impl
->num_params
= num_params
;
520 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
522 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
524 impl
->params
[i
] = this->var
;
528 if (overload
->return_type
== glsl_type::void_type
) {
529 impl
->return_var
= NULL
;
531 impl
->return_var
= ralloc(this->shader
, nir_variable
);
532 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
534 impl
->return_var
->type
= overload
->return_type
;
537 this->is_global
= false;
539 nir_builder_init(&b
, impl
);
540 b
.cursor
= nir_after_cf_list(&impl
->body
);
541 visit_exec_list(&ir
->body
, this);
543 this->is_global
= true;
545 overload
->impl
= NULL
;
550 nir_visitor::visit(ir_loop
*ir
)
552 nir_loop
*loop
= nir_loop_create(this->shader
);
553 nir_builder_cf_insert(&b
, &loop
->cf_node
);
555 b
.cursor
= nir_after_cf_list(&loop
->body
);
556 visit_exec_list(&ir
->body_instructions
, this);
557 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
561 nir_visitor::visit(ir_if
*ir
)
564 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
566 nir_if
*if_stmt
= nir_if_create(this->shader
);
567 if_stmt
->condition
= condition
;
568 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
570 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
571 visit_exec_list(&ir
->then_instructions
, this);
573 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
574 visit_exec_list(&ir
->else_instructions
, this);
576 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
580 nir_visitor::visit(ir_discard
*ir
)
583 * discards aren't treated as control flow, because before we lower them
584 * they can appear anywhere in the shader and the stuff after them may still
585 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
586 * discards will be immediately followed by a return.
589 nir_intrinsic_instr
*discard
;
591 discard
= nir_intrinsic_instr_create(this->shader
,
592 nir_intrinsic_discard_if
);
594 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
596 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
599 nir_builder_instr_insert(&b
, &discard
->instr
);
603 nir_visitor::visit(ir_emit_vertex
*ir
)
605 nir_intrinsic_instr
*instr
=
606 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
607 instr
->const_index
[0] = ir
->stream_id();
608 nir_builder_instr_insert(&b
, &instr
->instr
);
612 nir_visitor::visit(ir_end_primitive
*ir
)
614 nir_intrinsic_instr
*instr
=
615 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
616 instr
->const_index
[0] = ir
->stream_id();
617 nir_builder_instr_insert(&b
, &instr
->instr
);
621 nir_visitor::visit(ir_loop_jump
*ir
)
625 case ir_loop_jump::jump_break
:
626 type
= nir_jump_break
;
628 case ir_loop_jump::jump_continue
:
629 type
= nir_jump_continue
;
632 unreachable("not reached");
635 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
636 nir_builder_instr_insert(&b
, &instr
->instr
);
640 nir_visitor::visit(ir_return
*ir
)
642 if (ir
->value
!= NULL
) {
643 nir_intrinsic_instr
*copy
=
644 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
646 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
647 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
650 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
651 nir_builder_instr_insert(&b
, &instr
->instr
);
655 nir_visitor::visit(ir_call
*ir
)
657 if (ir
->callee
->is_intrinsic
) {
659 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
660 op
= nir_intrinsic_atomic_counter_read_var
;
661 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
662 op
= nir_intrinsic_atomic_counter_inc_var
;
663 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
664 op
= nir_intrinsic_atomic_counter_dec_var
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
666 op
= nir_intrinsic_image_load
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
668 op
= nir_intrinsic_image_store
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
670 op
= nir_intrinsic_image_atomic_add
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
672 op
= nir_intrinsic_image_atomic_min
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
674 op
= nir_intrinsic_image_atomic_max
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
676 op
= nir_intrinsic_image_atomic_and
;
677 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
678 op
= nir_intrinsic_image_atomic_or
;
679 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
680 op
= nir_intrinsic_image_atomic_xor
;
681 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
682 op
= nir_intrinsic_image_atomic_exchange
;
683 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
684 op
= nir_intrinsic_image_atomic_comp_swap
;
685 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
686 op
= nir_intrinsic_memory_barrier
;
687 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
688 op
= nir_intrinsic_image_size
;
689 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
690 op
= nir_intrinsic_image_samples
;
691 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
692 op
= nir_intrinsic_store_ssbo
;
693 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
694 op
= nir_intrinsic_load_ssbo
;
695 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
696 op
= nir_intrinsic_ssbo_atomic_add
;
697 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
698 op
= nir_intrinsic_ssbo_atomic_and
;
699 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
700 op
= nir_intrinsic_ssbo_atomic_or
;
701 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
702 op
= nir_intrinsic_ssbo_atomic_xor
;
703 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
704 assert(ir
->return_deref
);
705 if (ir
->return_deref
->type
== glsl_type::int_type
)
706 op
= nir_intrinsic_ssbo_atomic_imin
;
707 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
708 op
= nir_intrinsic_ssbo_atomic_umin
;
710 unreachable("Invalid type");
711 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
712 assert(ir
->return_deref
);
713 if (ir
->return_deref
->type
== glsl_type::int_type
)
714 op
= nir_intrinsic_ssbo_atomic_imax
;
715 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
716 op
= nir_intrinsic_ssbo_atomic_umax
;
718 unreachable("Invalid type");
719 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
720 op
= nir_intrinsic_ssbo_atomic_exchange
;
721 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
722 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
723 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
724 op
= nir_intrinsic_shader_clock
;
725 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
726 op
= nir_intrinsic_group_memory_barrier
;
727 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
728 op
= nir_intrinsic_memory_barrier_atomic_counter
;
729 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
730 op
= nir_intrinsic_memory_barrier_buffer
;
731 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
732 op
= nir_intrinsic_memory_barrier_image
;
733 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
734 op
= nir_intrinsic_memory_barrier_shared
;
736 unreachable("not reached");
739 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
740 nir_dest
*dest
= &instr
->dest
;
743 case nir_intrinsic_atomic_counter_read_var
:
744 case nir_intrinsic_atomic_counter_inc_var
:
745 case nir_intrinsic_atomic_counter_dec_var
: {
746 ir_dereference
*param
=
747 (ir_dereference
*) ir
->actual_parameters
.get_head();
748 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
749 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
750 nir_builder_instr_insert(&b
, &instr
->instr
);
753 case nir_intrinsic_image_load
:
754 case nir_intrinsic_image_store
:
755 case nir_intrinsic_image_atomic_add
:
756 case nir_intrinsic_image_atomic_min
:
757 case nir_intrinsic_image_atomic_max
:
758 case nir_intrinsic_image_atomic_and
:
759 case nir_intrinsic_image_atomic_or
:
760 case nir_intrinsic_image_atomic_xor
:
761 case nir_intrinsic_image_atomic_exchange
:
762 case nir_intrinsic_image_atomic_comp_swap
:
763 case nir_intrinsic_image_samples
:
764 case nir_intrinsic_image_size
: {
765 nir_ssa_undef_instr
*instr_undef
=
766 nir_ssa_undef_instr_create(shader
, 1);
767 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
769 /* Set the image variable dereference. */
770 exec_node
*param
= ir
->actual_parameters
.get_head();
771 ir_dereference
*image
= (ir_dereference
*)param
;
772 const glsl_type
*type
=
773 image
->variable_referenced()->type
->without_array();
775 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
776 param
= param
->get_next();
778 /* Set the intrinsic destination. */
779 if (ir
->return_deref
) {
780 const nir_intrinsic_info
*info
=
781 &nir_intrinsic_infos
[instr
->intrinsic
];
782 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
783 info
->dest_components
, NULL
);
786 if (op
== nir_intrinsic_image_size
||
787 op
== nir_intrinsic_image_samples
) {
788 nir_builder_instr_insert(&b
, &instr
->instr
);
792 /* Set the address argument, extending the coordinate vector to four
795 nir_ssa_def
*src_addr
=
796 evaluate_rvalue((ir_dereference
*)param
);
797 nir_ssa_def
*srcs
[4];
799 for (int i
= 0; i
< 4; i
++) {
800 if (i
< type
->coordinate_components())
801 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
803 srcs
[i
] = &instr_undef
->def
;
806 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
807 param
= param
->get_next();
809 /* Set the sample argument, which is undefined for single-sample
812 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
814 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
815 param
= param
->get_next();
817 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
820 /* Set the intrinsic parameters. */
821 if (!param
->is_tail_sentinel()) {
823 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
824 param
= param
->get_next();
827 if (!param
->is_tail_sentinel()) {
829 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
830 param
= param
->get_next();
832 nir_builder_instr_insert(&b
, &instr
->instr
);
835 case nir_intrinsic_memory_barrier
:
836 case nir_intrinsic_group_memory_barrier
:
837 case nir_intrinsic_memory_barrier_atomic_counter
:
838 case nir_intrinsic_memory_barrier_buffer
:
839 case nir_intrinsic_memory_barrier_image
:
840 case nir_intrinsic_memory_barrier_shared
:
841 nir_builder_instr_insert(&b
, &instr
->instr
);
843 case nir_intrinsic_shader_clock
:
844 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
845 nir_builder_instr_insert(&b
, &instr
->instr
);
847 case nir_intrinsic_store_ssbo
: {
848 exec_node
*param
= ir
->actual_parameters
.get_head();
849 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
851 param
= param
->get_next();
852 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
854 param
= param
->get_next();
855 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
857 param
= param
->get_next();
858 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
861 /* Check if we need the indirect version */
862 ir_constant
*const_offset
= offset
->as_constant();
864 op
= nir_intrinsic_store_ssbo_indirect
;
866 instr
= nir_intrinsic_instr_create(shader
, op
);
867 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
868 instr
->const_index
[0] = 0;
870 instr
->const_index
[0] = const_offset
->value
.u
[0];
873 instr
->const_index
[1] = write_mask
->value
.u
[0];
875 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
876 instr
->num_components
= val
->type
->vector_elements
;
878 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
879 nir_builder_instr_insert(&b
, &instr
->instr
);
882 case nir_intrinsic_load_ssbo
: {
883 exec_node
*param
= ir
->actual_parameters
.get_head();
884 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
886 param
= param
->get_next();
887 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
889 /* Check if we need the indirect version */
890 ir_constant
*const_offset
= offset
->as_constant();
892 op
= nir_intrinsic_load_ssbo_indirect
;
894 instr
= nir_intrinsic_instr_create(shader
, op
);
895 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
896 instr
->const_index
[0] = 0;
899 instr
->const_index
[0] = const_offset
->value
.u
[0];
902 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
904 const glsl_type
*type
= ir
->return_deref
->var
->type
;
905 instr
->num_components
= type
->vector_elements
;
907 /* Setup destination register */
908 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
909 type
->vector_elements
, NULL
);
911 /* Insert the created nir instruction now since in the case of boolean
912 * result we will need to emit another instruction after it
914 nir_builder_instr_insert(&b
, &instr
->instr
);
917 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
918 * consider a true boolean to be ~0. Fix this up with a != 0
921 if (type
->base_type
== GLSL_TYPE_BOOL
) {
922 nir_alu_instr
*load_ssbo_compare
=
923 nir_alu_instr_create(shader
, nir_op_ine
);
924 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
925 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
926 load_ssbo_compare
->src
[1].src
=
927 nir_src_for_ssa(nir_imm_int(&b
, 0));
928 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
929 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
930 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
931 &load_ssbo_compare
->dest
.dest
,
932 type
->vector_elements
, NULL
);
933 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
934 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
935 dest
= &load_ssbo_compare
->dest
.dest
;
939 case nir_intrinsic_ssbo_atomic_add
:
940 case nir_intrinsic_ssbo_atomic_imin
:
941 case nir_intrinsic_ssbo_atomic_umin
:
942 case nir_intrinsic_ssbo_atomic_imax
:
943 case nir_intrinsic_ssbo_atomic_umax
:
944 case nir_intrinsic_ssbo_atomic_and
:
945 case nir_intrinsic_ssbo_atomic_or
:
946 case nir_intrinsic_ssbo_atomic_xor
:
947 case nir_intrinsic_ssbo_atomic_exchange
:
948 case nir_intrinsic_ssbo_atomic_comp_swap
: {
949 int param_count
= ir
->actual_parameters
.length();
950 assert(param_count
== 3 || param_count
== 4);
953 exec_node
*param
= ir
->actual_parameters
.get_head();
954 ir_instruction
*inst
= (ir_instruction
*) param
;
955 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
958 param
= param
->get_next();
959 inst
= (ir_instruction
*) param
;
960 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
962 /* data1 parameter (this is always present) */
963 param
= param
->get_next();
964 inst
= (ir_instruction
*) param
;
965 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
967 /* data2 parameter (only with atomic_comp_swap) */
968 if (param_count
== 4) {
969 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
970 param
= param
->get_next();
971 inst
= (ir_instruction
*) param
;
972 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
976 assert(ir
->return_deref
);
977 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
978 ir
->return_deref
->type
->vector_elements
, NULL
);
979 nir_builder_instr_insert(&b
, &instr
->instr
);
983 unreachable("not reached");
986 if (ir
->return_deref
) {
987 nir_intrinsic_instr
*store_instr
=
988 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
989 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
991 store_instr
->variables
[0] =
992 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
993 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
995 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1001 struct hash_entry
*entry
=
1002 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1004 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
1006 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1009 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1010 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1014 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1015 nir_builder_instr_insert(&b
, &instr
->instr
);
1019 nir_visitor::visit(ir_assignment
*ir
)
1021 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1023 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1024 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1025 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1026 nir_intrinsic_instr
*copy
=
1027 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1029 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1030 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1032 if (ir
->condition
) {
1033 nir_if
*if_stmt
= nir_if_create(this->shader
);
1034 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1035 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1036 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1037 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1039 nir_builder_instr_insert(&b
, ©
->instr
);
1044 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1046 ir
->lhs
->accept(this);
1047 nir_deref_var
*lhs_deref
= this->deref_head
;
1048 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1050 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1052 * We have no good way to update only part of a variable, so just load
1053 * the LHS and do a vec operation to combine the old with the new, and
1055 * back into the LHS. Copy propagation should get rid of the mess.
1058 nir_intrinsic_instr
*load
=
1059 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1060 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1061 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1062 load
->variables
[0] = lhs_deref
;
1063 ralloc_steal(load
, load
->variables
[0]);
1064 nir_builder_instr_insert(&b
, &load
->instr
);
1066 nir_ssa_def
*srcs
[4];
1068 unsigned component
= 0;
1069 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1070 if (ir
->write_mask
& (1 << i
)) {
1071 /* GLSL IR will give us the input to the write-masked assignment
1072 * in a single packed vector. So, for example, if the
1073 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1074 * and z -> w and get the y component from the load.
1076 srcs
[i
] = nir_channel(&b
, src
, component
++);
1078 srcs
[i
] = nir_channel(&b
, &load
->dest
.ssa
, i
);
1082 src
= nir_vec(&b
, srcs
, ir
->lhs
->type
->vector_elements
);
1085 nir_intrinsic_instr
*store
=
1086 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1087 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1088 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1089 store
->variables
[0] = nir_deref_as_var(store_deref
);
1090 store
->src
[0] = nir_src_for_ssa(src
);
1092 if (ir
->condition
) {
1093 nir_if
*if_stmt
= nir_if_create(this->shader
);
1094 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1095 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1096 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1097 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1099 nir_builder_instr_insert(&b
, &store
->instr
);
1104 * Given an instruction, returns a pointer to its destination or NULL if there
1105 * is no destination.
1107 * Note that this only handles instructions we generate at this level.
1110 get_instr_dest(nir_instr
*instr
)
1112 nir_alu_instr
*alu_instr
;
1113 nir_intrinsic_instr
*intrinsic_instr
;
1114 nir_tex_instr
*tex_instr
;
1116 switch (instr
->type
) {
1117 case nir_instr_type_alu
:
1118 alu_instr
= nir_instr_as_alu(instr
);
1119 return &alu_instr
->dest
.dest
;
1121 case nir_instr_type_intrinsic
:
1122 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1123 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1124 return &intrinsic_instr
->dest
;
1128 case nir_instr_type_tex
:
1129 tex_instr
= nir_instr_as_tex(instr
);
1130 return &tex_instr
->dest
;
1133 unreachable("not reached");
1140 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1142 nir_dest
*dest
= get_instr_dest(instr
);
1145 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1147 nir_builder_instr_insert(&b
, instr
);
1150 assert(dest
->is_ssa
);
1151 this->result
= &dest
->ssa
;
1156 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1159 if (ir
->as_dereference() || ir
->as_constant()) {
1161 * A dereference is being used on the right hand side, which means we
1162 * must emit a variable load.
1165 nir_intrinsic_instr
*load_instr
=
1166 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1167 load_instr
->num_components
= ir
->type
->vector_elements
;
1168 load_instr
->variables
[0] = this->deref_head
;
1169 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1170 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1173 return this->result
;
1177 nir_visitor::visit(ir_expression
*ir
)
1179 /* Some special cases */
1180 switch (ir
->operation
) {
1181 case ir_binop_ubo_load
: {
1182 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1184 nir_intrinsic_op op
;
1186 op
= nir_intrinsic_load_ubo
;
1188 op
= nir_intrinsic_load_ubo_indirect
;
1190 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1191 load
->num_components
= ir
->type
->vector_elements
;
1192 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1193 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1195 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1196 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1199 * In UBO's, a true boolean value is any non-zero value, but we consider
1200 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1203 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1204 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1209 case ir_unop_interpolate_at_centroid
:
1210 case ir_binop_interpolate_at_offset
:
1211 case ir_binop_interpolate_at_sample
: {
1212 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1213 ir_swizzle
*swizzle
= NULL
;
1215 /* the api does not allow a swizzle here, but the varying packing code
1216 * may have pushed one into here.
1218 swizzle
= ir
->operands
[0]->as_swizzle();
1220 deref
= swizzle
->val
->as_dereference();
1224 deref
->accept(this);
1226 nir_intrinsic_op op
;
1227 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1228 switch (ir
->operation
) {
1229 case ir_unop_interpolate_at_centroid
:
1230 op
= nir_intrinsic_interp_var_at_centroid
;
1232 case ir_binop_interpolate_at_offset
:
1233 op
= nir_intrinsic_interp_var_at_offset
;
1235 case ir_binop_interpolate_at_sample
:
1236 op
= nir_intrinsic_interp_var_at_sample
;
1239 unreachable("Invalid interpolation intrinsic");
1242 /* This case can happen if the vertex shader does not write the
1243 * given varying. In this case, the linker will lower it to a
1244 * global variable. Since interpolating a variable makes no
1245 * sense, we'll just turn it into a load which will probably
1246 * eventually end up as an SSA definition.
1248 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1249 op
= nir_intrinsic_load_var
;
1252 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1253 intrin
->num_components
= deref
->type
->vector_elements
;
1254 intrin
->variables
[0] = this->deref_head
;
1255 ralloc_steal(intrin
, intrin
->variables
[0]);
1257 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1258 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1259 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1261 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1264 unsigned swiz
[4] = {
1265 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1268 result
= nir_swizzle(&b
, result
, swiz
,
1269 swizzle
->type
->vector_elements
, false);
1279 nir_ssa_def
*srcs
[4];
1280 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1281 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1283 glsl_base_type types
[4];
1284 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1286 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1288 types
[i
] = GLSL_TYPE_FLOAT
;
1290 glsl_base_type out_type
;
1292 out_type
= ir
->type
->base_type
;
1294 out_type
= GLSL_TYPE_FLOAT
;
1296 switch (ir
->operation
) {
1297 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1298 case ir_unop_logic_not
:
1299 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1302 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1303 : nir_ineg(&b
, srcs
[0]);
1306 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1307 : nir_iabs(&b
, srcs
[0]);
1309 case ir_unop_saturate
:
1310 assert(types
[0] == GLSL_TYPE_FLOAT
);
1311 result
= nir_fsat(&b
, srcs
[0]);
1314 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1315 : nir_isign(&b
, srcs
[0]);
1317 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1318 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1319 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1320 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1321 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1322 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1323 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1325 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1328 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1331 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1333 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1334 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1335 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1336 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1337 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1340 case ir_unop_bitcast_i2f
:
1341 case ir_unop_bitcast_f2i
:
1342 case ir_unop_bitcast_u2f
:
1343 case ir_unop_bitcast_f2u
:
1344 case ir_unop_subroutine_to_int
:
1346 result
= nir_imov(&b
, srcs
[0]);
1349 switch (ir
->operands
[0]->type
->vector_elements
) {
1351 result
= supports_ints
? nir_bany2(&b
, srcs
[0])
1352 : nir_fany2(&b
, srcs
[0]);
1355 result
= supports_ints
? nir_bany3(&b
, srcs
[0])
1356 : nir_fany3(&b
, srcs
[0]);
1359 result
= supports_ints
? nir_bany4(&b
, srcs
[0])
1360 : nir_fany4(&b
, srcs
[0]);
1363 unreachable("not reached");
1366 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1367 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1368 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1369 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1370 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1371 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1372 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1373 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1374 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1375 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1376 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1377 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1378 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1379 case ir_unop_pack_snorm_2x16
:
1380 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1382 case ir_unop_pack_snorm_4x8
:
1383 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1385 case ir_unop_pack_unorm_2x16
:
1386 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1388 case ir_unop_pack_unorm_4x8
:
1389 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1391 case ir_unop_pack_half_2x16
:
1392 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1394 case ir_unop_unpack_snorm_2x16
:
1395 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1397 case ir_unop_unpack_snorm_4x8
:
1398 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1400 case ir_unop_unpack_unorm_2x16
:
1401 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1403 case ir_unop_unpack_unorm_4x8
:
1404 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1406 case ir_unop_unpack_half_2x16
:
1407 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1409 case ir_unop_unpack_half_2x16_split_x
:
1410 result
= nir_unpack_half_2x16_split_x(&b
, srcs
[0]);
1412 case ir_unop_unpack_half_2x16_split_y
:
1413 result
= nir_unpack_half_2x16_split_y(&b
, srcs
[0]);
1415 case ir_unop_bitfield_reverse
:
1416 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1418 case ir_unop_bit_count
:
1419 result
= nir_bit_count(&b
, srcs
[0]);
1421 case ir_unop_find_msb
:
1423 case GLSL_TYPE_UINT
:
1424 result
= nir_ufind_msb(&b
, srcs
[0]);
1427 result
= nir_ifind_msb(&b
, srcs
[0]);
1430 unreachable("Invalid type for findMSB()");
1433 case ir_unop_find_lsb
:
1434 result
= nir_find_lsb(&b
, srcs
[0]);
1438 switch (ir
->type
->vector_elements
) {
1440 switch (ir
->operands
[0]->type
->vector_elements
) {
1441 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1442 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1443 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1444 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1445 default: unreachable("not reached");
1449 switch (ir
->operands
[0]->type
->vector_elements
) {
1450 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1451 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1452 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1453 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1454 default: unreachable("not reached");
1458 switch (ir
->operands
[0]->type
->vector_elements
) {
1459 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1460 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1461 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1462 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1463 default: unreachable("not reached");
1467 switch (ir
->operands
[0]->type
->vector_elements
) {
1468 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1469 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1470 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1471 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1472 default: unreachable("not reached");
1476 unreachable("not reached");
1479 case ir_unop_get_buffer_size
: {
1480 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1482 nir_intrinsic_get_buffer_size
);
1483 load
->num_components
= ir
->type
->vector_elements
;
1484 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1485 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1490 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1491 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1494 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1495 : nir_isub(&b
, srcs
[0], srcs
[1]);
1498 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1499 : nir_imul(&b
, srcs
[0], srcs
[1]);
1502 if (out_type
== GLSL_TYPE_FLOAT
)
1503 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1504 else if (out_type
== GLSL_TYPE_INT
)
1505 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1507 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1510 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1511 : nir_umod(&b
, srcs
[0], srcs
[1]);
1514 if (out_type
== GLSL_TYPE_FLOAT
)
1515 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1516 else if (out_type
== GLSL_TYPE_INT
)
1517 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1519 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1522 if (out_type
== GLSL_TYPE_FLOAT
)
1523 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1524 else if (out_type
== GLSL_TYPE_INT
)
1525 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1527 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1529 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1530 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1531 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1532 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1533 case ir_binop_logic_and
:
1534 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1535 : nir_fand(&b
, srcs
[0], srcs
[1]);
1537 case ir_binop_logic_or
:
1538 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1539 : nir_for(&b
, srcs
[0], srcs
[1]);
1541 case ir_binop_logic_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1542 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1543 : nir_for(&b
, srcs
[0], srcs
[1]);
1545 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1546 case ir_binop_rshift
:
1547 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1548 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1550 case ir_binop_imul_high
:
1551 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1552 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1554 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1555 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1557 if (supports_ints
) {
1558 if (types
[0] == GLSL_TYPE_FLOAT
)
1559 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1560 else if (types
[0] == GLSL_TYPE_INT
)
1561 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1563 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1565 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1568 case ir_binop_greater
:
1569 if (supports_ints
) {
1570 if (types
[0] == GLSL_TYPE_FLOAT
)
1571 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1572 else if (types
[0] == GLSL_TYPE_INT
)
1573 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1575 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1577 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1580 case ir_binop_lequal
:
1581 if (supports_ints
) {
1582 if (types
[0] == GLSL_TYPE_FLOAT
)
1583 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1584 else if (types
[0] == GLSL_TYPE_INT
)
1585 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1587 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1589 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1592 case ir_binop_gequal
:
1593 if (supports_ints
) {
1594 if (types
[0] == GLSL_TYPE_FLOAT
)
1595 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1596 else if (types
[0] == GLSL_TYPE_INT
)
1597 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1599 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1601 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1604 case ir_binop_equal
:
1605 if (supports_ints
) {
1606 if (types
[0] == GLSL_TYPE_FLOAT
)
1607 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1609 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1611 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1614 case ir_binop_nequal
:
1615 if (supports_ints
) {
1616 if (types
[0] == GLSL_TYPE_FLOAT
)
1617 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1619 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1621 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1624 case ir_binop_all_equal
:
1625 if (supports_ints
) {
1626 if (types
[0] == GLSL_TYPE_FLOAT
) {
1627 switch (ir
->operands
[0]->type
->vector_elements
) {
1628 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1629 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1630 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1631 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1633 unreachable("not reached");
1636 switch (ir
->operands
[0]->type
->vector_elements
) {
1637 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1638 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1639 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1640 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1642 unreachable("not reached");
1646 switch (ir
->operands
[0]->type
->vector_elements
) {
1647 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1648 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1649 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1650 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1652 unreachable("not reached");
1656 case ir_binop_any_nequal
:
1657 if (supports_ints
) {
1658 if (types
[0] == GLSL_TYPE_FLOAT
) {
1659 switch (ir
->operands
[0]->type
->vector_elements
) {
1660 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1661 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1662 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1663 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1665 unreachable("not reached");
1668 switch (ir
->operands
[0]->type
->vector_elements
) {
1669 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1670 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1671 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1672 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1674 unreachable("not reached");
1678 switch (ir
->operands
[0]->type
->vector_elements
) {
1679 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1680 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1681 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1682 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1684 unreachable("not reached");
1689 switch (ir
->operands
[0]->type
->vector_elements
) {
1690 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1691 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1692 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1694 unreachable("not reached");
1698 case ir_binop_pack_half_2x16_split
:
1699 result
= nir_pack_half_2x16_split(&b
, srcs
[0], srcs
[1]);
1701 case ir_binop_bfm
: result
= nir_bfm(&b
, srcs
[0], srcs
[1]); break;
1702 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1704 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1707 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1711 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1713 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1716 result
= nir_bfi(&b
, srcs
[0], srcs
[1], srcs
[2]);
1718 case ir_triop_bitfield_extract
:
1719 result
= (out_type
== GLSL_TYPE_INT
) ?
1720 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1721 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1723 case ir_quadop_bitfield_insert
:
1724 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1726 case ir_quadop_vector
:
1727 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1731 unreachable("not reached");
1736 nir_visitor::visit(ir_swizzle
*ir
)
1738 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1739 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1740 ir
->type
->vector_elements
, !supports_ints
);
1744 nir_visitor::visit(ir_texture
*ir
)
1751 num_srcs
= 1; /* coordinate */
1756 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1757 num_srcs
= 2; /* coordinate, bias/lod */
1761 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1767 if (ir
->lod_info
.lod
!= NULL
)
1768 num_srcs
= 2; /* coordinate, lod */
1770 num_srcs
= 1; /* coordinate */
1774 op
= nir_texop_txf_ms
;
1775 num_srcs
= 2; /* coordinate, sample_index */
1780 if (ir
->lod_info
.lod
!= NULL
)
1781 num_srcs
= 1; /* lod */
1788 num_srcs
= 1; /* coordinate */
1793 num_srcs
= 1; /* coordinate */
1796 case ir_query_levels
:
1797 op
= nir_texop_query_levels
;
1801 case ir_texture_samples
:
1802 op
= nir_texop_texture_samples
;
1807 unreachable("not reached");
1810 if (ir
->projector
!= NULL
)
1812 if (ir
->shadow_comparitor
!= NULL
)
1814 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1817 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1820 instr
->sampler_dim
=
1821 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1822 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1823 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1824 if (instr
->is_shadow
)
1825 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1826 switch (ir
->type
->base_type
) {
1827 case GLSL_TYPE_FLOAT
:
1828 instr
->dest_type
= nir_type_float
;
1831 instr
->dest_type
= nir_type_int
;
1833 case GLSL_TYPE_UINT
:
1834 instr
->dest_type
= nir_type_unsigned
;
1837 unreachable("not reached");
1840 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1842 unsigned src_number
= 0;
1844 if (ir
->coordinate
!= NULL
) {
1845 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1846 instr
->src
[src_number
].src
=
1847 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1848 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1852 if (ir
->projector
!= NULL
) {
1853 instr
->src
[src_number
].src
=
1854 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1855 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1859 if (ir
->shadow_comparitor
!= NULL
) {
1860 instr
->src
[src_number
].src
=
1861 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1862 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1866 if (ir
->offset
!= NULL
) {
1867 /* we don't support multiple offsets yet */
1868 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1870 ir_constant
*const_offset
= ir
->offset
->as_constant();
1871 if (const_offset
!= NULL
) {
1872 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1873 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1875 instr
->src
[src_number
].src
=
1876 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1877 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1884 instr
->src
[src_number
].src
=
1885 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1886 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1893 if (ir
->lod_info
.lod
!= NULL
) {
1894 instr
->src
[src_number
].src
=
1895 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1896 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1902 instr
->src
[src_number
].src
=
1903 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1904 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1906 instr
->src
[src_number
].src
=
1907 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1908 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1913 instr
->src
[src_number
].src
=
1914 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1915 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1920 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1927 assert(src_number
== num_srcs
);
1929 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1933 nir_visitor::visit(ir_constant
*ir
)
1936 * We don't know if this variable is an an array or struct that gets
1937 * dereferenced, so do the safe thing an make it a variable with a
1938 * constant initializer and return a dereference.
1942 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1943 var
->data
.read_only
= true;
1944 var
->constant_initializer
= constant_copy(ir
, var
);
1946 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1947 this->deref_tail
= &this->deref_head
->deref
;
1951 nir_visitor::visit(ir_dereference_variable
*ir
)
1953 struct hash_entry
*entry
=
1954 _mesa_hash_table_search(this->var_table
, ir
->var
);
1956 nir_variable
*var
= (nir_variable
*) entry
->data
;
1958 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1959 this->deref_head
= deref
;
1960 this->deref_tail
= &deref
->deref
;
1964 nir_visitor::visit(ir_dereference_record
*ir
)
1966 ir
->record
->accept(this);
1968 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1969 assert(field_index
>= 0);
1971 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1972 deref
->deref
.type
= ir
->type
;
1973 this->deref_tail
->child
= &deref
->deref
;
1974 this->deref_tail
= &deref
->deref
;
1978 nir_visitor::visit(ir_dereference_array
*ir
)
1980 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1981 deref
->deref
.type
= ir
->type
;
1983 ir_constant
*const_index
= ir
->array_index
->as_constant();
1984 if (const_index
!= NULL
) {
1985 deref
->deref_array_type
= nir_deref_array_type_direct
;
1986 deref
->base_offset
= const_index
->value
.u
[0];
1988 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1990 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
1993 ir
->array
->accept(this);
1995 this->deref_tail
->child
= &deref
->deref
;
1996 ralloc_steal(this->deref_tail
, deref
);
1997 this->deref_tail
= &deref
->deref
;
2001 nir_visitor::visit(ir_barrier
*ir
)
2003 nir_intrinsic_instr
*instr
=
2004 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2005 nir_builder_instr_insert(&b
, &instr
->instr
);