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
);
96 nir_function_impl
*impl
;
98 nir_ssa_def
*result
; /* result of the expression tree last visited */
100 nir_deref_instr
*evaluate_deref(ir_instruction
*ir
);
102 nir_constant
*constant_copy(ir_constant
*ir
, void *mem_ctx
);
104 /* most recent deref instruction created */
105 nir_deref_instr
*deref
;
107 /* whether the IR we're operating on is per-function or global */
110 ir_function_signature
*sig
;
112 /* map of ir_variable -> nir_variable */
113 struct hash_table
*var_table
;
115 /* map of ir_function_signature -> nir_function_overload */
116 struct hash_table
*overload_table
;
120 * This visitor runs before the main visitor, calling create_function() for
121 * each function so that the main visitor can resolve forward references in
125 class nir_function_visitor
: public ir_hierarchical_visitor
128 nir_function_visitor(nir_visitor
*v
) : visitor(v
)
131 virtual ir_visitor_status
visit_enter(ir_function
*);
134 nir_visitor
*visitor
;
137 /* glsl_to_nir can only handle converting certain function paramaters
138 * to NIR. This visitor checks for parameters it can't currently handle.
140 class ir_function_param_visitor
: public ir_hierarchical_visitor
143 ir_function_param_visitor()
148 virtual ir_visitor_status
visit_enter(ir_function_signature
*ir
)
151 if (ir
->is_intrinsic())
152 return visit_continue
;
154 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
155 if (!param
->type
->is_vector() || !param
->type
->is_scalar()) {
160 if (param
->data
.mode
== ir_var_function_inout
) {
166 return visit_continue
;
172 } /* end of anonymous namespace */
176 has_unsupported_function_param(exec_list
*ir
)
178 ir_function_param_visitor visitor
;
179 visit_list_elements(&visitor
, ir
);
180 return visitor
.unsupported
;
184 glsl_to_nir(struct gl_context
*ctx
,
185 const struct gl_shader_program
*shader_prog
,
186 gl_shader_stage stage
,
187 const nir_shader_compiler_options
*options
)
189 struct gl_linked_shader
*sh
= shader_prog
->_LinkedShaders
[stage
];
191 const struct gl_shader_compiler_options
*gl_options
=
192 &ctx
->Const
.ShaderCompilerOptions
[stage
];
194 /* glsl_to_nir can only handle converting certain function paramaters
195 * to NIR. If we find something we can't handle then we get the GLSL IR
196 * opts to remove it before we continue on.
198 * TODO: add missing glsl ir to nir support and remove this loop.
200 while (has_unsupported_function_param(sh
->ir
)) {
201 do_common_optimization(sh
->ir
, true, true, gl_options
,
202 ctx
->Const
.NativeIntegers
);
205 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
,
208 nir_visitor
v1(ctx
, shader
);
209 nir_function_visitor
v2(&v1
);
211 visit_exec_list(sh
->ir
, &v1
);
213 nir_validate_shader(shader
, "after glsl to nir, before function inline");
215 /* We have to lower away local constant initializers right before we
216 * inline functions. That way they get properly initialized at the top
217 * of the function and not at the top of its caller.
219 nir_lower_constant_initializers(shader
, (nir_variable_mode
)~0);
220 nir_lower_returns(shader
);
221 nir_inline_functions(shader
);
222 nir_opt_deref(shader
);
224 nir_validate_shader(shader
, "after function inlining and return lowering");
226 /* Now that we have inlined everything remove all of the functions except
229 foreach_list_typed_safe(nir_function
, function
, node
, &(shader
)->functions
){
230 if (strcmp("main", function
->name
) != 0) {
231 exec_node_remove(&function
->node
);
235 /* Remap the locations to slots so those requiring two slots will occupy
236 * two locations. For instance, if we have in the IR code a dvec3 attr0 in
237 * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
238 * locations/slots 0 and 1, and attr1 will use location/slot 2 */
239 if (shader
->info
.stage
== MESA_SHADER_VERTEX
)
240 nir_remap_dual_slot_attributes(shader
, &sh
->Program
->DualSlotInputs
);
242 shader
->info
.name
= ralloc_asprintf(shader
, "GLSL%d", shader_prog
->Name
);
243 if (shader_prog
->Label
)
244 shader
->info
.label
= ralloc_strdup(shader
, shader_prog
->Label
);
246 /* Check for transform feedback varyings specified via the API */
247 shader
->info
.has_transform_feedback_varyings
=
248 shader_prog
->TransformFeedback
.NumVarying
> 0;
250 /* Check for transform feedback varyings specified in the Shader */
251 if (shader_prog
->last_vert_prog
)
252 shader
->info
.has_transform_feedback_varyings
|=
253 shader_prog
->last_vert_prog
->sh
.LinkedTransformFeedback
->NumVarying
> 0;
255 if (shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
256 shader
->info
.fs
.pixel_center_integer
= sh
->Program
->info
.fs
.pixel_center_integer
;
257 shader
->info
.fs
.origin_upper_left
= sh
->Program
->info
.fs
.origin_upper_left
;
263 nir_visitor::nir_visitor(gl_context
*ctx
, nir_shader
*shader
)
265 this->supports_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
266 this->shader
= shader
;
267 this->is_global
= true;
268 this->var_table
= _mesa_pointer_hash_table_create(NULL
);
269 this->overload_table
= _mesa_pointer_hash_table_create(NULL
);
273 memset(&this->b
, 0, sizeof(this->b
));
276 nir_visitor::~nir_visitor()
278 _mesa_hash_table_destroy(this->var_table
, NULL
);
279 _mesa_hash_table_destroy(this->overload_table
, NULL
);
283 nir_visitor::evaluate_deref(ir_instruction
*ir
)
290 nir_visitor::constant_copy(ir_constant
*ir
, void *mem_ctx
)
295 nir_constant
*ret
= rzalloc(mem_ctx
, nir_constant
);
297 const unsigned rows
= ir
->type
->vector_elements
;
298 const unsigned cols
= ir
->type
->matrix_columns
;
301 ret
->num_elements
= 0;
302 switch (ir
->type
->base_type
) {
304 /* Only float base types can be matrices. */
307 for (unsigned r
= 0; r
< rows
; r
++)
308 ret
->values
[0][r
].u32
= ir
->value
.u
[r
];
313 /* Only float base types can be matrices. */
316 for (unsigned r
= 0; r
< rows
; r
++)
317 ret
->values
[0][r
].i32
= ir
->value
.i
[r
];
321 case GLSL_TYPE_FLOAT
:
322 for (unsigned c
= 0; c
< cols
; c
++) {
323 for (unsigned r
= 0; r
< rows
; r
++)
324 ret
->values
[c
][r
].f32
= ir
->value
.f
[c
* rows
+ r
];
328 case GLSL_TYPE_DOUBLE
:
329 for (unsigned c
= 0; c
< cols
; c
++) {
330 for (unsigned r
= 0; r
< rows
; r
++)
331 ret
->values
[c
][r
].f64
= ir
->value
.d
[c
* rows
+ r
];
335 case GLSL_TYPE_UINT64
:
336 /* Only float base types can be matrices. */
339 for (unsigned r
= 0; r
< rows
; r
++)
340 ret
->values
[0][r
].u64
= ir
->value
.u64
[r
];
343 case GLSL_TYPE_INT64
:
344 /* Only float base types can be matrices. */
347 for (unsigned r
= 0; r
< rows
; r
++)
348 ret
->values
[0][r
].i64
= ir
->value
.i64
[r
];
352 /* Only float base types can be matrices. */
355 for (unsigned r
= 0; r
< rows
; r
++)
356 ret
->values
[0][r
].b
= ir
->value
.b
[r
];
360 case GLSL_TYPE_STRUCT
:
361 case GLSL_TYPE_ARRAY
:
362 ret
->elements
= ralloc_array(mem_ctx
, nir_constant
*,
364 ret
->num_elements
= ir
->type
->length
;
366 for (i
= 0; i
< ir
->type
->length
; i
++)
367 ret
->elements
[i
] = constant_copy(ir
->const_elements
[i
], mem_ctx
);
371 unreachable("not reached");
377 static const glsl_type
*
378 wrap_type_in_array(const glsl_type
*elem_type
, const glsl_type
*array_type
)
380 if (!array_type
->is_array())
383 elem_type
= wrap_type_in_array(elem_type
, array_type
->fields
.array
);
385 return glsl_type::get_array_instance(elem_type
, array_type
->length
);
389 nir_visitor::visit(ir_variable
*ir
)
391 /* TODO: In future we should switch to using the NIR lowering pass but for
392 * now just ignore these variables as GLSL IR should have lowered them.
393 * Anything remaining are just dead vars that weren't cleaned up.
395 if (ir
->data
.mode
== ir_var_shader_shared
)
398 /* FINISHME: inout parameters */
399 assert(ir
->data
.mode
!= ir_var_function_inout
);
401 if (ir
->data
.mode
== ir_var_function_out
)
404 nir_variable
*var
= rzalloc(shader
, nir_variable
);
405 var
->type
= ir
->type
;
406 var
->name
= ralloc_strdup(var
, ir
->name
);
408 var
->data
.always_active_io
= ir
->data
.always_active_io
;
409 var
->data
.read_only
= ir
->data
.read_only
;
410 var
->data
.centroid
= ir
->data
.centroid
;
411 var
->data
.sample
= ir
->data
.sample
;
412 var
->data
.patch
= ir
->data
.patch
;
413 var
->data
.invariant
= ir
->data
.invariant
;
414 var
->data
.location
= ir
->data
.location
;
415 var
->data
.stream
= ir
->data
.stream
;
416 var
->data
.compact
= false;
418 switch(ir
->data
.mode
) {
420 case ir_var_temporary
:
422 var
->data
.mode
= nir_var_shader_temp
;
424 var
->data
.mode
= nir_var_function_temp
;
427 case ir_var_function_in
:
428 case ir_var_const_in
:
429 var
->data
.mode
= nir_var_function_temp
;
432 case ir_var_shader_in
:
433 if (shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
434 ir
->data
.location
== VARYING_SLOT_FACE
) {
435 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
436 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
437 var
->data
.mode
= nir_var_system_value
;
438 } else if (shader
->info
.stage
== MESA_SHADER_GEOMETRY
&&
439 ir
->data
.location
== VARYING_SLOT_PRIMITIVE_ID
) {
440 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
441 var
->data
.location
= SYSTEM_VALUE_PRIMITIVE_ID
;
442 var
->data
.mode
= nir_var_system_value
;
444 var
->data
.mode
= nir_var_shader_in
;
446 if (shader
->info
.stage
== MESA_SHADER_TESS_EVAL
&&
447 (ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
448 ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)) {
449 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
452 if (shader
->info
.stage
> MESA_SHADER_VERTEX
&&
453 ir
->data
.location
>= VARYING_SLOT_CLIP_DIST0
&&
454 ir
->data
.location
<= VARYING_SLOT_CULL_DIST1
) {
455 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
460 case ir_var_shader_out
:
461 var
->data
.mode
= nir_var_shader_out
;
462 if (shader
->info
.stage
== MESA_SHADER_TESS_CTRL
&&
463 (ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
||
464 ir
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
)) {
465 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
468 if (shader
->info
.stage
<= MESA_SHADER_GEOMETRY
&&
469 ir
->data
.location
>= VARYING_SLOT_CLIP_DIST0
&&
470 ir
->data
.location
<= VARYING_SLOT_CULL_DIST1
) {
471 var
->data
.compact
= ir
->type
->without_array()->is_scalar();
476 if (ir
->get_interface_type())
477 var
->data
.mode
= nir_var_mem_ubo
;
479 var
->data
.mode
= nir_var_uniform
;
482 case ir_var_shader_storage
:
483 var
->data
.mode
= nir_var_mem_ssbo
;
486 case ir_var_system_value
:
487 var
->data
.mode
= nir_var_system_value
;
491 unreachable("not reached");
494 unsigned image_access
= 0;
495 if (ir
->data
.memory_read_only
)
496 image_access
|= ACCESS_NON_WRITEABLE
;
497 if (ir
->data
.memory_write_only
)
498 image_access
|= ACCESS_NON_READABLE
;
499 if (ir
->data
.memory_coherent
)
500 image_access
|= ACCESS_COHERENT
;
501 if (ir
->data
.memory_volatile
)
502 image_access
|= ACCESS_VOLATILE
;
503 if (ir
->data
.memory_restrict
)
504 image_access
|= ACCESS_RESTRICT
;
506 /* For UBO and SSBO variables, we need explicit types */
507 if (var
->data
.mode
& (nir_var_mem_ubo
| nir_var_mem_ssbo
)) {
508 const glsl_type
*explicit_ifc_type
=
509 ir
->get_interface_type()->get_explicit_interface_type(supports_std430
);
511 if (ir
->type
->without_array()->is_interface()) {
512 /* If the type contains the interface, wrap the explicit type in the
513 * right number of arrays.
515 var
->type
= wrap_type_in_array(explicit_ifc_type
, ir
->type
);
517 /* Otherwise, this variable is one entry in the interface */
518 UNUSED
bool found
= false;
519 for (unsigned i
= 0; i
< explicit_ifc_type
->length
; i
++) {
520 const glsl_struct_field
*field
=
521 &explicit_ifc_type
->fields
.structure
[i
];
522 if (strcmp(ir
->name
, field
->name
) != 0)
525 var
->type
= field
->type
;
526 if (field
->memory_read_only
)
527 image_access
|= ACCESS_NON_WRITEABLE
;
528 if (field
->memory_write_only
)
529 image_access
|= ACCESS_NON_READABLE
;
530 if (field
->memory_coherent
)
531 image_access
|= ACCESS_COHERENT
;
532 if (field
->memory_volatile
)
533 image_access
|= ACCESS_VOLATILE
;
534 if (field
->memory_restrict
)
535 image_access
|= ACCESS_RESTRICT
;
544 var
->data
.interpolation
= ir
->data
.interpolation
;
545 var
->data
.location_frac
= ir
->data
.location_frac
;
547 switch (ir
->data
.depth_layout
) {
548 case ir_depth_layout_none
:
549 var
->data
.depth_layout
= nir_depth_layout_none
;
551 case ir_depth_layout_any
:
552 var
->data
.depth_layout
= nir_depth_layout_any
;
554 case ir_depth_layout_greater
:
555 var
->data
.depth_layout
= nir_depth_layout_greater
;
557 case ir_depth_layout_less
:
558 var
->data
.depth_layout
= nir_depth_layout_less
;
560 case ir_depth_layout_unchanged
:
561 var
->data
.depth_layout
= nir_depth_layout_unchanged
;
564 unreachable("not reached");
567 var
->data
.index
= ir
->data
.index
;
568 var
->data
.descriptor_set
= 0;
569 var
->data
.binding
= ir
->data
.binding
;
570 var
->data
.explicit_binding
= ir
->data
.explicit_binding
;
571 var
->data
.bindless
= ir
->data
.bindless
;
572 var
->data
.offset
= ir
->data
.offset
;
574 var
->data
.image
.access
= (gl_access_qualifier
)image_access
;
575 var
->data
.image
.format
= ir
->data
.image_format
;
577 var
->data
.fb_fetch_output
= ir
->data
.fb_fetch_output
;
578 var
->data
.explicit_xfb_buffer
= ir
->data
.explicit_xfb_buffer
;
579 var
->data
.explicit_xfb_stride
= ir
->data
.explicit_xfb_stride
;
580 var
->data
.xfb_buffer
= ir
->data
.xfb_buffer
;
581 var
->data
.xfb_stride
= ir
->data
.xfb_stride
;
583 var
->num_state_slots
= ir
->get_num_state_slots();
584 if (var
->num_state_slots
> 0) {
585 var
->state_slots
= rzalloc_array(var
, nir_state_slot
,
586 var
->num_state_slots
);
588 ir_state_slot
*state_slots
= ir
->get_state_slots();
589 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
590 for (unsigned j
= 0; j
< 5; j
++)
591 var
->state_slots
[i
].tokens
[j
] = state_slots
[i
].tokens
[j
];
592 var
->state_slots
[i
].swizzle
= state_slots
[i
].swizzle
;
595 var
->state_slots
= NULL
;
598 var
->constant_initializer
= constant_copy(ir
->constant_initializer
, var
);
600 var
->interface_type
= ir
->get_interface_type();
602 if (var
->data
.mode
== nir_var_function_temp
)
603 nir_function_impl_add_variable(impl
, var
);
605 nir_shader_add_variable(shader
, var
);
607 _mesa_hash_table_insert(var_table
, ir
, var
);
611 nir_function_visitor::visit_enter(ir_function
*ir
)
613 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
614 visitor
->create_function(sig
);
616 return visit_continue_with_parent
;
620 nir_visitor::create_function(ir_function_signature
*ir
)
622 if (ir
->is_intrinsic())
625 nir_function
*func
= nir_function_create(shader
, ir
->function_name());
626 if (strcmp(ir
->function_name(), "main") == 0)
627 func
->is_entrypoint
= true;
629 func
->num_params
= ir
->parameters
.length() +
630 (ir
->return_type
!= glsl_type::void_type
);
631 func
->params
= ralloc_array(shader
, nir_parameter
, func
->num_params
);
635 if (ir
->return_type
!= glsl_type::void_type
) {
636 /* The return value is a variable deref (basically an out parameter) */
637 func
->params
[np
].num_components
= 1;
638 func
->params
[np
].bit_size
= 32;
642 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
643 /* FINISHME: pass arrays, structs, etc by reference? */
644 assert(param
->type
->is_vector() || param
->type
->is_scalar());
646 if (param
->data
.mode
== ir_var_function_in
) {
647 func
->params
[np
].num_components
= param
->type
->vector_elements
;
648 func
->params
[np
].bit_size
= glsl_get_bit_size(param
->type
);
650 func
->params
[np
].num_components
= 1;
651 func
->params
[np
].bit_size
= 32;
655 assert(np
== func
->num_params
);
657 _mesa_hash_table_insert(this->overload_table
, ir
, func
);
661 nir_visitor::visit(ir_function
*ir
)
663 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
)
668 nir_visitor::visit(ir_function_signature
*ir
)
670 if (ir
->is_intrinsic())
675 struct hash_entry
*entry
=
676 _mesa_hash_table_search(this->overload_table
, ir
);
679 nir_function
*func
= (nir_function
*) entry
->data
;
681 if (ir
->is_defined
) {
682 nir_function_impl
*impl
= nir_function_impl_create(func
);
685 this->is_global
= false;
687 nir_builder_init(&b
, impl
);
688 b
.cursor
= nir_after_cf_list(&impl
->body
);
690 unsigned i
= (ir
->return_type
!= glsl_type::void_type
) ? 1 : 0;
692 foreach_in_list(ir_variable
, param
, &ir
->parameters
) {
694 nir_local_variable_create(impl
, param
->type
, param
->name
);
696 if (param
->data
.mode
== ir_var_function_in
) {
697 nir_store_var(&b
, var
, nir_load_param(&b
, i
), ~0);
700 _mesa_hash_table_insert(var_table
, param
, var
);
704 visit_exec_list(&ir
->body
, this);
706 this->is_global
= true;
713 nir_visitor::visit(ir_loop
*ir
)
716 visit_exec_list(&ir
->body_instructions
, this);
717 nir_pop_loop(&b
, NULL
);
721 nir_visitor::visit(ir_if
*ir
)
723 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
724 visit_exec_list(&ir
->then_instructions
, this);
725 nir_push_else(&b
, NULL
);
726 visit_exec_list(&ir
->else_instructions
, this);
727 nir_pop_if(&b
, NULL
);
731 nir_visitor::visit(ir_discard
*ir
)
734 * discards aren't treated as control flow, because before we lower them
735 * they can appear anywhere in the shader and the stuff after them may still
736 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
737 * discards will be immediately followed by a return.
740 nir_intrinsic_instr
*discard
;
742 discard
= nir_intrinsic_instr_create(this->shader
,
743 nir_intrinsic_discard_if
);
745 nir_src_for_ssa(evaluate_rvalue(ir
->condition
));
747 discard
= nir_intrinsic_instr_create(this->shader
, nir_intrinsic_discard
);
750 nir_builder_instr_insert(&b
, &discard
->instr
);
754 nir_visitor::visit(ir_emit_vertex
*ir
)
756 nir_intrinsic_instr
*instr
=
757 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_emit_vertex
);
758 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
759 nir_builder_instr_insert(&b
, &instr
->instr
);
763 nir_visitor::visit(ir_end_primitive
*ir
)
765 nir_intrinsic_instr
*instr
=
766 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_end_primitive
);
767 nir_intrinsic_set_stream_id(instr
, ir
->stream_id());
768 nir_builder_instr_insert(&b
, &instr
->instr
);
772 nir_visitor::visit(ir_loop_jump
*ir
)
776 case ir_loop_jump::jump_break
:
777 type
= nir_jump_break
;
779 case ir_loop_jump::jump_continue
:
780 type
= nir_jump_continue
;
783 unreachable("not reached");
786 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, type
);
787 nir_builder_instr_insert(&b
, &instr
->instr
);
791 nir_visitor::visit(ir_return
*ir
)
793 if (ir
->value
!= NULL
) {
794 nir_deref_instr
*ret_deref
=
795 nir_build_deref_cast(&b
, nir_load_param(&b
, 0),
796 nir_var_function_temp
, ir
->value
->type
, 0);
798 nir_ssa_def
*val
= evaluate_rvalue(ir
->value
);
799 nir_store_deref(&b
, ret_deref
, val
, ~0);
802 nir_jump_instr
*instr
= nir_jump_instr_create(this->shader
, nir_jump_return
);
803 nir_builder_instr_insert(&b
, &instr
->instr
);
807 intrinsic_set_std430_align(nir_intrinsic_instr
*intrin
, const glsl_type
*type
)
809 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
810 unsigned pow2_components
= util_next_power_of_two(type
->vector_elements
);
811 nir_intrinsic_set_align(intrin
, (bit_size
/ 8) * pow2_components
, 0);
815 nir_visitor::visit(ir_call
*ir
)
817 if (ir
->callee
->is_intrinsic()) {
820 switch (ir
->callee
->intrinsic_id
) {
821 case ir_intrinsic_generic_atomic_add
:
822 op
= ir
->return_deref
->type
->is_integer_32_64()
823 ? nir_intrinsic_deref_atomic_add
: nir_intrinsic_deref_atomic_fadd
;
825 case ir_intrinsic_generic_atomic_and
:
826 op
= nir_intrinsic_deref_atomic_and
;
828 case ir_intrinsic_generic_atomic_or
:
829 op
= nir_intrinsic_deref_atomic_or
;
831 case ir_intrinsic_generic_atomic_xor
:
832 op
= nir_intrinsic_deref_atomic_xor
;
834 case ir_intrinsic_generic_atomic_min
:
835 assert(ir
->return_deref
);
836 if (ir
->return_deref
->type
== glsl_type::int_type
)
837 op
= nir_intrinsic_deref_atomic_imin
;
838 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
839 op
= nir_intrinsic_deref_atomic_umin
;
840 else if (ir
->return_deref
->type
== glsl_type::float_type
)
841 op
= nir_intrinsic_deref_atomic_fmin
;
843 unreachable("Invalid type");
845 case ir_intrinsic_generic_atomic_max
:
846 assert(ir
->return_deref
);
847 if (ir
->return_deref
->type
== glsl_type::int_type
)
848 op
= nir_intrinsic_deref_atomic_imax
;
849 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
850 op
= nir_intrinsic_deref_atomic_umax
;
851 else if (ir
->return_deref
->type
== glsl_type::float_type
)
852 op
= nir_intrinsic_deref_atomic_fmax
;
854 unreachable("Invalid type");
856 case ir_intrinsic_generic_atomic_exchange
:
857 op
= nir_intrinsic_deref_atomic_exchange
;
859 case ir_intrinsic_generic_atomic_comp_swap
:
860 op
= ir
->return_deref
->type
->is_integer_32_64()
861 ? nir_intrinsic_deref_atomic_comp_swap
862 : nir_intrinsic_deref_atomic_fcomp_swap
;
864 case ir_intrinsic_atomic_counter_read
:
865 op
= nir_intrinsic_atomic_counter_read_deref
;
867 case ir_intrinsic_atomic_counter_increment
:
868 op
= nir_intrinsic_atomic_counter_inc_deref
;
870 case ir_intrinsic_atomic_counter_predecrement
:
871 op
= nir_intrinsic_atomic_counter_pre_dec_deref
;
873 case ir_intrinsic_atomic_counter_add
:
874 op
= nir_intrinsic_atomic_counter_add_deref
;
876 case ir_intrinsic_atomic_counter_and
:
877 op
= nir_intrinsic_atomic_counter_and_deref
;
879 case ir_intrinsic_atomic_counter_or
:
880 op
= nir_intrinsic_atomic_counter_or_deref
;
882 case ir_intrinsic_atomic_counter_xor
:
883 op
= nir_intrinsic_atomic_counter_xor_deref
;
885 case ir_intrinsic_atomic_counter_min
:
886 op
= nir_intrinsic_atomic_counter_min_deref
;
888 case ir_intrinsic_atomic_counter_max
:
889 op
= nir_intrinsic_atomic_counter_max_deref
;
891 case ir_intrinsic_atomic_counter_exchange
:
892 op
= nir_intrinsic_atomic_counter_exchange_deref
;
894 case ir_intrinsic_atomic_counter_comp_swap
:
895 op
= nir_intrinsic_atomic_counter_comp_swap_deref
;
897 case ir_intrinsic_image_load
:
898 op
= nir_intrinsic_image_deref_load
;
900 case ir_intrinsic_image_store
:
901 op
= nir_intrinsic_image_deref_store
;
903 case ir_intrinsic_image_atomic_add
:
904 op
= ir
->return_deref
->type
->is_integer_32_64()
905 ? nir_intrinsic_image_deref_atomic_add
906 : nir_intrinsic_image_deref_atomic_fadd
;
908 case ir_intrinsic_image_atomic_min
:
909 op
= nir_intrinsic_image_deref_atomic_min
;
911 case ir_intrinsic_image_atomic_max
:
912 op
= nir_intrinsic_image_deref_atomic_max
;
914 case ir_intrinsic_image_atomic_and
:
915 op
= nir_intrinsic_image_deref_atomic_and
;
917 case ir_intrinsic_image_atomic_or
:
918 op
= nir_intrinsic_image_deref_atomic_or
;
920 case ir_intrinsic_image_atomic_xor
:
921 op
= nir_intrinsic_image_deref_atomic_xor
;
923 case ir_intrinsic_image_atomic_exchange
:
924 op
= nir_intrinsic_image_deref_atomic_exchange
;
926 case ir_intrinsic_image_atomic_comp_swap
:
927 op
= nir_intrinsic_image_deref_atomic_comp_swap
;
929 case ir_intrinsic_memory_barrier
:
930 op
= nir_intrinsic_memory_barrier
;
932 case ir_intrinsic_image_size
:
933 op
= nir_intrinsic_image_deref_size
;
935 case ir_intrinsic_image_samples
:
936 op
= nir_intrinsic_image_deref_samples
;
938 case ir_intrinsic_ssbo_store
:
939 op
= nir_intrinsic_store_ssbo
;
941 case ir_intrinsic_ssbo_load
:
942 op
= nir_intrinsic_load_ssbo
;
944 case ir_intrinsic_ssbo_atomic_add
:
945 op
= ir
->return_deref
->type
->is_integer_32_64()
946 ? nir_intrinsic_ssbo_atomic_add
: nir_intrinsic_ssbo_atomic_fadd
;
948 case ir_intrinsic_ssbo_atomic_and
:
949 op
= nir_intrinsic_ssbo_atomic_and
;
951 case ir_intrinsic_ssbo_atomic_or
:
952 op
= nir_intrinsic_ssbo_atomic_or
;
954 case ir_intrinsic_ssbo_atomic_xor
:
955 op
= nir_intrinsic_ssbo_atomic_xor
;
957 case ir_intrinsic_ssbo_atomic_min
:
958 assert(ir
->return_deref
);
959 if (ir
->return_deref
->type
== glsl_type::int_type
)
960 op
= nir_intrinsic_ssbo_atomic_imin
;
961 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
962 op
= nir_intrinsic_ssbo_atomic_umin
;
963 else if (ir
->return_deref
->type
== glsl_type::float_type
)
964 op
= nir_intrinsic_ssbo_atomic_fmin
;
966 unreachable("Invalid type");
968 case ir_intrinsic_ssbo_atomic_max
:
969 assert(ir
->return_deref
);
970 if (ir
->return_deref
->type
== glsl_type::int_type
)
971 op
= nir_intrinsic_ssbo_atomic_imax
;
972 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
973 op
= nir_intrinsic_ssbo_atomic_umax
;
974 else if (ir
->return_deref
->type
== glsl_type::float_type
)
975 op
= nir_intrinsic_ssbo_atomic_fmax
;
977 unreachable("Invalid type");
979 case ir_intrinsic_ssbo_atomic_exchange
:
980 op
= nir_intrinsic_ssbo_atomic_exchange
;
982 case ir_intrinsic_ssbo_atomic_comp_swap
:
983 op
= ir
->return_deref
->type
->is_integer_32_64()
984 ? nir_intrinsic_ssbo_atomic_comp_swap
985 : nir_intrinsic_ssbo_atomic_fcomp_swap
;
987 case ir_intrinsic_shader_clock
:
988 op
= nir_intrinsic_shader_clock
;
990 case ir_intrinsic_begin_invocation_interlock
:
991 op
= nir_intrinsic_begin_invocation_interlock
;
993 case ir_intrinsic_end_invocation_interlock
:
994 op
= nir_intrinsic_end_invocation_interlock
;
996 case ir_intrinsic_group_memory_barrier
:
997 op
= nir_intrinsic_group_memory_barrier
;
999 case ir_intrinsic_memory_barrier_atomic_counter
:
1000 op
= nir_intrinsic_memory_barrier_atomic_counter
;
1002 case ir_intrinsic_memory_barrier_buffer
:
1003 op
= nir_intrinsic_memory_barrier_buffer
;
1005 case ir_intrinsic_memory_barrier_image
:
1006 op
= nir_intrinsic_memory_barrier_image
;
1008 case ir_intrinsic_memory_barrier_shared
:
1009 op
= nir_intrinsic_memory_barrier_shared
;
1011 case ir_intrinsic_shared_load
:
1012 op
= nir_intrinsic_load_shared
;
1014 case ir_intrinsic_shared_store
:
1015 op
= nir_intrinsic_store_shared
;
1017 case ir_intrinsic_shared_atomic_add
:
1018 op
= ir
->return_deref
->type
->is_integer_32_64()
1019 ? nir_intrinsic_shared_atomic_add
1020 : nir_intrinsic_shared_atomic_fadd
;
1022 case ir_intrinsic_shared_atomic_and
:
1023 op
= nir_intrinsic_shared_atomic_and
;
1025 case ir_intrinsic_shared_atomic_or
:
1026 op
= nir_intrinsic_shared_atomic_or
;
1028 case ir_intrinsic_shared_atomic_xor
:
1029 op
= nir_intrinsic_shared_atomic_xor
;
1031 case ir_intrinsic_shared_atomic_min
:
1032 assert(ir
->return_deref
);
1033 if (ir
->return_deref
->type
== glsl_type::int_type
)
1034 op
= nir_intrinsic_shared_atomic_imin
;
1035 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
1036 op
= nir_intrinsic_shared_atomic_umin
;
1037 else if (ir
->return_deref
->type
== glsl_type::float_type
)
1038 op
= nir_intrinsic_shared_atomic_fmin
;
1040 unreachable("Invalid type");
1042 case ir_intrinsic_shared_atomic_max
:
1043 assert(ir
->return_deref
);
1044 if (ir
->return_deref
->type
== glsl_type::int_type
)
1045 op
= nir_intrinsic_shared_atomic_imax
;
1046 else if (ir
->return_deref
->type
== glsl_type::uint_type
)
1047 op
= nir_intrinsic_shared_atomic_umax
;
1048 else if (ir
->return_deref
->type
== glsl_type::float_type
)
1049 op
= nir_intrinsic_shared_atomic_fmax
;
1051 unreachable("Invalid type");
1053 case ir_intrinsic_shared_atomic_exchange
:
1054 op
= nir_intrinsic_shared_atomic_exchange
;
1056 case ir_intrinsic_shared_atomic_comp_swap
:
1057 op
= ir
->return_deref
->type
->is_integer_32_64()
1058 ? nir_intrinsic_shared_atomic_comp_swap
1059 : nir_intrinsic_shared_atomic_fcomp_swap
;
1061 case ir_intrinsic_vote_any
:
1062 op
= nir_intrinsic_vote_any
;
1064 case ir_intrinsic_vote_all
:
1065 op
= nir_intrinsic_vote_all
;
1067 case ir_intrinsic_vote_eq
:
1068 op
= nir_intrinsic_vote_ieq
;
1070 case ir_intrinsic_ballot
:
1071 op
= nir_intrinsic_ballot
;
1073 case ir_intrinsic_read_invocation
:
1074 op
= nir_intrinsic_read_invocation
;
1076 case ir_intrinsic_read_first_invocation
:
1077 op
= nir_intrinsic_read_first_invocation
;
1080 unreachable("not reached");
1083 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(shader
, op
);
1084 nir_ssa_def
*ret
= &instr
->dest
.ssa
;
1087 case nir_intrinsic_deref_atomic_add
:
1088 case nir_intrinsic_deref_atomic_imin
:
1089 case nir_intrinsic_deref_atomic_umin
:
1090 case nir_intrinsic_deref_atomic_imax
:
1091 case nir_intrinsic_deref_atomic_umax
:
1092 case nir_intrinsic_deref_atomic_and
:
1093 case nir_intrinsic_deref_atomic_or
:
1094 case nir_intrinsic_deref_atomic_xor
:
1095 case nir_intrinsic_deref_atomic_exchange
:
1096 case nir_intrinsic_deref_atomic_comp_swap
:
1097 case nir_intrinsic_deref_atomic_fadd
:
1098 case nir_intrinsic_deref_atomic_fmin
:
1099 case nir_intrinsic_deref_atomic_fmax
:
1100 case nir_intrinsic_deref_atomic_fcomp_swap
: {
1101 int param_count
= ir
->actual_parameters
.length();
1102 assert(param_count
== 2 || param_count
== 3);
1105 exec_node
*param
= ir
->actual_parameters
.get_head();
1106 ir_rvalue
*rvalue
= (ir_rvalue
*) param
;
1107 ir_dereference
*deref
= rvalue
->as_dereference();
1108 ir_swizzle
*swizzle
= NULL
;
1110 /* We may have a swizzle to pick off a single vec4 component */
1111 swizzle
= rvalue
->as_swizzle();
1112 assert(swizzle
&& swizzle
->type
->vector_elements
== 1);
1113 deref
= swizzle
->val
->as_dereference();
1116 nir_deref_instr
*nir_deref
= evaluate_deref(deref
);
1118 nir_deref
= nir_build_deref_array_imm(&b
, nir_deref
,
1121 instr
->src
[0] = nir_src_for_ssa(&nir_deref
->dest
.ssa
);
1123 /* data1 parameter (this is always present) */
1124 param
= param
->get_next();
1125 ir_instruction
*inst
= (ir_instruction
*) param
;
1126 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1128 /* data2 parameter (only with atomic_comp_swap) */
1129 if (param_count
== 3) {
1130 assert(op
== nir_intrinsic_deref_atomic_comp_swap
||
1131 op
== nir_intrinsic_deref_atomic_fcomp_swap
);
1132 param
= param
->get_next();
1133 inst
= (ir_instruction
*) param
;
1134 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1138 assert(ir
->return_deref
);
1139 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1140 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1141 nir_builder_instr_insert(&b
, &instr
->instr
);
1144 case nir_intrinsic_atomic_counter_read_deref
:
1145 case nir_intrinsic_atomic_counter_inc_deref
:
1146 case nir_intrinsic_atomic_counter_pre_dec_deref
:
1147 case nir_intrinsic_atomic_counter_add_deref
:
1148 case nir_intrinsic_atomic_counter_min_deref
:
1149 case nir_intrinsic_atomic_counter_max_deref
:
1150 case nir_intrinsic_atomic_counter_and_deref
:
1151 case nir_intrinsic_atomic_counter_or_deref
:
1152 case nir_intrinsic_atomic_counter_xor_deref
:
1153 case nir_intrinsic_atomic_counter_exchange_deref
:
1154 case nir_intrinsic_atomic_counter_comp_swap_deref
: {
1155 /* Set the counter variable dereference. */
1156 exec_node
*param
= ir
->actual_parameters
.get_head();
1157 ir_dereference
*counter
= (ir_dereference
*)param
;
1159 instr
->src
[0] = nir_src_for_ssa(&evaluate_deref(counter
)->dest
.ssa
);
1160 param
= param
->get_next();
1162 /* Set the intrinsic destination. */
1163 if (ir
->return_deref
) {
1164 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1167 /* Set the intrinsic parameters. */
1168 if (!param
->is_tail_sentinel()) {
1170 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1171 param
= param
->get_next();
1174 if (!param
->is_tail_sentinel()) {
1176 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1177 param
= param
->get_next();
1180 nir_builder_instr_insert(&b
, &instr
->instr
);
1183 case nir_intrinsic_image_deref_load
:
1184 case nir_intrinsic_image_deref_store
:
1185 case nir_intrinsic_image_deref_atomic_add
:
1186 case nir_intrinsic_image_deref_atomic_min
:
1187 case nir_intrinsic_image_deref_atomic_max
:
1188 case nir_intrinsic_image_deref_atomic_and
:
1189 case nir_intrinsic_image_deref_atomic_or
:
1190 case nir_intrinsic_image_deref_atomic_xor
:
1191 case nir_intrinsic_image_deref_atomic_exchange
:
1192 case nir_intrinsic_image_deref_atomic_comp_swap
:
1193 case nir_intrinsic_image_deref_atomic_fadd
:
1194 case nir_intrinsic_image_deref_samples
:
1195 case nir_intrinsic_image_deref_size
: {
1196 nir_ssa_undef_instr
*instr_undef
=
1197 nir_ssa_undef_instr_create(shader
, 1, 32);
1198 nir_builder_instr_insert(&b
, &instr_undef
->instr
);
1200 /* Set the image variable dereference. */
1201 exec_node
*param
= ir
->actual_parameters
.get_head();
1202 ir_dereference
*image
= (ir_dereference
*)param
;
1203 nir_deref_instr
*deref
= evaluate_deref(image
);
1204 const glsl_type
*type
= deref
->type
;
1206 instr
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
1207 param
= param
->get_next();
1209 /* Set the intrinsic destination. */
1210 if (ir
->return_deref
) {
1211 unsigned num_components
= ir
->return_deref
->type
->vector_elements
;
1212 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1213 num_components
, 32, NULL
);
1216 if (op
== nir_intrinsic_image_deref_size
) {
1217 instr
->num_components
= instr
->dest
.ssa
.num_components
;
1218 } else if (op
== nir_intrinsic_image_deref_load
||
1219 op
== nir_intrinsic_image_deref_store
) {
1220 instr
->num_components
= 4;
1223 if (op
== nir_intrinsic_image_deref_size
||
1224 op
== nir_intrinsic_image_deref_samples
) {
1225 nir_builder_instr_insert(&b
, &instr
->instr
);
1229 /* Set the address argument, extending the coordinate vector to four
1232 nir_ssa_def
*src_addr
=
1233 evaluate_rvalue((ir_dereference
*)param
);
1234 nir_ssa_def
*srcs
[4];
1236 for (int i
= 0; i
< 4; i
++) {
1237 if (i
< type
->coordinate_components())
1238 srcs
[i
] = nir_channel(&b
, src_addr
, i
);
1240 srcs
[i
] = &instr_undef
->def
;
1243 instr
->src
[1] = nir_src_for_ssa(nir_vec(&b
, srcs
, 4));
1244 param
= param
->get_next();
1246 /* Set the sample argument, which is undefined for single-sample
1249 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
1251 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1252 param
= param
->get_next();
1254 instr
->src
[2] = nir_src_for_ssa(&instr_undef
->def
);
1257 /* Set the intrinsic parameters. */
1258 if (!param
->is_tail_sentinel()) {
1260 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1261 param
= param
->get_next();
1264 if (!param
->is_tail_sentinel()) {
1266 nir_src_for_ssa(evaluate_rvalue((ir_dereference
*)param
));
1267 param
= param
->get_next();
1269 nir_builder_instr_insert(&b
, &instr
->instr
);
1272 case nir_intrinsic_memory_barrier
:
1273 case nir_intrinsic_group_memory_barrier
:
1274 case nir_intrinsic_memory_barrier_atomic_counter
:
1275 case nir_intrinsic_memory_barrier_buffer
:
1276 case nir_intrinsic_memory_barrier_image
:
1277 case nir_intrinsic_memory_barrier_shared
:
1278 nir_builder_instr_insert(&b
, &instr
->instr
);
1280 case nir_intrinsic_shader_clock
:
1281 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 2, 32, NULL
);
1282 instr
->num_components
= 2;
1283 nir_builder_instr_insert(&b
, &instr
->instr
);
1285 case nir_intrinsic_begin_invocation_interlock
:
1286 nir_builder_instr_insert(&b
, &instr
->instr
);
1288 case nir_intrinsic_end_invocation_interlock
:
1289 nir_builder_instr_insert(&b
, &instr
->instr
);
1291 case nir_intrinsic_store_ssbo
: {
1292 exec_node
*param
= ir
->actual_parameters
.get_head();
1293 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
1295 param
= param
->get_next();
1296 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1298 param
= param
->get_next();
1299 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1301 param
= param
->get_next();
1302 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1305 nir_ssa_def
*nir_val
= evaluate_rvalue(val
);
1306 if (val
->type
->is_boolean())
1307 nir_val
= nir_b2i32(&b
, nir_val
);
1309 instr
->src
[0] = nir_src_for_ssa(nir_val
);
1310 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(block
));
1311 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(offset
));
1312 intrinsic_set_std430_align(instr
, val
->type
);
1313 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1314 instr
->num_components
= val
->type
->vector_elements
;
1316 nir_builder_instr_insert(&b
, &instr
->instr
);
1319 case nir_intrinsic_load_ssbo
: {
1320 exec_node
*param
= ir
->actual_parameters
.get_head();
1321 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
1323 param
= param
->get_next();
1324 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1326 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(block
));
1327 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1329 const glsl_type
*type
= ir
->return_deref
->var
->type
;
1330 instr
->num_components
= type
->vector_elements
;
1331 intrinsic_set_std430_align(instr
, type
);
1333 /* Setup destination register */
1334 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
1335 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1336 type
->vector_elements
, bit_size
, NULL
);
1338 /* Insert the created nir instruction now since in the case of boolean
1339 * result we will need to emit another instruction after it
1341 nir_builder_instr_insert(&b
, &instr
->instr
);
1344 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
1345 * consider a true boolean to be ~0. Fix this up with a != 0
1348 if (type
->is_boolean())
1349 ret
= nir_i2b(&b
, &instr
->dest
.ssa
);
1352 case nir_intrinsic_ssbo_atomic_add
:
1353 case nir_intrinsic_ssbo_atomic_imin
:
1354 case nir_intrinsic_ssbo_atomic_umin
:
1355 case nir_intrinsic_ssbo_atomic_imax
:
1356 case nir_intrinsic_ssbo_atomic_umax
:
1357 case nir_intrinsic_ssbo_atomic_and
:
1358 case nir_intrinsic_ssbo_atomic_or
:
1359 case nir_intrinsic_ssbo_atomic_xor
:
1360 case nir_intrinsic_ssbo_atomic_exchange
:
1361 case nir_intrinsic_ssbo_atomic_comp_swap
:
1362 case nir_intrinsic_ssbo_atomic_fadd
:
1363 case nir_intrinsic_ssbo_atomic_fmin
:
1364 case nir_intrinsic_ssbo_atomic_fmax
:
1365 case nir_intrinsic_ssbo_atomic_fcomp_swap
: {
1366 int param_count
= ir
->actual_parameters
.length();
1367 assert(param_count
== 3 || param_count
== 4);
1370 exec_node
*param
= ir
->actual_parameters
.get_head();
1371 ir_instruction
*inst
= (ir_instruction
*) param
;
1372 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1375 param
= param
->get_next();
1376 inst
= (ir_instruction
*) param
;
1377 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1379 /* data1 parameter (this is always present) */
1380 param
= param
->get_next();
1381 inst
= (ir_instruction
*) param
;
1382 instr
->src
[2] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1384 /* data2 parameter (only with atomic_comp_swap) */
1385 if (param_count
== 4) {
1386 assert(op
== nir_intrinsic_ssbo_atomic_comp_swap
||
1387 op
== nir_intrinsic_ssbo_atomic_fcomp_swap
);
1388 param
= param
->get_next();
1389 inst
= (ir_instruction
*) param
;
1390 instr
->src
[3] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1394 assert(ir
->return_deref
);
1395 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1396 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1397 nir_builder_instr_insert(&b
, &instr
->instr
);
1400 case nir_intrinsic_load_shared
: {
1401 exec_node
*param
= ir
->actual_parameters
.get_head();
1402 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1404 nir_intrinsic_set_base(instr
, 0);
1405 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(offset
));
1407 const glsl_type
*type
= ir
->return_deref
->var
->type
;
1408 instr
->num_components
= type
->vector_elements
;
1409 intrinsic_set_std430_align(instr
, type
);
1411 /* Setup destination register */
1412 unsigned bit_size
= type
->is_boolean() ? 32 : glsl_get_bit_size(type
);
1413 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1414 type
->vector_elements
, bit_size
, NULL
);
1416 nir_builder_instr_insert(&b
, &instr
->instr
);
1418 /* The value in shared memory is a 32-bit value */
1419 if (type
->is_boolean())
1420 ret
= nir_i2b(&b
, &instr
->dest
.ssa
);
1423 case nir_intrinsic_store_shared
: {
1424 exec_node
*param
= ir
->actual_parameters
.get_head();
1425 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
1427 param
= param
->get_next();
1428 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
1430 param
= param
->get_next();
1431 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
1434 nir_intrinsic_set_base(instr
, 0);
1435 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(offset
));
1437 nir_intrinsic_set_write_mask(instr
, write_mask
->value
.u
[0]);
1439 nir_ssa_def
*nir_val
= evaluate_rvalue(val
);
1440 /* The value in shared memory is a 32-bit value */
1441 if (val
->type
->is_boolean())
1442 nir_val
= nir_b2i32(&b
, nir_val
);
1444 instr
->src
[0] = nir_src_for_ssa(nir_val
);
1445 instr
->num_components
= val
->type
->vector_elements
;
1446 intrinsic_set_std430_align(instr
, val
->type
);
1448 nir_builder_instr_insert(&b
, &instr
->instr
);
1451 case nir_intrinsic_shared_atomic_add
:
1452 case nir_intrinsic_shared_atomic_imin
:
1453 case nir_intrinsic_shared_atomic_umin
:
1454 case nir_intrinsic_shared_atomic_imax
:
1455 case nir_intrinsic_shared_atomic_umax
:
1456 case nir_intrinsic_shared_atomic_and
:
1457 case nir_intrinsic_shared_atomic_or
:
1458 case nir_intrinsic_shared_atomic_xor
:
1459 case nir_intrinsic_shared_atomic_exchange
:
1460 case nir_intrinsic_shared_atomic_comp_swap
:
1461 case nir_intrinsic_shared_atomic_fadd
:
1462 case nir_intrinsic_shared_atomic_fmin
:
1463 case nir_intrinsic_shared_atomic_fmax
:
1464 case nir_intrinsic_shared_atomic_fcomp_swap
: {
1465 int param_count
= ir
->actual_parameters
.length();
1466 assert(param_count
== 2 || param_count
== 3);
1469 exec_node
*param
= ir
->actual_parameters
.get_head();
1470 ir_instruction
*inst
= (ir_instruction
*) param
;
1471 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1473 /* data1 parameter (this is always present) */
1474 param
= param
->get_next();
1475 inst
= (ir_instruction
*) param
;
1476 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1478 /* data2 parameter (only with atomic_comp_swap) */
1479 if (param_count
== 3) {
1480 assert(op
== nir_intrinsic_shared_atomic_comp_swap
||
1481 op
== nir_intrinsic_shared_atomic_fcomp_swap
);
1482 param
= param
->get_next();
1483 inst
= (ir_instruction
*) param
;
1485 nir_src_for_ssa(evaluate_rvalue(inst
->as_rvalue()));
1489 assert(ir
->return_deref
);
1490 unsigned bit_size
= glsl_get_bit_size(ir
->return_deref
->type
);
1491 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1492 ir
->return_deref
->type
->vector_elements
,
1494 nir_builder_instr_insert(&b
, &instr
->instr
);
1497 case nir_intrinsic_vote_any
:
1498 case nir_intrinsic_vote_all
:
1499 case nir_intrinsic_vote_ieq
: {
1500 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 1, NULL
);
1501 instr
->num_components
= 1;
1503 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1504 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1506 nir_builder_instr_insert(&b
, &instr
->instr
);
1510 case nir_intrinsic_ballot
: {
1511 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1512 ir
->return_deref
->type
->vector_elements
, 64, NULL
);
1513 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1515 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1516 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1518 nir_builder_instr_insert(&b
, &instr
->instr
);
1521 case nir_intrinsic_read_invocation
: {
1522 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1523 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1524 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1526 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1527 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1529 ir_rvalue
*invocation
= (ir_rvalue
*) ir
->actual_parameters
.get_head()->next
;
1530 instr
->src
[1] = nir_src_for_ssa(evaluate_rvalue(invocation
));
1532 nir_builder_instr_insert(&b
, &instr
->instr
);
1535 case nir_intrinsic_read_first_invocation
: {
1536 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1537 ir
->return_deref
->type
->vector_elements
, 32, NULL
);
1538 instr
->num_components
= ir
->return_deref
->type
->vector_elements
;
1540 ir_rvalue
*value
= (ir_rvalue
*) ir
->actual_parameters
.get_head();
1541 instr
->src
[0] = nir_src_for_ssa(evaluate_rvalue(value
));
1543 nir_builder_instr_insert(&b
, &instr
->instr
);
1547 unreachable("not reached");
1550 if (ir
->return_deref
)
1551 nir_store_deref(&b
, evaluate_deref(ir
->return_deref
), ret
, ~0);
1556 struct hash_entry
*entry
=
1557 _mesa_hash_table_search(this->overload_table
, ir
->callee
);
1559 nir_function
*callee
= (nir_function
*) entry
->data
;
1561 nir_call_instr
*call
= nir_call_instr_create(this->shader
, callee
);
1564 nir_deref_instr
*ret_deref
= NULL
;
1565 if (ir
->return_deref
) {
1566 nir_variable
*ret_tmp
=
1567 nir_local_variable_create(this->impl
, ir
->return_deref
->type
,
1569 ret_deref
= nir_build_deref_var(&b
, ret_tmp
);
1570 call
->params
[i
++] = nir_src_for_ssa(&ret_deref
->dest
.ssa
);
1573 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
1574 actual_node
, &ir
->actual_parameters
) {
1575 ir_rvalue
*param_rvalue
= (ir_rvalue
*) actual_node
;
1576 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
1578 if (sig_param
->data
.mode
== ir_var_function_out
) {
1579 nir_deref_instr
*out_deref
= evaluate_deref(param_rvalue
);
1580 call
->params
[i
] = nir_src_for_ssa(&out_deref
->dest
.ssa
);
1581 } else if (sig_param
->data
.mode
== ir_var_function_in
) {
1582 nir_ssa_def
*val
= evaluate_rvalue(param_rvalue
);
1583 nir_src src
= nir_src_for_ssa(val
);
1585 nir_src_copy(&call
->params
[i
], &src
, call
);
1586 } else if (sig_param
->data
.mode
== ir_var_function_inout
) {
1587 unreachable("unimplemented: inout parameters");
1593 nir_builder_instr_insert(&b
, &call
->instr
);
1595 if (ir
->return_deref
)
1596 nir_store_deref(&b
, evaluate_deref(ir
->return_deref
), nir_load_deref(&b
, ret_deref
), ~0);
1600 nir_visitor::visit(ir_assignment
*ir
)
1602 unsigned num_components
= ir
->lhs
->type
->vector_elements
;
1604 b
.exact
= ir
->lhs
->variable_referenced()->data
.invariant
||
1605 ir
->lhs
->variable_referenced()->data
.precise
;
1607 if ((ir
->rhs
->as_dereference() || ir
->rhs
->as_constant()) &&
1608 (ir
->write_mask
== (1 << num_components
) - 1 || ir
->write_mask
== 0)) {
1609 if (ir
->condition
) {
1610 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
1611 nir_copy_deref(&b
, evaluate_deref(ir
->lhs
), evaluate_deref(ir
->rhs
));
1612 nir_pop_if(&b
, NULL
);
1614 nir_copy_deref(&b
, evaluate_deref(ir
->lhs
), evaluate_deref(ir
->rhs
));
1619 assert(ir
->rhs
->type
->is_scalar() || ir
->rhs
->type
->is_vector());
1621 ir
->lhs
->accept(this);
1622 nir_deref_instr
*lhs_deref
= this->deref
;
1623 nir_ssa_def
*src
= evaluate_rvalue(ir
->rhs
);
1625 if (ir
->write_mask
!= (1 << num_components
) - 1 && ir
->write_mask
!= 0) {
1626 /* GLSL IR will give us the input to the write-masked assignment in a
1627 * single packed vector. So, for example, if the writemask is xzw, then
1628 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1632 unsigned component
= 0;
1633 for (unsigned i
= 0; i
< 4; i
++) {
1634 swiz
[i
] = ir
->write_mask
& (1 << i
) ? component
++ : 0;
1636 src
= nir_swizzle(&b
, src
, swiz
, num_components
, false);
1639 if (ir
->condition
) {
1640 nir_push_if(&b
, evaluate_rvalue(ir
->condition
));
1641 nir_store_deref(&b
, lhs_deref
, src
, ir
->write_mask
);
1642 nir_pop_if(&b
, NULL
);
1644 nir_store_deref(&b
, lhs_deref
, src
, ir
->write_mask
);
1649 * Given an instruction, returns a pointer to its destination or NULL if there
1650 * is no destination.
1652 * Note that this only handles instructions we generate at this level.
1655 get_instr_dest(nir_instr
*instr
)
1657 nir_alu_instr
*alu_instr
;
1658 nir_intrinsic_instr
*intrinsic_instr
;
1659 nir_tex_instr
*tex_instr
;
1661 switch (instr
->type
) {
1662 case nir_instr_type_alu
:
1663 alu_instr
= nir_instr_as_alu(instr
);
1664 return &alu_instr
->dest
.dest
;
1666 case nir_instr_type_intrinsic
:
1667 intrinsic_instr
= nir_instr_as_intrinsic(instr
);
1668 if (nir_intrinsic_infos
[intrinsic_instr
->intrinsic
].has_dest
)
1669 return &intrinsic_instr
->dest
;
1673 case nir_instr_type_tex
:
1674 tex_instr
= nir_instr_as_tex(instr
);
1675 return &tex_instr
->dest
;
1678 unreachable("not reached");
1685 nir_visitor::add_instr(nir_instr
*instr
, unsigned num_components
,
1688 nir_dest
*dest
= get_instr_dest(instr
);
1691 nir_ssa_dest_init(instr
, dest
, num_components
, bit_size
, NULL
);
1693 nir_builder_instr_insert(&b
, instr
);
1696 assert(dest
->is_ssa
);
1697 this->result
= &dest
->ssa
;
1702 nir_visitor::evaluate_rvalue(ir_rvalue
* ir
)
1705 if (ir
->as_dereference() || ir
->as_constant()) {
1707 * A dereference is being used on the right hand side, which means we
1708 * must emit a variable load.
1711 this->result
= nir_load_deref(&b
, this->deref
);
1714 return this->result
;
1718 type_is_float(glsl_base_type type
)
1720 return type
== GLSL_TYPE_FLOAT
|| type
== GLSL_TYPE_DOUBLE
||
1721 type
== GLSL_TYPE_FLOAT16
;
1725 type_is_signed(glsl_base_type type
)
1727 return type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_INT64
||
1728 type
== GLSL_TYPE_INT16
;
1732 type_is_int(glsl_base_type type
)
1734 return type
== GLSL_TYPE_UINT
|| type
== GLSL_TYPE_INT
||
1735 type
== GLSL_TYPE_UINT8
|| type
== GLSL_TYPE_INT8
||
1736 type
== GLSL_TYPE_UINT16
|| type
== GLSL_TYPE_INT16
||
1737 type
== GLSL_TYPE_UINT64
|| type
== GLSL_TYPE_INT64
;
1741 nir_visitor::visit(ir_expression
*ir
)
1743 /* Some special cases */
1744 switch (ir
->operation
) {
1745 case ir_binop_ubo_load
: {
1746 nir_intrinsic_instr
*load
=
1747 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_load_ubo
);
1748 unsigned bit_size
= ir
->type
->is_boolean() ? 32 :
1749 glsl_get_bit_size(ir
->type
);
1750 load
->num_components
= ir
->type
->vector_elements
;
1751 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
1752 load
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1753 intrinsic_set_std430_align(load
, ir
->type
);
1754 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
1757 * In UBO's, a true boolean value is any non-zero value, but we consider
1758 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1761 if (ir
->type
->is_boolean())
1762 this->result
= nir_i2b(&b
, &load
->dest
.ssa
);
1767 case ir_unop_interpolate_at_centroid
:
1768 case ir_binop_interpolate_at_offset
:
1769 case ir_binop_interpolate_at_sample
: {
1770 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1771 ir_swizzle
*swizzle
= NULL
;
1773 /* the api does not allow a swizzle here, but the varying packing code
1774 * may have pushed one into here.
1776 swizzle
= ir
->operands
[0]->as_swizzle();
1778 deref
= swizzle
->val
->as_dereference();
1782 deref
->accept(this);
1784 nir_intrinsic_op op
;
1785 if (this->deref
->mode
== nir_var_shader_in
) {
1786 switch (ir
->operation
) {
1787 case ir_unop_interpolate_at_centroid
:
1788 op
= nir_intrinsic_interp_deref_at_centroid
;
1790 case ir_binop_interpolate_at_offset
:
1791 op
= nir_intrinsic_interp_deref_at_offset
;
1793 case ir_binop_interpolate_at_sample
:
1794 op
= nir_intrinsic_interp_deref_at_sample
;
1797 unreachable("Invalid interpolation intrinsic");
1800 /* This case can happen if the vertex shader does not write the
1801 * given varying. In this case, the linker will lower it to a
1802 * global variable. Since interpolating a variable makes no
1803 * sense, we'll just turn it into a load which will probably
1804 * eventually end up as an SSA definition.
1806 assert(this->deref
->mode
== nir_var_shader_temp
);
1807 op
= nir_intrinsic_load_deref
;
1810 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(shader
, op
);
1811 intrin
->num_components
= deref
->type
->vector_elements
;
1812 intrin
->src
[0] = nir_src_for_ssa(&this->deref
->dest
.ssa
);
1814 if (intrin
->intrinsic
== nir_intrinsic_interp_deref_at_offset
||
1815 intrin
->intrinsic
== nir_intrinsic_interp_deref_at_sample
)
1816 intrin
->src
[1] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[1]));
1818 unsigned bit_size
= glsl_get_bit_size(deref
->type
);
1819 add_instr(&intrin
->instr
, deref
->type
->vector_elements
, bit_size
);
1822 unsigned swiz
[4] = {
1823 swizzle
->mask
.x
, swizzle
->mask
.y
, swizzle
->mask
.z
, swizzle
->mask
.w
1826 result
= nir_swizzle(&b
, result
, swiz
,
1827 swizzle
->type
->vector_elements
, false);
1833 case ir_unop_ssbo_unsized_array_length
: {
1834 nir_intrinsic_instr
*intrin
=
1835 nir_intrinsic_instr_create(b
.shader
,
1836 nir_intrinsic_deref_buffer_array_length
);
1838 ir_dereference
*deref
= ir
->operands
[0]->as_dereference();
1839 intrin
->src
[0] = nir_src_for_ssa(&evaluate_deref(deref
)->dest
.ssa
);
1841 add_instr(&intrin
->instr
, 1, 32);
1849 nir_ssa_def
*srcs
[4];
1850 for (unsigned i
= 0; i
< ir
->num_operands
; i
++)
1851 srcs
[i
] = evaluate_rvalue(ir
->operands
[i
]);
1853 glsl_base_type types
[4];
1854 for (unsigned i
= 0; i
< ir
->num_operands
; i
++)
1855 types
[i
] = ir
->operands
[i
]->type
->base_type
;
1857 glsl_base_type out_type
= ir
->type
->base_type
;
1859 switch (ir
->operation
) {
1860 case ir_unop_bit_not
: result
= nir_inot(&b
, srcs
[0]); break;
1861 case ir_unop_logic_not
:
1862 result
= nir_inot(&b
, srcs
[0]);
1865 result
= type_is_float(types
[0]) ? nir_fneg(&b
, srcs
[0])
1866 : nir_ineg(&b
, srcs
[0]);
1869 result
= type_is_float(types
[0]) ? nir_fabs(&b
, srcs
[0])
1870 : nir_iabs(&b
, srcs
[0]);
1872 case ir_unop_saturate
:
1873 assert(type_is_float(types
[0]));
1874 result
= nir_fsat(&b
, srcs
[0]);
1877 result
= type_is_float(types
[0]) ? nir_fsign(&b
, srcs
[0])
1878 : nir_isign(&b
, srcs
[0]);
1880 case ir_unop_rcp
: result
= nir_frcp(&b
, srcs
[0]); break;
1881 case ir_unop_rsq
: result
= nir_frsq(&b
, srcs
[0]); break;
1882 case ir_unop_sqrt
: result
= nir_fsqrt(&b
, srcs
[0]); break;
1883 case ir_unop_exp
: unreachable("ir_unop_exp should have been lowered");
1884 case ir_unop_log
: unreachable("ir_unop_log should have been lowered");
1885 case ir_unop_exp2
: result
= nir_fexp2(&b
, srcs
[0]); break;
1886 case ir_unop_log2
: result
= nir_flog2(&b
, srcs
[0]); break;
1888 result
= nir_i2f32(&b
, srcs
[0]);
1891 result
= nir_u2f32(&b
, srcs
[0]);
1894 result
= nir_b2f32(&b
, srcs
[0]);
1897 result
= nir_f2i32(&b
, srcs
[0]);
1900 result
= nir_f2u32(&b
, srcs
[0]);
1932 case ir_unop_i642u64
:
1933 case ir_unop_u642i64
: {
1934 nir_alu_type src_type
= nir_get_nir_type_for_glsl_base_type(types
[0]);
1935 nir_alu_type dst_type
= nir_get_nir_type_for_glsl_base_type(out_type
);
1936 result
= nir_build_alu(&b
, nir_type_conversion_op(src_type
, dst_type
,
1937 nir_rounding_mode_undef
),
1938 srcs
[0], NULL
, NULL
, NULL
);
1939 /* b2i and b2f don't have fixed bit-size versions so the builder will
1940 * just assume 32 and we have to fix it up here.
1942 result
->bit_size
= nir_alu_type_get_type_size(dst_type
);
1946 case ir_unop_bitcast_i2f
:
1947 case ir_unop_bitcast_f2i
:
1948 case ir_unop_bitcast_u2f
:
1949 case ir_unop_bitcast_f2u
:
1950 case ir_unop_bitcast_i642d
:
1951 case ir_unop_bitcast_d2i64
:
1952 case ir_unop_bitcast_u642d
:
1953 case ir_unop_bitcast_d2u64
:
1954 case ir_unop_subroutine_to_int
:
1956 result
= nir_imov(&b
, srcs
[0]);
1958 case ir_unop_trunc
: result
= nir_ftrunc(&b
, srcs
[0]); break;
1959 case ir_unop_ceil
: result
= nir_fceil(&b
, srcs
[0]); break;
1960 case ir_unop_floor
: result
= nir_ffloor(&b
, srcs
[0]); break;
1961 case ir_unop_fract
: result
= nir_ffract(&b
, srcs
[0]); break;
1962 case ir_unop_frexp_exp
: result
= nir_frexp_exp(&b
, srcs
[0]); break;
1963 case ir_unop_frexp_sig
: result
= nir_frexp_sig(&b
, srcs
[0]); break;
1964 case ir_unop_round_even
: result
= nir_fround_even(&b
, srcs
[0]); break;
1965 case ir_unop_sin
: result
= nir_fsin(&b
, srcs
[0]); break;
1966 case ir_unop_cos
: result
= nir_fcos(&b
, srcs
[0]); break;
1967 case ir_unop_dFdx
: result
= nir_fddx(&b
, srcs
[0]); break;
1968 case ir_unop_dFdy
: result
= nir_fddy(&b
, srcs
[0]); break;
1969 case ir_unop_dFdx_fine
: result
= nir_fddx_fine(&b
, srcs
[0]); break;
1970 case ir_unop_dFdy_fine
: result
= nir_fddy_fine(&b
, srcs
[0]); break;
1971 case ir_unop_dFdx_coarse
: result
= nir_fddx_coarse(&b
, srcs
[0]); break;
1972 case ir_unop_dFdy_coarse
: result
= nir_fddy_coarse(&b
, srcs
[0]); break;
1973 case ir_unop_pack_snorm_2x16
:
1974 result
= nir_pack_snorm_2x16(&b
, srcs
[0]);
1976 case ir_unop_pack_snorm_4x8
:
1977 result
= nir_pack_snorm_4x8(&b
, srcs
[0]);
1979 case ir_unop_pack_unorm_2x16
:
1980 result
= nir_pack_unorm_2x16(&b
, srcs
[0]);
1982 case ir_unop_pack_unorm_4x8
:
1983 result
= nir_pack_unorm_4x8(&b
, srcs
[0]);
1985 case ir_unop_pack_half_2x16
:
1986 result
= nir_pack_half_2x16(&b
, srcs
[0]);
1988 case ir_unop_unpack_snorm_2x16
:
1989 result
= nir_unpack_snorm_2x16(&b
, srcs
[0]);
1991 case ir_unop_unpack_snorm_4x8
:
1992 result
= nir_unpack_snorm_4x8(&b
, srcs
[0]);
1994 case ir_unop_unpack_unorm_2x16
:
1995 result
= nir_unpack_unorm_2x16(&b
, srcs
[0]);
1997 case ir_unop_unpack_unorm_4x8
:
1998 result
= nir_unpack_unorm_4x8(&b
, srcs
[0]);
2000 case ir_unop_unpack_half_2x16
:
2001 result
= nir_unpack_half_2x16(&b
, srcs
[0]);
2003 case ir_unop_pack_sampler_2x32
:
2004 case ir_unop_pack_image_2x32
:
2005 case ir_unop_pack_double_2x32
:
2006 case ir_unop_pack_int_2x32
:
2007 case ir_unop_pack_uint_2x32
:
2008 result
= nir_pack_64_2x32(&b
, srcs
[0]);
2010 case ir_unop_unpack_sampler_2x32
:
2011 case ir_unop_unpack_image_2x32
:
2012 case ir_unop_unpack_double_2x32
:
2013 case ir_unop_unpack_int_2x32
:
2014 case ir_unop_unpack_uint_2x32
:
2015 result
= nir_unpack_64_2x32(&b
, srcs
[0]);
2017 case ir_unop_bitfield_reverse
:
2018 result
= nir_bitfield_reverse(&b
, srcs
[0]);
2020 case ir_unop_bit_count
:
2021 result
= nir_bit_count(&b
, srcs
[0]);
2023 case ir_unop_find_msb
:
2025 case GLSL_TYPE_UINT
:
2026 result
= nir_ufind_msb(&b
, srcs
[0]);
2029 result
= nir_ifind_msb(&b
, srcs
[0]);
2032 unreachable("Invalid type for findMSB()");
2035 case ir_unop_find_lsb
:
2036 result
= nir_find_lsb(&b
, srcs
[0]);
2040 switch (ir
->type
->vector_elements
) {
2042 switch (ir
->operands
[0]->type
->vector_elements
) {
2043 case 1: result
= nir_fnoise1_1(&b
, srcs
[0]); break;
2044 case 2: result
= nir_fnoise1_2(&b
, srcs
[0]); break;
2045 case 3: result
= nir_fnoise1_3(&b
, srcs
[0]); break;
2046 case 4: result
= nir_fnoise1_4(&b
, srcs
[0]); break;
2047 default: unreachable("not reached");
2051 switch (ir
->operands
[0]->type
->vector_elements
) {
2052 case 1: result
= nir_fnoise2_1(&b
, srcs
[0]); break;
2053 case 2: result
= nir_fnoise2_2(&b
, srcs
[0]); break;
2054 case 3: result
= nir_fnoise2_3(&b
, srcs
[0]); break;
2055 case 4: result
= nir_fnoise2_4(&b
, srcs
[0]); break;
2056 default: unreachable("not reached");
2060 switch (ir
->operands
[0]->type
->vector_elements
) {
2061 case 1: result
= nir_fnoise3_1(&b
, srcs
[0]); break;
2062 case 2: result
= nir_fnoise3_2(&b
, srcs
[0]); break;
2063 case 3: result
= nir_fnoise3_3(&b
, srcs
[0]); break;
2064 case 4: result
= nir_fnoise3_4(&b
, srcs
[0]); break;
2065 default: unreachable("not reached");
2069 switch (ir
->operands
[0]->type
->vector_elements
) {
2070 case 1: result
= nir_fnoise4_1(&b
, srcs
[0]); break;
2071 case 2: result
= nir_fnoise4_2(&b
, srcs
[0]); break;
2072 case 3: result
= nir_fnoise4_3(&b
, srcs
[0]); break;
2073 case 4: result
= nir_fnoise4_4(&b
, srcs
[0]); break;
2074 default: unreachable("not reached");
2078 unreachable("not reached");
2081 case ir_unop_get_buffer_size
: {
2082 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(
2084 nir_intrinsic_get_buffer_size
);
2085 load
->num_components
= ir
->type
->vector_elements
;
2086 load
->src
[0] = nir_src_for_ssa(evaluate_rvalue(ir
->operands
[0]));
2087 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
2088 add_instr(&load
->instr
, ir
->type
->vector_elements
, bit_size
);
2093 result
= type_is_float(out_type
) ? nir_fadd(&b
, srcs
[0], srcs
[1])
2094 : nir_iadd(&b
, srcs
[0], srcs
[1]);
2097 result
= type_is_float(out_type
) ? nir_fsub(&b
, srcs
[0], srcs
[1])
2098 : nir_isub(&b
, srcs
[0], srcs
[1]);
2101 if (type_is_float(out_type
))
2102 result
= nir_fmul(&b
, srcs
[0], srcs
[1]);
2103 else if (out_type
== GLSL_TYPE_INT64
&&
2104 (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
||
2105 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
))
2106 result
= nir_imul_2x32_64(&b
, srcs
[0], srcs
[1]);
2107 else if (out_type
== GLSL_TYPE_UINT64
&&
2108 (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_UINT
||
2109 ir
->operands
[1]->type
->base_type
== GLSL_TYPE_UINT
))
2110 result
= nir_umul_2x32_64(&b
, srcs
[0], srcs
[1]);
2112 result
= nir_imul(&b
, srcs
[0], srcs
[1]);
2115 if (type_is_float(out_type
))
2116 result
= nir_fdiv(&b
, srcs
[0], srcs
[1]);
2117 else if (type_is_signed(out_type
))
2118 result
= nir_idiv(&b
, srcs
[0], srcs
[1]);
2120 result
= nir_udiv(&b
, srcs
[0], srcs
[1]);
2123 result
= type_is_float(out_type
) ? nir_fmod(&b
, srcs
[0], srcs
[1])
2124 : nir_umod(&b
, srcs
[0], srcs
[1]);
2127 if (type_is_float(out_type
))
2128 result
= nir_fmin(&b
, srcs
[0], srcs
[1]);
2129 else if (type_is_signed(out_type
))
2130 result
= nir_imin(&b
, srcs
[0], srcs
[1]);
2132 result
= nir_umin(&b
, srcs
[0], srcs
[1]);
2135 if (type_is_float(out_type
))
2136 result
= nir_fmax(&b
, srcs
[0], srcs
[1]);
2137 else if (type_is_signed(out_type
))
2138 result
= nir_imax(&b
, srcs
[0], srcs
[1]);
2140 result
= nir_umax(&b
, srcs
[0], srcs
[1]);
2142 case ir_binop_pow
: result
= nir_fpow(&b
, srcs
[0], srcs
[1]); break;
2143 case ir_binop_bit_and
: result
= nir_iand(&b
, srcs
[0], srcs
[1]); break;
2144 case ir_binop_bit_or
: result
= nir_ior(&b
, srcs
[0], srcs
[1]); break;
2145 case ir_binop_bit_xor
: result
= nir_ixor(&b
, srcs
[0], srcs
[1]); break;
2146 case ir_binop_logic_and
:
2147 result
= nir_iand(&b
, srcs
[0], srcs
[1]);
2149 case ir_binop_logic_or
:
2150 result
= nir_ior(&b
, srcs
[0], srcs
[1]);
2152 case ir_binop_logic_xor
:
2153 result
= nir_ixor(&b
, srcs
[0], srcs
[1]);
2155 case ir_binop_lshift
: result
= nir_ishl(&b
, srcs
[0], srcs
[1]); break;
2156 case ir_binop_rshift
:
2157 result
= (type_is_signed(out_type
)) ? nir_ishr(&b
, srcs
[0], srcs
[1])
2158 : nir_ushr(&b
, srcs
[0], srcs
[1]);
2160 case ir_binop_imul_high
:
2161 result
= (out_type
== GLSL_TYPE_INT
) ? nir_imul_high(&b
, srcs
[0], srcs
[1])
2162 : nir_umul_high(&b
, srcs
[0], srcs
[1]);
2164 case ir_binop_carry
: result
= nir_uadd_carry(&b
, srcs
[0], srcs
[1]); break;
2165 case ir_binop_borrow
: result
= nir_usub_borrow(&b
, srcs
[0], srcs
[1]); break;
2167 if (type_is_float(types
[0]))
2168 result
= nir_flt(&b
, srcs
[0], srcs
[1]);
2169 else if (type_is_signed(types
[0]))
2170 result
= nir_ilt(&b
, srcs
[0], srcs
[1]);
2172 result
= nir_ult(&b
, srcs
[0], srcs
[1]);
2174 case ir_binop_gequal
:
2175 if (type_is_float(types
[0]))
2176 result
= nir_fge(&b
, srcs
[0], srcs
[1]);
2177 else if (type_is_signed(types
[0]))
2178 result
= nir_ige(&b
, srcs
[0], srcs
[1]);
2180 result
= nir_uge(&b
, srcs
[0], srcs
[1]);
2182 case ir_binop_equal
:
2183 if (type_is_float(types
[0]))
2184 result
= nir_feq(&b
, srcs
[0], srcs
[1]);
2186 result
= nir_ieq(&b
, srcs
[0], srcs
[1]);
2188 case ir_binop_nequal
:
2189 if (type_is_float(types
[0]))
2190 result
= nir_fne(&b
, srcs
[0], srcs
[1]);
2192 result
= nir_ine(&b
, srcs
[0], srcs
[1]);
2194 case ir_binop_all_equal
:
2195 if (type_is_float(types
[0])) {
2196 switch (ir
->operands
[0]->type
->vector_elements
) {
2197 case 1: result
= nir_feq(&b
, srcs
[0], srcs
[1]); break;
2198 case 2: result
= nir_ball_fequal2(&b
, srcs
[0], srcs
[1]); break;
2199 case 3: result
= nir_ball_fequal3(&b
, srcs
[0], srcs
[1]); break;
2200 case 4: result
= nir_ball_fequal4(&b
, srcs
[0], srcs
[1]); break;
2202 unreachable("not reached");
2205 switch (ir
->operands
[0]->type
->vector_elements
) {
2206 case 1: result
= nir_ieq(&b
, srcs
[0], srcs
[1]); break;
2207 case 2: result
= nir_ball_iequal2(&b
, srcs
[0], srcs
[1]); break;
2208 case 3: result
= nir_ball_iequal3(&b
, srcs
[0], srcs
[1]); break;
2209 case 4: result
= nir_ball_iequal4(&b
, srcs
[0], srcs
[1]); break;
2211 unreachable("not reached");
2215 case ir_binop_any_nequal
:
2216 if (type_is_float(types
[0])) {
2217 switch (ir
->operands
[0]->type
->vector_elements
) {
2218 case 1: result
= nir_fne(&b
, srcs
[0], srcs
[1]); break;
2219 case 2: result
= nir_bany_fnequal2(&b
, srcs
[0], srcs
[1]); break;
2220 case 3: result
= nir_bany_fnequal3(&b
, srcs
[0], srcs
[1]); break;
2221 case 4: result
= nir_bany_fnequal4(&b
, srcs
[0], srcs
[1]); break;
2223 unreachable("not reached");
2226 switch (ir
->operands
[0]->type
->vector_elements
) {
2227 case 1: result
= nir_ine(&b
, srcs
[0], srcs
[1]); break;
2228 case 2: result
= nir_bany_inequal2(&b
, srcs
[0], srcs
[1]); break;
2229 case 3: result
= nir_bany_inequal3(&b
, srcs
[0], srcs
[1]); break;
2230 case 4: result
= nir_bany_inequal4(&b
, srcs
[0], srcs
[1]); break;
2232 unreachable("not reached");
2237 switch (ir
->operands
[0]->type
->vector_elements
) {
2238 case 2: result
= nir_fdot2(&b
, srcs
[0], srcs
[1]); break;
2239 case 3: result
= nir_fdot3(&b
, srcs
[0], srcs
[1]); break;
2240 case 4: result
= nir_fdot4(&b
, srcs
[0], srcs
[1]); break;
2242 unreachable("not reached");
2245 case ir_binop_vector_extract
: {
2246 result
= nir_channel(&b
, srcs
[0], 0);
2247 for (unsigned i
= 1; i
< ir
->operands
[0]->type
->vector_elements
; i
++) {
2248 nir_ssa_def
*swizzled
= nir_channel(&b
, srcs
[0], i
);
2249 result
= nir_bcsel(&b
, nir_ieq(&b
, srcs
[1], nir_imm_int(&b
, i
)),
2255 case ir_binop_ldexp
: result
= nir_ldexp(&b
, srcs
[0], srcs
[1]); break;
2257 result
= nir_ffma(&b
, srcs
[0], srcs
[1], srcs
[2]);
2260 result
= nir_flrp(&b
, srcs
[0], srcs
[1], srcs
[2]);
2263 result
= nir_bcsel(&b
, srcs
[0], srcs
[1], srcs
[2]);
2265 case ir_triop_bitfield_extract
:
2266 result
= (out_type
== GLSL_TYPE_INT
) ?
2267 nir_ibitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]) :
2268 nir_ubitfield_extract(&b
, srcs
[0], srcs
[1], srcs
[2]);
2270 case ir_quadop_bitfield_insert
:
2271 result
= nir_bitfield_insert(&b
, srcs
[0], srcs
[1], srcs
[2], srcs
[3]);
2273 case ir_quadop_vector
:
2274 result
= nir_vec(&b
, srcs
, ir
->type
->vector_elements
);
2278 unreachable("not reached");
2283 nir_visitor::visit(ir_swizzle
*ir
)
2285 unsigned swizzle
[4] = { ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
};
2286 result
= nir_swizzle(&b
, evaluate_rvalue(ir
->val
), swizzle
,
2287 ir
->type
->vector_elements
, false);
2291 nir_visitor::visit(ir_texture
*ir
)
2298 num_srcs
= 1; /* coordinate */
2303 op
= (ir
->op
== ir_txb
) ? nir_texop_txb
: nir_texop_txl
;
2304 num_srcs
= 2; /* coordinate, bias/lod */
2308 op
= nir_texop_txd
; /* coordinate, dPdx, dPdy */
2314 if (ir
->lod_info
.lod
!= NULL
)
2315 num_srcs
= 2; /* coordinate, lod */
2317 num_srcs
= 1; /* coordinate */
2321 op
= nir_texop_txf_ms
;
2322 num_srcs
= 2; /* coordinate, sample_index */
2327 if (ir
->lod_info
.lod
!= NULL
)
2328 num_srcs
= 1; /* lod */
2335 num_srcs
= 1; /* coordinate */
2340 num_srcs
= 1; /* coordinate */
2343 case ir_query_levels
:
2344 op
= nir_texop_query_levels
;
2348 case ir_texture_samples
:
2349 op
= nir_texop_texture_samples
;
2353 case ir_samples_identical
:
2354 op
= nir_texop_samples_identical
;
2355 num_srcs
= 1; /* coordinate */
2359 unreachable("not reached");
2362 if (ir
->projector
!= NULL
)
2364 if (ir
->shadow_comparator
!= NULL
)
2366 /* offsets are constants we store inside nir_tex_intrs.offsets */
2367 if (ir
->offset
!= NULL
&& !ir
->offset
->type
->is_array())
2370 /* Add one for the texture deref */
2373 nir_tex_instr
*instr
= nir_tex_instr_create(this->shader
, num_srcs
);
2376 instr
->sampler_dim
=
2377 (glsl_sampler_dim
) ir
->sampler
->type
->sampler_dimensionality
;
2378 instr
->is_array
= ir
->sampler
->type
->sampler_array
;
2379 instr
->is_shadow
= ir
->sampler
->type
->sampler_shadow
;
2380 if (instr
->is_shadow
)
2381 instr
->is_new_style_shadow
= (ir
->type
->vector_elements
== 1);
2382 switch (ir
->type
->base_type
) {
2383 case GLSL_TYPE_FLOAT
:
2384 instr
->dest_type
= nir_type_float
;
2387 instr
->dest_type
= nir_type_int
;
2389 case GLSL_TYPE_BOOL
:
2390 case GLSL_TYPE_UINT
:
2391 instr
->dest_type
= nir_type_uint
;
2394 unreachable("not reached");
2397 nir_deref_instr
*sampler_deref
= evaluate_deref(ir
->sampler
);
2399 /* check for bindless handles */
2400 if (sampler_deref
->mode
!= nir_var_uniform
||
2401 nir_deref_instr_get_variable(sampler_deref
)->data
.bindless
) {
2402 nir_ssa_def
*load
= nir_load_deref(&b
, sampler_deref
);
2403 instr
->src
[0].src
= nir_src_for_ssa(load
);
2404 instr
->src
[0].src_type
= nir_tex_src_texture_handle
;
2405 instr
->src
[1].src
= nir_src_for_ssa(load
);
2406 instr
->src
[1].src_type
= nir_tex_src_sampler_handle
;
2408 instr
->src
[0].src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2409 instr
->src
[0].src_type
= nir_tex_src_texture_deref
;
2410 instr
->src
[1].src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2411 instr
->src
[1].src_type
= nir_tex_src_sampler_deref
;
2414 unsigned src_number
= 2;
2416 if (ir
->coordinate
!= NULL
) {
2417 instr
->coord_components
= ir
->coordinate
->type
->vector_elements
;
2418 instr
->src
[src_number
].src
=
2419 nir_src_for_ssa(evaluate_rvalue(ir
->coordinate
));
2420 instr
->src
[src_number
].src_type
= nir_tex_src_coord
;
2424 if (ir
->projector
!= NULL
) {
2425 instr
->src
[src_number
].src
=
2426 nir_src_for_ssa(evaluate_rvalue(ir
->projector
));
2427 instr
->src
[src_number
].src_type
= nir_tex_src_projector
;
2431 if (ir
->shadow_comparator
!= NULL
) {
2432 instr
->src
[src_number
].src
=
2433 nir_src_for_ssa(evaluate_rvalue(ir
->shadow_comparator
));
2434 instr
->src
[src_number
].src_type
= nir_tex_src_comparator
;
2438 if (ir
->offset
!= NULL
) {
2439 if (ir
->offset
->type
->is_array()) {
2440 for (int i
= 0; i
< ir
->offset
->type
->array_size(); i
++) {
2441 const ir_constant
*c
=
2442 ir
->offset
->as_constant()->get_array_element(i
);
2444 for (unsigned j
= 0; j
< 2; ++j
) {
2445 int val
= c
->get_int_component(j
);
2446 assert(val
<= 31 && val
>= -32);
2447 instr
->tg4_offsets
[i
][j
] = val
;
2451 assert(ir
->offset
->type
->is_vector() || ir
->offset
->type
->is_scalar());
2453 instr
->src
[src_number
].src
=
2454 nir_src_for_ssa(evaluate_rvalue(ir
->offset
));
2455 instr
->src
[src_number
].src_type
= nir_tex_src_offset
;
2462 instr
->src
[src_number
].src
=
2463 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.bias
));
2464 instr
->src
[src_number
].src_type
= nir_tex_src_bias
;
2471 if (ir
->lod_info
.lod
!= NULL
) {
2472 instr
->src
[src_number
].src
=
2473 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.lod
));
2474 instr
->src
[src_number
].src_type
= nir_tex_src_lod
;
2480 instr
->src
[src_number
].src
=
2481 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdx
));
2482 instr
->src
[src_number
].src_type
= nir_tex_src_ddx
;
2484 instr
->src
[src_number
].src
=
2485 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.grad
.dPdy
));
2486 instr
->src
[src_number
].src_type
= nir_tex_src_ddy
;
2491 instr
->src
[src_number
].src
=
2492 nir_src_for_ssa(evaluate_rvalue(ir
->lod_info
.sample_index
));
2493 instr
->src
[src_number
].src_type
= nir_tex_src_ms_index
;
2498 instr
->component
= ir
->lod_info
.component
->as_constant()->value
.u
[0];
2505 assert(src_number
== num_srcs
);
2507 unsigned bit_size
= glsl_get_bit_size(ir
->type
);
2508 add_instr(&instr
->instr
, nir_tex_instr_dest_size(instr
), bit_size
);
2512 nir_visitor::visit(ir_constant
*ir
)
2515 * We don't know if this variable is an array or struct that gets
2516 * dereferenced, so do the safe thing an make it a variable with a
2517 * constant initializer and return a dereference.
2521 nir_local_variable_create(this->impl
, ir
->type
, "const_temp");
2522 var
->data
.read_only
= true;
2523 var
->constant_initializer
= constant_copy(ir
, var
);
2525 this->deref
= nir_build_deref_var(&b
, var
);
2529 nir_visitor::visit(ir_dereference_variable
*ir
)
2531 if (ir
->variable_referenced()->data
.mode
== ir_var_function_out
) {
2532 unsigned i
= (sig
->return_type
!= glsl_type::void_type
) ? 1 : 0;
2534 foreach_in_list(ir_variable
, param
, &sig
->parameters
) {
2535 if (param
== ir
->variable_referenced()) {
2541 this->deref
= nir_build_deref_cast(&b
, nir_load_param(&b
, i
),
2542 nir_var_function_temp
, ir
->type
, 0);
2546 assert(ir
->variable_referenced()->data
.mode
!= ir_var_function_inout
);
2548 struct hash_entry
*entry
=
2549 _mesa_hash_table_search(this->var_table
, ir
->var
);
2551 nir_variable
*var
= (nir_variable
*) entry
->data
;
2553 this->deref
= nir_build_deref_var(&b
, var
);
2557 nir_visitor::visit(ir_dereference_record
*ir
)
2559 ir
->record
->accept(this);
2561 int field_index
= ir
->field_idx
;
2562 assert(field_index
>= 0);
2564 this->deref
= nir_build_deref_struct(&b
, this->deref
, field_index
);
2568 nir_visitor::visit(ir_dereference_array
*ir
)
2570 nir_ssa_def
*index
= evaluate_rvalue(ir
->array_index
);
2572 ir
->array
->accept(this);
2574 this->deref
= nir_build_deref_array(&b
, this->deref
, index
);
2578 nir_visitor::visit(ir_barrier
*)
2580 nir_intrinsic_instr
*instr
=
2581 nir_intrinsic_instr_create(this->shader
, nir_intrinsic_barrier
);
2582 nir_builder_instr_insert(&b
, &instr
->instr
);
2586 glsl_float64_funcs_to_nir(struct gl_context
*ctx
,
2587 const nir_shader_compiler_options
*options
)
2589 /* We pretend it's a vertex shader. Ultimately, the stage shouldn't
2590 * matter because we're not optimizing anything here.
2592 struct gl_shader
*sh
= _mesa_new_shader(-1, MESA_SHADER_VERTEX
);
2593 sh
->Source
= float64_source
;
2594 sh
->CompileStatus
= COMPILE_FAILURE
;
2595 _mesa_glsl_compile_shader(ctx
, sh
, false, false, true);
2597 if (!sh
->CompileStatus
) {
2600 "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
2601 sh
->InfoLog
, float64_source
);
2606 nir_shader
*nir
= nir_shader_create(NULL
, MESA_SHADER_VERTEX
, options
, NULL
);
2608 nir_visitor
v1(ctx
, nir
);
2609 nir_function_visitor
v2(&v1
);
2611 visit_exec_list(sh
->ir
, &v1
);
2613 /* _mesa_delete_shader will try to free sh->Source but it's static const */
2615 _mesa_delete_shader(ctx
, sh
);
2617 nir_validate_shader(nir
, "float64_funcs_to_nir");
2619 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_function_temp
);
2620 NIR_PASS_V(nir
, nir_lower_returns
);
2621 NIR_PASS_V(nir
, nir_inline_functions
);
2622 NIR_PASS_V(nir
, nir_opt_deref
);