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 "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
35 * pass to lower GLSL IR to NIR
37 * This will lower variable dereferences to loads/stores of corresponding
38 * variables in NIR - the variables will be converted to registers in a later
44 class nir_visitor
: public ir_visitor
47 nir_visitor(nir_shader
*shader
);
50 virtual void visit(ir_variable
*);
51 virtual void visit(ir_function
*);
52 virtual void visit(ir_function_signature
*);
53 virtual void visit(ir_loop
*);
54 virtual void visit(ir_if
*);
55 virtual void visit(ir_discard
*);
56 virtual void visit(ir_loop_jump
*);
57 virtual void visit(ir_return
*);
58 virtual void visit(ir_call
*);
59 virtual void visit(ir_assignment
*);
60 virtual void visit(ir_emit_vertex
*);
61 virtual void visit(ir_end_primitive
*);
62 virtual void visit(ir_expression
*);
63 virtual void visit(ir_swizzle
*);
64 virtual void visit(ir_texture
*);
65 virtual void visit(ir_constant
*);
66 virtual void visit(ir_dereference_variable
*);
67 virtual void visit(ir_dereference_record
*);
68 virtual void visit(ir_dereference_array
*);
69 virtual void visit(ir_barrier
*);
71 void create_function(ir_function
*ir
);
74 void create_overload(ir_function_signature
*ir
, nir_function
*function
);
75 void add_instr(nir_instr
*instr
, unsigned num_components
);
76 nir_src
evaluate_rvalue(ir_rvalue
*ir
);
78 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
);
79 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
);
80 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
82 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_src src1
,
83 nir_src src2
, nir_src src3
);
88 nir_function_impl
*impl
;
89 exec_list
*cf_node_list
;
90 nir_instr
*result
; /* result of the expression tree last visited */
92 nir_deref_var
*evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
);
94 /* the head of the dereference chain we're creating */
95 nir_deref_var
*deref_head
;
96 /* the tail of the dereference chain we're creating */
97 nir_deref
*deref_tail
;
99 nir_variable
*var
; /* variable created by ir_variable visitor */
101 /* whether the IR we're operating on is per-function or global */
104 /* map of ir_variable -> nir_variable */
105 struct hash_table
*var_table
;
107 /* map of ir_function_signature -> nir_function_overload */
108 struct hash_table
*overload_table
;
112 * This visitor runs before the main visitor, calling create_function() for
113 * each function so that the main visitor can resolve forward references in
117 class nir_function_visitor
: public ir_hierarchical_visitor
120 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
123 virtual ir_visitor_status
visit_enter(ir_function
*);
126 nir_visitor
*visitor
;
129 }; /* end of anonymous namespace */
132 glsl_to_nir(const struct gl_shader_program
*shader_prog
,
133 gl_shader_stage stage
,
134 const nir_shader_compiler_options
*options
)
136 struct gl_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
138 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
140 nir_visitor
v1(shader
);
141 nir_function_visitor
v2(&v1
);
143 visit_exec_list(sh
->ir
, &v1
);
145 nir_lower_outputs_to_temporaries(shader
);
147 /* TODO: Use _mesa_fls instead */
148 unsigned num_textures
= 0;
149 for (unsigned i
= 0; i
< 8 * sizeof(sh
->Program
->SamplersUsed
); i
++)
150 if (sh
->Program
->SamplersUsed
& (1 << i
))
153 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", sh
->Name
);
154 shader
->info
.num_textures
= num_textures
;
155 shader
->info
.num_ubos
= sh
->NumUniformBlocks
;
156 shader
->info
.num_abos
= shader_prog
->NumAtomicBuffers
;
157 shader
->info
.num_ssbos
= shader_prog
->NumBufferInterfaceBlocks
;
158 shader
->info
.num_images
= sh
->NumImages
;
159 shader
->info
.inputs_read
= sh
->Program
->InputsRead
;
160 shader
->info
.outputs_written
= sh
->Program
->OutputsWritten
;
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
= sh
->Program
->UsesClipDistanceOut
;
164 shader
->info
.separate_shader
= shader_prog
->SeparateShader
;
165 shader
->info
.gs
.vertices_out
= sh
->Geom
.VerticesOut
;
166 shader
->info
.gs
.invocations
= sh
->Geom
.Invocations
;
167 shader
->info
.has_transform_feedback_varyings
=
168 shader_prog
->TransformFeedback
.NumVarying
> 0;
173 nir_visitor::nir_visitor(nir_shader
*shader
)
175 this->supports_ints
= shader
->options
->native_integers
;
176 this->shader
= shader
;
177 this->is_global
= true;
178 this->var_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
179 _mesa_key_pointer_equal
);
180 this->overload_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
181 _mesa_key_pointer_equal
);
184 nir_visitor::~nir_visitor()
186 _mesa_hash_table_destroy(this->var_table
, NULL
);
187 _mesa_hash_table_destroy(this->overload_table
, NULL
);
191 nir_visitor::evaluate_deref(nir_instr
*mem_ctx
, ir_instruction
*ir
)
194 ralloc_steal(mem_ctx
, this->deref_head
);
195 return this->deref_head
;
198 static nir_constant
*
199 constant_copy(ir_constant
*ir
, void *mem_ctx
)
204 nir_constant
*ret
= ralloc(mem_ctx
, nir_constant
);
206 unsigned total_elems
= ir
->type
->components();
208 switch (ir
->type
->base_type
) {
210 for (i
= 0; i
< total_elems
; i
++)
211 ret
->value
.u
[i
] = ir
->value
.u
[i
];
215 for (i
= 0; i
< total_elems
; i
++)
216 ret
->value
.i
[i
] = ir
->value
.i
[i
];
219 case GLSL_TYPE_FLOAT
:
220 for (i
= 0; i
< total_elems
; i
++)
221 ret
->value
.f
[i
] = ir
->value
.f
[i
];
225 for (i
= 0; i
< total_elems
; i
++)
226 ret
->value
.b
[i
] = ir
->value
.b
[i
];
229 case GLSL_TYPE_STRUCT
:
230 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
233 foreach_in_list(ir_constant
, field
, &ir
->components
) {
234 ret
->elements
[i
] = constant_copy(field
, mem_ctx
);
239 case GLSL_TYPE_ARRAY
:
240 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
243 for (i
= 0; i
< ir
->type
->length
; i
++)
244 ret
->elements
[i
] = constant_copy(ir
->array_elements
[i
], mem_ctx
);
248 unreachable("not reached");
255 nir_visitor::visit(ir_variable
*ir
)
257 nir_variable
*var
= ralloc(shader
, nir_variable
);
258 var
->type
= ir
->type
;
259 var
->name
= ralloc_strdup(var
, ir
->name
);
261 if (ir
->is_interface_instance() && ir
->get_max_ifc_array_access() != NULL
) {
262 unsigned size
= ir
->get_interface_type()->length
;
263 var
->max_ifc_array_access
= ralloc_array(var
, unsigned, size
);
264 memcpy(var
->max_ifc_array_access
, ir
->get_max_ifc_array_access(),
265 size
* sizeof(unsigned));
267 var
->max_ifc_array_access
= NULL
;
270 var
->data
.read_only
= ir
->data
.read_only
;
271 var
->data
.centroid
= ir
->data
.centroid
;
272 var
->data
.sample
= ir
->data
.sample
;
273 var
->data
.invariant
= ir
->data
.invariant
;
274 var
->data
.location
= ir
->data
.location
;
276 switch(ir
->data
.mode
) {
278 case ir_var_temporary
:
280 var
->data
.mode
= nir_var_global
;
282 var
->data
.mode
= nir_var_local
;
285 case ir_var_function_in
:
286 case ir_var_function_out
:
287 case ir_var_function_inout
:
288 case ir_var_const_in
:
289 var
->data
.mode
= nir_var_local
;
292 case ir_var_shader_in
:
293 if (shader
->stage
== MESA_SHADER_FRAGMENT
&&
294 ir
->data
.location
== VARYING_SLOT_FACE
) {
295 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
296 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
297 var
->data
.mode
= nir_var_system_value
;
298 } else if (shader
->stage
== MESA_SHADER_GEOMETRY
&&
299 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
300 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
301 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
302 var
->data
.mode
= nir_var_system_value
;
304 var
->data
.mode
= nir_var_shader_in
;
308 case ir_var_shader_out
:
309 var
->data
.mode
= nir_var_shader_out
;
313 var
->data
.mode
= nir_var_uniform
;
316 case ir_var_shader_storage
:
317 var
->data
.mode
= nir_var_shader_storage
;
320 case ir_var_system_value
:
321 var
->data
.mode
= nir_var_system_value
;
325 unreachable("not reached");
328 var
->data
.interpolation
= ir
->data
.interpolation
;
329 var
->data
.origin_upper_left
= ir
->data
.origin_upper_left
;
330 var
->data
.pixel_center_integer
= ir
->data
.pixel_center_integer
;
331 var
->data
.explicit_location
= ir
->data
.explicit_location
;
332 var
->data
.explicit_index
= ir
->data
.explicit_index
;
333 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
334 var
->data
.has_initializer
= ir
->data
.has_initializer
;
335 var
->data
.is_unmatched_generic_inout
= ir
->data
.is_unmatched_generic_inout
;
336 var
->data
.location_frac
= ir
->data
.location_frac
;
337 var
->data
.from_named_ifc_block_array
= ir
->data
.from_named_ifc_block_array
;
338 var
->data
.from_named_ifc_block_nonarray
= ir
->data
.from_named_ifc_block_nonarray
;
340 switch (ir
->data
.depth_layout
) {
341 case ir_depth_layout_none
:
342 var
->data
.depth_layout
= nir_depth_layout_none
;
344 case ir_depth_layout_any
:
345 var
->data
.depth_layout
= nir_depth_layout_any
;
347 case ir_depth_layout_greater
:
348 var
->data
.depth_layout
= nir_depth_layout_greater
;
350 case ir_depth_layout_less
:
351 var
->data
.depth_layout
= nir_depth_layout_less
;
353 case ir_depth_layout_unchanged
:
354 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
357 unreachable("not reached");
360 var
->data
.index
= ir
->data
.index
;
361 var
->data
.binding
= ir
->data
.binding
;
362 /* XXX Get rid of buffer_index */
363 var
->data
.atomic
.buffer_index
= ir
->data
.binding
;
364 var
->data
.atomic
.offset
= ir
->data
.atomic
.offset
;
365 var
->data
.image
.read_only
= ir
->data
.image_read_only
;
366 var
->data
.image
.write_only
= ir
->data
.image_write_only
;
367 var
->data
.image
.coherent
= ir
->data
.image_coherent
;
368 var
->data
.image
._volatile
= ir
->data
.image_volatile
;
369 var
->data
.image
.restrict_flag
= ir
->data
.image_restrict
;
370 var
->data
.image
.format
= ir
->data
.image_format
;
371 var
->data
.max_array_access
= ir
->data
.max_array_access
;
373 var
->num_state_slots
= ir
->get_num_state_slots();
374 if (var
->num_state_slots
> 0) {
375 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
376 var
->num_state_slots
);
378 ir_state_slot
*state_slots
= ir
->get_state_slots();
379 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
380 for (unsigned j
= 0; j
< 5; j
++)
381 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
382 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
385 var
->state_slots
= NULL
;
388 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
390 var
->interface_type
= ir
->get_interface_type();
392 switch (var
->data
.mode
) {
394 exec_list_push_tail(&impl
->locals
, &var
->node
);
398 exec_list_push_tail(&shader
->globals
, &var
->node
);
401 case nir_var_shader_in
:
402 exec_list_push_tail(&shader
->inputs
, &var
->node
);
405 case nir_var_shader_out
:
406 exec_list_push_tail(&shader
->outputs
, &var
->node
);
409 case nir_var_uniform
:
410 case nir_var_shader_storage
:
411 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
414 case nir_var_system_value
:
415 exec_list_push_tail(&shader
->system_values
, &var
->node
);
419 unreachable("not reached");
422 _mesa_hash_table_insert(var_table
, ir
, var
);
427 nir_function_visitor::visit_enter(ir_function
*ir
)
429 visitor
->create_function(ir
);
430 return visit_continue_with_parent
;
435 nir_visitor::create_function(ir_function
*ir
)
437 nir_function
*func
= nir_function_create(this->shader
, ir
->name
);
438 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
439 create_overload(sig
, func
);
446 nir_visitor::create_overload(ir_function_signature
*ir
, nir_function
*function
)
448 if (ir
->is_intrinsic
)
451 nir_function_overload
*overload
= nir_function_overload_create(function
);
453 unsigned num_params
= ir
->parameters
.length();
454 overload
->num_params
= num_params
;
455 overload
->params
= ralloc_array(shader
, nir_parameter
, num_params
);
458 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
459 switch (param
->data
.mode
) {
460 case ir_var_function_in
:
461 overload
->params
[i
].param_type
= nir_parameter_in
;
464 case ir_var_function_out
:
465 overload
->params
[i
].param_type
= nir_parameter_out
;
468 case ir_var_function_inout
:
469 overload
->params
[i
].param_type
= nir_parameter_inout
;
473 unreachable("not reached");
476 overload
->params
[i
].type
= param
->type
;
480 overload
->return_type
= ir
->return_type
;
482 _mesa_hash_table_insert(this->overload_table
, ir
, overload
);
486 nir_visitor::visit(ir_function
*ir
)
488 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
493 nir_visitor::visit(ir_function_signature
*ir
)
495 if (ir
->is_intrinsic
)
498 struct hash_entry
*entry
=
499 _mesa_hash_table_search(this->overload_table
, ir
);
502 nir_function_overload
*overload
= (nir_function_overload
*) entry
->data
;
504 if (ir
->is_defined
) {
505 nir_function_impl
*impl
= nir_function_impl_create(overload
);
508 unsigned num_params
= overload
->num_params
;
509 impl
->num_params
= num_params
;
510 impl
->params
= ralloc_array(this->shader
, nir_variable
*, num_params
);
512 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
514 impl
->params
[i
] = this->var
;
518 if (overload
->return_type
== glsl_type::void_type
) {
519 impl
->return_var
= NULL
;
521 impl
->return_var
= ralloc(this->shader
, nir_variable
);
522 impl
->return_var
->name
= ralloc_strdup(impl
->return_var
,
524 impl
->return_var
->type
= overload
->return_type
;
527 this->is_global
= false;
529 this->cf_node_list
= &impl
->body
;
530 visit_exec_list(&ir
->body
, this);
532 this->is_global
= true;
534 overload
->impl
= NULL
;
539 nir_visitor::visit(ir_loop
*ir
)
541 exec_list
*old_list
= this->cf_node_list
;
543 nir_loop
*loop
= nir_loop_create(this->shader
);
544 nir_cf_node_insert_end(old_list
, &loop
->cf_node
);
545 this->cf_node_list
= &loop
->body
;
546 visit_exec_list(&ir
->body_instructions
, this);
548 this->cf_node_list
= old_list
;
552 nir_visitor::visit(ir_if
*ir
)
554 nir_src condition
= evaluate_rvalue(ir
->condition
);
556 exec_list
*old_list
= this->cf_node_list
;
558 nir_if
*if_stmt
= nir_if_create(this->shader
);
559 if_stmt
->condition
= condition
;
560 nir_cf_node_insert_end(old_list
, &if_stmt
->cf_node
);
562 this->cf_node_list
= &if_stmt
->then_list
;
563 visit_exec_list(&ir
->then_instructions
, this);
565 this->cf_node_list
= &if_stmt
->else_list
;
566 visit_exec_list(&ir
->else_instructions
, this);
568 this->cf_node_list
= old_list
;
572 nir_visitor::visit(ir_discard
*ir
)
575 * discards aren't treated as control flow, because before we lower them
576 * they can appear anywhere in the shader and the stuff after them may still
577 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
578 * discards will be immediately followed by a return.
581 nir_intrinsic_instr
*discard
;
583 discard
= nir_intrinsic_instr_create(this->shader
,
584 nir_intrinsic_discard_if
);
585 discard
->src
[0] = evaluate_rvalue(ir
->condition
);
587 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
589 nir_instr_insert_after_cf_list(this->cf_node_list
, &discard
->instr
);
593 nir_visitor::visit(ir_emit_vertex
*ir
)
595 nir_intrinsic_instr
*instr
=
596 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
597 instr
->const_index
[0] = ir
->stream_id();
598 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
602 nir_visitor::visit(ir_end_primitive
*ir
)
604 nir_intrinsic_instr
*instr
=
605 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
606 instr
->const_index
[0] = ir
->stream_id();
607 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
611 nir_visitor::visit(ir_loop_jump
*ir
)
615 case ir_loop_jump::jump_break
:
616 type
= nir_jump_break
;
618 case ir_loop_jump::jump_continue
:
619 type
= nir_jump_continue
;
622 unreachable("not reached");
625 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
626 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
630 nir_visitor::visit(ir_return
*ir
)
632 if (ir
->value
!= NULL
) {
633 nir_intrinsic_instr
*copy
=
634 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
636 copy
->variables
[0] = nir_deref_var_create(copy
, this->impl
->return_var
);
637 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->value
);
640 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
641 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
645 nir_visitor::visit(ir_call
*ir
)
647 if (ir
->callee
->is_intrinsic
) {
649 if (strcmp(ir
->callee_name(), "__intrinsic_atomic_read") == 0) {
650 op
= nir_intrinsic_atomic_counter_read_var
;
651 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_increment") == 0) {
652 op
= nir_intrinsic_atomic_counter_inc_var
;
653 } else if (strcmp(ir
->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
654 op
= nir_intrinsic_atomic_counter_dec_var
;
655 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_load") == 0) {
656 op
= nir_intrinsic_image_load
;
657 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_store") == 0) {
658 op
= nir_intrinsic_image_store
;
659 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_add") == 0) {
660 op
= nir_intrinsic_image_atomic_add
;
661 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_min") == 0) {
662 op
= nir_intrinsic_image_atomic_min
;
663 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_max") == 0) {
664 op
= nir_intrinsic_image_atomic_max
;
665 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_and") == 0) {
666 op
= nir_intrinsic_image_atomic_and
;
667 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_or") == 0) {
668 op
= nir_intrinsic_image_atomic_or
;
669 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
670 op
= nir_intrinsic_image_atomic_xor
;
671 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
672 op
= nir_intrinsic_image_atomic_exchange
;
673 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
674 op
= nir_intrinsic_image_atomic_comp_swap
;
675 } else if (strcmp(ir
->callee_name(), "__intrinsic_memory_barrier") == 0) {
676 op
= nir_intrinsic_memory_barrier
;
677 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_size") == 0) {
678 op
= nir_intrinsic_image_size
;
679 } else if (strcmp(ir
->callee_name(), "__intrinsic_image_samples") == 0) {
680 op
= nir_intrinsic_image_samples
;
681 } else if (strcmp(ir
->callee_name(), "__intrinsic_store_ssbo") == 0) {
682 op
= nir_intrinsic_store_ssbo
;
683 } else if (strcmp(ir
->callee_name(), "__intrinsic_load_ssbo") == 0) {
684 op
= nir_intrinsic_load_ssbo
;
685 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
686 op
= nir_intrinsic_ssbo_atomic_add
;
687 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
688 op
= nir_intrinsic_ssbo_atomic_and
;
689 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
690 op
= nir_intrinsic_ssbo_atomic_or
;
691 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
692 op
= nir_intrinsic_ssbo_atomic_xor
;
693 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
694 op
= nir_intrinsic_ssbo_atomic_min
;
695 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
696 op
= nir_intrinsic_ssbo_atomic_max
;
697 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
698 op
= nir_intrinsic_ssbo_atomic_exchange
;
699 } else if (strcmp(ir
->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
700 op
= nir_intrinsic_ssbo_atomic_comp_swap
;
702 unreachable("not reached");
705 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
706 nir_dest
*dest
= &instr
->dest
;
709 case nir_intrinsic_atomic_counter_read_var
:
710 case nir_intrinsic_atomic_counter_inc_var
:
711 case nir_intrinsic_atomic_counter_dec_var
: {
712 ir_dereference
*param
=
713 (ir_dereference
*) ir
->actual_parameters
.get_head();
714 instr
->variables
[0] = evaluate_deref(&instr
->instr
, param
);
715 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
716 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
719 case nir_intrinsic_image_load
:
720 case nir_intrinsic_image_store
:
721 case nir_intrinsic_image_atomic_add
:
722 case nir_intrinsic_image_atomic_min
:
723 case nir_intrinsic_image_atomic_max
:
724 case nir_intrinsic_image_atomic_and
:
725 case nir_intrinsic_image_atomic_or
:
726 case nir_intrinsic_image_atomic_xor
:
727 case nir_intrinsic_image_atomic_exchange
:
728 case nir_intrinsic_image_atomic_comp_swap
:
729 case nir_intrinsic_image_samples
:
730 case nir_intrinsic_image_size
: {
731 nir_ssa_undef_instr
*instr_undef
=
732 nir_ssa_undef_instr_create(shader
, 1);
733 nir_instr_insert_after_cf_list(this->cf_node_list
,
734 &instr_undef
->instr
);
736 /* Set the image variable dereference. */
737 exec_node
*param
= ir
->actual_parameters
.get_head();
738 ir_dereference
*image
= (ir_dereference
*)param
;
739 const glsl_type
*type
=
740 image
->variable_referenced()->type
->without_array();
742 instr
->variables
[0] = evaluate_deref(&instr
->instr
, image
);
743 param
= param
->get_next();
745 /* Set the intrinsic destination. */
746 if (ir
->return_deref
) {
747 const nir_intrinsic_info
*info
=
748 &nir_intrinsic_infos
[instr
->intrinsic
];
749 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
750 info
->dest_components
, NULL
);
753 if (op
== nir_intrinsic_image_size
||
754 op
== nir_intrinsic_image_samples
) {
755 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
759 /* Set the address argument, extending the coordinate vector to four
762 const nir_src src_addr
= evaluate_rvalue((ir_dereference
*)param
);
763 nir_alu_instr
*instr_addr
= nir_alu_instr_create(shader
, nir_op_vec4
);
764 nir_ssa_dest_init(&instr_addr
->instr
, &instr_addr
->dest
.dest
, 4, NULL
);
766 for (int i
= 0; i
< 4; i
++) {
767 if (i
< type
->coordinate_components()) {
768 instr_addr
->src
[i
].src
= src_addr
;
769 instr_addr
->src
[i
].swizzle
[0] = i
;
771 instr_addr
->src
[i
].src
= nir_src_for_ssa(&instr_undef
->def
);
775 nir_instr_insert_after_cf_list(cf_node_list
, &instr_addr
->instr
);
776 instr
->src
[0] = nir_src_for_ssa(&instr_addr
->dest
.dest
.ssa
);
777 param
= param
->get_next();
779 /* Set the sample argument, which is undefined for single-sample
782 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
783 instr
->src
[1] = evaluate_rvalue((ir_dereference
*)param
);
784 param
= param
->get_next();
786 instr
->src
[1] = nir_src_for_ssa(&instr_undef
->def
);
789 /* Set the intrinsic parameters. */
790 if (!param
->is_tail_sentinel()) {
791 instr
->src
[2] = evaluate_rvalue((ir_dereference
*)param
);
792 param
= param
->get_next();
795 if (!param
->is_tail_sentinel()) {
796 instr
->src
[3] = evaluate_rvalue((ir_dereference
*)param
);
797 param
= param
->get_next();
799 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
802 case nir_intrinsic_memory_barrier
:
803 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
805 case nir_intrinsic_store_ssbo
: {
806 exec_node
*param
= ir
->actual_parameters
.get_head();
807 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
809 param
= param
->get_next();
810 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
812 param
= param
->get_next();
813 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
815 param
= param
->get_next();
816 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
819 /* Check if we need the indirect version */
820 ir_constant
*const_offset
= offset
->as_constant();
822 op
= nir_intrinsic_store_ssbo_indirect
;
824 instr
= nir_intrinsic_instr_create(shader
, op
);
825 instr
->src
[2] = evaluate_rvalue(offset
);
826 instr
->const_index
[0] = 0;
828 instr
->const_index
[0] = const_offset
->value
.u
[0];
831 instr
->const_index
[1] = write_mask
->value
.u
[0];
833 instr
->src
[0] = evaluate_rvalue(val
);
834 instr
->num_components
= val
->type
->vector_elements
;
836 instr
->src
[1] = evaluate_rvalue(block
);
837 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
840 case nir_intrinsic_load_ssbo
: {
841 exec_node
*param
= ir
->actual_parameters
.get_head();
842 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
844 param
= param
->get_next();
845 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
847 /* Check if we need the indirect version */
848 ir_constant
*const_offset
= offset
->as_constant();
850 op
= nir_intrinsic_load_ssbo_indirect
;
852 instr
= nir_intrinsic_instr_create(shader
, op
);
853 instr
->src
[1] = evaluate_rvalue(offset
);
854 instr
->const_index
[0] = 0;
857 instr
->const_index
[0] = const_offset
->value
.u
[0];
860 instr
->src
[0] = evaluate_rvalue(block
);
862 const glsl_type
*type
= ir
->return_deref
->var
->type
;
863 instr
->num_components
= type
->vector_elements
;
865 /* Setup destination register */
866 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
867 type
->vector_elements
, NULL
);
869 /* Insert the created nir instruction now since in the case of boolean
870 * result we will need to emit another instruction after it
872 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
875 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
876 * consider a true boolean to be ~0. Fix this up with a != 0
879 if (type
->base_type
== GLSL_TYPE_BOOL
) {
880 nir_load_const_instr
*const_zero
=
881 nir_load_const_instr_create(shader
, 1);
882 const_zero
->value
.u
[0] = 0;
883 nir_instr_insert_after_cf_list(this->cf_node_list
,
886 nir_alu_instr
*load_ssbo_compare
=
887 nir_alu_instr_create(shader
, nir_op_ine
);
888 load_ssbo_compare
->src
[0].src
.is_ssa
= true;
889 load_ssbo_compare
->src
[0].src
.ssa
= &instr
->dest
.ssa
;
890 load_ssbo_compare
->src
[1].src
.is_ssa
= true;
891 load_ssbo_compare
->src
[1].src
.ssa
= &const_zero
->def
;
892 for (unsigned i
= 0; i
< type
->vector_elements
; i
++)
893 load_ssbo_compare
->src
[1].swizzle
[i
] = 0;
894 nir_ssa_dest_init(&load_ssbo_compare
->instr
,
895 &load_ssbo_compare
->dest
.dest
,
896 type
->vector_elements
, NULL
);
897 load_ssbo_compare
->dest
.write_mask
= (1 << type
->vector_elements
) - 1;
898 nir_instr_insert_after_cf_list(this->cf_node_list
,
899 &load_ssbo_compare
->instr
);
900 dest
= &load_ssbo_compare
->dest
.dest
;
904 case nir_intrinsic_ssbo_atomic_add
:
905 case nir_intrinsic_ssbo_atomic_min
:
906 case nir_intrinsic_ssbo_atomic_max
:
907 case nir_intrinsic_ssbo_atomic_and
:
908 case nir_intrinsic_ssbo_atomic_or
:
909 case nir_intrinsic_ssbo_atomic_xor
:
910 case nir_intrinsic_ssbo_atomic_exchange
:
911 case nir_intrinsic_ssbo_atomic_comp_swap
: {
912 int param_count
= ir
->actual_parameters
.length();
913 assert(param_count
== 3 || param_count
== 4);
916 exec_node
*param
= ir
->actual_parameters
.get_head();
917 ir_instruction
*inst
= (ir_instruction
*) param
;
918 instr
->src
[0] = evaluate_rvalue(inst
->as_rvalue());
921 param
= param
->get_next();
922 inst
= (ir_instruction
*) param
;
923 instr
->src
[1] = evaluate_rvalue(inst
->as_rvalue());
925 /* data1 parameter (this is always present) */
926 param
= param
->get_next();
927 inst
= (ir_instruction
*) param
;
928 instr
->src
[2] = evaluate_rvalue(inst
->as_rvalue());
930 /* data2 parameter (only with atomic_comp_swap) */
931 if (param_count
== 4) {
932 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
);
933 param
= param
->get_next();
934 inst
= (ir_instruction
*) param
;
935 instr
->src
[3] = evaluate_rvalue(inst
->as_rvalue());
939 assert(ir
->return_deref
);
940 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
941 ir
->return_deref
->type
->vector_elements
, NULL
);
942 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
946 unreachable("not reached");
949 if (ir
->return_deref
) {
950 nir_intrinsic_instr
*store_instr
=
951 nir_intrinsic_instr_create(shader
, nir_intrinsic_store_var
);
952 store_instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
954 store_instr
->variables
[0] =
955 evaluate_deref(&store_instr
->instr
, ir
->return_deref
);
956 store_instr
->src
[0] = nir_src_for_ssa(&dest
->ssa
);
958 nir_instr_insert_after_cf_list(this->cf_node_list
,
959 &store_instr
->instr
);
965 struct hash_entry
*entry
=
966 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
968 nir_function_overload
*callee
= (nir_function_overload
*) entry
->data
;
970 nir_call_instr
*instr
= nir_call_instr_create(this->shader
, callee
);
973 foreach_in_list(ir_dereference
, param
, &ir
->actual_parameters
) {
974 instr
->params
[i
] = evaluate_deref(&instr
->instr
, param
);
978 instr
->return_deref
= evaluate_deref(&instr
->instr
, ir
->return_deref
);
979 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);
983 nir_visitor::visit(ir_assignment
*ir
)
985 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
987 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
988 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
989 /* We're doing a plain-as-can-be copy, so emit a copy_var */
990 nir_intrinsic_instr
*copy
=
991 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_copy_var
);
993 copy
->variables
[0] = evaluate_deref(©
->instr
, ir
->lhs
);
994 copy
->variables
[1] = evaluate_deref(©
->instr
, ir
->rhs
);
997 nir_if
*if_stmt
= nir_if_create(this->shader
);
998 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
999 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
1000 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, ©
->instr
);
1002 nir_instr_insert_after_cf_list(this->cf_node_list
, ©
->instr
);
1007 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1009 ir
->lhs
->accept(this);
1010 nir_deref_var
*lhs_deref
= this->deref_head
;
1011 nir_src src
= evaluate_rvalue(ir
->rhs
);
1013 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1015 * We have no good way to update only part of a variable, so just load
1016 * the LHS and do a vec operation to combine the old with the new, and
1018 * back into the LHS. Copy propagation should get rid of the mess.
1021 nir_intrinsic_instr
*load
=
1022 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1023 load
->num_components
= ir
->lhs
->type
->vector_elements
;
1024 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
1025 load
->variables
[0] = lhs_deref
;
1026 ralloc_steal(load
, load
->variables
[0]);
1027 nir_instr_insert_after_cf_list(this->cf_node_list
, &load
->instr
);
1030 switch (ir
->lhs
->type
->vector_elements
) {
1031 case 1: vec_op
= nir_op_imov
; break;
1032 case 2: vec_op
= nir_op_vec2
; break;
1033 case 3: vec_op
= nir_op_vec3
; break;
1034 case 4: vec_op
= nir_op_vec4
; break;
1035 default: unreachable("Invalid number of components"); break;
1037 nir_alu_instr
*vec
= nir_alu_instr_create(this->shader
, vec_op
);
1038 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
1039 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1041 unsigned component
= 0;
1042 for (unsigned i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1043 if (ir
->write_mask
& (1 << i
)) {
1044 vec
->src
[i
].src
= src
;
1046 /* GLSL IR will give us the input to the write-masked assignment
1047 * in a single packed vector. So, for example, if the
1048 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1049 * and z -> w and get the y component from the load.
1051 vec
->src
[i
].swizzle
[0] = component
++;
1053 vec
->src
[i
].src
.is_ssa
= true;
1054 vec
->src
[i
].src
.ssa
= &load
->dest
.ssa
;
1055 vec
->src
[i
].swizzle
[0] = i
;
1059 nir_instr_insert_after_cf_list(this->cf_node_list
, &vec
->instr
);
1062 src
.ssa
= &vec
->dest
.dest
.ssa
;
1065 nir_intrinsic_instr
*store
=
1066 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_store_var
);
1067 store
->num_components
= ir
->lhs
->type
->vector_elements
;
1068 nir_deref
*store_deref
= nir_copy_deref(store
, &lhs_deref
->deref
);
1069 store
->variables
[0] = nir_deref_as_var(store_deref
);
1070 store
->src
[0] = src
;
1072 if (ir
->condition
) {
1073 nir_if
*if_stmt
= nir_if_create(this->shader
);
1074 if_stmt
->condition
= evaluate_rvalue(ir
->condition
);
1075 nir_cf_node_insert_end(this->cf_node_list
, &if_stmt
->cf_node
);
1076 nir_instr_insert_after_cf_list(&if_stmt
->then_list
, &store
->instr
);
1078 nir_instr_insert_after_cf_list(this->cf_node_list
, &store
->instr
);
1083 * Given an instruction, returns a pointer to its destination or NULL if there
1084 * is no destination.
1086 * Note that this only handles instructions we generate at this level.
1089 get_instr_dest(nir_instr
*instr
)
1091 nir_alu_instr
*alu_instr
;
1092 nir_intrinsic_instr
*intrinsic_instr
;
1093 nir_tex_instr
*tex_instr
;
1095 switch (instr
->type
) {
1096 case nir_instr_type_alu
:
1097 alu_instr
= nir_instr_as_alu(instr
);
1098 return &alu_instr
->dest
.dest
;
1100 case nir_instr_type_intrinsic
:
1101 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1102 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1103 return &intrinsic_instr
->dest
;
1107 case nir_instr_type_tex
:
1108 tex_instr
= nir_instr_as_tex(instr
);
1109 return &tex_instr
->dest
;
1112 unreachable("not reached");
1119 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
)
1121 nir_dest
*dest
= get_instr_dest(instr
);
1124 nir_ssa_dest_init(instr
, dest
, num_components
, NULL
);
1126 nir_instr_insert_after_cf_list(this->cf_node_list
, instr
);
1127 this->result
= instr
;
1131 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1134 if (ir
->as_dereference() || ir
->as_constant()) {
1136 * A dereference is being used on the right hand side, which means we
1137 * must emit a variable load.
1140 nir_intrinsic_instr
*load_instr
=
1141 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_var
);
1142 load_instr
->num_components
= ir
->type
->vector_elements
;
1143 load_instr
->variables
[0] = this->deref_head
;
1144 ralloc_steal(load_instr
, load_instr
->variables
[0]);
1145 add_instr(&load_instr
->instr
, ir
->type
->vector_elements
);
1148 nir_dest
*dest
= get_instr_dest(this->result
);
1149 assert(dest
->is_ssa
);
1151 return nir_src_for_ssa(&dest
->ssa
);
1155 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src
*srcs
)
1157 nir_alu_instr
*instr
= nir_alu_instr_create(this->shader
, op
);
1158 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
1159 instr
->src
[i
].src
= srcs
[i
];
1160 instr
->dest
.write_mask
= (1 << dest_size
) - 1;
1161 add_instr(&instr
->instr
, dest_size
);
1166 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
)
1168 assert(nir_op_infos
[op
].num_inputs
== 1);
1169 return emit(op
, dest_size
, &src1
);
1173 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
1176 assert(nir_op_infos
[op
].num_inputs
== 2);
1177 nir_src srcs
[] = { src1
, src2
};
1178 return emit(op
, dest_size
, srcs
);
1182 nir_visitor::emit(nir_op op
, unsigned dest_size
, nir_src src1
,
1183 nir_src src2
, nir_src src3
)
1185 assert(nir_op_infos
[op
].num_inputs
== 3);
1186 nir_src srcs
[] = { src1
, src2
, src3
};
1187 return emit(op
, dest_size
, srcs
);
1191 nir_visitor::visit(ir_expression
*ir
)
1193 /* Some special cases */
1194 switch (ir
->operation
) {
1195 case ir_binop_ubo_load
: {
1196 ir_constant
*const_index
= ir
->operands
[1]->as_constant();
1198 nir_intrinsic_op op
;
1200 op
= nir_intrinsic_load_ubo
;
1202 op
= nir_intrinsic_load_ubo_indirect
;
1204 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(this->shader
, op
);
1205 load
->num_components
= ir
->type
->vector_elements
;
1206 load
->const_index
[0] = const_index
? const_index
->value
.u
[0] : 0; /* base offset */
1207 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
1209 load
->src
[1] = evaluate_rvalue(ir
->operands
[1]);
1210 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1213 * In UBO's, a true boolean value is any non-zero value, but we consider
1214 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1217 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1218 nir_load_const_instr
*const_zero
= nir_load_const_instr_create(shader
, 1);
1219 const_zero
->value
.u
[0] = 0;
1220 nir_instr_insert_after_cf_list(this->cf_node_list
, &const_zero
->instr
);
1222 nir_alu_instr
*compare
= nir_alu_instr_create(shader
, nir_op_ine
);
1223 compare
->src
[0].src
.is_ssa
= true;
1224 compare
->src
[0].src
.ssa
= &load
->dest
.ssa
;
1225 compare
->src
[1].src
.is_ssa
= true;
1226 compare
->src
[1].src
.ssa
= &const_zero
->def
;
1227 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1228 compare
->src
[1].swizzle
[i
] = 0;
1229 compare
->dest
.write_mask
= (1 << ir
->type
->vector_elements
) - 1;
1231 add_instr(&compare
->instr
, ir
->type
->vector_elements
);
1237 case ir_unop_interpolate_at_centroid
:
1238 case ir_binop_interpolate_at_offset
:
1239 case ir_binop_interpolate_at_sample
: {
1240 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1241 ir_swizzle
*swizzle
= NULL
;
1243 /* the api does not allow a swizzle here, but the varying packing code
1244 * may have pushed one into here.
1246 swizzle
= ir
->operands
[0]->as_swizzle();
1248 deref
= swizzle
->val
->as_dereference();
1252 deref
->accept(this);
1254 nir_intrinsic_op op
;
1255 if (this->deref_head
->var
->data
.mode
== nir_var_shader_in
) {
1256 switch (ir
->operation
) {
1257 case ir_unop_interpolate_at_centroid
:
1258 op
= nir_intrinsic_interp_var_at_centroid
;
1260 case ir_binop_interpolate_at_offset
:
1261 op
= nir_intrinsic_interp_var_at_offset
;
1263 case ir_binop_interpolate_at_sample
:
1264 op
= nir_intrinsic_interp_var_at_sample
;
1267 unreachable("Invalid interpolation intrinsic");
1270 /* This case can happen if the vertex shader does not write the
1271 * given varying. In this case, the linker will lower it to a
1272 * global variable. Since interpolating a variable makes no
1273 * sense, we'll just turn it into a load which will probably
1274 * eventually end up as an SSA definition.
1276 assert(this->deref_head
->var
->data
.mode
== nir_var_global
);
1277 op
= nir_intrinsic_load_var
;
1280 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1281 intrin
->num_components
= deref
->type
->vector_elements
;
1282 intrin
->variables
[0] = this->deref_head
;
1283 ralloc_steal(intrin
, intrin
->variables
[0]);
1285 if (intrin
->intrinsic
== nir_intrinsic_interp_var_at_offset
||
1286 intrin
->intrinsic
== nir_intrinsic_interp_var_at_sample
)
1287 intrin
->src
[0] = evaluate_rvalue(ir
->operands
[1]);
1289 add_instr(&intrin
->instr
, deref
->type
->vector_elements
);
1292 nir_alu_instr
*mov
= nir_alu_instr_create(shader
, nir_op_imov
);
1293 mov
->dest
.write_mask
= (1 << swizzle
->type
->vector_elements
) - 1;
1294 mov
->src
[0].src
.is_ssa
= true;
1295 mov
->src
[0].src
.ssa
= &intrin
->dest
.ssa
;
1297 mov
->src
[0].swizzle
[0] = swizzle
->mask
.x
;
1298 mov
->src
[0].swizzle
[1] = swizzle
->mask
.y
;
1299 mov
->src
[0].swizzle
[2] = swizzle
->mask
.z
;
1300 mov
->src
[0].swizzle
[3] = swizzle
->mask
.w
;
1301 for (unsigned i
= deref
->type
->vector_elements
; i
< 4; i
++)
1302 mov
->src
[0].swizzle
[i
] = 0;
1304 add_instr(&mov
->instr
, swizzle
->type
->vector_elements
);
1315 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1316 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1318 glsl_base_type types
[4];
1319 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++)
1321 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1323 types
[i
] = GLSL_TYPE_FLOAT
;
1325 glsl_base_type out_type
;
1327 out_type
= ir
->type
->base_type
;
1329 out_type
= GLSL_TYPE_FLOAT
;
1331 unsigned dest_size
= ir
->type
->vector_elements
;
1333 nir_alu_instr
*instr
;
1336 switch (ir
->operation
) {
1337 case ir_unop_bit_not
: emit(nir_op_inot
, dest_size
, srcs
); break;
1338 case ir_unop_logic_not
:
1339 emit(supports_ints
? nir_op_inot
: nir_op_fnot
, dest_size
, srcs
);
1342 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fneg
: nir_op_ineg
,
1346 instr
= emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fabs
: nir_op_iabs
,
1349 case ir_unop_saturate
:
1350 assert(types
[0] == GLSL_TYPE_FLOAT
);
1351 instr
= emit(nir_op_fsat
, dest_size
, srcs
);
1354 emit(types
[0] == GLSL_TYPE_FLOAT
? nir_op_fsign
: nir_op_isign
,
1357 case ir_unop_rcp
: emit(nir_op_frcp
, dest_size
, srcs
); break;
1358 case ir_unop_rsq
: emit(nir_op_frsq
, dest_size
, srcs
); break;
1359 case ir_unop_sqrt
: emit(nir_op_fsqrt
, dest_size
, srcs
); break;
1360 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1361 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1362 case ir_unop_exp2
: emit(nir_op_fexp2
, dest_size
, srcs
); break;
1363 case ir_unop_log2
: emit(nir_op_flog2
, dest_size
, srcs
); break;
1365 emit(supports_ints
? nir_op_i2f
: nir_op_fmov
, dest_size
, srcs
);
1368 emit(supports_ints
? nir_op_u2f
: nir_op_fmov
, dest_size
, srcs
);
1371 emit(supports_ints
? nir_op_b2f
: nir_op_fmov
, dest_size
, srcs
);
1373 case ir_unop_f2i
: emit(nir_op_f2i
, dest_size
, srcs
); break;
1374 case ir_unop_f2u
: emit(nir_op_f2u
, dest_size
, srcs
); break;
1375 case ir_unop_f2b
: emit(nir_op_f2b
, dest_size
, srcs
); break;
1376 case ir_unop_i2b
: emit(nir_op_i2b
, dest_size
, srcs
); break;
1377 case ir_unop_b2i
: emit(nir_op_b2i
, dest_size
, srcs
); break;
1380 case ir_unop_bitcast_i2f
:
1381 case ir_unop_bitcast_f2i
:
1382 case ir_unop_bitcast_u2f
:
1383 case ir_unop_bitcast_f2u
:
1384 case ir_unop_subroutine_to_int
:
1386 emit(nir_op_imov
, dest_size
, srcs
);
1389 switch (ir
->operands
[0]->type
->vector_elements
) {
1391 emit(supports_ints
? nir_op_bany2
: nir_op_fany2
,
1395 emit(supports_ints
? nir_op_bany3
: nir_op_fany3
,
1399 emit(supports_ints
? nir_op_bany4
: nir_op_fany4
,
1403 unreachable("not reached");
1406 case ir_unop_trunc
: emit(nir_op_ftrunc
, dest_size
, srcs
); break;
1407 case ir_unop_ceil
: emit(nir_op_fceil
, dest_size
, srcs
); break;
1408 case ir_unop_floor
: emit(nir_op_ffloor
, dest_size
, srcs
); break;
1409 case ir_unop_fract
: emit(nir_op_ffract
, dest_size
, srcs
); break;
1410 case ir_unop_round_even
: emit(nir_op_fround_even
, dest_size
, srcs
); break;
1411 case ir_unop_sin
: emit(nir_op_fsin
, dest_size
, srcs
); break;
1412 case ir_unop_cos
: emit(nir_op_fcos
, dest_size
, srcs
); break;
1413 case ir_unop_dFdx
: emit(nir_op_fddx
, dest_size
, srcs
); break;
1414 case ir_unop_dFdy
: emit(nir_op_fddy
, dest_size
, srcs
); break;
1415 case ir_unop_dFdx_fine
: emit(nir_op_fddx_fine
, dest_size
, srcs
); break;
1416 case ir_unop_dFdy_fine
: emit(nir_op_fddy_fine
, dest_size
, srcs
); break;
1417 case ir_unop_dFdx_coarse
: emit(nir_op_fddx_coarse
, dest_size
, srcs
); break;
1418 case ir_unop_dFdy_coarse
: emit(nir_op_fddy_coarse
, dest_size
, srcs
); break;
1419 case ir_unop_pack_snorm_2x16
:
1420 emit(nir_op_pack_snorm_2x16
, dest_size
, srcs
);
1422 case ir_unop_pack_snorm_4x8
:
1423 emit(nir_op_pack_snorm_4x8
, dest_size
, srcs
);
1425 case ir_unop_pack_unorm_2x16
:
1426 emit(nir_op_pack_unorm_2x16
, dest_size
, srcs
);
1428 case ir_unop_pack_unorm_4x8
:
1429 emit(nir_op_pack_unorm_4x8
, dest_size
, srcs
);
1431 case ir_unop_pack_half_2x16
:
1432 emit(nir_op_pack_half_2x16
, dest_size
, srcs
);
1434 case ir_unop_unpack_snorm_2x16
:
1435 emit(nir_op_unpack_snorm_2x16
, dest_size
, srcs
);
1437 case ir_unop_unpack_snorm_4x8
:
1438 emit(nir_op_unpack_snorm_4x8
, dest_size
, srcs
);
1440 case ir_unop_unpack_unorm_2x16
:
1441 emit(nir_op_unpack_unorm_2x16
, dest_size
, srcs
);
1443 case ir_unop_unpack_unorm_4x8
:
1444 emit(nir_op_unpack_unorm_4x8
, dest_size
, srcs
);
1446 case ir_unop_unpack_half_2x16
:
1447 emit(nir_op_unpack_half_2x16
, dest_size
, srcs
);
1449 case ir_unop_unpack_half_2x16_split_x
:
1450 emit(nir_op_unpack_half_2x16_split_x
, dest_size
, srcs
);
1452 case ir_unop_unpack_half_2x16_split_y
:
1453 emit(nir_op_unpack_half_2x16_split_y
, dest_size
, srcs
);
1455 case ir_unop_bitfield_reverse
:
1456 emit(nir_op_bitfield_reverse
, dest_size
, srcs
);
1458 case ir_unop_bit_count
:
1459 emit(nir_op_bit_count
, dest_size
, srcs
);
1461 case ir_unop_find_msb
:
1463 case GLSL_TYPE_UINT
:
1464 emit(nir_op_ufind_msb
, dest_size
, srcs
);
1467 emit(nir_op_ifind_msb
, dest_size
, srcs
);
1470 unreachable("Invalid type for findMSB()");
1473 case ir_unop_find_lsb
:
1474 emit(nir_op_find_lsb
, dest_size
, srcs
);
1478 switch (ir
->type
->vector_elements
) {
1480 switch (ir
->operands
[0]->type
->vector_elements
) {
1481 case 1: emit(nir_op_fnoise1_1
, dest_size
, srcs
); break;
1482 case 2: emit(nir_op_fnoise1_2
, dest_size
, srcs
); break;
1483 case 3: emit(nir_op_fnoise1_3
, dest_size
, srcs
); break;
1484 case 4: emit(nir_op_fnoise1_4
, dest_size
, srcs
); break;
1485 default: unreachable("not reached");
1489 switch (ir
->operands
[0]->type
->vector_elements
) {
1490 case 1: emit(nir_op_fnoise2_1
, dest_size
, srcs
); break;
1491 case 2: emit(nir_op_fnoise2_2
, dest_size
, srcs
); break;
1492 case 3: emit(nir_op_fnoise2_3
, dest_size
, srcs
); break;
1493 case 4: emit(nir_op_fnoise2_4
, dest_size
, srcs
); break;
1494 default: unreachable("not reached");
1498 switch (ir
->operands
[0]->type
->vector_elements
) {
1499 case 1: emit(nir_op_fnoise3_1
, dest_size
, srcs
); break;
1500 case 2: emit(nir_op_fnoise3_2
, dest_size
, srcs
); break;
1501 case 3: emit(nir_op_fnoise3_3
, dest_size
, srcs
); break;
1502 case 4: emit(nir_op_fnoise3_4
, dest_size
, srcs
); break;
1503 default: unreachable("not reached");
1507 switch (ir
->operands
[0]->type
->vector_elements
) {
1508 case 1: emit(nir_op_fnoise4_1
, dest_size
, srcs
); break;
1509 case 2: emit(nir_op_fnoise4_2
, dest_size
, srcs
); break;
1510 case 3: emit(nir_op_fnoise4_3
, dest_size
, srcs
); break;
1511 case 4: emit(nir_op_fnoise4_4
, dest_size
, srcs
); break;
1512 default: unreachable("not reached");
1516 unreachable("not reached");
1519 case ir_unop_get_buffer_size
: {
1520 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1522 nir_intrinsic_get_buffer_size
);
1523 load
->num_components
= ir
->type
->vector_elements
;
1524 load
->src
[0] = evaluate_rvalue(ir
->operands
[0]);
1525 add_instr(&load
->instr
, ir
->type
->vector_elements
);
1537 case ir_binop_bit_and
:
1538 case ir_binop_bit_or
:
1539 case ir_binop_bit_xor
:
1540 case ir_binop_logic_and
:
1541 case ir_binop_logic_or
:
1542 case ir_binop_logic_xor
:
1543 case ir_binop_lshift
:
1544 case ir_binop_rshift
:
1545 switch (ir
->operation
) {
1547 if (out_type
== GLSL_TYPE_FLOAT
)
1553 if (out_type
== GLSL_TYPE_FLOAT
)
1559 if (out_type
== GLSL_TYPE_FLOAT
)
1565 if (out_type
== GLSL_TYPE_FLOAT
)
1567 else if (out_type
== GLSL_TYPE_INT
)
1573 if (out_type
== GLSL_TYPE_FLOAT
)
1579 if (out_type
== GLSL_TYPE_FLOAT
)
1581 else if (out_type
== GLSL_TYPE_INT
)
1587 if (out_type
== GLSL_TYPE_FLOAT
)
1589 else if (out_type
== GLSL_TYPE_INT
)
1594 case ir_binop_bit_and
:
1597 case ir_binop_bit_or
:
1600 case ir_binop_bit_xor
:
1603 case ir_binop_logic_and
:
1609 case ir_binop_logic_or
:
1615 case ir_binop_logic_xor
:
1621 case ir_binop_lshift
:
1624 case ir_binop_rshift
:
1625 if (out_type
== GLSL_TYPE_INT
)
1635 unreachable("not reached");
1638 instr
= emit(op
, dest_size
, srcs
);
1640 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1641 ir
->operands
[1]->type
->vector_elements
== 1) {
1642 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1644 instr
->src
[1].swizzle
[i
] = 0;
1648 if (ir
->operands
[1]->type
->vector_elements
!= 1 &&
1649 ir
->operands
[0]->type
->vector_elements
== 1) {
1650 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
;
1652 instr
->src
[0].swizzle
[i
] = 0;
1657 case ir_binop_imul_high
:
1658 emit(out_type
== GLSL_TYPE_UINT
? nir_op_umul_high
: nir_op_imul_high
,
1661 case ir_binop_carry
: emit(nir_op_uadd_carry
, dest_size
, srcs
); break;
1662 case ir_binop_borrow
: emit(nir_op_usub_borrow
, dest_size
, srcs
); break;
1664 if (supports_ints
) {
1665 if (types
[0] == GLSL_TYPE_FLOAT
)
1666 emit(nir_op_flt
, dest_size
, srcs
);
1667 else if (types
[0] == GLSL_TYPE_INT
)
1668 emit(nir_op_ilt
, dest_size
, srcs
);
1670 emit(nir_op_ult
, dest_size
, srcs
);
1672 emit(nir_op_slt
, dest_size
, srcs
);
1675 case ir_binop_greater
:
1676 if (supports_ints
) {
1677 if (types
[0] == GLSL_TYPE_FLOAT
)
1678 emit(nir_op_flt
, dest_size
, srcs
[1], srcs
[0]);
1679 else if (types
[0] == GLSL_TYPE_INT
)
1680 emit(nir_op_ilt
, dest_size
, srcs
[1], srcs
[0]);
1682 emit(nir_op_ult
, dest_size
, srcs
[1], srcs
[0]);
1684 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1687 case ir_binop_lequal
:
1688 if (supports_ints
) {
1689 if (types
[0] == GLSL_TYPE_FLOAT
)
1690 emit(nir_op_fge
, dest_size
, srcs
[1], srcs
[0]);
1691 else if (types
[0] == GLSL_TYPE_INT
)
1692 emit(nir_op_ige
, dest_size
, srcs
[1], srcs
[0]);
1694 emit(nir_op_uge
, dest_size
, srcs
[1], srcs
[0]);
1696 emit(nir_op_slt
, dest_size
, srcs
[1], srcs
[0]);
1699 case ir_binop_gequal
:
1700 if (supports_ints
) {
1701 if (types
[0] == GLSL_TYPE_FLOAT
)
1702 emit(nir_op_fge
, dest_size
, srcs
);
1703 else if (types
[0] == GLSL_TYPE_INT
)
1704 emit(nir_op_ige
, dest_size
, srcs
);
1706 emit(nir_op_uge
, dest_size
, srcs
);
1708 emit(nir_op_slt
, dest_size
, srcs
);
1711 case ir_binop_equal
:
1712 if (supports_ints
) {
1713 if (types
[0] == GLSL_TYPE_FLOAT
)
1714 emit(nir_op_feq
, dest_size
, srcs
);
1716 emit(nir_op_ieq
, dest_size
, srcs
);
1718 emit(nir_op_seq
, dest_size
, srcs
);
1721 case ir_binop_nequal
:
1722 if (supports_ints
) {
1723 if (types
[0] == GLSL_TYPE_FLOAT
)
1724 emit(nir_op_fne
, dest_size
, srcs
);
1726 emit(nir_op_ine
, dest_size
, srcs
);
1728 emit(nir_op_sne
, dest_size
, srcs
);
1731 case ir_binop_all_equal
:
1732 if (supports_ints
) {
1733 if (types
[0] == GLSL_TYPE_FLOAT
) {
1734 switch (ir
->operands
[0]->type
->vector_elements
) {
1735 case 1: emit(nir_op_feq
, dest_size
, srcs
); break;
1736 case 2: emit(nir_op_ball_fequal2
, dest_size
, srcs
); break;
1737 case 3: emit(nir_op_ball_fequal3
, dest_size
, srcs
); break;
1738 case 4: emit(nir_op_ball_fequal4
, dest_size
, srcs
); break;
1740 unreachable("not reached");
1743 switch (ir
->operands
[0]->type
->vector_elements
) {
1744 case 1: emit(nir_op_ieq
, dest_size
, srcs
); break;
1745 case 2: emit(nir_op_ball_iequal2
, dest_size
, srcs
); break;
1746 case 3: emit(nir_op_ball_iequal3
, dest_size
, srcs
); break;
1747 case 4: emit(nir_op_ball_iequal4
, dest_size
, srcs
); break;
1749 unreachable("not reached");
1753 switch (ir
->operands
[0]->type
->vector_elements
) {
1754 case 1: emit(nir_op_seq
, dest_size
, srcs
); break;
1755 case 2: emit(nir_op_fall_equal2
, dest_size
, srcs
); break;
1756 case 3: emit(nir_op_fall_equal3
, dest_size
, srcs
); break;
1757 case 4: emit(nir_op_fall_equal4
, dest_size
, srcs
); break;
1759 unreachable("not reached");
1763 case ir_binop_any_nequal
:
1764 if (supports_ints
) {
1765 if (types
[0] == GLSL_TYPE_FLOAT
) {
1766 switch (ir
->operands
[0]->type
->vector_elements
) {
1767 case 1: emit(nir_op_fne
, dest_size
, srcs
); break;
1768 case 2: emit(nir_op_bany_fnequal2
, dest_size
, srcs
); break;
1769 case 3: emit(nir_op_bany_fnequal3
, dest_size
, srcs
); break;
1770 case 4: emit(nir_op_bany_fnequal4
, dest_size
, srcs
); break;
1772 unreachable("not reached");
1775 switch (ir
->operands
[0]->type
->vector_elements
) {
1776 case 1: emit(nir_op_ine
, dest_size
, srcs
); break;
1777 case 2: emit(nir_op_bany_inequal2
, dest_size
, srcs
); break;
1778 case 3: emit(nir_op_bany_inequal3
, dest_size
, srcs
); break;
1779 case 4: emit(nir_op_bany_inequal4
, dest_size
, srcs
); break;
1781 unreachable("not reached");
1785 switch (ir
->operands
[0]->type
->vector_elements
) {
1786 case 1: emit(nir_op_sne
, dest_size
, srcs
); break;
1787 case 2: emit(nir_op_fany_nequal2
, dest_size
, srcs
); break;
1788 case 3: emit(nir_op_fany_nequal3
, dest_size
, srcs
); break;
1789 case 4: emit(nir_op_fany_nequal4
, dest_size
, srcs
); break;
1791 unreachable("not reached");
1796 switch (ir
->operands
[0]->type
->vector_elements
) {
1797 case 2: emit(nir_op_fdot2
, dest_size
, srcs
); break;
1798 case 3: emit(nir_op_fdot3
, dest_size
, srcs
); break;
1799 case 4: emit(nir_op_fdot4
, dest_size
, srcs
); break;
1801 unreachable("not reached");
1805 case ir_binop_pack_half_2x16_split
:
1806 emit(nir_op_pack_half_2x16_split
, dest_size
, srcs
);
1808 case ir_binop_bfm
: emit(nir_op_bfm
, dest_size
, srcs
); break;
1809 case ir_binop_ldexp
: emit(nir_op_ldexp
, dest_size
, srcs
); break;
1810 case ir_triop_fma
: emit(nir_op_ffma
, dest_size
, srcs
); break;
1812 instr
= emit(nir_op_flrp
, dest_size
, srcs
);
1813 if (ir
->operands
[0]->type
->vector_elements
!= 1 &&
1814 ir
->operands
[2]->type
->vector_elements
== 1) {
1815 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
;
1817 instr
->src
[2].swizzle
[i
] = 0;
1823 emit(nir_op_bcsel
, dest_size
, srcs
);
1825 emit(nir_op_fcsel
, dest_size
, srcs
);
1828 instr
= emit(nir_op_bfi
, dest_size
, srcs
);
1829 for (unsigned i
= 0; i
< ir
->operands
[1]->type
->vector_elements
; i
++) {
1830 instr
->src
[0].swizzle
[i
] = 0;
1833 case ir_triop_bitfield_extract
:
1834 instr
= emit(out_type
== GLSL_TYPE_INT
? nir_op_ibitfield_extract
:
1835 nir_op_ubitfield_extract
, dest_size
, srcs
);
1836 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1837 instr
->src
[1].swizzle
[i
] = 0;
1838 instr
->src
[2].swizzle
[i
] = 0;
1841 case ir_quadop_bitfield_insert
:
1842 instr
= emit(nir_op_bitfield_insert
, dest_size
, srcs
);
1843 for (unsigned i
= 0; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
1844 instr
->src
[2].swizzle
[i
] = 0;
1845 instr
->src
[3].swizzle
[i
] = 0;
1848 case ir_quadop_vector
:
1849 switch (ir
->type
->vector_elements
) {
1850 case 2: emit(nir_op_vec2
, dest_size
, srcs
); break;
1851 case 3: emit(nir_op_vec3
, dest_size
, srcs
); break;
1852 case 4: emit(nir_op_vec4
, dest_size
, srcs
); break;
1853 default: unreachable("not reached");
1858 unreachable("not reached");
1863 nir_visitor::visit(ir_swizzle
*ir
)
1865 nir_alu_instr
*instr
= emit(supports_ints
? nir_op_imov
: nir_op_fmov
,
1866 ir
->type
->vector_elements
,
1867 evaluate_rvalue(ir
->val
));
1869 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
1870 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++)
1871 instr
->src
[0].swizzle
[i
] = swizzle
[i
];
1875 nir_visitor::visit(ir_texture
*ir
)
1882 num_srcs
= 1; /* coordinate */
1887 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
1888 num_srcs
= 2; /* coordinate, bias/lod */
1892 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
1898 if (ir
->lod_info
.lod
!= NULL
)
1899 num_srcs
= 2; /* coordinate, lod */
1901 num_srcs
= 1; /* coordinate */
1905 op
= nir_texop_txf_ms
;
1906 num_srcs
= 2; /* coordinate, sample_index */
1911 if (ir
->lod_info
.lod
!= NULL
)
1912 num_srcs
= 1; /* lod */
1919 num_srcs
= 1; /* coordinate */
1924 num_srcs
= 1; /* coordinate */
1927 case ir_query_levels
:
1928 op
= nir_texop_query_levels
;
1932 case ir_texture_samples
:
1933 op
= nir_texop_texture_samples
;
1938 unreachable("not reached");
1941 if (ir
->projector
!= NULL
)
1943 if (ir
->shadow_comparitor
!= NULL
)
1945 if (ir
->offset
!= NULL
&& ir
->offset
->as_constant() == NULL
)
1948 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
1951 instr
->sampler_dim
=
1952 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
1953 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
1954 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
1955 if (instr
->is_shadow
)
1956 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
1957 switch (ir
->type
->base_type
) {
1958 case GLSL_TYPE_FLOAT
:
1959 instr
->dest_type
= nir_type_float
;
1962 instr
->dest_type
= nir_type_int
;
1964 case GLSL_TYPE_UINT
:
1965 instr
->dest_type
= nir_type_unsigned
;
1968 unreachable("not reached");
1971 instr
->sampler
= evaluate_deref(&instr
->instr
, ir
->sampler
);
1973 unsigned src_number
= 0;
1975 if (ir
->coordinate
!= NULL
) {
1976 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
1977 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->coordinate
);
1978 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
1982 if (ir
->projector
!= NULL
) {
1983 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->projector
);
1984 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
1988 if (ir
->shadow_comparitor
!= NULL
) {
1989 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->shadow_comparitor
);
1990 instr
->src
[src_number
].src_type
= nir_tex_src_comparitor
;
1994 if (ir
->offset
!= NULL
) {
1995 /* we don't support multiple offsets yet */
1996 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
1998 ir_constant
*const_offset
= ir
->offset
->as_constant();
1999 if (const_offset
!= NULL
) {
2000 for (unsigned i
= 0; i
< const_offset
->type
->vector_elements
; i
++)
2001 instr
->const_offset
[i
] = const_offset
->value
.i
[i
];
2003 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->offset
);
2004 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
2011 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.bias
);
2012 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
2019 if (ir
->lod_info
.lod
!= NULL
) {
2020 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.lod
);
2021 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
2027 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdx
);
2028 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
2030 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.grad
.dPdy
);
2031 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
2036 instr
->src
[src_number
].src
= evaluate_rvalue(ir
->lod_info
.sample_index
);
2037 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
2042 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
2049 assert(src_number
== num_srcs
);
2051 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
));
2055 nir_visitor::visit(ir_constant
*ir
)
2058 * We don't know if this variable is an an array or struct that gets
2059 * dereferenced, so do the safe thing an make it a variable with a
2060 * constant initializer and return a dereference.
2063 nir_variable
*var
= ralloc(this->shader
, nir_variable
);
2064 var
->name
= ralloc_strdup(var
, "const_temp");
2065 var
->type
= ir
->type
;
2066 var
->data
.mode
= nir_var_local
;
2067 var
->data
.read_only
= true;
2068 var
->constant_initializer
= constant_copy(ir
, var
);
2069 exec_list_push_tail(&this->impl
->locals
, &var
->node
);
2071 this->deref_head
= nir_deref_var_create(this->shader
, var
);
2072 this->deref_tail
= &this->deref_head
->deref
;
2076 nir_visitor::visit(ir_dereference_variable
*ir
)
2078 struct hash_entry
*entry
=
2079 _mesa_hash_table_search(this->var_table
, ir
->var
);
2081 nir_variable
*var
= (nir_variable
*) entry
->data
;
2083 nir_deref_var
*deref
= nir_deref_var_create(this->shader
, var
);
2084 this->deref_head
= deref
;
2085 this->deref_tail
= &deref
->deref
;
2089 nir_visitor::visit(ir_dereference_record
*ir
)
2091 ir
->record
->accept(this);
2093 int field_index
= this->deref_tail
->type
->field_index(ir
->field
);
2094 assert(field_index
>= 0);
2096 nir_deref_struct
*deref
= nir_deref_struct_create(this->deref_tail
, field_index
);
2097 deref
->deref
.type
= ir
->type
;
2098 this->deref_tail
->child
= &deref
->deref
;
2099 this->deref_tail
= &deref
->deref
;
2103 nir_visitor::visit(ir_dereference_array
*ir
)
2105 nir_deref_array
*deref
= nir_deref_array_create(this->shader
);
2106 deref
->deref
.type
= ir
->type
;
2108 ir_constant
*const_index
= ir
->array_index
->as_constant();
2109 if (const_index
!= NULL
) {
2110 deref
->deref_array_type
= nir_deref_array_type_direct
;
2111 deref
->base_offset
= const_index
->value
.u
[0];
2113 deref
->deref_array_type
= nir_deref_array_type_indirect
;
2114 deref
->indirect
= evaluate_rvalue(ir
->array_index
);
2117 ir
->array
->accept(this);
2119 this->deref_tail
->child
= &deref
->deref
;
2120 ralloc_steal(this->deref_tail
, deref
);
2121 this->deref_tail
= &deref
->deref
;
2125 nir_visitor::visit(ir_barrier
*ir
)
2127 nir_intrinsic_instr
*instr
=
2128 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2129 nir_instr_insert_after_cf_list(this->cf_node_list
, &instr
->instr
);