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
, gl_shader
*sh
);
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
);
91 nir_function_impl
*impl
;
93 nir_ssa_def
*result
; /* result of the expression tree last visited */
95 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
97 /* the head of the dereference chain we're creating */
98 nir_deref_var
*deref_head
;
99 /* the tail of the dereference chain we're creating */
100 nir_deref
*deref_tail
;
102 nir_variable
*var
; /* variable created by ir_variable visitor */
104 /* whether the IR we're operating on is per-function or global */
107 /* map of ir_variable -> nir_variable */
108 struct hash_table
*var_table
;
110 /* map of ir_function_signature -> nir_function_overload */
111 struct hash_table
*overload_table
;
115 * This visitor runs before the main visitor, calling create_function() for
116 * each function so that the main visitor can resolve forward references in
120 class nir_function_visitor
: public ir_hierarchical_visitor
123 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
126 virtual ir_visitor_status
visit_enter(ir_function
*);
129 nir_visitor
*visitor
;
132 }; /* end of anonymous namespace */
135 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
136 gl_shader_stage stage
,
137 const nir_shader_compiler_options
*options
)
139 struct gl_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
141 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
143 nir_visitor
v1(shader
, sh
);
144 nir_function_visitor
v2(&v1
);
146 visit_exec_list(sh
->ir
, &v1
);
148 nir_lower_outputs_to_temporaries(shader
);
150 /* TODO: Use _mesa_fls instead */
151 unsigned num_textures
= 0;
152 for (unsigned i
= 0; i
< 8 * sizeof(sh
->Program
->SamplersUsed
); i
++)
153 if (sh
->Program
->SamplersUsed
& (1 << i
))
156 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
157 if (shader_prog
->Label
)
158 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
159 shader
->info
.num_textures
= num_textures
;
160 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
161 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
162 shader
->info
.num_ssbos
= sh
->NumShaderStorageBlocks
;
163 shader
->info
.num_images
= sh
->NumImages
;
164 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
165 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
166 shader
->info
.patch_inputs_read
= sh
->Program
->PatchInputsRead
;
167 shader
->info
.patch_outputs_written
= sh
->Program
->PatchOutputsWritten
;
168 shader
->info
.system_values_read
= sh
->Program
->SystemValuesRead
;
169 shader
->info
.uses_texture_gather
= sh
->Program
->UsesGather
;
170 shader
->info
.uses_clip_distance_out
=
171 sh
->Program
->ClipDistanceArraySize
!= 0;
172 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
173 shader
->info
.has_transform_feedback_varyings
=
174 shader_prog
->TransformFeedback
.NumVarying
> 0;
177 case MESA_SHADER_GEOMETRY
:
178 shader
->info
.gs
.vertices_in
= shader_prog
->Geom
.VerticesIn
;
179 shader
->info
.gs
.output_primitive
= sh
->Geom
.OutputType
;
180 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
181 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
182 shader
->info
.gs
.uses_end_primitive
= shader_prog
->Geom
.UsesEndPrimitive
;
183 shader
->info
.gs
.uses_streams
= shader_prog
->Geom
.UsesStreams
;
186 case MESA_SHADER_FRAGMENT
: {
187 struct gl_fragment_program
*fp
=
188 (struct gl_fragment_program
*)sh
->Program
;
190 shader
->info
.fs
.uses_discard
= fp
->UsesKill
;
191 shader
->info
.fs
.early_fragment_tests
= sh
->EarlyFragmentTests
;
192 shader
->info
.fs
.depth_layout
= fp
->FragDepthLayout
;
196 case MESA_SHADER_COMPUTE
: {
197 struct gl_compute_program
*cp
= (struct gl_compute_program
*)sh
->Program
;
198 shader
->info
.cs
.local_size
[0] = cp
->LocalSize
[0];
199 shader
->info
.cs
.local_size
[1] = cp
->LocalSize
[1];
200 shader
->info
.cs
.local_size
[2] = cp
->LocalSize
[2];
205 break; /* No stage-specific info */
211 nir_visitor::nir_visitor(nir_shader
*shader
, gl_shader
*sh
)
213 this->supports_ints
= shader
->options
->native_integers
;
214 this->shader
= shader
;
216 this->is_global
= true;
217 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
218 _mesa_key_pointer_equal
);
219 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
220 _mesa_key_pointer_equal
);
223 nir_visitor::~nir_visitor()
225 _mesa_hash_table_destroy(this->var_table
, NULL
);
226 _mesa_hash_table_destroy(this->overload_table
, NULL
);
230 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
233 ralloc_steal(mem_ctx
, this->deref_head
);
234 return this->deref_head
;
237 static nir_constant
*
238 constant_copy(ir_constant
*ir
, void *mem_ctx
)
243 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
245 unsigned total_elems
= ir
->type
->components();
247 switch (ir
->type
->base_type
) {
249 for (i
= 0; i
< total_elems
; i
++)
250 ret
->value
.u
[i
] = ir
->value
.u
[i
];
254 for (i
= 0; i
< total_elems
; i
++)
255 ret
->value
.i
[i
] = ir
->value
.i
[i
];
258 case GLSL_TYPE_FLOAT
:
259 for (i
= 0; i
< total_elems
; i
++)
260 ret
->value
.f
[i
] = ir
->value
.f
[i
];
264 for (i
= 0; i
< total_elems
; i
++)
265 ret
->value
.b
[i
] = ir
->value
.b
[i
];
268 case GLSL_TYPE_STRUCT
:
269 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
272 foreach_in_list(ir_constant
, field
, &ir
->components
) {
273 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
278 case GLSL_TYPE_ARRAY
:
279 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
282 for (i
= 0; i
< ir
->type
->length
; i
++)
283 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
287 unreachable("not reached");
294 nir_visitor::visit(ir_variable
*ir
)
296 nir_variable
*var
= ralloc(shader
, nir_variable
);
297 var
->type
= ir
->type
;
298 var
->name
= ralloc_strdup(var
, ir
->name
);
300 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
301 unsigned size
= ir
->get_interface_type()->length
;
302 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
303 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
304 size
* sizeof(unsigned));
306 var
->max_ifc_array_access
= NULL
;
309 var
->data
.read_only
= ir
->data
.read_only
;
310 var
->data
.centroid
= ir
->data
.centroid
;
311 var
->data
.sample
= ir
->data
.sample
;
312 var
->data
.patch
= ir
->data
.patch
;
313 var
->data
.invariant
= ir
->data
.invariant
;
314 var
->data
.location
= ir
->data
.location
;
316 switch(ir
->data
.mode
) {
318 case ir_var_temporary
:
320 var
->data
.mode
= nir_var_global
;
322 var
->data
.mode
= nir_var_local
;
325 case ir_var_function_in
:
326 case ir_var_function_out
:
327 case ir_var_function_inout
:
328 case ir_var_const_in
:
329 var
->data
.mode
= nir_var_local
;
332 case ir_var_shader_in
:
333 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
334 ir
->data
.location
== VARYING_SLOT_FACE
) {
335 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
336 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
337 var
->data
.mode
= nir_var_system_value
;
338 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
339 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
340 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
341 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
342 var
->data
.mode
= nir_var_system_value
;
344 var
->data
.mode
= nir_var_shader_in
;
348 case ir_var_shader_out
:
349 var
->data
.mode
= nir_var_shader_out
;
353 var
->data
.mode
= nir_var_uniform
;
356 case ir_var_shader_storage
:
357 var
->data
.mode
= nir_var_shader_storage
;
360 case ir_var_system_value
:
361 var
->data
.mode
= nir_var_system_value
;
365 unreachable("not reached");
368 var
->data
.interpolation
= ir
->data
.interpolation
;
369 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
370 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
371 var
->data
.explicit_location
= ir
->data
.explicit_location
;
372 var
->data
.explicit_index
= ir
->data
.explicit_index
;
373 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
374 var
->data
.has_initializer
= ir
->data
.has_initializer
;
375 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
376 var
->data
.location_frac
= ir
->data
.location_frac
;
377 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
378 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
380 switch (ir
->data
.depth_layout
) {
381 case ir_depth_layout_none
:
382 var
->data
.depth_layout
= nir_depth_layout_none
;
384 case ir_depth_layout_any
:
385 var
->data
.depth_layout
= nir_depth_layout_any
;
387 case ir_depth_layout_greater
:
388 var
->data
.depth_layout
= nir_depth_layout_greater
;
390 case ir_depth_layout_less
:
391 var
->data
.depth_layout
= nir_depth_layout_less
;
393 case ir_depth_layout_unchanged
:
394 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
397 unreachable("not reached");
400 var
->data
.index
= ir
->data
.index
;
401 var
->data
.descriptor_set
= 0;
402 var
->data
.binding
= ir
->data
.binding
;
403 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
404 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
405 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
406 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
407 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
408 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
409 var
->data
.image
.format
= ir
->data
.image_format
;
410 var
->data
.max_array_access
= ir
->data
.max_array_access
;
412 var
->num_state_slots
= ir
->get_num_state_slots();
413 if (var
->num_state_slots
> 0) {
414 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
415 var
->num_state_slots
);
417 ir_state_slot
*state_slots
= ir
->get_state_slots();
418 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
419 for (unsigned j
= 0; j
< 5; j
++)
420 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
421 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
424 var
->state_slots
= NULL
;
427 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
429 var
->interface_type
= ir
->get_interface_type();
431 if (var
->data
.mode
== nir_var_local
)
432 nir_function_impl_add_variable(impl
, var
);
434 nir_shader_add_variable(shader
, var
);
436 _mesa_hash_table_insert(var_table
, ir
, var
);
441 nir_function_visitor::visit_enter(ir_function
*ir
)
443 visitor
->create_function(ir
);
444 return visit_continue_with_parent
;
449 nir_visitor::create_function(ir_function
*ir
)
451 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
452 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
453 create_overload(sig
, func
);
460 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
462 if (ir
->is_intrinsic
)
465 nir_function_overload
*overload
= nir_function_overload_create(function
);
467 unsigned num_params
= ir
->parameters
.length();
468 overload
->num_params
= num_params
;
469 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
472 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
473 switch (param
->data
.mode
) {
474 case ir_var_function_in
:
475 overload
->params
[i
].param_type
= nir_parameter_in
;
478 case ir_var_function_out
:
479 overload
->params
[i
].param_type
= nir_parameter_out
;
482 case ir_var_function_inout
:
483 overload
->params
[i
].param_type
= nir_parameter_inout
;
487 unreachable("not reached");
490 overload
->params
[i
].type
= param
->type
;
494 overload
->return_type
= ir
->return_type
;
496 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
500 nir_visitor::visit(ir_function
*ir
)
502 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
507 nir_visitor::visit(ir_function_signature
*ir
)
509 if (ir
->is_intrinsic
)
512 struct hash_entry
*entry
=
513 _mesa_hash_table_search(this->overload_table
, ir
);
516 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
518 if (ir
->is_defined
) {
519 nir_function_impl
*impl
= nir_function_impl_create(overload
);
522 unsigned num_params
= overload
->num_params
;
523 impl
->num_params
= num_params
;
524 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
526 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
528 impl
->params
[i
] = this->var
;
532 if (overload
->return_type
== glsl_type::void_type
) {
533 impl
->return_var
= NULL
;
535 impl
->return_var
= ralloc(this->shader
, nir_variable
);
536 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
538 impl
->return_var
->type
= overload
->return_type
;
541 this->is_global
= false;
543 nir_builder_init(&b
, impl
);
544 b
.cursor
= nir_after_cf_list(&impl
->body
);
545 visit_exec_list(&ir
->body
, this);
547 this->is_global
= true;
549 overload
->impl
= NULL
;
554 nir_visitor::visit(ir_loop
*ir
)
556 nir_loop
*loop
= nir_loop_create(this->shader
);
557 nir_builder_cf_insert(&b
, &loop
->cf_node
);
559 b
.cursor
= nir_after_cf_list(&loop
->body
);
560 visit_exec_list(&ir
->body_instructions
, this);
561 b
.cursor
= nir_after_cf_node(&loop
->cf_node
);
565 nir_visitor::visit(ir_if
*ir
)
568 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
570 nir_if
*if_stmt
= nir_if_create(this->shader
);
571 if_stmt
->condition
= condition
;
572 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
574 b
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
575 visit_exec_list(&ir
->then_instructions
, this);
577 b
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
578 visit_exec_list(&ir
->else_instructions
, this);
580 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
584 nir_visitor::visit(ir_discard
*ir
)
587 * discards aren't treated as control flow, because before we lower them
588 * they can appear anywhere in the shader and the stuff after them may still
589 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
590 * discards will be immediately followed by a return.
593 nir_intrinsic_instr
*discard
;
595 discard
= nir_intrinsic_instr_create(this->shader
,
596 nir_intrinsic_discard_if
);
598 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
600 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
603 nir_builder_instr_insert(&b
, &discard
->instr
);
607 nir_visitor::visit(ir_emit_vertex
*ir
)
609 nir_intrinsic_instr
*instr
=
610 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
611 instr
->const_index
[0] = ir
->stream_id();
612 nir_builder_instr_insert(&b
, &instr
->instr
);
616 nir_visitor::visit(ir_end_primitive
*ir
)
618 nir_intrinsic_instr
*instr
=
619 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
620 instr
->const_index
[0] = ir
->stream_id();
621 nir_builder_instr_insert(&b
, &instr
->instr
);
625 nir_visitor::visit(ir_loop_jump
*ir
)
629 case ir_loop_jump::jump_break
:
630 type
= nir_jump_break
;
632 case ir_loop_jump::jump_continue
:
633 type
= nir_jump_continue
;
636 unreachable("not reached");
639 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
640 nir_builder_instr_insert(&b
, &instr
->instr
);
644 nir_visitor::visit(ir_return
*ir
)
646 if (ir
->value
!= NULL
) {
647 nir_intrinsic_instr
*copy
=
648 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
650 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
651 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
654 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
655 nir_builder_instr_insert(&b
, &instr
->instr
);
659 nir_visitor::visit(ir_call
*ir
)
661 if (ir
->callee
->is_intrinsic
) {
663 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
664 op
= nir_intrinsic_atomic_counter_read_var
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
666 op
= nir_intrinsic_atomic_counter_inc_var
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
668 op
= nir_intrinsic_atomic_counter_dec_var
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
670 op
= nir_intrinsic_image_load
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
672 op
= nir_intrinsic_image_store
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
674 op
= nir_intrinsic_image_atomic_add
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
676 op
= nir_intrinsic_image_atomic_min
;
677 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
678 op
= nir_intrinsic_image_atomic_max
;
679 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
680 op
= nir_intrinsic_image_atomic_and
;
681 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
682 op
= nir_intrinsic_image_atomic_or
;
683 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
684 op
= nir_intrinsic_image_atomic_xor
;
685 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
686 op
= nir_intrinsic_image_atomic_exchange
;
687 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
688 op
= nir_intrinsic_image_atomic_comp_swap
;
689 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
690 op
= nir_intrinsic_memory_barrier
;
691 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
692 op
= nir_intrinsic_image_size
;
693 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
694 op
= nir_intrinsic_image_samples
;
695 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
696 op
= nir_intrinsic_store_ssbo
;
697 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
698 op
= nir_intrinsic_load_ssbo
;
699 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
700 op
= nir_intrinsic_ssbo_atomic_add
;
701 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
702 op
= nir_intrinsic_ssbo_atomic_and
;
703 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
704 op
= nir_intrinsic_ssbo_atomic_or
;
705 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
706 op
= nir_intrinsic_ssbo_atomic_xor
;
707 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
708 assert(ir
->return_deref
);
709 if (ir
->return_deref
->type
== glsl_type::int_type
)
710 op
= nir_intrinsic_ssbo_atomic_imin
;
711 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
712 op
= nir_intrinsic_ssbo_atomic_umin
;
714 unreachable("Invalid type");
715 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
716 assert(ir
->return_deref
);
717 if (ir
->return_deref
->type
== glsl_type::int_type
)
718 op
= nir_intrinsic_ssbo_atomic_imax
;
719 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
720 op
= nir_intrinsic_ssbo_atomic_umax
;
722 unreachable("Invalid type");
723 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
724 op
= nir_intrinsic_ssbo_atomic_exchange
;
725 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
726 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
727 } else if (strcmp(ir
->callee_name(), "__intrinsic_shader_clock") == 0) {
728 op
= nir_intrinsic_shader_clock
;
729 } else if (strcmp(ir
->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
730 op
= nir_intrinsic_group_memory_barrier
;
731 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
732 op
= nir_intrinsic_memory_barrier_atomic_counter
;
733 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
734 op
= nir_intrinsic_memory_barrier_buffer
;
735 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
736 op
= nir_intrinsic_memory_barrier_image
;
737 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
738 op
= nir_intrinsic_memory_barrier_shared
;
740 unreachable("not reached");
743 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
744 nir_dest
*dest
= &instr
->dest
;
747 case nir_intrinsic_atomic_counter_read_var
:
748 case nir_intrinsic_atomic_counter_inc_var
:
749 case nir_intrinsic_atomic_counter_dec_var
: {
750 ir_dereference
*param
=
751 (ir_dereference
*) ir
->actual_parameters
.get_head();
752 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
753 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
754 nir_builder_instr_insert(&b
, &instr
->instr
);
757 case nir_intrinsic_image_load
:
758 case nir_intrinsic_image_store
:
759 case nir_intrinsic_image_atomic_add
:
760 case nir_intrinsic_image_atomic_min
:
761 case nir_intrinsic_image_atomic_max
:
762 case nir_intrinsic_image_atomic_and
:
763 case nir_intrinsic_image_atomic_or
:
764 case nir_intrinsic_image_atomic_xor
:
765 case nir_intrinsic_image_atomic_exchange
:
766 case nir_intrinsic_image_atomic_comp_swap
:
767 case nir_intrinsic_image_samples
:
768 case nir_intrinsic_image_size
: {
769 nir_ssa_undef_instr
*instr_undef
=
770 nir_ssa_undef_instr_create(shader
, 1);
771 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
773 /* Set the image variable dereference. */
774 exec_node
*param
= ir
->actual_parameters
.get_head();
775 ir_dereference
*image
= (ir_dereference
*)param
;
776 const glsl_type
*type
=
777 image
->variable_referenced()->type
->without_array();
779 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
780 param
= param
->get_next();
782 /* Set the intrinsic destination. */
783 if (ir
->return_deref
) {
784 const nir_intrinsic_info
*info
=
785 &nir_intrinsic_infos
[instr
->intrinsic
];
786 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
787 info
->dest_components
, NULL
);
790 if (op
== nir_intrinsic_image_size
||
791 op
== nir_intrinsic_image_samples
) {
792 nir_builder_instr_insert(&b
, &instr
->instr
);
796 /* Set the address argument, extending the coordinate vector to four
799 nir_ssa_def
*src_addr
=
800 evaluate_rvalue((ir_dereference
*)param
);
801 nir_ssa_def
*srcs
[4];
803 for (int i
= 0; i
< 4; i
++) {
804 if (i
< type
->coordinate_components())
805 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
807 srcs
[i
] = &instr_undef
->def
;
810 instr
->src
[0] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
811 param
= param
->get_next();
813 /* Set the sample argument, which is undefined for single-sample
816 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
818 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
819 param
= param
->get_next();
821 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
824 /* Set the intrinsic parameters. */
825 if (!param
->is_tail_sentinel()) {
827 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
828 param
= param
->get_next();
831 if (!param
->is_tail_sentinel()) {
833 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
834 param
= param
->get_next();
836 nir_builder_instr_insert(&b
, &instr
->instr
);
839 case nir_intrinsic_memory_barrier
:
840 case nir_intrinsic_group_memory_barrier
:
841 case nir_intrinsic_memory_barrier_atomic_counter
:
842 case nir_intrinsic_memory_barrier_buffer
:
843 case nir_intrinsic_memory_barrier_image
:
844 case nir_intrinsic_memory_barrier_shared
:
845 nir_builder_instr_insert(&b
, &instr
->instr
);
847 case nir_intrinsic_shader_clock
:
848 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
849 nir_builder_instr_insert(&b
, &instr
->instr
);
851 case nir_intrinsic_store_ssbo
: {
852 exec_node
*param
= ir
->actual_parameters
.get_head();
853 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
855 param
= param
->get_next();
856 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
858 param
= param
->get_next();
859 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
861 param
= param
->get_next();
862 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
865 /* Check if we need the indirect version */
866 ir_constant
*const_offset
= offset
->as_constant();
868 op
= nir_intrinsic_store_ssbo_indirect
;
870 instr
= nir_intrinsic_instr_create(shader
, op
);
871 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
872 instr
->const_index
[0] = 0;
874 instr
->const_index
[0] = const_offset
->value
.u
[0];
877 instr
->const_index
[1] = write_mask
->value
.u
[0];
879 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(val
));
880 instr
->num_components
= val
->type
->vector_elements
;
882 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
883 nir_builder_instr_insert(&b
, &instr
->instr
);
886 case nir_intrinsic_load_ssbo
: {
887 exec_node
*param
= ir
->actual_parameters
.get_head();
888 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
890 param
= param
->get_next();
891 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
893 /* Check if we need the indirect version */
894 ir_constant
*const_offset
= offset
->as_constant();
896 op
= nir_intrinsic_load_ssbo_indirect
;
898 instr
= nir_intrinsic_instr_create(shader
, op
);
899 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
900 instr
->const_index
[0] = 0;
903 instr
->const_index
[0] = const_offset
->value
.u
[0];
906 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
908 const glsl_type
*type
= ir
->return_deref
->var
->type
;
909 instr
->num_components
= type
->vector_elements
;
911 /* Setup destination register */
912 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
913 type
->vector_elements
, NULL
);
915 /* Insert the created nir instruction now since in the case of boolean
916 * result we will need to emit another instruction after it
918 nir_builder_instr_insert(&b
, &instr
->instr
);
921 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
922 * consider a true boolean to be ~0. Fix this up with a != 0
925 if (type
->base_type
== GLSL_TYPE_BOOL
) {
926 nir_alu_instr
*load_ssbo_compare
=
927 nir_alu_instr_create(shader
, nir_op_ine
);
928 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
929 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
930 load_ssbo_compare
->src
[1].src
=
931 nir_src_for_ssa(nir_imm_int(&b
, 0));
932 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
933 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
934 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
935 &load_ssbo_compare
->dest
.dest
,
936 type
->vector_elements
, NULL
);
937 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
938 nir_builder_instr_insert(&b
, &load_ssbo_compare
->instr
);
939 dest
= &load_ssbo_compare
->dest
.dest
;
943 case nir_intrinsic_ssbo_atomic_add
:
944 case nir_intrinsic_ssbo_atomic_imin
:
945 case nir_intrinsic_ssbo_atomic_umin
:
946 case nir_intrinsic_ssbo_atomic_imax
:
947 case nir_intrinsic_ssbo_atomic_umax
:
948 case nir_intrinsic_ssbo_atomic_and
:
949 case nir_intrinsic_ssbo_atomic_or
:
950 case nir_intrinsic_ssbo_atomic_xor
:
951 case nir_intrinsic_ssbo_atomic_exchange
:
952 case nir_intrinsic_ssbo_atomic_comp_swap
: {
953 int param_count
= ir
->actual_parameters
.length();
954 assert(param_count
== 3 || param_count
== 4);
957 exec_node
*param
= ir
->actual_parameters
.get_head();
958 ir_instruction
*inst
= (ir_instruction
*) param
;
959 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
962 param
= param
->get_next();
963 inst
= (ir_instruction
*) param
;
964 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
966 /* data1 parameter (this is always present) */
967 param
= param
->get_next();
968 inst
= (ir_instruction
*) param
;
969 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
971 /* data2 parameter (only with atomic_comp_swap) */
972 if (param_count
== 4) {
973 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
974 param
= param
->get_next();
975 inst
= (ir_instruction
*) param
;
976 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
980 assert(ir
->return_deref
);
981 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
982 ir
->return_deref
->type
->vector_elements
, NULL
);
983 nir_builder_instr_insert(&b
, &instr
->instr
);
987 unreachable("not reached");
990 if (ir
->return_deref
) {
991 nir_intrinsic_instr
*store_instr
=
992 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
993 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
995 store_instr
->variables
[0] =
996 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
997 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
999 nir_builder_instr_insert(&b
, &store_instr
->instr
);
1005 struct hash_entry
*entry
=
1006 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1008 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
1010 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
1013 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
1014 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
1018 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
1019 nir_builder_instr_insert(&b
, &instr
->instr
);
1023 nir_visitor::visit(ir_assignment
*ir
)
1025 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1027 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1028 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1029 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1030 nir_intrinsic_instr
*copy
=
1031 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
1033 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
1034 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
1036 if (ir
->condition
) {
1037 nir_if
*if_stmt
= nir_if_create(this->shader
);
1038 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1039 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1040 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1041 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1043 nir_builder_instr_insert(&b
, ©
->instr
);
1048 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1050 ir
->lhs
->accept(this);
1051 nir_deref_var
*lhs_deref
= this->deref_head
;
1052 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1054 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1056 * We have no good way to update only part of a variable, so just load
1057 * the LHS and do a vec operation to combine the old with the new, and
1059 * back into the LHS. Copy propagation should get rid of the mess.
1062 nir_intrinsic_instr
*load
=
1063 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1064 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1065 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1066 load
->variables
[0] = lhs_deref
;
1067 ralloc_steal(load
, load
->variables
[0]);
1068 nir_builder_instr_insert(&b
, &load
->instr
);
1070 nir_ssa_def
*srcs
[4];
1072 unsigned component
= 0;
1073 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1074 if (ir
->write_mask
& (1 << i
)) {
1075 /* GLSL IR will give us the input to the write-masked assignment
1076 * in a single packed vector. So, for example, if the
1077 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1078 * and z -> w and get the y component from the load.
1080 srcs
[i
] = nir_channel(&b
, src
, component
++);
1082 srcs
[i
] = nir_channel(&b
, &load
->dest
.ssa
, i
);
1086 src
= nir_vec(&b
, srcs
, ir
->lhs
->type
->vector_elements
);
1089 nir_intrinsic_instr
*store
=
1090 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1091 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1092 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1093 store
->variables
[0] = nir_deref_as_var(store_deref
);
1094 store
->src
[0] = nir_src_for_ssa(src
);
1096 if (ir
->condition
) {
1097 nir_if
*if_stmt
= nir_if_create(this->shader
);
1098 if_stmt
->condition
= nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
1099 nir_builder_cf_insert(&b
, &if_stmt
->cf_node
);
1100 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1101 b
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
1103 nir_builder_instr_insert(&b
, &store
->instr
);
1108 * Given an instruction, returns a pointer to its destination or NULL if there
1109 * is no destination.
1111 * Note that this only handles instructions we generate at this level.
1114 get_instr_dest(nir_instr
*instr
)
1116 nir_alu_instr
*alu_instr
;
1117 nir_intrinsic_instr
*intrinsic_instr
;
1118 nir_tex_instr
*tex_instr
;
1120 switch (instr
->type
) {
1121 case nir_instr_type_alu
:
1122 alu_instr
= nir_instr_as_alu(instr
);
1123 return &alu_instr
->dest
.dest
;
1125 case nir_instr_type_intrinsic
:
1126 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1127 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1128 return &intrinsic_instr
->dest
;
1132 case nir_instr_type_tex
:
1133 tex_instr
= nir_instr_as_tex(instr
);
1134 return &tex_instr
->dest
;
1137 unreachable("not reached");
1144 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1146 nir_dest
*dest
= get_instr_dest(instr
);
1149 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1151 nir_builder_instr_insert(&b
, instr
);
1154 assert(dest
->is_ssa
);
1155 this->result
= &dest
->ssa
;
1160 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1163 if (ir
->as_dereference() || ir
->as_constant()) {
1165 * A dereference is being used on the right hand side, which means we
1166 * must emit a variable load.
1169 nir_intrinsic_instr
*load_instr
=
1170 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1171 load_instr
->num_components
= ir
->type
->vector_elements
;
1172 load_instr
->variables
[0] = this->deref_head
;
1173 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1174 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1177 return this->result
;
1181 nir_visitor::visit(ir_expression
*ir
)
1183 /* Some special cases */
1184 switch (ir
->operation
) {
1185 case ir_binop_ubo_load
: {
1186 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1188 nir_intrinsic_op op
;
1190 op
= nir_intrinsic_load_ubo
;
1192 op
= nir_intrinsic_load_ubo_indirect
;
1195 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1196 load
->num_components
= ir
->type
->vector_elements
;
1197 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1198 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1200 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1201 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1204 * In UBO's, a true boolean value is any non-zero value, but we consider
1205 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1208 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
)
1209 this->result
= nir_ine(&b
, &load
->dest
.ssa
, nir_imm_int(&b
, 0));
1214 case ir_unop_interpolate_at_centroid
:
1215 case ir_binop_interpolate_at_offset
:
1216 case ir_binop_interpolate_at_sample
: {
1217 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1218 ir_swizzle
*swizzle
= NULL
;
1220 /* the api does not allow a swizzle here, but the varying packing code
1221 * may have pushed one into here.
1223 swizzle
= ir
->operands
[0]->as_swizzle();
1225 deref
= swizzle
->val
->as_dereference();
1229 deref
->accept(this);
1231 nir_intrinsic_op op
;
1232 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1233 switch (ir
->operation
) {
1234 case ir_unop_interpolate_at_centroid
:
1235 op
= nir_intrinsic_interp_var_at_centroid
;
1237 case ir_binop_interpolate_at_offset
:
1238 op
= nir_intrinsic_interp_var_at_offset
;
1240 case ir_binop_interpolate_at_sample
:
1241 op
= nir_intrinsic_interp_var_at_sample
;
1244 unreachable("Invalid interpolation intrinsic");
1247 /* This case can happen if the vertex shader does not write the
1248 * given varying. In this case, the linker will lower it to a
1249 * global variable. Since interpolating a variable makes no
1250 * sense, we'll just turn it into a load which will probably
1251 * eventually end up as an SSA definition.
1253 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1254 op
= nir_intrinsic_load_var
;
1257 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1258 intrin
->num_components
= deref
->type
->vector_elements
;
1259 intrin
->variables
[0] = this->deref_head
;
1260 ralloc_steal(intrin
, intrin
->variables
[0]);
1262 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1263 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1264 intrin
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1266 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1269 unsigned swiz
[4] = {
1270 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1273 result
= nir_swizzle(&b
, result
, swiz
,
1274 swizzle
->type
->vector_elements
, false);
1284 nir_ssa_def
*srcs
[4];
1285 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1286 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1288 glsl_base_type types
[4];
1289 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1291 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1293 types
[i
] = GLSL_TYPE_FLOAT
;
1295 glsl_base_type out_type
;
1297 out_type
= ir
->type
->base_type
;
1299 out_type
= GLSL_TYPE_FLOAT
;
1301 switch (ir
->operation
) {
1302 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1303 case ir_unop_logic_not
:
1304 result
= supports_ints
? nir_inot(&b
, srcs
[0]) : nir_fnot(&b
, srcs
[0]);
1307 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fneg(&b
, srcs
[0])
1308 : nir_ineg(&b
, srcs
[0]);
1311 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fabs(&b
, srcs
[0])
1312 : nir_iabs(&b
, srcs
[0]);
1314 case ir_unop_saturate
:
1315 assert(types
[0] == GLSL_TYPE_FLOAT
);
1316 result
= nir_fsat(&b
, srcs
[0]);
1319 result
= (types
[0] == GLSL_TYPE_FLOAT
) ? nir_fsign(&b
, srcs
[0])
1320 : nir_isign(&b
, srcs
[0]);
1322 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1323 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1324 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1325 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1326 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1327 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1328 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1330 result
= supports_ints
? nir_i2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1333 result
= supports_ints
? nir_u2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1336 result
= supports_ints
? nir_b2f(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1338 case ir_unop_f2i
: result
= nir_f2i(&b
, srcs
[0]); break;
1339 case ir_unop_f2u
: result
= nir_f2u(&b
, srcs
[0]); break;
1340 case ir_unop_f2b
: result
= nir_f2b(&b
, srcs
[0]); break;
1341 case ir_unop_i2b
: result
= nir_i2b(&b
, srcs
[0]); break;
1342 case ir_unop_b2i
: result
= nir_b2i(&b
, srcs
[0]); break;
1345 case ir_unop_bitcast_i2f
:
1346 case ir_unop_bitcast_f2i
:
1347 case ir_unop_bitcast_u2f
:
1348 case ir_unop_bitcast_f2u
:
1349 case ir_unop_subroutine_to_int
:
1351 result
= nir_imov(&b
, srcs
[0]);
1354 switch (ir
->operands
[0]->type
->vector_elements
) {
1356 result
= supports_ints
? nir_bany2(&b
, srcs
[0])
1357 : nir_fany2(&b
, srcs
[0]);
1360 result
= supports_ints
? nir_bany3(&b
, srcs
[0])
1361 : nir_fany3(&b
, srcs
[0]);
1364 result
= supports_ints
? nir_bany4(&b
, srcs
[0])
1365 : nir_fany4(&b
, srcs
[0]);
1368 unreachable("not reached");
1371 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1372 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1373 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1374 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1375 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1376 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1377 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1378 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1379 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1380 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1381 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1382 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1383 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1384 case ir_unop_pack_snorm_2x16
:
1385 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1387 case ir_unop_pack_snorm_4x8
:
1388 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1390 case ir_unop_pack_unorm_2x16
:
1391 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1393 case ir_unop_pack_unorm_4x8
:
1394 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1396 case ir_unop_pack_half_2x16
:
1397 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1399 case ir_unop_unpack_snorm_2x16
:
1400 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1402 case ir_unop_unpack_snorm_4x8
:
1403 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1405 case ir_unop_unpack_unorm_2x16
:
1406 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1408 case ir_unop_unpack_unorm_4x8
:
1409 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1411 case ir_unop_unpack_half_2x16
:
1412 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1414 case ir_unop_unpack_half_2x16_split_x
:
1415 result
= nir_unpack_half_2x16_split_x(&b
, srcs
[0]);
1417 case ir_unop_unpack_half_2x16_split_y
:
1418 result
= nir_unpack_half_2x16_split_y(&b
, srcs
[0]);
1420 case ir_unop_bitfield_reverse
:
1421 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1423 case ir_unop_bit_count
:
1424 result
= nir_bit_count(&b
, srcs
[0]);
1426 case ir_unop_find_msb
:
1428 case GLSL_TYPE_UINT
:
1429 result
= nir_ufind_msb(&b
, srcs
[0]);
1432 result
= nir_ifind_msb(&b
, srcs
[0]);
1435 unreachable("Invalid type for findMSB()");
1438 case ir_unop_find_lsb
:
1439 result
= nir_find_lsb(&b
, srcs
[0]);
1443 switch (ir
->type
->vector_elements
) {
1445 switch (ir
->operands
[0]->type
->vector_elements
) {
1446 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1447 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1448 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1449 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1450 default: unreachable("not reached");
1454 switch (ir
->operands
[0]->type
->vector_elements
) {
1455 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1456 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1457 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1458 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1459 default: unreachable("not reached");
1463 switch (ir
->operands
[0]->type
->vector_elements
) {
1464 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1465 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1466 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1467 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1468 default: unreachable("not reached");
1472 switch (ir
->operands
[0]->type
->vector_elements
) {
1473 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1474 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1475 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1476 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1477 default: unreachable("not reached");
1481 unreachable("not reached");
1484 case ir_unop_get_buffer_size
: {
1485 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1487 nir_intrinsic_get_buffer_size
);
1488 load
->num_components
= ir
->type
->vector_elements
;
1489 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1490 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1495 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1496 : nir_iadd(&b
, srcs
[0], srcs
[1]);
1499 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
1500 : nir_isub(&b
, srcs
[0], srcs
[1]);
1503 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmul(&b
, srcs
[0], srcs
[1])
1504 : nir_imul(&b
, srcs
[0], srcs
[1]);
1507 if (out_type
== GLSL_TYPE_FLOAT
)
1508 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
1509 else if (out_type
== GLSL_TYPE_INT
)
1510 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
1512 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
1515 result
= (out_type
== GLSL_TYPE_FLOAT
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
1516 : nir_umod(&b
, srcs
[0], srcs
[1]);
1519 if (out_type
== GLSL_TYPE_FLOAT
)
1520 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
1521 else if (out_type
== GLSL_TYPE_INT
)
1522 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
1524 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
1527 if (out_type
== GLSL_TYPE_FLOAT
)
1528 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
1529 else if (out_type
== GLSL_TYPE_INT
)
1530 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
1532 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
1534 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
1535 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
1536 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
1537 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1538 case ir_binop_logic_and
:
1539 result
= supports_ints
? nir_iand(&b
, srcs
[0], srcs
[1])
1540 : nir_fand(&b
, srcs
[0], srcs
[1]);
1542 case ir_binop_logic_or
:
1543 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1544 : nir_for(&b
, srcs
[0], srcs
[1]);
1546 case ir_binop_logic_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
1547 result
= supports_ints
? nir_ior(&b
, srcs
[0], srcs
[1])
1548 : nir_for(&b
, srcs
[0], srcs
[1]);
1550 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
1551 case ir_binop_rshift
:
1552 result
= (out_type
== GLSL_TYPE_INT
) ? nir_ishr(&b
, srcs
[0], srcs
[1])
1553 : nir_ushr(&b
, srcs
[0], srcs
[1]);
1555 case ir_binop_imul_high
:
1556 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
1557 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
1559 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
1560 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
1562 if (supports_ints
) {
1563 if (types
[0] == GLSL_TYPE_FLOAT
)
1564 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
1565 else if (types
[0] == GLSL_TYPE_INT
)
1566 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
1568 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
1570 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1573 case ir_binop_greater
:
1574 if (supports_ints
) {
1575 if (types
[0] == GLSL_TYPE_FLOAT
)
1576 result
= nir_flt(&b
, srcs
[1], srcs
[0]);
1577 else if (types
[0] == GLSL_TYPE_INT
)
1578 result
= nir_ilt(&b
, srcs
[1], srcs
[0]);
1580 result
= nir_ult(&b
, srcs
[1], srcs
[0]);
1582 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1585 case ir_binop_lequal
:
1586 if (supports_ints
) {
1587 if (types
[0] == GLSL_TYPE_FLOAT
)
1588 result
= nir_fge(&b
, srcs
[1], srcs
[0]);
1589 else if (types
[0] == GLSL_TYPE_INT
)
1590 result
= nir_ige(&b
, srcs
[1], srcs
[0]);
1592 result
= nir_uge(&b
, srcs
[1], srcs
[0]);
1594 result
= nir_slt(&b
, srcs
[1], srcs
[0]);
1597 case ir_binop_gequal
:
1598 if (supports_ints
) {
1599 if (types
[0] == GLSL_TYPE_FLOAT
)
1600 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
1601 else if (types
[0] == GLSL_TYPE_INT
)
1602 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
1604 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
1606 result
= nir_slt(&b
, srcs
[0], srcs
[1]);
1609 case ir_binop_equal
:
1610 if (supports_ints
) {
1611 if (types
[0] == GLSL_TYPE_FLOAT
)
1612 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
1614 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
1616 result
= nir_seq(&b
, srcs
[0], srcs
[1]);
1619 case ir_binop_nequal
:
1620 if (supports_ints
) {
1621 if (types
[0] == GLSL_TYPE_FLOAT
)
1622 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
1624 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
1626 result
= nir_sne(&b
, srcs
[0], srcs
[1]);
1629 case ir_binop_all_equal
:
1630 if (supports_ints
) {
1631 if (types
[0] == GLSL_TYPE_FLOAT
) {
1632 switch (ir
->operands
[0]->type
->vector_elements
) {
1633 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
1634 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
1635 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
1636 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
1638 unreachable("not reached");
1641 switch (ir
->operands
[0]->type
->vector_elements
) {
1642 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
1643 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
1644 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
1645 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
1647 unreachable("not reached");
1651 switch (ir
->operands
[0]->type
->vector_elements
) {
1652 case 1: result
= nir_seq(&b
, srcs
[0], srcs
[1]); break;
1653 case 2: result
= nir_fall_equal2(&b
, srcs
[0], srcs
[1]); break;
1654 case 3: result
= nir_fall_equal3(&b
, srcs
[0], srcs
[1]); break;
1655 case 4: result
= nir_fall_equal4(&b
, srcs
[0], srcs
[1]); break;
1657 unreachable("not reached");
1661 case ir_binop_any_nequal
:
1662 if (supports_ints
) {
1663 if (types
[0] == GLSL_TYPE_FLOAT
) {
1664 switch (ir
->operands
[0]->type
->vector_elements
) {
1665 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
1666 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
1667 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
1668 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
1670 unreachable("not reached");
1673 switch (ir
->operands
[0]->type
->vector_elements
) {
1674 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
1675 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
1676 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
1677 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
1679 unreachable("not reached");
1683 switch (ir
->operands
[0]->type
->vector_elements
) {
1684 case 1: result
= nir_sne(&b
, srcs
[0], srcs
[1]); break;
1685 case 2: result
= nir_fany_nequal2(&b
, srcs
[0], srcs
[1]); break;
1686 case 3: result
= nir_fany_nequal3(&b
, srcs
[0], srcs
[1]); break;
1687 case 4: result
= nir_fany_nequal4(&b
, srcs
[0], srcs
[1]); break;
1689 unreachable("not reached");
1694 switch (ir
->operands
[0]->type
->vector_elements
) {
1695 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
1696 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
1697 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
1699 unreachable("not reached");
1703 case ir_binop_pack_half_2x16_split
:
1704 result
= nir_pack_half_2x16_split(&b
, srcs
[0], srcs
[1]);
1706 case ir_binop_bfm
: result
= nir_bfm(&b
, srcs
[0], srcs
[1]); break;
1707 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
1709 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
1712 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
1716 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1718 result
= nir_fcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
1721 result
= nir_bfi(&b
, srcs
[0], srcs
[1], srcs
[2]);
1723 case ir_triop_bitfield_extract
:
1724 result
= (out_type
== GLSL_TYPE_INT
) ?
1725 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
1726 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
1728 case ir_quadop_bitfield_insert
:
1729 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
1731 case ir_quadop_vector
:
1732 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
1736 unreachable("not reached");
1741 nir_visitor::visit(ir_swizzle
*ir
)
1743 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1744 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
1745 ir
->type
->vector_elements
, !supports_ints
);
1749 nir_visitor::visit(ir_texture
*ir
)
1756 num_srcs
= 1; /* coordinate */
1761 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1762 num_srcs
= 2; /* coordinate, bias/lod */
1766 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1772 if (ir
->lod_info
.lod
!= NULL
)
1773 num_srcs
= 2; /* coordinate, lod */
1775 num_srcs
= 1; /* coordinate */
1779 op
= nir_texop_txf_ms
;
1780 num_srcs
= 2; /* coordinate, sample_index */
1785 if (ir
->lod_info
.lod
!= NULL
)
1786 num_srcs
= 1; /* lod */
1793 num_srcs
= 1; /* coordinate */
1798 num_srcs
= 1; /* coordinate */
1801 case ir_query_levels
:
1802 op
= nir_texop_query_levels
;
1806 case ir_texture_samples
:
1807 op
= nir_texop_texture_samples
;
1812 unreachable("not reached");
1815 if (ir
->projector
!= NULL
)
1817 if (ir
->shadow_comparitor
!= NULL
)
1819 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1822 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1825 instr
->sampler_dim
=
1826 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1827 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1828 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1829 if (instr
->is_shadow
)
1830 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1831 switch (ir
->type
->base_type
) {
1832 case GLSL_TYPE_FLOAT
:
1833 instr
->dest_type
= nir_type_float
;
1836 instr
->dest_type
= nir_type_int
;
1838 case GLSL_TYPE_UINT
:
1839 instr
->dest_type
= nir_type_unsigned
;
1842 unreachable("not reached");
1845 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1847 unsigned src_number
= 0;
1849 if (ir
->coordinate
!= NULL
) {
1850 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1851 instr
->src
[src_number
].src
=
1852 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
1853 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1857 if (ir
->projector
!= NULL
) {
1858 instr
->src
[src_number
].src
=
1859 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
1860 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1864 if (ir
->shadow_comparitor
!= NULL
) {
1865 instr
->src
[src_number
].src
=
1866 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparitor
));
1867 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1871 if (ir
->offset
!= NULL
) {
1872 /* we don't support multiple offsets yet */
1873 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1875 ir_constant
*const_offset
= ir
->offset
->as_constant();
1876 if (const_offset
!= NULL
) {
1877 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
1878 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
1880 instr
->src
[src_number
].src
=
1881 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
1882 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
1889 instr
->src
[src_number
].src
=
1890 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
1891 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
1898 if (ir
->lod_info
.lod
!= NULL
) {
1899 instr
->src
[src_number
].src
=
1900 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
1901 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
1907 instr
->src
[src_number
].src
=
1908 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
1909 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
1911 instr
->src
[src_number
].src
=
1912 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
1913 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
1918 instr
->src
[src_number
].src
=
1919 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
1920 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
1925 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
1932 assert(src_number
== num_srcs
);
1934 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
1938 nir_visitor::visit(ir_constant
*ir
)
1941 * We don't know if this variable is an an array or struct that gets
1942 * dereferenced, so do the safe thing an make it a variable with a
1943 * constant initializer and return a dereference.
1947 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
1948 var
->data
.read_only
= true;
1949 var
->constant_initializer
= constant_copy(ir
, var
);
1951 this->deref_head
= nir_deref_var_create(this->shader
, var
);
1952 this->deref_tail
= &this->deref_head
->deref
;
1956 nir_visitor::visit(ir_dereference_variable
*ir
)
1958 struct hash_entry
*entry
=
1959 _mesa_hash_table_search(this->var_table
, ir
->var
);
1961 nir_variable
*var
= (nir_variable
*) entry
->data
;
1963 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
1964 this->deref_head
= deref
;
1965 this->deref_tail
= &deref
->deref
;
1969 nir_visitor::visit(ir_dereference_record
*ir
)
1971 ir
->record
->accept(this);
1973 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
1974 assert(field_index
>= 0);
1976 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
1977 deref
->deref
.type
= ir
->type
;
1978 this->deref_tail
->child
= &deref
->deref
;
1979 this->deref_tail
= &deref
->deref
;
1983 nir_visitor::visit(ir_dereference_array
*ir
)
1985 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
1986 deref
->deref
.type
= ir
->type
;
1988 ir_constant
*const_index
= ir
->array_index
->as_constant();
1989 if (const_index
!= NULL
) {
1990 deref
->deref_array_type
= nir_deref_array_type_direct
;
1991 deref
->base_offset
= const_index
->value
.u
[0];
1993 deref
->deref_array_type
= nir_deref_array_type_indirect
;
1995 nir_src_for_ssa(evaluate_rvalue(ir
->array_index
));
1998 ir
->array
->accept(this);
2000 this->deref_tail
->child
= &deref
->deref
;
2001 ralloc_steal(this->deref_tail
, deref
);
2002 this->deref_tail
= &deref
->deref
;
2006 nir_visitor::visit(ir_barrier
*ir
)
2008 nir_intrinsic_instr
*instr
=
2009 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2010 nir_builder_instr_insert(&b
, &instr
->instr
);