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"
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
*ir
);
76 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
77 void add_instr(nir_instr
*instr
, unsigned num_components
);
78 nir_ssa_def
*evaluate_rvalue(ir_rvalue
*ir
);
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
**srcs
);
81 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
);
82 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
84 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
85 nir_ssa_def
*src2
, nir_ssa_def
*src3
);
90 nir_function_impl
*impl
;
92 nir_ssa_def
*result
; /* result of the expression tree last visited */
94 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
96 /* the head of the dereference chain we're creating */
97 nir_deref_var
*deref_head
;
98 /* the tail of the dereference chain we're creating */
99 nir_deref
*deref_tail
;
101 nir_variable
*var
; /* variable created by ir_variable visitor */
103 /* whether the IR we're operating on is per-function or global */
106 /* map of ir_variable -> nir_variable */
107 struct hash_table
*var_table
;
109 /* map of ir_function_signature -> nir_function_overload */
110 struct hash_table
*overload_table
;
114 * This visitor runs before the main visitor, calling create_function() for
115 * each function so that the main visitor can resolve forward references in
119 class nir_function_visitor
: public ir_hierarchical_visitor
122 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
125 virtual ir_visitor_status
visit_enter(ir_function
*);
128 nir_visitor
*visitor
;
131 }; /* end of anonymous namespace */
134 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
135 gl_shader_stage stage
,
136 const nir_shader_compiler_options
*options
)
138 struct gl_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
140 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
142 nir_visitor
v1(shader
);
143 nir_function_visitor
v2(&v1
);
145 visit_exec_list(sh
->ir
, &v1
);
147 nir_lower_outputs_to_temporaries(shader
);
149 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
150 if (shader_prog
->Label
)
151 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
152 shader
->info
.num_textures
= _mesa_fls(sh
->Program
->SamplersUsed
);
153 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
154 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
155 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
156 shader
->info
.num_images
= sh
->NumImages
;
157 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
158 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
159 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
160 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
161 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
162 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
163 shader
->info
.uses_clip_distance_out
=
164 sh
->Program
->ClipDistanceArraySize
!= 0;
165 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
166 shader
->info
.has_transform_feedback_varyings
=
167 shader_prog
->TransformFeedback
.NumVarying
> 0;
170 case MESA_SHADER_GEOMETRY
:
171 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
172 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
173 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
174 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
175 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
176 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
179 case MESA_SHADER_FRAGMENT
: {
180 struct gl_fragment_program
*fp
=
181 (struct gl_fragment_program
*)sh
->Program
;
183 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
184 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
185 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
189 case MESA_SHADER_COMPUTE
: {
190 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
191 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
192 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
193 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
198 break; /* No stage-specific info */
204 nir_visitor::nir_visitor(nir_shader
*shader
)
206 this->supports_ints
= shader
->options
->native_integers
;
207 this->shader
= shader
;
208 this->is_global
= true;
209 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
210 _mesa_key_pointer_equal
);
211 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
212 _mesa_key_pointer_equal
);
215 nir_visitor::~nir_visitor()
217 _mesa_hash_table_destroy(this->var_table
, NULL
);
218 _mesa_hash_table_destroy(this->overload_table
, NULL
);
222 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
225 ralloc_steal(mem_ctx
, this->deref_head
);
226 return this->deref_head
;
229 static nir_constant
*
230 constant_copy(ir_constant
*ir
, void *mem_ctx
)
235 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
237 unsigned total_elems
= ir
->type
->components();
239 switch (ir
->type
->base_type
) {
241 for (i
= 0; i
< total_elems
; i
++)
242 ret
->value
.u
[i
] = ir
->value
.u
[i
];
246 for (i
= 0; i
< total_elems
; i
++)
247 ret
->value
.i
[i
] = ir
->value
.i
[i
];
250 case GLSL_TYPE_FLOAT
:
251 for (i
= 0; i
< total_elems
; i
++)
252 ret
->value
.f
[i
] = ir
->value
.f
[i
];
256 for (i
= 0; i
< total_elems
; i
++)
257 ret
->value
.b
[i
] = ir
->value
.b
[i
];
260 case GLSL_TYPE_STRUCT
:
261 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
264 foreach_in_list(ir_constant
, field
, &ir
->components
) {
265 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
270 case GLSL_TYPE_ARRAY
:
271 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
274 for (i
= 0; i
< ir
->type
->length
; i
++)
275 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
279 unreachable("not reached");
286 nir_visitor::visit(ir_variable
*ir
)
288 nir_variable
*var
= ralloc(shader
, nir_variable
);
289 var
->type
= ir
->type
;
290 var
->name
= ralloc_strdup(var
, ir
->name
);
292 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
293 unsigned size
= ir
->get_interface_type()->length
;
294 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
295 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
296 size
* sizeof(unsigned));
298 var
->max_ifc_array_access
= NULL
;
301 var
->data
.read_only
= ir
->data
.read_only
;
302 var
->data
.centroid
= ir
->data
.centroid
;
303 var
->data
.sample
= ir
->data
.sample
;
304 var
->data
.patch
= ir
->data
.patch
;
305 var
->data
.invariant
= ir
->data
.invariant
;
306 var
->data
.location
= ir
->data
.location
;
308 switch(ir
->data
.mode
) {
310 case ir_var_temporary
:
312 var
->data
.mode
= nir_var_global
;
314 var
->data
.mode
= nir_var_local
;
317 case ir_var_function_in
:
318 case ir_var_function_out
:
319 case ir_var_function_inout
:
320 case ir_var_const_in
:
321 var
->data
.mode
= nir_var_local
;
324 case ir_var_shader_in
:
325 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
326 ir
->data
.location
== VARYING_SLOT_FACE
) {
327 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
328 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
329 var
->data
.mode
= nir_var_system_value
;
330 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
331 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
332 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
333 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
334 var
->data
.mode
= nir_var_system_value
;
336 var
->data
.mode
= nir_var_shader_in
;
340 case ir_var_shader_out
:
341 var
->data
.mode
= nir_var_shader_out
;
345 var
->data
.mode
= nir_var_uniform
;
348 case ir_var_shader_storage
:
349 var
->data
.mode
= nir_var_shader_storage
;
352 case ir_var_system_value
:
353 var
->data
.mode
= nir_var_system_value
;
357 unreachable("not reached");
360 var
->data
.interpolation
= ir
->data
.interpolation
;
361 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
362 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
363 var
->data
.explicit_location
= ir
->data
.explicit_location
;
364 var
->data
.explicit_index
= ir
->data
.explicit_index
;
365 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
366 var
->data
.has_initializer
= ir
->data
.has_initializer
;
367 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
368 var
->data
.location_frac
= ir
->data
.location_frac
;
369 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
370 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
372 switch (ir
->data
.depth_layout
) {
373 case ir_depth_layout_none
:
374 var
->data
.depth_layout
= nir_depth_layout_none
;
376 case ir_depth_layout_any
:
377 var
->data
.depth_layout
= nir_depth_layout_any
;
379 case ir_depth_layout_greater
:
380 var
->data
.depth_layout
= nir_depth_layout_greater
;
382 case ir_depth_layout_less
:
383 var
->data
.depth_layout
= nir_depth_layout_less
;
385 case ir_depth_layout_unchanged
:
386 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
389 unreachable("not reached");
392 var
->data
.index
= ir
->data
.index
;
393 var
->data
.binding
= ir
->data
.binding
;
394 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
395 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
396 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
397 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
398 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
399 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
400 var
->data
.image
.format
= ir
->data
.image_format
;
401 var
->data
.max_array_access
= ir
->data
.max_array_access
;
403 var
->num_state_slots
= ir
->get_num_state_slots();
404 if (var
->num_state_slots
> 0) {
405 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
406 var
->num_state_slots
);
408 ir_state_slot
*state_slots
= ir
->get_state_slots();
409 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
410 for (unsigned j
= 0; j
< 5; j
++)
411 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
412 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
415 var
->state_slots
= NULL
;
418 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
420 var
->interface_type
= ir
->get_interface_type();
422 if (var
->data
.mode
== nir_var_local
)
423 nir_function_impl_add_variable(impl
, var
);
425 nir_shader_add_variable(shader
, var
);
427 _mesa_hash_table_insert(var_table
, ir
, var
);
432 nir_function_visitor::visit_enter(ir_function
*ir
)
434 visitor
->create_function(ir
);
435 return visit_continue_with_parent
;
440 nir_visitor::create_function(ir_function
*ir
)
442 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
443 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
444 create_overload(sig
, func
);
451 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
453 if (ir
->is_intrinsic
)
456 nir_function_overload
*overload
= nir_function_overload_create(function
);
458 unsigned num_params
= ir
->parameters
.length();
459 overload
->num_params
= num_params
;
460 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
463 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
464 switch (param
->data
.mode
) {
465 case ir_var_function_in
:
466 overload
->params
[i
].param_type
= nir_parameter_in
;
469 case ir_var_function_out
:
470 overload
->params
[i
].param_type
= nir_parameter_out
;
473 case ir_var_function_inout
:
474 overload
->params
[i
].param_type
= nir_parameter_inout
;
478 unreachable("not reached");
481 overload
->params
[i
].type
= param
->type
;
485 overload
->return_type
= ir
->return_type
;
487 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
491 nir_visitor::visit(ir_function
*ir
)
493 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
498 nir_visitor::visit(ir_function_signature
*ir
)
500 if (ir
->is_intrinsic
)
503 struct hash_entry
*entry
=
504 _mesa_hash_table_search(this->overload_table
, ir
);
507 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
509 if (ir
->is_defined
) {
510 nir_function_impl
*impl
= nir_function_impl_create(overload
);
513 unsigned num_params
= overload
->num_params
;
514 impl
->num_params
= num_params
;
515 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
517 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
519 impl
->params
[i
] = this->var
;
523 if (overload
->return_type
== glsl_type::void_type
) {
524 impl
->return_var
= NULL
;
526 impl
->return_var
= ralloc(this->shader
, nir_variable
);
527 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
529 impl
->return_var
->type
= overload
->return_type
;
532 this->is_global
= false;
534 nir_builder_init(&b
, impl
);
535 b
.cursor
= nir_after_cf_list(&impl
->body
);
536 visit_exec_list(&ir
->body
, this);
538 this->is_global
= true;
540 overload
->impl
= NULL
;
545 nir_visitor::visit(ir_loop
*ir
)
547 nir_loop
*loop
= nir_loop_create(this->shader
);
548 nir_builder_cf_insert(&b
, &loop
->cf_node
);
550 b
.cursor
= nir_after_cf_list(&loop
->body
);
551 visit_exec_list(&ir
->body_instructions
, this);
552 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
556 nir_visitor::visit(ir_if
*ir
)
559 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
561 nir_if
*if_stmt
= nir_if_create(this->shader
);
562 if_stmt
->condition
= condition
;
563 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
565 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
566 visit_exec_list(&ir
->then_instructions
, this);
568 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
569 visit_exec_list(&ir
->else_instructions
, this);
571 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
575 nir_visitor::visit(ir_discard
*ir
)
578 * discards aren't treated as control flow, because before we lower them
579 * they can appear anywhere in the shader and the stuff after them may still
580 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
581 * discards will be immediately followed by a return.
584 nir_intrinsic_instr
*discard
;
586 discard
= nir_intrinsic_instr_create(this->shader
,
587 nir_intrinsic_discard_if
);
589 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
591 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
594 nir_builder_instr_insert(&b
, &discard
->instr
);
598 nir_visitor::visit(ir_emit_vertex
*ir
)
600 nir_intrinsic_instr
*instr
=
601 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
602 instr
->const_index
[0] = ir
->stream_id();
603 nir_builder_instr_insert(&b
, &instr
->instr
);
607 nir_visitor::visit(ir_end_primitive
*ir
)
609 nir_intrinsic_instr
*instr
=
610 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
611 instr
->const_index
[0] = ir
->stream_id();
612 nir_builder_instr_insert(&b
, &instr
->instr
);
616 nir_visitor::visit(ir_loop_jump
*ir
)
620 case ir_loop_jump::jump_break
:
621 type
= nir_jump_break
;
623 case ir_loop_jump::jump_continue
:
624 type
= nir_jump_continue
;
627 unreachable("not reached");
630 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
631 nir_builder_instr_insert(&b
, &instr
->instr
);
635 nir_visitor::visit(ir_return
*ir
)
637 if (ir
->value
!= NULL
) {
638 nir_intrinsic_instr
*copy
=
639 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
641 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
642 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
645 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
646 nir_builder_instr_insert(&b
, &instr
->instr
);
650 nir_visitor::visit(ir_call
*ir
)
652 if (ir
->callee
->is_intrinsic
) {
654 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
655 op
= nir_intrinsic_atomic_counter_read_var
;
656 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
657 op
= nir_intrinsic_atomic_counter_inc_var
;
658 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
659 op
= nir_intrinsic_atomic_counter_dec_var
;
660 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
661 op
= nir_intrinsic_image_load
;
662 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
663 op
= nir_intrinsic_image_store
;
664 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
665 op
= nir_intrinsic_image_atomic_add
;
666 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
667 op
= nir_intrinsic_image_atomic_min
;
668 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
669 op
= nir_intrinsic_image_atomic_max
;
670 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
671 op
= nir_intrinsic_image_atomic_and
;
672 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
673 op
= nir_intrinsic_image_atomic_or
;
674 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
675 op
= nir_intrinsic_image_atomic_xor
;
676 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
677 op
= nir_intrinsic_image_atomic_exchange
;
678 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
679 op
= nir_intrinsic_image_atomic_comp_swap
;
680 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
681 op
= nir_intrinsic_memory_barrier
;
682 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
683 op
= nir_intrinsic_image_size
;
684 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
685 op
= nir_intrinsic_image_samples
;
686 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
687 op
= nir_intrinsic_store_ssbo
;
688 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
689 op
= nir_intrinsic_load_ssbo
;
690 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
691 op
= nir_intrinsic_ssbo_atomic_add
;
692 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
693 op
= nir_intrinsic_ssbo_atomic_and
;
694 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
695 op
= nir_intrinsic_ssbo_atomic_or
;
696 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
697 op
= nir_intrinsic_ssbo_atomic_xor
;
698 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
699 assert(ir
->return_deref
);
700 if (ir
->return_deref
->type
== glsl_type::int_type
)
701 op
= nir_intrinsic_ssbo_atomic_imin
;
702 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
703 op
= nir_intrinsic_ssbo_atomic_umin
;
705 unreachable("Invalid type");
706 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
707 assert(ir
->return_deref
);
708 if (ir
->return_deref
->type
== glsl_type::int_type
)
709 op
= nir_intrinsic_ssbo_atomic_imax
;
710 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
711 op
= nir_intrinsic_ssbo_atomic_umax
;
713 unreachable("Invalid type");
714 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
715 op
= nir_intrinsic_ssbo_atomic_exchange
;
716 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
717 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
718 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
719 op
= nir_intrinsic_shader_clock
;
720 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
721 op
= nir_intrinsic_group_memory_barrier
;
722 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
723 op
= nir_intrinsic_memory_barrier_atomic_counter
;
724 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
725 op
= nir_intrinsic_memory_barrier_buffer
;
726 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
727 op
= nir_intrinsic_memory_barrier_image
;
728 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
729 op
= nir_intrinsic_memory_barrier_shared
;
731 unreachable("not reached");
734 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
735 nir_dest
*dest
= &instr
->dest
;
738 case nir_intrinsic_atomic_counter_read_var
:
739 case nir_intrinsic_atomic_counter_inc_var
:
740 case nir_intrinsic_atomic_counter_dec_var
: {
741 ir_dereference
*param
=
742 (ir_dereference
*) ir
->actual_parameters
.get_head();
743 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
744 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
745 nir_builder_instr_insert(&b
, &instr
->instr
);
748 case nir_intrinsic_image_load
:
749 case nir_intrinsic_image_store
:
750 case nir_intrinsic_image_atomic_add
:
751 case nir_intrinsic_image_atomic_min
:
752 case nir_intrinsic_image_atomic_max
:
753 case nir_intrinsic_image_atomic_and
:
754 case nir_intrinsic_image_atomic_or
:
755 case nir_intrinsic_image_atomic_xor
:
756 case nir_intrinsic_image_atomic_exchange
:
757 case nir_intrinsic_image_atomic_comp_swap
:
758 case nir_intrinsic_image_samples
:
759 case nir_intrinsic_image_size
: {
760 nir_ssa_undef_instr
*instr_undef
=
761 nir_ssa_undef_instr_create(shader
, 1);
762 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
764 /* Set the image variable dereference. */
765 exec_node
*param
= ir
->actual_parameters
.get_head();
766 ir_dereference
*image
= (ir_dereference
*)param
;
767 const glsl_type
*type
=
768 image
->variable_referenced()->type
->without_array();
770 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
771 param
= param
->get_next();
773 /* Set the intrinsic destination. */
774 if (ir
->return_deref
) {
775 const nir_intrinsic_info
*info
=
776 &nir_intrinsic_infos
[instr
->intrinsic
];
777 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
778 info
->dest_components
, NULL
);
781 if (op
== nir_intrinsic_image_size
||
782 op
== nir_intrinsic_image_samples
) {
783 nir_builder_instr_insert(&b
, &instr
->instr
);
787 /* Set the address argument, extending the coordinate vector to four
790 nir_ssa_def
*src_addr
=
791 evaluate_rvalue((ir_dereference
*)param
);
792 nir_ssa_def
*srcs
[4];
794 for (int i
= 0; i
< 4; i
++) {
795 if (i
< type
->coordinate_components())
796 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
798 srcs
[i
] = &instr_undef
->def
;
801 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
802 param
= param
->get_next();
804 /* Set the sample argument, which is undefined for single-sample
807 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
809 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
810 param
= param
->get_next();
812 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
815 /* Set the intrinsic parameters. */
816 if (!param
->is_tail_sentinel()) {
818 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
819 param
= param
->get_next();
822 if (!param
->is_tail_sentinel()) {
824 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
825 param
= param
->get_next();
827 nir_builder_instr_insert(&b
, &instr
->instr
);
830 case nir_intrinsic_memory_barrier
:
831 case nir_intrinsic_group_memory_barrier
:
832 case nir_intrinsic_memory_barrier_atomic_counter
:
833 case nir_intrinsic_memory_barrier_buffer
:
834 case nir_intrinsic_memory_barrier_image
:
835 case nir_intrinsic_memory_barrier_shared
:
836 nir_builder_instr_insert(&b
, &instr
->instr
);
838 case nir_intrinsic_shader_clock
:
839 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
840 nir_builder_instr_insert(&b
, &instr
->instr
);
842 case nir_intrinsic_store_ssbo
: {
843 exec_node
*param
= ir
->actual_parameters
.get_head();
844 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
846 param
= param
->get_next();
847 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
849 param
= param
->get_next();
850 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
852 param
= param
->get_next();
853 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
856 /* Check if we need the indirect version */
857 ir_constant
*const_offset
= offset
->as_constant();
859 op
= nir_intrinsic_store_ssbo_indirect
;
861 instr
= nir_intrinsic_instr_create(shader
, op
);
862 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
863 instr
->const_index
[0] = 0;
865 instr
->const_index
[0] = const_offset
->value
.u
[0];
868 instr
->const_index
[1] = write_mask
->value
.u
[0];
870 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
871 instr
->num_components
= val
->type
->vector_elements
;
873 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
874 nir_builder_instr_insert(&b
, &instr
->instr
);
877 case nir_intrinsic_load_ssbo
: {
878 exec_node
*param
= ir
->actual_parameters
.get_head();
879 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
881 param
= param
->get_next();
882 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
884 /* Check if we need the indirect version */
885 ir_constant
*const_offset
= offset
->as_constant();
887 op
= nir_intrinsic_load_ssbo_indirect
;
889 instr
= nir_intrinsic_instr_create(shader
, op
);
890 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
891 instr
->const_index
[0] = 0;
894 instr
->const_index
[0] = const_offset
->value
.u
[0];
897 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
899 const glsl_type
*type
= ir
->return_deref
->var
->type
;
900 instr
->num_components
= type
->vector_elements
;
902 /* Setup destination register */
903 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
904 type
->vector_elements
, NULL
);
906 /* Insert the created nir instruction now since in the case of boolean
907 * result we will need to emit another instruction after it
909 nir_builder_instr_insert(&b
, &instr
->instr
);
912 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
913 * consider a true boolean to be ~0. Fix this up with a != 0
916 if (type
->base_type
== GLSL_TYPE_BOOL
) {
917 nir_alu_instr
*load_ssbo_compare
=
918 nir_alu_instr_create(shader
, nir_op_ine
);
919 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
920 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
921 load_ssbo_compare
->src
[1].src
=
922 nir_src_for_ssa(nir_imm_int(&b
, 0));
923 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
924 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
925 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
926 &load_ssbo_compare
->dest
.dest
,
927 type
->vector_elements
, NULL
);
928 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
929 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
930 dest
= &load_ssbo_compare
->dest
.dest
;
934 case nir_intrinsic_ssbo_atomic_add
:
935 case nir_intrinsic_ssbo_atomic_imin
:
936 case nir_intrinsic_ssbo_atomic_umin
:
937 case nir_intrinsic_ssbo_atomic_imax
:
938 case nir_intrinsic_ssbo_atomic_umax
:
939 case nir_intrinsic_ssbo_atomic_and
:
940 case nir_intrinsic_ssbo_atomic_or
:
941 case nir_intrinsic_ssbo_atomic_xor
:
942 case nir_intrinsic_ssbo_atomic_exchange
:
943 case nir_intrinsic_ssbo_atomic_comp_swap
: {
944 int param_count
= ir
->actual_parameters
.length();
945 assert(param_count
== 3 || param_count
== 4);
948 exec_node
*param
= ir
->actual_parameters
.get_head();
949 ir_instruction
*inst
= (ir_instruction
*) param
;
950 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
953 param
= param
->get_next();
954 inst
= (ir_instruction
*) param
;
955 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
957 /* data1 parameter (this is always present) */
958 param
= param
->get_next();
959 inst
= (ir_instruction
*) param
;
960 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
962 /* data2 parameter (only with atomic_comp_swap) */
963 if (param_count
== 4) {
964 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
965 param
= param
->get_next();
966 inst
= (ir_instruction
*) param
;
967 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
971 assert(ir
->return_deref
);
972 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
973 ir
->return_deref
->type
->vector_elements
, NULL
);
974 nir_builder_instr_insert(&b
, &instr
->instr
);
978 unreachable("not reached");
981 if (ir
->return_deref
) {
982 nir_intrinsic_instr
*store_instr
=
983 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
984 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
986 store_instr
->variables
[0] =
987 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
988 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
990 nir_builder_instr_insert(&b
, &store_instr
->instr
);
996 struct hash_entry
*entry
=
997 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
999 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
1001 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1004 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1005 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1009 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1010 nir_builder_instr_insert(&b
, &instr
->instr
);
1014 nir_visitor::visit(ir_assignment
*ir
)
1016 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1018 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1019 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1020 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1021 nir_intrinsic_instr
*copy
=
1022 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1024 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1025 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1027 if (ir
->condition
) {
1028 nir_if
*if_stmt
= nir_if_create(this->shader
);
1029 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1030 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1031 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1032 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1034 nir_builder_instr_insert(&b
, ©
->instr
);
1039 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1041 ir
->lhs
->accept(this);
1042 nir_deref_var
*lhs_deref
= this->deref_head
;
1043 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1045 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1047 * We have no good way to update only part of a variable, so just load
1048 * the LHS and do a vec operation to combine the old with the new, and
1050 * back into the LHS. Copy propagation should get rid of the mess.
1053 nir_intrinsic_instr
*load
=
1054 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1055 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1056 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1057 load
->variables
[0] = lhs_deref
;
1058 ralloc_steal(load
, load
->variables
[0]);
1059 nir_builder_instr_insert(&b
, &load
->instr
);
1061 nir_ssa_def
*srcs
[4];
1063 unsigned component
= 0;
1064 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1065 if (ir
->write_mask
& (1 << i
)) {
1066 /* GLSL IR will give us the input to the write-masked assignment
1067 * in a single packed vector. So, for example, if the
1068 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1069 * and z -> w and get the y component from the load.
1071 srcs
[i
] = nir_channel(&b
, src
, component
++);
1073 srcs
[i
] = nir_channel(&b
, &load
->dest
.ssa
, i
);
1077 src
= nir_vec(&b
, srcs
, ir
->lhs
->type
->vector_elements
);
1080 nir_intrinsic_instr
*store
=
1081 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1082 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1083 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1084 store
->variables
[0] = nir_deref_as_var(store_deref
);
1085 store
->src
[0] = nir_src_for_ssa(src
);
1087 if (ir
->condition
) {
1088 nir_if
*if_stmt
= nir_if_create(this->shader
);
1089 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1090 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1091 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1092 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1094 nir_builder_instr_insert(&b
, &store
->instr
);
1099 * Given an instruction, returns a pointer to its destination or NULL if there
1100 * is no destination.
1102 * Note that this only handles instructions we generate at this level.
1105 get_instr_dest(nir_instr
*instr
)
1107 nir_alu_instr
*alu_instr
;
1108 nir_intrinsic_instr
*intrinsic_instr
;
1109 nir_tex_instr
*tex_instr
;
1111 switch (instr
->type
) {
1112 case nir_instr_type_alu
:
1113 alu_instr
= nir_instr_as_alu(instr
);
1114 return &alu_instr
->dest
.dest
;
1116 case nir_instr_type_intrinsic
:
1117 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1118 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1119 return &intrinsic_instr
->dest
;
1123 case nir_instr_type_tex
:
1124 tex_instr
= nir_instr_as_tex(instr
);
1125 return &tex_instr
->dest
;
1128 unreachable("not reached");
1135 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1137 nir_dest
*dest
= get_instr_dest(instr
);
1140 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1142 nir_builder_instr_insert(&b
, instr
);
1145 assert(dest
->is_ssa
);
1146 this->result
= &dest
->ssa
;
1151 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1154 if (ir
->as_dereference() || ir
->as_constant()) {
1156 * A dereference is being used on the right hand side, which means we
1157 * must emit a variable load.
1160 nir_intrinsic_instr
*load_instr
=
1161 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1162 load_instr
->num_components
= ir
->type
->vector_elements
;
1163 load_instr
->variables
[0] = this->deref_head
;
1164 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1165 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1168 return this->result
;
1172 nir_visitor::visit(ir_expression
*ir
)
1174 /* Some special cases */
1175 switch (ir
->operation
) {
1176 case ir_binop_ubo_load
: {
1177 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1179 nir_intrinsic_op op
;
1181 op
= nir_intrinsic_load_ubo
;
1183 op
= nir_intrinsic_load_ubo_indirect
;
1185 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1186 load
->num_components
= ir
->type
->vector_elements
;
1187 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1188 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1190 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1191 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1194 * In UBO's, a true boolean value is any non-zero value, but we consider
1195 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1198 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1199 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1204 case ir_unop_interpolate_at_centroid
:
1205 case ir_binop_interpolate_at_offset
:
1206 case ir_binop_interpolate_at_sample
: {
1207 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1208 ir_swizzle
*swizzle
= NULL
;
1210 /* the api does not allow a swizzle here, but the varying packing code
1211 * may have pushed one into here.
1213 swizzle
= ir
->operands
[0]->as_swizzle();
1215 deref
= swizzle
->val
->as_dereference();
1219 deref
->accept(this);
1221 nir_intrinsic_op op
;
1222 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1223 switch (ir
->operation
) {
1224 case ir_unop_interpolate_at_centroid
:
1225 op
= nir_intrinsic_interp_var_at_centroid
;
1227 case ir_binop_interpolate_at_offset
:
1228 op
= nir_intrinsic_interp_var_at_offset
;
1230 case ir_binop_interpolate_at_sample
:
1231 op
= nir_intrinsic_interp_var_at_sample
;
1234 unreachable("Invalid interpolation intrinsic");
1237 /* This case can happen if the vertex shader does not write the
1238 * given varying. In this case, the linker will lower it to a
1239 * global variable. Since interpolating a variable makes no
1240 * sense, we'll just turn it into a load which will probably
1241 * eventually end up as an SSA definition.
1243 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1244 op
= nir_intrinsic_load_var
;
1247 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1248 intrin
->num_components
= deref
->type
->vector_elements
;
1249 intrin
->variables
[0] = this->deref_head
;
1250 ralloc_steal(intrin
, intrin
->variables
[0]);
1252 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1253 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1254 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1256 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1259 unsigned swiz
[4] = {
1260 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1263 result
= nir_swizzle(&b
, result
, swiz
,
1264 swizzle
->type
->vector_elements
, false);
1274 nir_ssa_def
*srcs
[4];
1275 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1276 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1278 glsl_base_type types
[4];
1279 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1281 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1283 types
[i
] = GLSL_TYPE_FLOAT
;
1285 glsl_base_type out_type
;
1287 out_type
= ir
->type
->base_type
;
1289 out_type
= GLSL_TYPE_FLOAT
;
1291 switch (ir
->operation
) {
1292 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1293 case ir_unop_logic_not
:
1294 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1297 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1298 : nir_ineg(&b
, srcs
[0]);
1301 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1302 : nir_iabs(&b
, srcs
[0]);
1304 case ir_unop_saturate
:
1305 assert(types
[0] == GLSL_TYPE_FLOAT
);
1306 result
= nir_fsat(&b
, srcs
[0]);
1309 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1310 : nir_isign(&b
, srcs
[0]);
1312 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1313 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1314 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1315 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1316 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1317 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1318 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1320 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1323 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1326 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1328 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1329 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1330 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1331 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1332 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1335 case ir_unop_bitcast_i2f
:
1336 case ir_unop_bitcast_f2i
:
1337 case ir_unop_bitcast_u2f
:
1338 case ir_unop_bitcast_f2u
:
1339 case ir_unop_subroutine_to_int
:
1341 result
= nir_imov(&b
, srcs
[0]);
1344 switch (ir
->operands
[0]->type
->vector_elements
) {
1346 result
= supports_ints
? nir_bany2(&b
, srcs
[0])
1347 : nir_fany2(&b
, srcs
[0]);
1350 result
= supports_ints
? nir_bany3(&b
, srcs
[0])
1351 : nir_fany3(&b
, srcs
[0]);
1354 result
= supports_ints
? nir_bany4(&b
, srcs
[0])
1355 : nir_fany4(&b
, srcs
[0]);
1358 unreachable("not reached");
1361 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1362 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1363 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1364 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1365 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1366 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1367 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1368 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1369 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1370 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1371 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1372 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1373 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1374 case ir_unop_pack_snorm_2x16
:
1375 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1377 case ir_unop_pack_snorm_4x8
:
1378 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1380 case ir_unop_pack_unorm_2x16
:
1381 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1383 case ir_unop_pack_unorm_4x8
:
1384 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1386 case ir_unop_pack_half_2x16
:
1387 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1389 case ir_unop_unpack_snorm_2x16
:
1390 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1392 case ir_unop_unpack_snorm_4x8
:
1393 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1395 case ir_unop_unpack_unorm_2x16
:
1396 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1398 case ir_unop_unpack_unorm_4x8
:
1399 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1401 case ir_unop_unpack_half_2x16
:
1402 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1404 case ir_unop_unpack_half_2x16_split_x
:
1405 result
= nir_unpack_half_2x16_split_x(&b
, srcs
[0]);
1407 case ir_unop_unpack_half_2x16_split_y
:
1408 result
= nir_unpack_half_2x16_split_y(&b
, srcs
[0]);
1410 case ir_unop_bitfield_reverse
:
1411 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1413 case ir_unop_bit_count
:
1414 result
= nir_bit_count(&b
, srcs
[0]);
1416 case ir_unop_find_msb
:
1418 case GLSL_TYPE_UINT
:
1419 result
= nir_ufind_msb(&b
, srcs
[0]);
1422 result
= nir_ifind_msb(&b
, srcs
[0]);
1425 unreachable("Invalid type for findMSB()");
1428 case ir_unop_find_lsb
:
1429 result
= nir_find_lsb(&b
, srcs
[0]);
1433 switch (ir
->type
->vector_elements
) {
1435 switch (ir
->operands
[0]->type
->vector_elements
) {
1436 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1437 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1438 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1439 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1440 default: unreachable("not reached");
1444 switch (ir
->operands
[0]->type
->vector_elements
) {
1445 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1446 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1447 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1448 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1449 default: unreachable("not reached");
1453 switch (ir
->operands
[0]->type
->vector_elements
) {
1454 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1455 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1456 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1457 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1458 default: unreachable("not reached");
1462 switch (ir
->operands
[0]->type
->vector_elements
) {
1463 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1464 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1465 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1466 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1467 default: unreachable("not reached");
1471 unreachable("not reached");
1474 case ir_unop_get_buffer_size
: {
1475 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1477 nir_intrinsic_get_buffer_size
);
1478 load
->num_components
= ir
->type
->vector_elements
;
1479 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1480 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1485 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1486 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1489 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1490 : nir_isub(&b
, srcs
[0], srcs
[1]);
1493 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1494 : nir_imul(&b
, srcs
[0], srcs
[1]);
1497 if (out_type
== GLSL_TYPE_FLOAT
)
1498 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1499 else if (out_type
== GLSL_TYPE_INT
)
1500 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1502 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1505 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1506 : nir_umod(&b
, srcs
[0], srcs
[1]);
1509 if (out_type
== GLSL_TYPE_FLOAT
)
1510 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1511 else if (out_type
== GLSL_TYPE_INT
)
1512 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1514 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1517 if (out_type
== GLSL_TYPE_FLOAT
)
1518 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1519 else if (out_type
== GLSL_TYPE_INT
)
1520 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1522 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1524 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1525 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1526 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1527 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1528 case ir_binop_logic_and
:
1529 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1530 : nir_fand(&b
, srcs
[0], srcs
[1]);
1532 case ir_binop_logic_or
:
1533 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1534 : nir_for(&b
, srcs
[0], srcs
[1]);
1536 case ir_binop_logic_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1537 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1538 : nir_for(&b
, srcs
[0], srcs
[1]);
1540 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1541 case ir_binop_rshift
:
1542 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1543 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1545 case ir_binop_imul_high
:
1546 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1547 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1549 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1550 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1552 if (supports_ints
) {
1553 if (types
[0] == GLSL_TYPE_FLOAT
)
1554 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1555 else if (types
[0] == GLSL_TYPE_INT
)
1556 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1558 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1560 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1563 case ir_binop_greater
:
1564 if (supports_ints
) {
1565 if (types
[0] == GLSL_TYPE_FLOAT
)
1566 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1567 else if (types
[0] == GLSL_TYPE_INT
)
1568 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1570 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1572 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1575 case ir_binop_lequal
:
1576 if (supports_ints
) {
1577 if (types
[0] == GLSL_TYPE_FLOAT
)
1578 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1579 else if (types
[0] == GLSL_TYPE_INT
)
1580 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1582 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1584 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1587 case ir_binop_gequal
:
1588 if (supports_ints
) {
1589 if (types
[0] == GLSL_TYPE_FLOAT
)
1590 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1591 else if (types
[0] == GLSL_TYPE_INT
)
1592 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1594 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1596 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1599 case ir_binop_equal
:
1600 if (supports_ints
) {
1601 if (types
[0] == GLSL_TYPE_FLOAT
)
1602 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1604 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1606 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1609 case ir_binop_nequal
:
1610 if (supports_ints
) {
1611 if (types
[0] == GLSL_TYPE_FLOAT
)
1612 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1614 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1616 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1619 case ir_binop_all_equal
:
1620 if (supports_ints
) {
1621 if (types
[0] == GLSL_TYPE_FLOAT
) {
1622 switch (ir
->operands
[0]->type
->vector_elements
) {
1623 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1624 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1625 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1626 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1628 unreachable("not reached");
1631 switch (ir
->operands
[0]->type
->vector_elements
) {
1632 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1633 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1634 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1635 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1637 unreachable("not reached");
1641 switch (ir
->operands
[0]->type
->vector_elements
) {
1642 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1643 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1644 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1645 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1647 unreachable("not reached");
1651 case ir_binop_any_nequal
:
1652 if (supports_ints
) {
1653 if (types
[0] == GLSL_TYPE_FLOAT
) {
1654 switch (ir
->operands
[0]->type
->vector_elements
) {
1655 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1656 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1657 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1658 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1660 unreachable("not reached");
1663 switch (ir
->operands
[0]->type
->vector_elements
) {
1664 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1665 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1666 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1667 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1669 unreachable("not reached");
1673 switch (ir
->operands
[0]->type
->vector_elements
) {
1674 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1675 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1676 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1677 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1679 unreachable("not reached");
1684 switch (ir
->operands
[0]->type
->vector_elements
) {
1685 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1686 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1687 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1689 unreachable("not reached");
1693 case ir_binop_pack_half_2x16_split
:
1694 result
= nir_pack_half_2x16_split(&b
, srcs
[0], srcs
[1]);
1696 case ir_binop_bfm
: result
= nir_bfm(&b
, srcs
[0], srcs
[1]); break;
1697 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1699 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1702 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1706 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1708 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1711 result
= nir_bfi(&b
, srcs
[0], srcs
[1], srcs
[2]);
1713 case ir_triop_bitfield_extract
:
1714 result
= (out_type
== GLSL_TYPE_INT
) ?
1715 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1716 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1718 case ir_quadop_bitfield_insert
:
1719 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1721 case ir_quadop_vector
:
1722 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1726 unreachable("not reached");
1731 nir_visitor::visit(ir_swizzle
*ir
)
1733 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1734 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1735 ir
->type
->vector_elements
, !supports_ints
);
1739 nir_visitor::visit(ir_texture
*ir
)
1746 num_srcs
= 1; /* coordinate */
1751 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1752 num_srcs
= 2; /* coordinate, bias/lod */
1756 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1762 if (ir
->lod_info
.lod
!= NULL
)
1763 num_srcs
= 2; /* coordinate, lod */
1765 num_srcs
= 1; /* coordinate */
1769 op
= nir_texop_txf_ms
;
1770 num_srcs
= 2; /* coordinate, sample_index */
1775 if (ir
->lod_info
.lod
!= NULL
)
1776 num_srcs
= 1; /* lod */
1783 num_srcs
= 1; /* coordinate */
1788 num_srcs
= 1; /* coordinate */
1791 case ir_query_levels
:
1792 op
= nir_texop_query_levels
;
1796 case ir_texture_samples
:
1797 op
= nir_texop_texture_samples
;
1802 unreachable("not reached");
1805 if (ir
->projector
!= NULL
)
1807 if (ir
->shadow_comparitor
!= NULL
)
1809 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1812 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1815 instr
->sampler_dim
=
1816 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1817 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1818 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1819 if (instr
->is_shadow
)
1820 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1821 switch (ir
->type
->base_type
) {
1822 case GLSL_TYPE_FLOAT
:
1823 instr
->dest_type
= nir_type_float
;
1826 instr
->dest_type
= nir_type_int
;
1828 case GLSL_TYPE_UINT
:
1829 instr
->dest_type
= nir_type_unsigned
;
1832 unreachable("not reached");
1835 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1837 unsigned src_number
= 0;
1839 if (ir
->coordinate
!= NULL
) {
1840 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1841 instr
->src
[src_number
].src
=
1842 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1843 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1847 if (ir
->projector
!= NULL
) {
1848 instr
->src
[src_number
].src
=
1849 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1850 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1854 if (ir
->shadow_comparitor
!= NULL
) {
1855 instr
->src
[src_number
].src
=
1856 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1857 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1861 if (ir
->offset
!= NULL
) {
1862 /* we don't support multiple offsets yet */
1863 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1865 ir_constant
*const_offset
= ir
->offset
->as_constant();
1866 if (const_offset
!= NULL
) {
1867 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1868 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1870 instr
->src
[src_number
].src
=
1871 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1872 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1879 instr
->src
[src_number
].src
=
1880 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1881 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1888 if (ir
->lod_info
.lod
!= NULL
) {
1889 instr
->src
[src_number
].src
=
1890 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1891 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1897 instr
->src
[src_number
].src
=
1898 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1899 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1901 instr
->src
[src_number
].src
=
1902 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1903 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1908 instr
->src
[src_number
].src
=
1909 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1910 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1915 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1922 assert(src_number
== num_srcs
);
1924 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1928 nir_visitor::visit(ir_constant
*ir
)
1931 * We don't know if this variable is an an array or struct that gets
1932 * dereferenced, so do the safe thing an make it a variable with a
1933 * constant initializer and return a dereference.
1937 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1938 var
->data
.read_only
= true;
1939 var
->constant_initializer
= constant_copy(ir
, var
);
1941 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1942 this->deref_tail
= &this->deref_head
->deref
;
1946 nir_visitor::visit(ir_dereference_variable
*ir
)
1948 struct hash_entry
*entry
=
1949 _mesa_hash_table_search(this->var_table
, ir
->var
);
1951 nir_variable
*var
= (nir_variable
*) entry
->data
;
1953 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1954 this->deref_head
= deref
;
1955 this->deref_tail
= &deref
->deref
;
1959 nir_visitor::visit(ir_dereference_record
*ir
)
1961 ir
->record
->accept(this);
1963 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1964 assert(field_index
>= 0);
1966 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1967 deref
->deref
.type
= ir
->type
;
1968 this->deref_tail
->child
= &deref
->deref
;
1969 this->deref_tail
= &deref
->deref
;
1973 nir_visitor::visit(ir_dereference_array
*ir
)
1975 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1976 deref
->deref
.type
= ir
->type
;
1978 ir_constant
*const_index
= ir
->array_index
->as_constant();
1979 if (const_index
!= NULL
) {
1980 deref
->deref_array_type
= nir_deref_array_type_direct
;
1981 deref
->base_offset
= const_index
->value
.u
[0];
1983 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1985 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
1988 ir
->array
->accept(this);
1990 this->deref_tail
->child
= &deref
->deref
;
1991 ralloc_steal(this->deref_tail
, deref
);
1992 this->deref_tail
= &deref
->deref
;
1996 nir_visitor::visit(ir_barrier
*ir
)
1998 nir_intrinsic_instr
*instr
=
1999 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2000 nir_builder_instr_insert(&b
, &instr
->instr
);