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 "float64_glsl.h"
29 #include "glsl_to_nir.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
33 #include "ir_optimization.h"
35 #include "compiler/nir/nir_control_flow.h"
36 #include "compiler/nir/nir_builder.h"
37 #include "main/errors.h"
38 #include "main/imports.h"
39 #include "main/mtypes.h"
40 #include "main/shaderobj.h"
41 #include "util/u_math.h"
44 * pass to lower GLSL IR to NIR
46 * This will lower variable dereferences to loads/stores of corresponding
47 * variables in NIR - the variables will be converted to registers in a later
53 class nir_visitor
: public ir_visitor
56 nir_visitor(gl_context
*ctx
, nir_shader
*shader
);
59 virtual void visit(ir_variable
*);
60 virtual void visit(ir_function
*);
61 virtual void visit(ir_function_signature
*);
62 virtual void visit(ir_loop
*);
63 virtual void visit(ir_if
*);
64 virtual void visit(ir_discard
*);
65 virtual void visit(ir_loop_jump
*);
66 virtual void visit(ir_return
*);
67 virtual void visit(ir_call
*);
68 virtual void visit(ir_assignment
*);
69 virtual void visit(ir_emit_vertex
*);
70 virtual void visit(ir_end_primitive
*);
71 virtual void visit(ir_expression
*);
72 virtual void visit(ir_swizzle
*);
73 virtual void visit(ir_texture
*);
74 virtual void visit(ir_constant
*);
75 virtual void visit(ir_dereference_variable
*);
76 virtual void visit(ir_dereference_record
*);
77 virtual void visit(ir_dereference_array
*);
78 virtual void visit(ir_barrier
*);
80 void create_function(ir_function_signature
*ir
);
83 void add_instr(nir_instr
*instr
, unsigned num_components
, unsigned bit_size
);
84 nir_ssa_def
*evaluate_rvalue(ir_rvalue
*ir
);
86 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
**srcs
);
87 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
);
88 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
90 nir_alu_instr
*emit(nir_op op
, unsigned dest_size
, nir_ssa_def
*src1
,
91 nir_ssa_def
*src2
, nir_ssa_def
*src3
);
97 nir_function_impl
*impl
;
99 nir_ssa_def
*result
; /* result of the expression tree last visited */
101 nir_deref_instr
*evaluate_deref(ir_instruction
*ir
);
103 nir_constant
*constant_copy(ir_constant
*ir
, void *mem_ctx
);
105 /* most recent deref instruction created */
106 nir_deref_instr
*deref
;
108 /* whether the IR we're operating on is per-function or global */
111 ir_function_signature
*sig
;
113 /* map of ir_variable -> nir_variable */
114 struct hash_table
*var_table
;
116 /* map of ir_function_signature -> nir_function_overload */
117 struct hash_table
*overload_table
;
121 * This visitor runs before the main visitor, calling create_function() for
122 * each function so that the main visitor can resolve forward references in
126 class nir_function_visitor
: public ir_hierarchical_visitor
129 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
132 virtual ir_visitor_status
visit_enter(ir_function
*);
135 nir_visitor
*visitor
;
138 /* glsl_to_nir can only handle converting certain function paramaters
139 * to NIR. This visitor checks for parameters it can't currently handle.
141 class ir_function_param_visitor
: public ir_hierarchical_visitor
144 ir_function_param_visitor()
149 virtual ir_visitor_status
visit_enter(ir_function_signature
*ir
)
152 if (ir
->is_intrinsic())
153 return visit_continue
;
155 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
156 if (!param
->type
->is_vector() || !param
->type
->is_scalar()) {
161 if (param
->data
.mode
== ir_var_function_inout
) {
167 return visit_continue
;
173 } /* end of anonymous namespace */
177 has_unsupported_function_param(exec_list
*ir
)
179 ir_function_param_visitor visitor
;
180 visit_list_elements(&visitor
, ir
);
181 return visitor
.unsupported
;
185 glsl_to_nir(struct gl_context
*ctx
,
186 const struct gl_shader_program
*shader_prog
,
187 gl_shader_stage stage
,
188 const nir_shader_compiler_options
*options
)
190 struct gl_linked_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
192 const struct gl_shader_compiler_options
*gl_options
=
193 &ctx
->Const
.ShaderCompilerOptions
[stage
];
195 /* glsl_to_nir can only handle converting certain function paramaters
196 * to NIR. If we find something we can't handle then we get the GLSL IR
197 * opts to remove it before we continue on.
199 * TODO: add missing glsl ir to nir support and remove this loop.
201 while (has_unsupported_function_param(sh
->ir
)) {
202 do_common_optimization(sh
->ir
, true, true, gl_options
,
203 ctx
->Const
.NativeIntegers
);
206 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
,
209 nir_visitor
v1(ctx
, shader
);
210 nir_function_visitor
v2(&v1
);
212 visit_exec_list(sh
->ir
, &v1
);
214 nir_validate_shader(shader
, "after glsl to nir, before function inline");
216 /* We have to lower away local constant initializers right before we
217 * inline functions. That way they get properly initialized at the top
218 * of the function and not at the top of its caller.
220 nir_lower_constant_initializers(shader
, (nir_variable_mode
)~0);
221 nir_lower_returns(shader
);
222 nir_inline_functions(shader
);
223 nir_opt_deref(shader
);
225 nir_validate_shader(shader
, "after function inlining and return lowering");
227 /* Now that we have inlined everything remove all of the functions except
230 foreach_list_typed_safe(nir_function
, function
, node
, &(shader
)->functions
){
231 if (strcmp("main", function
->name
) != 0) {
232 exec_node_remove(&function
->node
);
236 /* Remap the locations to slots so those requiring two slots will occupy
237 * two locations. For instance, if we have in the IR code a dvec3 attr0 in
238 * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
239 * locations/slots 0 and 1, and attr1 will use location/slot 2 */
240 if (shader
->info
.stage
== MESA_SHADER_VERTEX
)
241 nir_remap_dual_slot_attributes(shader
, &sh
->Program
->DualSlotInputs
);
243 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
244 if (shader_prog
->Label
)
245 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
247 /* Check for transform feedback varyings specified via the API */
248 shader
->info
.has_transform_feedback_varyings
=
249 shader_prog
->TransformFeedback
.NumVarying
> 0;
251 /* Check for transform feedback varyings specified in the Shader */
252 if (shader_prog
->last_vert_prog
)
253 shader
->info
.has_transform_feedback_varyings
|=
254 shader_prog
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0;
256 if (shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
257 shader
->info
.fs
.pixel_center_integer
= sh
->Program
->info
.fs
.pixel_center_integer
;
258 shader
->info
.fs
.origin_upper_left
= sh
->Program
->info
.fs
.origin_upper_left
;
264 nir_visitor::nir_visitor(gl_context
*ctx
, nir_shader
*shader
)
266 this->supports_ints
= shader
->options
->native_integers
;
267 this->supports_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
268 this->shader
= shader
;
269 this->is_global
= true;
270 this->var_table
= _mesa_pointer_hash_table_create(NULL
);
271 this->overload_table
= _mesa_pointer_hash_table_create(NULL
);
275 memset(&this->b
, 0, sizeof(this->b
));
278 nir_visitor::~nir_visitor()
280 _mesa_hash_table_destroy(this->var_table
, NULL
);
281 _mesa_hash_table_destroy(this->overload_table
, NULL
);
285 nir_visitor::evaluate_deref(ir_instruction
*ir
)
292 nir_visitor::constant_copy(ir_constant
*ir
, void *mem_ctx
)
297 nir_constant
*ret
= rzalloc(mem_ctx
, nir_constant
);
299 const unsigned rows
= ir
->type
->vector_elements
;
300 const unsigned cols
= ir
->type
->matrix_columns
;
303 ret
->num_elements
= 0;
304 switch (ir
->type
->base_type
) {
306 /* Only float base types can be matrices. */
309 for (unsigned r
= 0; r
< rows
; r
++)
311 ret
->values
[0].u32
[r
] = ir
->value
.u
[r
];
313 ret
->values
[0].f32
[r
] = ir
->value
.u
[r
];
318 /* Only float base types can be matrices. */
321 for (unsigned r
= 0; r
< rows
; r
++)
323 ret
->values
[0].i32
[r
] = ir
->value
.i
[r
];
325 ret
->values
[0].f32
[r
] = ir
->value
.i
[r
];
329 case GLSL_TYPE_FLOAT
:
330 for (unsigned c
= 0; c
< cols
; c
++) {
331 for (unsigned r
= 0; r
< rows
; r
++)
332 ret
->values
[c
].f32
[r
] = ir
->value
.f
[c
* rows
+ r
];
336 case GLSL_TYPE_DOUBLE
:
337 for (unsigned c
= 0; c
< cols
; c
++) {
338 for (unsigned r
= 0; r
< rows
; r
++)
339 ret
->values
[c
].f64
[r
] = ir
->value
.d
[c
* rows
+ r
];
343 case GLSL_TYPE_UINT64
:
344 /* Only float base types can be matrices. */
347 for (unsigned r
= 0; r
< rows
; r
++)
348 ret
->values
[0].u64
[r
] = ir
->value
.u64
[r
];
351 case GLSL_TYPE_INT64
:
352 /* Only float base types can be matrices. */
355 for (unsigned r
= 0; r
< rows
; r
++)
356 ret
->values
[0].i64
[r
] = ir
->value
.i64
[r
];
360 /* Only float base types can be matrices. */
363 for (unsigned r
= 0; r
< rows
; r
++)
364 ret
->values
[0].b
[r
] = ir
->value
.b
[r
];
368 case GLSL_TYPE_STRUCT
:
369 case GLSL_TYPE_ARRAY
:
370 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
372 ret
->num_elements
= ir
->type
->length
;
374 for (i
= 0; i
< ir
->type
->length
; i
++)
375 ret
->elements
[i
] = constant_copy(ir
->const_elements
[i
], mem_ctx
);
379 unreachable("not reached");
385 static const glsl_type
*
386 wrap_type_in_array(const glsl_type
*elem_type
, const glsl_type
*array_type
)
388 if (!array_type
->is_array())
391 elem_type
= wrap_type_in_array(elem_type
, array_type
->fields
.array
);
393 return glsl_type::get_array_instance(elem_type
, array_type
->length
);
397 nir_visitor::visit(ir_variable
*ir
)
399 /* TODO: In future we should switch to using the NIR lowering pass but for
400 * now just ignore these variables as GLSL IR should have lowered them.
401 * Anything remaining are just dead vars that weren't cleaned up.
403 if (ir
->data
.mode
== ir_var_shader_shared
)
406 /* FINISHME: inout parameters */
407 assert(ir
->data
.mode
!= ir_var_function_inout
);
409 if (ir
->data
.mode
== ir_var_function_out
)
412 nir_variable
*var
= rzalloc(shader
, nir_variable
);
413 var
->type
= ir
->type
;
414 var
->name
= ralloc_strdup(var
, ir
->name
);
416 var
->data
.always_active_io
= ir
->data
.always_active_io
;
417 var
->data
.read_only
= ir
->data
.read_only
;
418 var
->data
.centroid
= ir
->data
.centroid
;
419 var
->data
.sample
= ir
->data
.sample
;
420 var
->data
.patch
= ir
->data
.patch
;
421 var
->data
.invariant
= ir
->data
.invariant
;
422 var
->data
.location
= ir
->data
.location
;
423 var
->data
.stream
= ir
->data
.stream
;
424 var
->data
.compact
= false;
426 switch(ir
->data
.mode
) {
428 case ir_var_temporary
:
430 var
->data
.mode
= nir_var_shader_temp
;
432 var
->data
.mode
= nir_var_function_temp
;
435 case ir_var_function_in
:
436 case ir_var_const_in
:
437 var
->data
.mode
= nir_var_function_temp
;
440 case ir_var_shader_in
:
441 if (shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
442 ir
->data
.location
== VARYING_SLOT_FACE
) {
443 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
444 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
445 var
->data
.mode
= nir_var_system_value
;
446 } else if (shader
->info
.stage
== MESA_SHADER_GEOMETRY
&&
447 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
448 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
449 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
450 var
->data
.mode
= nir_var_system_value
;
452 var
->data
.mode
= nir_var_shader_in
;
454 if (shader
->info
.stage
== MESA_SHADER_TESS_EVAL
&&
455 (ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
456 ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)) {
457 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
460 if (shader
->info
.stage
> MESA_SHADER_VERTEX
&&
461 ir
->data
.location
>= VARYING_SLOT_CLIP_DIST0
&&
462 ir
->data
.location
<= VARYING_SLOT_CULL_DIST1
) {
463 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
468 case ir_var_shader_out
:
469 var
->data
.mode
= nir_var_shader_out
;
470 if (shader
->info
.stage
== MESA_SHADER_TESS_CTRL
&&
471 (ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
472 ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)) {
473 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
476 if (shader
->info
.stage
<= MESA_SHADER_GEOMETRY
&&
477 ir
->data
.location
>= VARYING_SLOT_CLIP_DIST0
&&
478 ir
->data
.location
<= VARYING_SLOT_CULL_DIST1
) {
479 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
484 if (ir
->get_interface_type())
485 var
->data
.mode
= nir_var_mem_ubo
;
487 var
->data
.mode
= nir_var_uniform
;
490 case ir_var_shader_storage
:
491 var
->data
.mode
= nir_var_mem_ssbo
;
494 case ir_var_system_value
:
495 var
->data
.mode
= nir_var_system_value
;
499 unreachable("not reached");
502 unsigned image_access
= 0;
503 if (ir
->data
.memory_read_only
)
504 image_access
|= ACCESS_NON_WRITEABLE
;
505 if (ir
->data
.memory_write_only
)
506 image_access
|= ACCESS_NON_READABLE
;
507 if (ir
->data
.memory_coherent
)
508 image_access
|= ACCESS_COHERENT
;
509 if (ir
->data
.memory_volatile
)
510 image_access
|= ACCESS_VOLATILE
;
511 if (ir
->data
.memory_restrict
)
512 image_access
|= ACCESS_RESTRICT
;
514 /* For UBO and SSBO variables, we need explicit types */
515 if (var
->data
.mode
& (nir_var_mem_ubo
| nir_var_mem_ssbo
)) {
516 const glsl_type
*explicit_ifc_type
=
517 ir
->get_interface_type()->get_explicit_interface_type(supports_std430
);
519 if (ir
->type
->without_array()->is_interface()) {
520 /* If the type contains the interface, wrap the explicit type in the
521 * right number of arrays.
523 var
->type
= wrap_type_in_array(explicit_ifc_type
, ir
->type
);
525 /* Otherwise, this variable is one entry in the interface */
527 for (unsigned i
= 0; i
< explicit_ifc_type
->length
; i
++) {
528 const glsl_struct_field
*field
=
529 &explicit_ifc_type
->fields
.structure
[i
];
530 if (strcmp(ir
->name
, field
->name
) != 0)
533 var
->type
= field
->type
;
534 if (field
->memory_read_only
)
535 image_access
|= ACCESS_NON_WRITEABLE
;
536 if (field
->memory_write_only
)
537 image_access
|= ACCESS_NON_READABLE
;
538 if (field
->memory_coherent
)
539 image_access
|= ACCESS_COHERENT
;
540 if (field
->memory_volatile
)
541 image_access
|= ACCESS_VOLATILE
;
542 if (field
->memory_restrict
)
543 image_access
|= ACCESS_RESTRICT
;
552 var
->data
.interpolation
= ir
->data
.interpolation
;
553 var
->data
.location_frac
= ir
->data
.location_frac
;
555 switch (ir
->data
.depth_layout
) {
556 case ir_depth_layout_none
:
557 var
->data
.depth_layout
= nir_depth_layout_none
;
559 case ir_depth_layout_any
:
560 var
->data
.depth_layout
= nir_depth_layout_any
;
562 case ir_depth_layout_greater
:
563 var
->data
.depth_layout
= nir_depth_layout_greater
;
565 case ir_depth_layout_less
:
566 var
->data
.depth_layout
= nir_depth_layout_less
;
568 case ir_depth_layout_unchanged
:
569 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
572 unreachable("not reached");
575 var
->data
.index
= ir
->data
.index
;
576 var
->data
.descriptor_set
= 0;
577 var
->data
.binding
= ir
->data
.binding
;
578 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
579 var
->data
.bindless
= ir
->data
.bindless
;
580 var
->data
.offset
= ir
->data
.offset
;
582 var
->data
.image
.access
= (gl_access_qualifier
)image_access
;
583 var
->data
.image
.format
= ir
->data
.image_format
;
585 var
->data
.fb_fetch_output
= ir
->data
.fb_fetch_output
;
586 var
->data
.explicit_xfb_buffer
= ir
->data
.explicit_xfb_buffer
;
587 var
->data
.explicit_xfb_stride
= ir
->data
.explicit_xfb_stride
;
588 var
->data
.xfb_buffer
= ir
->data
.xfb_buffer
;
589 var
->data
.xfb_stride
= ir
->data
.xfb_stride
;
591 var
->num_state_slots
= ir
->get_num_state_slots();
592 if (var
->num_state_slots
> 0) {
593 var
->state_slots
= rzalloc_array(var
, nir_state_slot
,
594 var
->num_state_slots
);
596 ir_state_slot
*state_slots
= ir
->get_state_slots();
597 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
598 for (unsigned j
= 0; j
< 5; j
++)
599 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
600 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
603 var
->state_slots
= NULL
;
606 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
608 var
->interface_type
= ir
->get_interface_type();
610 if (var
->data
.mode
== nir_var_function_temp
)
611 nir_function_impl_add_variable(impl
, var
);
613 nir_shader_add_variable(shader
, var
);
615 _mesa_hash_table_insert(var_table
, ir
, var
);
619 nir_function_visitor::visit_enter(ir_function
*ir
)
621 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
622 visitor
->create_function(sig
);
624 return visit_continue_with_parent
;
628 nir_visitor::create_function(ir_function_signature
*ir
)
630 if (ir
->is_intrinsic())
633 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
634 if (strcmp(ir
->function_name(), "main") == 0)
635 func
->is_entrypoint
= true;
637 func
->num_params
= ir
->parameters
.length() +
638 (ir
->return_type
!= glsl_type::void_type
);
639 func
->params
= ralloc_array(shader
, nir_parameter
, func
->num_params
);
643 if (ir
->return_type
!= glsl_type::void_type
) {
644 /* The return value is a variable deref (basically an out parameter) */
645 func
->params
[np
].num_components
= 1;
646 func
->params
[np
].bit_size
= 32;
650 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
651 /* FINISHME: pass arrays, structs, etc by reference? */
652 assert(param
->type
->is_vector() || param
->type
->is_scalar());
654 if (param
->data
.mode
== ir_var_function_in
) {
655 func
->params
[np
].num_components
= param
->type
->vector_elements
;
656 func
->params
[np
].bit_size
= glsl_get_bit_size(param
->type
);
658 func
->params
[np
].num_components
= 1;
659 func
->params
[np
].bit_size
= 32;
663 assert(np
== func
->num_params
);
665 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
669 nir_visitor::visit(ir_function
*ir
)
671 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
676 nir_visitor::visit(ir_function_signature
*ir
)
678 if (ir
->is_intrinsic())
683 struct hash_entry
*entry
=
684 _mesa_hash_table_search(this->overload_table
, ir
);
687 nir_function
*func
= (nir_function
*) entry
->data
;
689 if (ir
->is_defined
) {
690 nir_function_impl
*impl
= nir_function_impl_create(func
);
693 this->is_global
= false;
695 nir_builder_init(&b
, impl
);
696 b
.cursor
= nir_after_cf_list(&impl
->body
);
698 unsigned i
= (ir
->return_type
!= glsl_type::void_type
) ? 1 : 0;
700 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
702 nir_local_variable_create(impl
, param
->type
, param
->name
);
704 if (param
->data
.mode
== ir_var_function_in
) {
705 nir_store_var(&b
, var
, nir_load_param(&b
, i
), ~0);
708 _mesa_hash_table_insert(var_table
, param
, var
);
712 visit_exec_list(&ir
->body
, this);
714 this->is_global
= true;
721 nir_visitor::visit(ir_loop
*ir
)
724 visit_exec_list(&ir
->body_instructions
, this);
725 nir_pop_loop(&b
, NULL
);
729 nir_visitor::visit(ir_if
*ir
)
731 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
732 visit_exec_list(&ir
->then_instructions
, this);
733 nir_push_else(&b
, NULL
);
734 visit_exec_list(&ir
->else_instructions
, this);
735 nir_pop_if(&b
, NULL
);
739 nir_visitor::visit(ir_discard
*ir
)
742 * discards aren't treated as control flow, because before we lower them
743 * they can appear anywhere in the shader and the stuff after them may still
744 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
745 * discards will be immediately followed by a return.
748 nir_intrinsic_instr
*discard
;
750 discard
= nir_intrinsic_instr_create(this->shader
,
751 nir_intrinsic_discard_if
);
753 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
755 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
758 nir_builder_instr_insert(&b
, &discard
->instr
);
762 nir_visitor::visit(ir_emit_vertex
*ir
)
764 nir_intrinsic_instr
*instr
=
765 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
766 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
767 nir_builder_instr_insert(&b
, &instr
->instr
);
771 nir_visitor::visit(ir_end_primitive
*ir
)
773 nir_intrinsic_instr
*instr
=
774 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
775 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
776 nir_builder_instr_insert(&b
, &instr
->instr
);
780 nir_visitor::visit(ir_loop_jump
*ir
)
784 case ir_loop_jump::jump_break
:
785 type
= nir_jump_break
;
787 case ir_loop_jump::jump_continue
:
788 type
= nir_jump_continue
;
791 unreachable("not reached");
794 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
795 nir_builder_instr_insert(&b
, &instr
->instr
);
799 nir_visitor::visit(ir_return
*ir
)
801 if (ir
->value
!= NULL
) {
802 nir_deref_instr
*ret_deref
=
803 nir_build_deref_cast(&b
, nir_load_param(&b
, 0),
804 nir_var_function_temp
, ir
->value
->type
, 0);
806 nir_ssa_def
*val
= evaluate_rvalue(ir
->value
);
807 nir_store_deref(&b
, ret_deref
, val
, ~0);
810 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
811 nir_builder_instr_insert(&b
, &instr
->instr
);
815 intrinsic_set_std430_align(nir_intrinsic_instr
*intrin
, const glsl_type
*type
)
817 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
818 unsigned pow2_components
= util_next_power_of_two(type
->vector_elements
);
819 nir_intrinsic_set_align(intrin
, (bit_size
/ 8) * pow2_components
, 0);
823 nir_visitor::visit(ir_call
*ir
)
825 if (ir
->callee
->is_intrinsic()) {
828 switch (ir
->callee
->intrinsic_id
) {
829 case ir_intrinsic_atomic_counter_read
:
830 op
= nir_intrinsic_atomic_counter_read_deref
;
832 case ir_intrinsic_atomic_counter_increment
:
833 op
= nir_intrinsic_atomic_counter_inc_deref
;
835 case ir_intrinsic_atomic_counter_predecrement
:
836 op
= nir_intrinsic_atomic_counter_pre_dec_deref
;
838 case ir_intrinsic_atomic_counter_add
:
839 op
= nir_intrinsic_atomic_counter_add_deref
;
841 case ir_intrinsic_atomic_counter_and
:
842 op
= nir_intrinsic_atomic_counter_and_deref
;
844 case ir_intrinsic_atomic_counter_or
:
845 op
= nir_intrinsic_atomic_counter_or_deref
;
847 case ir_intrinsic_atomic_counter_xor
:
848 op
= nir_intrinsic_atomic_counter_xor_deref
;
850 case ir_intrinsic_atomic_counter_min
:
851 op
= nir_intrinsic_atomic_counter_min_deref
;
853 case ir_intrinsic_atomic_counter_max
:
854 op
= nir_intrinsic_atomic_counter_max_deref
;
856 case ir_intrinsic_atomic_counter_exchange
:
857 op
= nir_intrinsic_atomic_counter_exchange_deref
;
859 case ir_intrinsic_atomic_counter_comp_swap
:
860 op
= nir_intrinsic_atomic_counter_comp_swap_deref
;
862 case ir_intrinsic_image_load
:
863 op
= nir_intrinsic_image_deref_load
;
865 case ir_intrinsic_image_store
:
866 op
= nir_intrinsic_image_deref_store
;
868 case ir_intrinsic_image_atomic_add
:
869 op
= ir
->return_deref
->type
->is_integer_32_64()
870 ? nir_intrinsic_image_deref_atomic_add
871 : nir_intrinsic_image_deref_atomic_fadd
;
873 case ir_intrinsic_image_atomic_min
:
874 op
= nir_intrinsic_image_deref_atomic_min
;
876 case ir_intrinsic_image_atomic_max
:
877 op
= nir_intrinsic_image_deref_atomic_max
;
879 case ir_intrinsic_image_atomic_and
:
880 op
= nir_intrinsic_image_deref_atomic_and
;
882 case ir_intrinsic_image_atomic_or
:
883 op
= nir_intrinsic_image_deref_atomic_or
;
885 case ir_intrinsic_image_atomic_xor
:
886 op
= nir_intrinsic_image_deref_atomic_xor
;
888 case ir_intrinsic_image_atomic_exchange
:
889 op
= nir_intrinsic_image_deref_atomic_exchange
;
891 case ir_intrinsic_image_atomic_comp_swap
:
892 op
= nir_intrinsic_image_deref_atomic_comp_swap
;
894 case ir_intrinsic_memory_barrier
:
895 op
= nir_intrinsic_memory_barrier
;
897 case ir_intrinsic_image_size
:
898 op
= nir_intrinsic_image_deref_size
;
900 case ir_intrinsic_image_samples
:
901 op
= nir_intrinsic_image_deref_samples
;
903 case ir_intrinsic_ssbo_store
:
904 op
= nir_intrinsic_store_ssbo
;
906 case ir_intrinsic_ssbo_load
:
907 op
= nir_intrinsic_load_ssbo
;
909 case ir_intrinsic_ssbo_atomic_add
:
910 op
= ir
->return_deref
->type
->is_integer_32_64()
911 ? nir_intrinsic_ssbo_atomic_add
: nir_intrinsic_ssbo_atomic_fadd
;
913 case ir_intrinsic_ssbo_atomic_and
:
914 op
= nir_intrinsic_ssbo_atomic_and
;
916 case ir_intrinsic_ssbo_atomic_or
:
917 op
= nir_intrinsic_ssbo_atomic_or
;
919 case ir_intrinsic_ssbo_atomic_xor
:
920 op
= nir_intrinsic_ssbo_atomic_xor
;
922 case ir_intrinsic_ssbo_atomic_min
:
923 assert(ir
->return_deref
);
924 if (ir
->return_deref
->type
== glsl_type::int_type
)
925 op
= nir_intrinsic_ssbo_atomic_imin
;
926 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
927 op
= nir_intrinsic_ssbo_atomic_umin
;
928 else if (ir
->return_deref
->type
== glsl_type::float_type
)
929 op
= nir_intrinsic_ssbo_atomic_fmin
;
931 unreachable("Invalid type");
933 case ir_intrinsic_ssbo_atomic_max
:
934 assert(ir
->return_deref
);
935 if (ir
->return_deref
->type
== glsl_type::int_type
)
936 op
= nir_intrinsic_ssbo_atomic_imax
;
937 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
938 op
= nir_intrinsic_ssbo_atomic_umax
;
939 else if (ir
->return_deref
->type
== glsl_type::float_type
)
940 op
= nir_intrinsic_ssbo_atomic_fmax
;
942 unreachable("Invalid type");
944 case ir_intrinsic_ssbo_atomic_exchange
:
945 op
= nir_intrinsic_ssbo_atomic_exchange
;
947 case ir_intrinsic_ssbo_atomic_comp_swap
:
948 op
= ir
->return_deref
->type
->is_integer_32_64()
949 ? nir_intrinsic_ssbo_atomic_comp_swap
950 : nir_intrinsic_ssbo_atomic_fcomp_swap
;
952 case ir_intrinsic_shader_clock
:
953 op
= nir_intrinsic_shader_clock
;
955 case ir_intrinsic_begin_invocation_interlock
:
956 op
= nir_intrinsic_begin_invocation_interlock
;
958 case ir_intrinsic_end_invocation_interlock
:
959 op
= nir_intrinsic_end_invocation_interlock
;
961 case ir_intrinsic_group_memory_barrier
:
962 op
= nir_intrinsic_group_memory_barrier
;
964 case ir_intrinsic_memory_barrier_atomic_counter
:
965 op
= nir_intrinsic_memory_barrier_atomic_counter
;
967 case ir_intrinsic_memory_barrier_buffer
:
968 op
= nir_intrinsic_memory_barrier_buffer
;
970 case ir_intrinsic_memory_barrier_image
:
971 op
= nir_intrinsic_memory_barrier_image
;
973 case ir_intrinsic_memory_barrier_shared
:
974 op
= nir_intrinsic_memory_barrier_shared
;
976 case ir_intrinsic_shared_load
:
977 op
= nir_intrinsic_load_shared
;
979 case ir_intrinsic_shared_store
:
980 op
= nir_intrinsic_store_shared
;
982 case ir_intrinsic_shared_atomic_add
:
983 op
= ir
->return_deref
->type
->is_integer_32_64()
984 ? nir_intrinsic_shared_atomic_add
985 : nir_intrinsic_shared_atomic_fadd
;
987 case ir_intrinsic_shared_atomic_and
:
988 op
= nir_intrinsic_shared_atomic_and
;
990 case ir_intrinsic_shared_atomic_or
:
991 op
= nir_intrinsic_shared_atomic_or
;
993 case ir_intrinsic_shared_atomic_xor
:
994 op
= nir_intrinsic_shared_atomic_xor
;
996 case ir_intrinsic_shared_atomic_min
:
997 assert(ir
->return_deref
);
998 if (ir
->return_deref
->type
== glsl_type::int_type
)
999 op
= nir_intrinsic_shared_atomic_imin
;
1000 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
1001 op
= nir_intrinsic_shared_atomic_umin
;
1002 else if (ir
->return_deref
->type
== glsl_type::float_type
)
1003 op
= nir_intrinsic_shared_atomic_fmin
;
1005 unreachable("Invalid type");
1007 case ir_intrinsic_shared_atomic_max
:
1008 assert(ir
->return_deref
);
1009 if (ir
->return_deref
->type
== glsl_type::int_type
)
1010 op
= nir_intrinsic_shared_atomic_imax
;
1011 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
1012 op
= nir_intrinsic_shared_atomic_umax
;
1013 else if (ir
->return_deref
->type
== glsl_type::float_type
)
1014 op
= nir_intrinsic_shared_atomic_fmax
;
1016 unreachable("Invalid type");
1018 case ir_intrinsic_shared_atomic_exchange
:
1019 op
= nir_intrinsic_shared_atomic_exchange
;
1021 case ir_intrinsic_shared_atomic_comp_swap
:
1022 op
= ir
->return_deref
->type
->is_integer_32_64()
1023 ? nir_intrinsic_shared_atomic_comp_swap
1024 : nir_intrinsic_shared_atomic_fcomp_swap
;
1026 case ir_intrinsic_vote_any
:
1027 op
= nir_intrinsic_vote_any
;
1029 case ir_intrinsic_vote_all
:
1030 op
= nir_intrinsic_vote_all
;
1032 case ir_intrinsic_vote_eq
:
1033 op
= nir_intrinsic_vote_ieq
;
1035 case ir_intrinsic_ballot
:
1036 op
= nir_intrinsic_ballot
;
1038 case ir_intrinsic_read_invocation
:
1039 op
= nir_intrinsic_read_invocation
;
1041 case ir_intrinsic_read_first_invocation
:
1042 op
= nir_intrinsic_read_first_invocation
;
1045 unreachable("not reached");
1048 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
1049 nir_ssa_def
*ret
= &instr
->dest
.ssa
;
1052 case nir_intrinsic_atomic_counter_read_deref
:
1053 case nir_intrinsic_atomic_counter_inc_deref
:
1054 case nir_intrinsic_atomic_counter_pre_dec_deref
:
1055 case nir_intrinsic_atomic_counter_add_deref
:
1056 case nir_intrinsic_atomic_counter_min_deref
:
1057 case nir_intrinsic_atomic_counter_max_deref
:
1058 case nir_intrinsic_atomic_counter_and_deref
:
1059 case nir_intrinsic_atomic_counter_or_deref
:
1060 case nir_intrinsic_atomic_counter_xor_deref
:
1061 case nir_intrinsic_atomic_counter_exchange_deref
:
1062 case nir_intrinsic_atomic_counter_comp_swap_deref
: {
1063 /* Set the counter variable dereference. */
1064 exec_node
*param
= ir
->actual_parameters
.get_head();
1065 ir_dereference
*counter
= (ir_dereference
*)param
;
1067 instr
->src
[0] = nir_src_for_ssa(&evaluate_deref(counter
)->dest
.ssa
);
1068 param
= param
->get_next();
1070 /* Set the intrinsic destination. */
1071 if (ir
->return_deref
) {
1072 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1075 /* Set the intrinsic parameters. */
1076 if (!param
->is_tail_sentinel()) {
1078 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1079 param
= param
->get_next();
1082 if (!param
->is_tail_sentinel()) {
1084 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1085 param
= param
->get_next();
1088 nir_builder_instr_insert(&b
, &instr
->instr
);
1091 case nir_intrinsic_image_deref_load
:
1092 case nir_intrinsic_image_deref_store
:
1093 case nir_intrinsic_image_deref_atomic_add
:
1094 case nir_intrinsic_image_deref_atomic_min
:
1095 case nir_intrinsic_image_deref_atomic_max
:
1096 case nir_intrinsic_image_deref_atomic_and
:
1097 case nir_intrinsic_image_deref_atomic_or
:
1098 case nir_intrinsic_image_deref_atomic_xor
:
1099 case nir_intrinsic_image_deref_atomic_exchange
:
1100 case nir_intrinsic_image_deref_atomic_comp_swap
:
1101 case nir_intrinsic_image_deref_atomic_fadd
:
1102 case nir_intrinsic_image_deref_samples
:
1103 case nir_intrinsic_image_deref_size
: {
1104 nir_ssa_undef_instr
*instr_undef
=
1105 nir_ssa_undef_instr_create(shader
, 1, 32);
1106 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
1108 /* Set the image variable dereference. */
1109 exec_node
*param
= ir
->actual_parameters
.get_head();
1110 ir_dereference
*image
= (ir_dereference
*)param
;
1111 const glsl_type
*type
=
1112 image
->variable_referenced()->type
->without_array();
1114 instr
->src
[0] = nir_src_for_ssa(&evaluate_deref(image
)->dest
.ssa
);
1115 param
= param
->get_next();
1117 /* Set the intrinsic destination. */
1118 if (ir
->return_deref
) {
1119 unsigned num_components
= ir
->return_deref
->type
->vector_elements
;
1120 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1121 num_components
, 32, NULL
);
1124 if (op
== nir_intrinsic_image_deref_size
) {
1125 instr
->num_components
= instr
->dest
.ssa
.num_components
;
1126 } else if (op
== nir_intrinsic_image_deref_load
||
1127 op
== nir_intrinsic_image_deref_store
) {
1128 instr
->num_components
= 4;
1131 if (op
== nir_intrinsic_image_deref_size
||
1132 op
== nir_intrinsic_image_deref_samples
) {
1133 nir_builder_instr_insert(&b
, &instr
->instr
);
1137 /* Set the address argument, extending the coordinate vector to four
1140 nir_ssa_def
*src_addr
=
1141 evaluate_rvalue((ir_dereference
*)param
);
1142 nir_ssa_def
*srcs
[4];
1144 for (int i
= 0; i
< 4; i
++) {
1145 if (i
< type
->coordinate_components())
1146 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
1148 srcs
[i
] = &instr_undef
->def
;
1151 instr
->src
[1] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
1152 param
= param
->get_next();
1154 /* Set the sample argument, which is undefined for single-sample
1157 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
1159 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1160 param
= param
->get_next();
1162 instr
->src
[2] = nir_src_for_ssa(&instr_undef
->def
);
1165 /* Set the intrinsic parameters. */
1166 if (!param
->is_tail_sentinel()) {
1168 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1169 param
= param
->get_next();
1172 if (!param
->is_tail_sentinel()) {
1174 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1175 param
= param
->get_next();
1177 nir_builder_instr_insert(&b
, &instr
->instr
);
1180 case nir_intrinsic_memory_barrier
:
1181 case nir_intrinsic_group_memory_barrier
:
1182 case nir_intrinsic_memory_barrier_atomic_counter
:
1183 case nir_intrinsic_memory_barrier_buffer
:
1184 case nir_intrinsic_memory_barrier_image
:
1185 case nir_intrinsic_memory_barrier_shared
:
1186 nir_builder_instr_insert(&b
, &instr
->instr
);
1188 case nir_intrinsic_shader_clock
:
1189 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 2, 32, NULL
);
1190 instr
->num_components
= 2;
1191 nir_builder_instr_insert(&b
, &instr
->instr
);
1193 case nir_intrinsic_begin_invocation_interlock
:
1194 nir_builder_instr_insert(&b
, &instr
->instr
);
1196 case nir_intrinsic_end_invocation_interlock
:
1197 nir_builder_instr_insert(&b
, &instr
->instr
);
1199 case nir_intrinsic_store_ssbo
: {
1200 exec_node
*param
= ir
->actual_parameters
.get_head();
1201 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
1203 param
= param
->get_next();
1204 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1206 param
= param
->get_next();
1207 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1209 param
= param
->get_next();
1210 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1213 nir_ssa_def
*nir_val
= evaluate_rvalue(val
);
1214 if (val
->type
->is_boolean())
1215 nir_val
= nir_b2i32(&b
, nir_val
);
1217 instr
->src
[0] = nir_src_for_ssa(nir_val
);
1218 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
1219 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
1220 intrinsic_set_std430_align(instr
, val
->type
);
1221 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1222 instr
->num_components
= val
->type
->vector_elements
;
1224 nir_builder_instr_insert(&b
, &instr
->instr
);
1227 case nir_intrinsic_load_ssbo
: {
1228 exec_node
*param
= ir
->actual_parameters
.get_head();
1229 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
1231 param
= param
->get_next();
1232 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1234 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
1235 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1237 const glsl_type
*type
= ir
->return_deref
->var
->type
;
1238 instr
->num_components
= type
->vector_elements
;
1239 intrinsic_set_std430_align(instr
, type
);
1241 /* Setup destination register */
1242 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
1243 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1244 type
->vector_elements
, bit_size
, NULL
);
1246 /* Insert the created nir instruction now since in the case of boolean
1247 * result we will need to emit another instruction after it
1249 nir_builder_instr_insert(&b
, &instr
->instr
);
1252 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
1253 * consider a true boolean to be ~0. Fix this up with a != 0
1256 if (type
->is_boolean())
1257 ret
= nir_i2b(&b
, &instr
->dest
.ssa
);
1260 case nir_intrinsic_ssbo_atomic_add
:
1261 case nir_intrinsic_ssbo_atomic_imin
:
1262 case nir_intrinsic_ssbo_atomic_umin
:
1263 case nir_intrinsic_ssbo_atomic_imax
:
1264 case nir_intrinsic_ssbo_atomic_umax
:
1265 case nir_intrinsic_ssbo_atomic_and
:
1266 case nir_intrinsic_ssbo_atomic_or
:
1267 case nir_intrinsic_ssbo_atomic_xor
:
1268 case nir_intrinsic_ssbo_atomic_exchange
:
1269 case nir_intrinsic_ssbo_atomic_comp_swap
:
1270 case nir_intrinsic_ssbo_atomic_fadd
:
1271 case nir_intrinsic_ssbo_atomic_fmin
:
1272 case nir_intrinsic_ssbo_atomic_fmax
:
1273 case nir_intrinsic_ssbo_atomic_fcomp_swap
: {
1274 int param_count
= ir
->actual_parameters
.length();
1275 assert(param_count
== 3 || param_count
== 4);
1278 exec_node
*param
= ir
->actual_parameters
.get_head();
1279 ir_instruction
*inst
= (ir_instruction
*) param
;
1280 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1283 param
= param
->get_next();
1284 inst
= (ir_instruction
*) param
;
1285 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1287 /* data1 parameter (this is always present) */
1288 param
= param
->get_next();
1289 inst
= (ir_instruction
*) param
;
1290 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1292 /* data2 parameter (only with atomic_comp_swap) */
1293 if (param_count
== 4) {
1294 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
||
1295 op
== nir_intrinsic_ssbo_atomic_fcomp_swap
);
1296 param
= param
->get_next();
1297 inst
= (ir_instruction
*) param
;
1298 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1302 assert(ir
->return_deref
);
1303 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1304 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1305 nir_builder_instr_insert(&b
, &instr
->instr
);
1308 case nir_intrinsic_load_shared
: {
1309 exec_node
*param
= ir
->actual_parameters
.get_head();
1310 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1312 nir_intrinsic_set_base(instr
, 0);
1313 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
1315 const glsl_type
*type
= ir
->return_deref
->var
->type
;
1316 instr
->num_components
= type
->vector_elements
;
1317 intrinsic_set_std430_align(instr
, type
);
1319 /* Setup destination register */
1320 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
1321 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1322 type
->vector_elements
, bit_size
, NULL
);
1324 nir_builder_instr_insert(&b
, &instr
->instr
);
1326 /* The value in shared memory is a 32-bit value */
1327 if (type
->is_boolean())
1328 ret
= nir_i2b(&b
, &instr
->dest
.ssa
);
1331 case nir_intrinsic_store_shared
: {
1332 exec_node
*param
= ir
->actual_parameters
.get_head();
1333 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1335 param
= param
->get_next();
1336 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1338 param
= param
->get_next();
1339 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1342 nir_intrinsic_set_base(instr
, 0);
1343 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1345 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1347 nir_ssa_def
*nir_val
= evaluate_rvalue(val
);
1348 /* The value in shared memory is a 32-bit value */
1349 if (val
->type
->is_boolean())
1350 nir_val
= nir_b2i32(&b
, nir_val
);
1352 instr
->src
[0] = nir_src_for_ssa(nir_val
);
1353 instr
->num_components
= val
->type
->vector_elements
;
1354 intrinsic_set_std430_align(instr
, val
->type
);
1356 nir_builder_instr_insert(&b
, &instr
->instr
);
1359 case nir_intrinsic_shared_atomic_add
:
1360 case nir_intrinsic_shared_atomic_imin
:
1361 case nir_intrinsic_shared_atomic_umin
:
1362 case nir_intrinsic_shared_atomic_imax
:
1363 case nir_intrinsic_shared_atomic_umax
:
1364 case nir_intrinsic_shared_atomic_and
:
1365 case nir_intrinsic_shared_atomic_or
:
1366 case nir_intrinsic_shared_atomic_xor
:
1367 case nir_intrinsic_shared_atomic_exchange
:
1368 case nir_intrinsic_shared_atomic_comp_swap
:
1369 case nir_intrinsic_shared_atomic_fadd
:
1370 case nir_intrinsic_shared_atomic_fmin
:
1371 case nir_intrinsic_shared_atomic_fmax
:
1372 case nir_intrinsic_shared_atomic_fcomp_swap
: {
1373 int param_count
= ir
->actual_parameters
.length();
1374 assert(param_count
== 2 || param_count
== 3);
1377 exec_node
*param
= ir
->actual_parameters
.get_head();
1378 ir_instruction
*inst
= (ir_instruction
*) param
;
1379 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1381 /* data1 parameter (this is always present) */
1382 param
= param
->get_next();
1383 inst
= (ir_instruction
*) param
;
1384 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1386 /* data2 parameter (only with atomic_comp_swap) */
1387 if (param_count
== 3) {
1388 assert(op
== nir_intrinsic_shared_atomic_comp_swap
||
1389 op
== nir_intrinsic_shared_atomic_fcomp_swap
);
1390 param
= param
->get_next();
1391 inst
= (ir_instruction
*) param
;
1393 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1397 assert(ir
->return_deref
);
1398 unsigned bit_size
= glsl_get_bit_size(ir
->return_deref
->type
);
1399 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1400 ir
->return_deref
->type
->vector_elements
,
1402 nir_builder_instr_insert(&b
, &instr
->instr
);
1405 case nir_intrinsic_vote_any
:
1406 case nir_intrinsic_vote_all
:
1407 case nir_intrinsic_vote_ieq
: {
1408 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 1, NULL
);
1409 instr
->num_components
= 1;
1411 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1412 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1414 nir_builder_instr_insert(&b
, &instr
->instr
);
1418 case nir_intrinsic_ballot
: {
1419 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1420 ir
->return_deref
->type
->vector_elements
, 64, NULL
);
1421 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1423 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1424 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1426 nir_builder_instr_insert(&b
, &instr
->instr
);
1429 case nir_intrinsic_read_invocation
: {
1430 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1431 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1432 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1434 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1435 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1437 ir_rvalue
*invocation
= (ir_rvalue
*) ir
->actual_parameters
.get_head()->next
;
1438 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(invocation
));
1440 nir_builder_instr_insert(&b
, &instr
->instr
);
1443 case nir_intrinsic_read_first_invocation
: {
1444 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1445 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1446 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1448 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1449 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1451 nir_builder_instr_insert(&b
, &instr
->instr
);
1455 unreachable("not reached");
1458 if (ir
->return_deref
)
1459 nir_store_deref(&b
, evaluate_deref(ir
->return_deref
), ret
, ~0);
1464 struct hash_entry
*entry
=
1465 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1467 nir_function
*callee
= (nir_function
*) entry
->data
;
1469 nir_call_instr
*call
= nir_call_instr_create(this->shader
, callee
);
1472 nir_deref_instr
*ret_deref
= NULL
;
1473 if (ir
->return_deref
) {
1474 nir_variable
*ret_tmp
=
1475 nir_local_variable_create(this->impl
, ir
->return_deref
->type
,
1477 ret_deref
= nir_build_deref_var(&b
, ret_tmp
);
1478 call
->params
[i
++] = nir_src_for_ssa(&ret_deref
->dest
.ssa
);
1481 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
1482 actual_node
, &ir
->actual_parameters
) {
1483 ir_rvalue
*param_rvalue
= (ir_rvalue
*) actual_node
;
1484 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
1486 if (sig_param
->data
.mode
== ir_var_function_out
) {
1487 nir_deref_instr
*out_deref
= evaluate_deref(param_rvalue
);
1488 call
->params
[i
] = nir_src_for_ssa(&out_deref
->dest
.ssa
);
1489 } else if (sig_param
->data
.mode
== ir_var_function_in
) {
1490 nir_ssa_def
*val
= evaluate_rvalue(param_rvalue
);
1491 nir_src src
= nir_src_for_ssa(val
);
1493 nir_src_copy(&call
->params
[i
], &src
, call
);
1494 } else if (sig_param
->data
.mode
== ir_var_function_inout
) {
1495 unreachable("unimplemented: inout parameters");
1501 nir_builder_instr_insert(&b
, &call
->instr
);
1503 if (ir
->return_deref
)
1504 nir_store_deref(&b
, evaluate_deref(ir
->return_deref
), nir_load_deref(&b
, ret_deref
), ~0);
1508 nir_visitor::visit(ir_assignment
*ir
)
1510 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1512 b
.exact
= ir
->lhs
->variable_referenced()->data
.invariant
||
1513 ir
->lhs
->variable_referenced()->data
.precise
;
1515 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1516 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1517 if (ir
->condition
) {
1518 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
1519 nir_copy_deref(&b
, evaluate_deref(ir
->lhs
), evaluate_deref(ir
->rhs
));
1520 nir_pop_if(&b
, NULL
);
1522 nir_copy_deref(&b
, evaluate_deref(ir
->lhs
), evaluate_deref(ir
->rhs
));
1527 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1529 ir
->lhs
->accept(this);
1530 nir_deref_instr
*lhs_deref
= this->deref
;
1531 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1533 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1534 /* GLSL IR will give us the input to the write-masked assignment in a
1535 * single packed vector. So, for example, if the writemask is xzw, then
1536 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1540 unsigned component
= 0;
1541 for (unsigned i
= 0; i
< 4; i
++) {
1542 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1544 src
= nir_swizzle(&b
, src
, swiz
, num_components
, false);
1547 if (ir
->condition
) {
1548 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
1549 nir_store_deref(&b
, lhs_deref
, src
, ir
->write_mask
);
1550 nir_pop_if(&b
, NULL
);
1552 nir_store_deref(&b
, lhs_deref
, src
, ir
->write_mask
);
1557 * Given an instruction, returns a pointer to its destination or NULL if there
1558 * is no destination.
1560 * Note that this only handles instructions we generate at this level.
1563 get_instr_dest(nir_instr
*instr
)
1565 nir_alu_instr
*alu_instr
;
1566 nir_intrinsic_instr
*intrinsic_instr
;
1567 nir_tex_instr
*tex_instr
;
1569 switch (instr
->type
) {
1570 case nir_instr_type_alu
:
1571 alu_instr
= nir_instr_as_alu(instr
);
1572 return &alu_instr
->dest
.dest
;
1574 case nir_instr_type_intrinsic
:
1575 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1576 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1577 return &intrinsic_instr
->dest
;
1581 case nir_instr_type_tex
:
1582 tex_instr
= nir_instr_as_tex(instr
);
1583 return &tex_instr
->dest
;
1586 unreachable("not reached");
1593 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
,
1596 nir_dest
*dest
= get_instr_dest(instr
);
1599 nir_ssa_dest_init(instr
, dest
, num_components
, bit_size
, NULL
);
1601 nir_builder_instr_insert(&b
, instr
);
1604 assert(dest
->is_ssa
);
1605 this->result
= &dest
->ssa
;
1610 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1613 if (ir
->as_dereference() || ir
->as_constant()) {
1615 * A dereference is being used on the right hand side, which means we
1616 * must emit a variable load.
1619 this->result
= nir_load_deref(&b
, this->deref
);
1622 return this->result
;
1626 type_is_float(glsl_base_type type
)
1628 return type
== GLSL_TYPE_FLOAT
|| type
== GLSL_TYPE_DOUBLE
||
1629 type
== GLSL_TYPE_FLOAT16
;
1633 type_is_signed(glsl_base_type type
)
1635 return type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_INT64
||
1636 type
== GLSL_TYPE_INT16
;
1640 type_is_int(glsl_base_type type
)
1642 return type
== GLSL_TYPE_UINT
|| type
== GLSL_TYPE_INT
||
1643 type
== GLSL_TYPE_UINT8
|| type
== GLSL_TYPE_INT8
||
1644 type
== GLSL_TYPE_UINT16
|| type
== GLSL_TYPE_INT16
||
1645 type
== GLSL_TYPE_UINT64
|| type
== GLSL_TYPE_INT64
;
1649 nir_visitor::visit(ir_expression
*ir
)
1651 /* Some special cases */
1652 switch (ir
->operation
) {
1653 case ir_binop_ubo_load
: {
1654 nir_intrinsic_instr
*load
=
1655 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1656 unsigned bit_size
= ir
->type
->is_boolean() ? 32 :
1657 glsl_get_bit_size(ir
->type
);
1658 load
->num_components
= ir
->type
->vector_elements
;
1659 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1660 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1661 intrinsic_set_std430_align(load
, ir
->type
);
1662 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1665 * In UBO's, a true boolean value is any non-zero value, but we consider
1666 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1669 if (ir
->type
->is_boolean())
1670 this->result
= nir_i2b(&b
, &load
->dest
.ssa
);
1675 case ir_unop_interpolate_at_centroid
:
1676 case ir_binop_interpolate_at_offset
:
1677 case ir_binop_interpolate_at_sample
: {
1678 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1679 ir_swizzle
*swizzle
= NULL
;
1681 /* the api does not allow a swizzle here, but the varying packing code
1682 * may have pushed one into here.
1684 swizzle
= ir
->operands
[0]->as_swizzle();
1686 deref
= swizzle
->val
->as_dereference();
1690 deref
->accept(this);
1692 nir_intrinsic_op op
;
1693 if (this->deref
->mode
== nir_var_shader_in
) {
1694 switch (ir
->operation
) {
1695 case ir_unop_interpolate_at_centroid
:
1696 op
= nir_intrinsic_interp_deref_at_centroid
;
1698 case ir_binop_interpolate_at_offset
:
1699 op
= nir_intrinsic_interp_deref_at_offset
;
1701 case ir_binop_interpolate_at_sample
:
1702 op
= nir_intrinsic_interp_deref_at_sample
;
1705 unreachable("Invalid interpolation intrinsic");
1708 /* This case can happen if the vertex shader does not write the
1709 * given varying. In this case, the linker will lower it to a
1710 * global variable. Since interpolating a variable makes no
1711 * sense, we'll just turn it into a load which will probably
1712 * eventually end up as an SSA definition.
1714 assert(this->deref
->mode
== nir_var_shader_temp
);
1715 op
= nir_intrinsic_load_deref
;
1718 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1719 intrin
->num_components
= deref
->type
->vector_elements
;
1720 intrin
->src
[0] = nir_src_for_ssa(&this->deref
->dest
.ssa
);
1722 if (intrin
->intrinsic
== nir_intrinsic_interp_deref_at_offset
||
1723 intrin
->intrinsic
== nir_intrinsic_interp_deref_at_sample
)
1724 intrin
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1726 unsigned bit_size
= glsl_get_bit_size(deref
->type
);
1727 add_instr(&intrin
->instr
, deref
->type
->vector_elements
, bit_size
);
1730 unsigned swiz
[4] = {
1731 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1734 result
= nir_swizzle(&b
, result
, swiz
,
1735 swizzle
->type
->vector_elements
, false);
1745 nir_ssa_def
*srcs
[4];
1746 for (unsigned i
= 0; i
< ir
->num_operands
; i
++)
1747 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1749 glsl_base_type types
[4];
1750 for (unsigned i
= 0; i
< ir
->num_operands
; i
++)
1751 if (supports_ints
|| !type_is_int(ir
->operands
[i
]->type
->base_type
))
1752 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1754 types
[i
] = GLSL_TYPE_FLOAT
;
1756 glsl_base_type out_type
;
1757 if (supports_ints
|| !type_is_int(ir
->type
->base_type
))
1758 out_type
= ir
->type
->base_type
;
1760 out_type
= GLSL_TYPE_FLOAT
;
1762 switch (ir
->operation
) {
1763 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1764 case ir_unop_logic_not
:
1765 result
= nir_inot(&b
, srcs
[0]);
1768 result
= type_is_float(types
[0]) ? nir_fneg(&b
, srcs
[0])
1769 : nir_ineg(&b
, srcs
[0]);
1772 result
= type_is_float(types
[0]) ? nir_fabs(&b
, srcs
[0])
1773 : nir_iabs(&b
, srcs
[0]);
1775 case ir_unop_saturate
:
1776 assert(type_is_float(types
[0]));
1777 result
= nir_fsat(&b
, srcs
[0]);
1780 result
= type_is_float(types
[0]) ? nir_fsign(&b
, srcs
[0])
1781 : nir_isign(&b
, srcs
[0]);
1783 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1784 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1785 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1786 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1787 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1788 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1789 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1791 result
= supports_ints
? nir_i2f32(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1794 result
= supports_ints
? nir_u2f32(&b
, srcs
[0]) : nir_fmov(&b
, srcs
[0]);
1797 result
= nir_b2f32(&b
, srcs
[0]);
1800 result
= supports_ints
? nir_f2i32(&b
, srcs
[0]) : nir_ftrunc(&b
, srcs
[0]);
1803 result
= supports_ints
? nir_f2u32(&b
, srcs
[0]) : nir_ftrunc(&b
, srcs
[0]);
1835 case ir_unop_i642u64
:
1836 case ir_unop_u642i64
: {
1837 nir_alu_type src_type
= nir_get_nir_type_for_glsl_base_type(types
[0]);
1838 nir_alu_type dst_type
= nir_get_nir_type_for_glsl_base_type(out_type
);
1839 result
= nir_build_alu(&b
, nir_type_conversion_op(src_type
, dst_type
,
1840 nir_rounding_mode_undef
),
1841 srcs
[0], NULL
, NULL
, NULL
);
1842 /* b2i and b2f don't have fixed bit-size versions so the builder will
1843 * just assume 32 and we have to fix it up here.
1845 result
->bit_size
= nir_alu_type_get_type_size(dst_type
);
1849 case ir_unop_bitcast_i2f
:
1850 case ir_unop_bitcast_f2i
:
1851 case ir_unop_bitcast_u2f
:
1852 case ir_unop_bitcast_f2u
:
1853 case ir_unop_bitcast_i642d
:
1854 case ir_unop_bitcast_d2i64
:
1855 case ir_unop_bitcast_u642d
:
1856 case ir_unop_bitcast_d2u64
:
1857 case ir_unop_subroutine_to_int
:
1859 result
= nir_imov(&b
, srcs
[0]);
1861 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1862 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1863 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1864 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1865 case ir_unop_frexp_exp
: result
= nir_frexp_exp(&b
, srcs
[0]); break;
1866 case ir_unop_frexp_sig
: result
= nir_frexp_sig(&b
, srcs
[0]); break;
1867 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1868 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1869 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1870 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1871 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1872 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1873 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1874 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1875 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1876 case ir_unop_pack_snorm_2x16
:
1877 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1879 case ir_unop_pack_snorm_4x8
:
1880 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1882 case ir_unop_pack_unorm_2x16
:
1883 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1885 case ir_unop_pack_unorm_4x8
:
1886 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1888 case ir_unop_pack_half_2x16
:
1889 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1891 case ir_unop_unpack_snorm_2x16
:
1892 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1894 case ir_unop_unpack_snorm_4x8
:
1895 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1897 case ir_unop_unpack_unorm_2x16
:
1898 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1900 case ir_unop_unpack_unorm_4x8
:
1901 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
1903 case ir_unop_unpack_half_2x16
:
1904 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
1906 case ir_unop_pack_sampler_2x32
:
1907 case ir_unop_pack_image_2x32
:
1908 case ir_unop_pack_double_2x32
:
1909 case ir_unop_pack_int_2x32
:
1910 case ir_unop_pack_uint_2x32
:
1911 result
= nir_pack_64_2x32(&b
, srcs
[0]);
1913 case ir_unop_unpack_sampler_2x32
:
1914 case ir_unop_unpack_image_2x32
:
1915 case ir_unop_unpack_double_2x32
:
1916 case ir_unop_unpack_int_2x32
:
1917 case ir_unop_unpack_uint_2x32
:
1918 result
= nir_unpack_64_2x32(&b
, srcs
[0]);
1920 case ir_unop_bitfield_reverse
:
1921 result
= nir_bitfield_reverse(&b
, srcs
[0]);
1923 case ir_unop_bit_count
:
1924 result
= nir_bit_count(&b
, srcs
[0]);
1926 case ir_unop_find_msb
:
1928 case GLSL_TYPE_UINT
:
1929 result
= nir_ufind_msb(&b
, srcs
[0]);
1932 result
= nir_ifind_msb(&b
, srcs
[0]);
1935 unreachable("Invalid type for findMSB()");
1938 case ir_unop_find_lsb
:
1939 result
= nir_find_lsb(&b
, srcs
[0]);
1943 switch (ir
->type
->vector_elements
) {
1945 switch (ir
->operands
[0]->type
->vector_elements
) {
1946 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
1947 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
1948 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
1949 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
1950 default: unreachable("not reached");
1954 switch (ir
->operands
[0]->type
->vector_elements
) {
1955 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
1956 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
1957 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
1958 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
1959 default: unreachable("not reached");
1963 switch (ir
->operands
[0]->type
->vector_elements
) {
1964 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
1965 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
1966 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
1967 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
1968 default: unreachable("not reached");
1972 switch (ir
->operands
[0]->type
->vector_elements
) {
1973 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
1974 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
1975 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
1976 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
1977 default: unreachable("not reached");
1981 unreachable("not reached");
1984 case ir_unop_get_buffer_size
: {
1985 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
1987 nir_intrinsic_get_buffer_size
);
1988 load
->num_components
= ir
->type
->vector_elements
;
1989 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1990 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
1991 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1996 result
= type_is_float(out_type
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
1997 : nir_iadd(&b
, srcs
[0], srcs
[1]);
2000 result
= type_is_float(out_type
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
2001 : nir_isub(&b
, srcs
[0], srcs
[1]);
2004 if (type_is_float(out_type
))
2005 result
= nir_fmul(&b
, srcs
[0], srcs
[1]);
2006 else if (out_type
== GLSL_TYPE_INT64
&&
2007 (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
||
2008 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
))
2009 result
= nir_imul_2x32_64(&b
, srcs
[0], srcs
[1]);
2010 else if (out_type
== GLSL_TYPE_UINT64
&&
2011 (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
||
2012 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_UINT
))
2013 result
= nir_umul_2x32_64(&b
, srcs
[0], srcs
[1]);
2015 result
= nir_imul(&b
, srcs
[0], srcs
[1]);
2018 if (type_is_float(out_type
))
2019 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
2020 else if (type_is_signed(out_type
))
2021 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
2023 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
2026 result
= type_is_float(out_type
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
2027 : nir_umod(&b
, srcs
[0], srcs
[1]);
2030 if (type_is_float(out_type
))
2031 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
2032 else if (type_is_signed(out_type
))
2033 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
2035 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
2038 if (type_is_float(out_type
))
2039 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
2040 else if (type_is_signed(out_type
))
2041 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
2043 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
2045 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
2046 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
2047 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
2048 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
2049 case ir_binop_logic_and
:
2050 result
= nir_iand(&b
, srcs
[0], srcs
[1]);
2052 case ir_binop_logic_or
:
2053 result
= nir_ior(&b
, srcs
[0], srcs
[1]);
2055 case ir_binop_logic_xor
:
2056 result
= nir_ixor(&b
, srcs
[0], srcs
[1]);
2058 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
2059 case ir_binop_rshift
:
2060 result
= (type_is_signed(out_type
)) ? nir_ishr(&b
, srcs
[0], srcs
[1])
2061 : nir_ushr(&b
, srcs
[0], srcs
[1]);
2063 case ir_binop_imul_high
:
2064 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
2065 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
2067 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
2068 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
2070 if (type_is_float(types
[0]))
2071 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
2072 else if (type_is_signed(types
[0]))
2073 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
2075 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
2077 case ir_binop_gequal
:
2078 if (type_is_float(types
[0]))
2079 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
2080 else if (type_is_signed(types
[0]))
2081 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
2083 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
2085 case ir_binop_equal
:
2086 if (type_is_float(types
[0]))
2087 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
2089 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
2091 case ir_binop_nequal
:
2092 if (type_is_float(types
[0]))
2093 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
2095 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
2097 case ir_binop_all_equal
:
2098 if (type_is_float(types
[0])) {
2099 switch (ir
->operands
[0]->type
->vector_elements
) {
2100 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
2101 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
2102 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
2103 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
2105 unreachable("not reached");
2108 switch (ir
->operands
[0]->type
->vector_elements
) {
2109 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
2110 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
2111 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
2112 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
2114 unreachable("not reached");
2118 case ir_binop_any_nequal
:
2119 if (type_is_float(types
[0])) {
2120 switch (ir
->operands
[0]->type
->vector_elements
) {
2121 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
2122 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
2123 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
2124 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
2126 unreachable("not reached");
2129 switch (ir
->operands
[0]->type
->vector_elements
) {
2130 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
2131 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
2132 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
2133 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
2135 unreachable("not reached");
2140 switch (ir
->operands
[0]->type
->vector_elements
) {
2141 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
2142 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
2143 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
2145 unreachable("not reached");
2148 case ir_binop_vector_extract
: {
2149 result
= nir_channel(&b
, srcs
[0], 0);
2150 for (unsigned i
= 1; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
2151 nir_ssa_def
*swizzled
= nir_channel(&b
, srcs
[0], i
);
2152 result
= nir_bcsel(&b
, nir_ieq(&b
, srcs
[1], nir_imm_int(&b
, i
)),
2158 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
2160 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
2163 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
2166 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
2168 case ir_triop_bitfield_extract
:
2169 result
= (out_type
== GLSL_TYPE_INT
) ?
2170 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
2171 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
2173 case ir_quadop_bitfield_insert
:
2174 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
2176 case ir_quadop_vector
:
2177 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
2181 unreachable("not reached");
2186 nir_visitor::visit(ir_swizzle
*ir
)
2188 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
2189 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
2190 ir
->type
->vector_elements
, false);
2194 nir_visitor::visit(ir_texture
*ir
)
2201 num_srcs
= 1; /* coordinate */
2206 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
2207 num_srcs
= 2; /* coordinate, bias/lod */
2211 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
2217 if (ir
->lod_info
.lod
!= NULL
)
2218 num_srcs
= 2; /* coordinate, lod */
2220 num_srcs
= 1; /* coordinate */
2224 op
= nir_texop_txf_ms
;
2225 num_srcs
= 2; /* coordinate, sample_index */
2230 if (ir
->lod_info
.lod
!= NULL
)
2231 num_srcs
= 1; /* lod */
2238 num_srcs
= 1; /* coordinate */
2243 num_srcs
= 1; /* coordinate */
2246 case ir_query_levels
:
2247 op
= nir_texop_query_levels
;
2251 case ir_texture_samples
:
2252 op
= nir_texop_texture_samples
;
2256 case ir_samples_identical
:
2257 op
= nir_texop_samples_identical
;
2258 num_srcs
= 1; /* coordinate */
2262 unreachable("not reached");
2265 if (ir
->projector
!= NULL
)
2267 if (ir
->shadow_comparator
!= NULL
)
2269 if (ir
->offset
!= NULL
)
2272 /* Add one for the texture deref */
2275 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
2278 instr
->sampler_dim
=
2279 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
2280 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
2281 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
2282 if (instr
->is_shadow
)
2283 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
2284 switch (ir
->type
->base_type
) {
2285 case GLSL_TYPE_FLOAT
:
2286 instr
->dest_type
= nir_type_float
;
2289 instr
->dest_type
= nir_type_int
;
2291 case GLSL_TYPE_BOOL
:
2292 case GLSL_TYPE_UINT
:
2293 instr
->dest_type
= nir_type_uint
;
2296 unreachable("not reached");
2299 nir_deref_instr
*sampler_deref
= evaluate_deref(ir
->sampler
);
2300 instr
->src
[0].src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2301 instr
->src
[0].src_type
= nir_tex_src_texture_deref
;
2302 instr
->src
[1].src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2303 instr
->src
[1].src_type
= nir_tex_src_sampler_deref
;
2305 unsigned src_number
= 2;
2307 if (ir
->coordinate
!= NULL
) {
2308 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
2309 instr
->src
[src_number
].src
=
2310 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
2311 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
2315 if (ir
->projector
!= NULL
) {
2316 instr
->src
[src_number
].src
=
2317 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
2318 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
2322 if (ir
->shadow_comparator
!= NULL
) {
2323 instr
->src
[src_number
].src
=
2324 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparator
));
2325 instr
->src
[src_number
].src_type
= nir_tex_src_comparator
;
2329 if (ir
->offset
!= NULL
) {
2330 /* we don't support multiple offsets yet */
2331 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
2333 instr
->src
[src_number
].src
=
2334 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
2335 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
2341 instr
->src
[src_number
].src
=
2342 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
2343 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
2350 if (ir
->lod_info
.lod
!= NULL
) {
2351 instr
->src
[src_number
].src
=
2352 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
2353 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
2359 instr
->src
[src_number
].src
=
2360 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
2361 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
2363 instr
->src
[src_number
].src
=
2364 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
2365 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
2370 instr
->src
[src_number
].src
=
2371 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
2372 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
2377 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
2384 assert(src_number
== num_srcs
);
2386 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
2387 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
), bit_size
);
2391 nir_visitor::visit(ir_constant
*ir
)
2394 * We don't know if this variable is an array or struct that gets
2395 * dereferenced, so do the safe thing an make it a variable with a
2396 * constant initializer and return a dereference.
2400 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
2401 var
->data
.read_only
= true;
2402 var
->constant_initializer
= constant_copy(ir
, var
);
2404 this->deref
= nir_build_deref_var(&b
, var
);
2408 nir_visitor::visit(ir_dereference_variable
*ir
)
2410 if (ir
->variable_referenced()->data
.mode
== ir_var_function_out
) {
2411 unsigned i
= (sig
->return_type
!= glsl_type::void_type
) ? 1 : 0;
2413 foreach_in_list(ir_variable
, param
, &sig
->parameters
) {
2414 if (param
== ir
->variable_referenced()) {
2420 this->deref
= nir_build_deref_cast(&b
, nir_load_param(&b
, i
),
2421 nir_var_function_temp
, ir
->type
, 0);
2425 assert(ir
->variable_referenced()->data
.mode
!= ir_var_function_inout
);
2427 struct hash_entry
*entry
=
2428 _mesa_hash_table_search(this->var_table
, ir
->var
);
2430 nir_variable
*var
= (nir_variable
*) entry
->data
;
2432 this->deref
= nir_build_deref_var(&b
, var
);
2436 nir_visitor::visit(ir_dereference_record
*ir
)
2438 ir
->record
->accept(this);
2440 int field_index
= ir
->field_idx
;
2441 assert(field_index
>= 0);
2443 this->deref
= nir_build_deref_struct(&b
, this->deref
, field_index
);
2447 nir_visitor::visit(ir_dereference_array
*ir
)
2449 nir_ssa_def
*index
= evaluate_rvalue(ir
->array_index
);
2451 ir
->array
->accept(this);
2453 this->deref
= nir_build_deref_array(&b
, this->deref
, index
);
2457 nir_visitor::visit(ir_barrier
*)
2459 nir_intrinsic_instr
*instr
=
2460 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2461 nir_builder_instr_insert(&b
, &instr
->instr
);
2465 glsl_float64_funcs_to_nir(struct gl_context
*ctx
,
2466 const nir_shader_compiler_options
*options
)
2468 /* We pretend it's a vertex shader. Ultimately, the stage shouldn't
2469 * matter because we're not optimizing anything here.
2471 struct gl_shader
*sh
= _mesa_new_shader(-1, MESA_SHADER_VERTEX
);
2472 sh
->Source
= float64_source
;
2473 sh
->CompileStatus
= COMPILE_FAILURE
;
2474 _mesa_glsl_compile_shader(ctx
, sh
, false, false, true);
2476 if (!sh
->CompileStatus
) {
2479 "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
2480 sh
->InfoLog
, float64_source
);
2485 nir_shader
*nir
= nir_shader_create(NULL
, MESA_SHADER_VERTEX
, options
, NULL
);
2487 nir_visitor
v1(ctx
, nir
);
2488 nir_function_visitor
v2(&v1
);
2490 visit_exec_list(sh
->ir
, &v1
);
2492 /* _mesa_delete_shader will try to free sh->Source but it's static const */
2494 _mesa_delete_shader(ctx
, sh
);
2496 nir_validate_shader(nir
, "float64_funcs_to_nir");
2498 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_function_temp
);
2499 NIR_PASS_V(nir
, nir_lower_returns
);
2500 NIR_PASS_V(nir
, nir_inline_functions
);
2501 NIR_PASS_V(nir
, nir_opt_deref
);