glsl_to_nir: drop supports_ints
[mesa.git] / src / compiler / glsl / glsl_to_nir.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "float64_glsl.h"
29 #include "glsl_to_nir.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
32 #include "ir.h"
33 #include "ir_optimization.h"
34 #include "program.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"
42
43 /*
44 * pass to lower GLSL IR to NIR
45 *
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
48 * pass.
49 */
50
51 namespace {
52
53 class nir_visitor : public ir_visitor
54 {
55 public:
56 nir_visitor(gl_context *ctx, nir_shader *shader);
57 ~nir_visitor();
58
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 *);
79
80 void create_function(ir_function_signature *ir);
81
82 private:
83 void add_instr(nir_instr *instr, unsigned num_components, unsigned bit_size);
84 nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
85
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,
89 nir_ssa_def *src2);
90 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
91 nir_ssa_def *src2, nir_ssa_def *src3);
92
93 bool supports_std430;
94
95 nir_shader *shader;
96 nir_function_impl *impl;
97 nir_builder b;
98 nir_ssa_def *result; /* result of the expression tree last visited */
99
100 nir_deref_instr *evaluate_deref(ir_instruction *ir);
101
102 nir_constant *constant_copy(ir_constant *ir, void *mem_ctx);
103
104 /* most recent deref instruction created */
105 nir_deref_instr *deref;
106
107 /* whether the IR we're operating on is per-function or global */
108 bool is_global;
109
110 ir_function_signature *sig;
111
112 /* map of ir_variable -> nir_variable */
113 struct hash_table *var_table;
114
115 /* map of ir_function_signature -> nir_function_overload */
116 struct hash_table *overload_table;
117 };
118
119 /*
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
122 * calls.
123 */
124
125 class nir_function_visitor : public ir_hierarchical_visitor
126 {
127 public:
128 nir_function_visitor(nir_visitor *v) : visitor(v)
129 {
130 }
131 virtual ir_visitor_status visit_enter(ir_function *);
132
133 private:
134 nir_visitor *visitor;
135 };
136
137 /* glsl_to_nir can only handle converting certain function paramaters
138 * to NIR. This visitor checks for parameters it can't currently handle.
139 */
140 class ir_function_param_visitor : public ir_hierarchical_visitor
141 {
142 public:
143 ir_function_param_visitor()
144 : unsupported(false)
145 {
146 }
147
148 virtual ir_visitor_status visit_enter(ir_function_signature *ir)
149 {
150
151 if (ir->is_intrinsic())
152 return visit_continue;
153
154 foreach_in_list(ir_variable, param, &ir->parameters) {
155 if (!param->type->is_vector() || !param->type->is_scalar()) {
156 unsupported = true;
157 return visit_stop;
158 }
159
160 if (param->data.mode == ir_var_function_inout) {
161 unsupported = true;
162 return visit_stop;
163 }
164 }
165
166 return visit_continue;
167 }
168
169 bool unsupported;
170 };
171
172 } /* end of anonymous namespace */
173
174
175 static bool
176 has_unsupported_function_param(exec_list *ir)
177 {
178 ir_function_param_visitor visitor;
179 visit_list_elements(&visitor, ir);
180 return visitor.unsupported;
181 }
182
183 nir_shader *
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)
188 {
189 struct gl_linked_shader *sh = shader_prog->_LinkedShaders[stage];
190
191 const struct gl_shader_compiler_options *gl_options =
192 &ctx->Const.ShaderCompilerOptions[stage];
193
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.
197 *
198 * TODO: add missing glsl ir to nir support and remove this loop.
199 */
200 while (has_unsupported_function_param(sh->ir)) {
201 do_common_optimization(sh->ir, true, true, gl_options,
202 ctx->Const.NativeIntegers);
203 }
204
205 nir_shader *shader = nir_shader_create(NULL, stage, options,
206 &sh->Program->info);
207
208 nir_visitor v1(ctx, shader);
209 nir_function_visitor v2(&v1);
210 v2.run(sh->ir);
211 visit_exec_list(sh->ir, &v1);
212
213 nir_validate_shader(shader, "after glsl to nir, before function inline");
214
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.
218 */
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);
223
224 nir_validate_shader(shader, "after function inlining and return lowering");
225
226 /* Now that we have inlined everything remove all of the functions except
227 * main().
228 */
229 foreach_list_typed_safe(nir_function, function, node, &(shader)->functions){
230 if (strcmp("main", function->name) != 0) {
231 exec_node_remove(&function->node);
232 }
233 }
234
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);
241
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);
245
246 /* Check for transform feedback varyings specified via the API */
247 shader->info.has_transform_feedback_varyings =
248 shader_prog->TransformFeedback.NumVarying > 0;
249
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;
254
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;
258 }
259
260 return shader;
261 }
262
263 nir_visitor::nir_visitor(gl_context *ctx, nir_shader *shader)
264 {
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);
270 this->result = NULL;
271 this->impl = NULL;
272 this->deref = NULL;
273 memset(&this->b, 0, sizeof(this->b));
274 }
275
276 nir_visitor::~nir_visitor()
277 {
278 _mesa_hash_table_destroy(this->var_table, NULL);
279 _mesa_hash_table_destroy(this->overload_table, NULL);
280 }
281
282 nir_deref_instr *
283 nir_visitor::evaluate_deref(ir_instruction *ir)
284 {
285 ir->accept(this);
286 return this->deref;
287 }
288
289 nir_constant *
290 nir_visitor::constant_copy(ir_constant *ir, void *mem_ctx)
291 {
292 if (ir == NULL)
293 return NULL;
294
295 nir_constant *ret = rzalloc(mem_ctx, nir_constant);
296
297 const unsigned rows = ir->type->vector_elements;
298 const unsigned cols = ir->type->matrix_columns;
299 unsigned i;
300
301 ret->num_elements = 0;
302 switch (ir->type->base_type) {
303 case GLSL_TYPE_UINT:
304 /* Only float base types can be matrices. */
305 assert(cols == 1);
306
307 for (unsigned r = 0; r < rows; r++)
308 ret->values[0][r].u32 = ir->value.u[r];
309
310 break;
311
312 case GLSL_TYPE_INT:
313 /* Only float base types can be matrices. */
314 assert(cols == 1);
315
316 for (unsigned r = 0; r < rows; r++)
317 ret->values[0][r].i32 = ir->value.i[r];
318
319 break;
320
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];
325 }
326 break;
327
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];
332 }
333 break;
334
335 case GLSL_TYPE_UINT64:
336 /* Only float base types can be matrices. */
337 assert(cols == 1);
338
339 for (unsigned r = 0; r < rows; r++)
340 ret->values[0][r].u64 = ir->value.u64[r];
341 break;
342
343 case GLSL_TYPE_INT64:
344 /* Only float base types can be matrices. */
345 assert(cols == 1);
346
347 for (unsigned r = 0; r < rows; r++)
348 ret->values[0][r].i64 = ir->value.i64[r];
349 break;
350
351 case GLSL_TYPE_BOOL:
352 /* Only float base types can be matrices. */
353 assert(cols == 1);
354
355 for (unsigned r = 0; r < rows; r++)
356 ret->values[0][r].b = ir->value.b[r];
357
358 break;
359
360 case GLSL_TYPE_STRUCT:
361 case GLSL_TYPE_ARRAY:
362 ret->elements = ralloc_array(mem_ctx, nir_constant *,
363 ir->type->length);
364 ret->num_elements = ir->type->length;
365
366 for (i = 0; i < ir->type->length; i++)
367 ret->elements[i] = constant_copy(ir->const_elements[i], mem_ctx);
368 break;
369
370 default:
371 unreachable("not reached");
372 }
373
374 return ret;
375 }
376
377 static const glsl_type *
378 wrap_type_in_array(const glsl_type *elem_type, const glsl_type *array_type)
379 {
380 if (!array_type->is_array())
381 return elem_type;
382
383 elem_type = wrap_type_in_array(elem_type, array_type->fields.array);
384
385 return glsl_type::get_array_instance(elem_type, array_type->length);
386 }
387
388 void
389 nir_visitor::visit(ir_variable *ir)
390 {
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.
394 */
395 if (ir->data.mode == ir_var_shader_shared)
396 return;
397
398 /* FINISHME: inout parameters */
399 assert(ir->data.mode != ir_var_function_inout);
400
401 if (ir->data.mode == ir_var_function_out)
402 return;
403
404 nir_variable *var = rzalloc(shader, nir_variable);
405 var->type = ir->type;
406 var->name = ralloc_strdup(var, ir->name);
407
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;
417
418 switch(ir->data.mode) {
419 case ir_var_auto:
420 case ir_var_temporary:
421 if (is_global)
422 var->data.mode = nir_var_shader_temp;
423 else
424 var->data.mode = nir_var_function_temp;
425 break;
426
427 case ir_var_function_in:
428 case ir_var_const_in:
429 var->data.mode = nir_var_function_temp;
430 break;
431
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;
443 } else {
444 var->data.mode = nir_var_shader_in;
445
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();
450 }
451
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();
456 }
457 }
458 break;
459
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();
466 }
467
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();
472 }
473 break;
474
475 case ir_var_uniform:
476 if (ir->get_interface_type())
477 var->data.mode = nir_var_mem_ubo;
478 else
479 var->data.mode = nir_var_uniform;
480 break;
481
482 case ir_var_shader_storage:
483 var->data.mode = nir_var_mem_ssbo;
484 break;
485
486 case ir_var_system_value:
487 var->data.mode = nir_var_system_value;
488 break;
489
490 default:
491 unreachable("not reached");
492 }
493
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;
505
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);
510
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.
514 */
515 var->type = wrap_type_in_array(explicit_ifc_type, ir->type);
516 } else {
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)
523 continue;
524
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;
536
537 found = true;
538 break;
539 }
540 assert(found);
541 }
542 }
543
544 var->data.interpolation = ir->data.interpolation;
545 var->data.location_frac = ir->data.location_frac;
546
547 switch (ir->data.depth_layout) {
548 case ir_depth_layout_none:
549 var->data.depth_layout = nir_depth_layout_none;
550 break;
551 case ir_depth_layout_any:
552 var->data.depth_layout = nir_depth_layout_any;
553 break;
554 case ir_depth_layout_greater:
555 var->data.depth_layout = nir_depth_layout_greater;
556 break;
557 case ir_depth_layout_less:
558 var->data.depth_layout = nir_depth_layout_less;
559 break;
560 case ir_depth_layout_unchanged:
561 var->data.depth_layout = nir_depth_layout_unchanged;
562 break;
563 default:
564 unreachable("not reached");
565 }
566
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;
573
574 var->data.image.access = (gl_access_qualifier)image_access;
575 var->data.image.format = ir->data.image_format;
576
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;
582
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);
587
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;
593 }
594 } else {
595 var->state_slots = NULL;
596 }
597
598 var->constant_initializer = constant_copy(ir->constant_initializer, var);
599
600 var->interface_type = ir->get_interface_type();
601
602 if (var->data.mode == nir_var_function_temp)
603 nir_function_impl_add_variable(impl, var);
604 else
605 nir_shader_add_variable(shader, var);
606
607 _mesa_hash_table_insert(var_table, ir, var);
608 }
609
610 ir_visitor_status
611 nir_function_visitor::visit_enter(ir_function *ir)
612 {
613 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
614 visitor->create_function(sig);
615 }
616 return visit_continue_with_parent;
617 }
618
619 void
620 nir_visitor::create_function(ir_function_signature *ir)
621 {
622 if (ir->is_intrinsic())
623 return;
624
625 nir_function *func = nir_function_create(shader, ir->function_name());
626 if (strcmp(ir->function_name(), "main") == 0)
627 func->is_entrypoint = true;
628
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);
632
633 unsigned np = 0;
634
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;
639 np++;
640 }
641
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());
645
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);
649 } else {
650 func->params[np].num_components = 1;
651 func->params[np].bit_size = 32;
652 }
653 np++;
654 }
655 assert(np == func->num_params);
656
657 _mesa_hash_table_insert(this->overload_table, ir, func);
658 }
659
660 void
661 nir_visitor::visit(ir_function *ir)
662 {
663 foreach_in_list(ir_function_signature, sig, &ir->signatures)
664 sig->accept(this);
665 }
666
667 void
668 nir_visitor::visit(ir_function_signature *ir)
669 {
670 if (ir->is_intrinsic())
671 return;
672
673 this->sig = ir;
674
675 struct hash_entry *entry =
676 _mesa_hash_table_search(this->overload_table, ir);
677
678 assert(entry);
679 nir_function *func = (nir_function *) entry->data;
680
681 if (ir->is_defined) {
682 nir_function_impl *impl = nir_function_impl_create(func);
683 this->impl = impl;
684
685 this->is_global = false;
686
687 nir_builder_init(&b, impl);
688 b.cursor = nir_after_cf_list(&impl->body);
689
690 unsigned i = (ir->return_type != glsl_type::void_type) ? 1 : 0;
691
692 foreach_in_list(ir_variable, param, &ir->parameters) {
693 nir_variable *var =
694 nir_local_variable_create(impl, param->type, param->name);
695
696 if (param->data.mode == ir_var_function_in) {
697 nir_store_var(&b, var, nir_load_param(&b, i), ~0);
698 }
699
700 _mesa_hash_table_insert(var_table, param, var);
701 i++;
702 }
703
704 visit_exec_list(&ir->body, this);
705
706 this->is_global = true;
707 } else {
708 func->impl = NULL;
709 }
710 }
711
712 void
713 nir_visitor::visit(ir_loop *ir)
714 {
715 nir_push_loop(&b);
716 visit_exec_list(&ir->body_instructions, this);
717 nir_pop_loop(&b, NULL);
718 }
719
720 void
721 nir_visitor::visit(ir_if *ir)
722 {
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);
728 }
729
730 void
731 nir_visitor::visit(ir_discard *ir)
732 {
733 /*
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.
738 */
739
740 nir_intrinsic_instr *discard;
741 if (ir->condition) {
742 discard = nir_intrinsic_instr_create(this->shader,
743 nir_intrinsic_discard_if);
744 discard->src[0] =
745 nir_src_for_ssa(evaluate_rvalue(ir->condition));
746 } else {
747 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
748 }
749
750 nir_builder_instr_insert(&b, &discard->instr);
751 }
752
753 void
754 nir_visitor::visit(ir_emit_vertex *ir)
755 {
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);
760 }
761
762 void
763 nir_visitor::visit(ir_end_primitive *ir)
764 {
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);
769 }
770
771 void
772 nir_visitor::visit(ir_loop_jump *ir)
773 {
774 nir_jump_type type;
775 switch (ir->mode) {
776 case ir_loop_jump::jump_break:
777 type = nir_jump_break;
778 break;
779 case ir_loop_jump::jump_continue:
780 type = nir_jump_continue;
781 break;
782 default:
783 unreachable("not reached");
784 }
785
786 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
787 nir_builder_instr_insert(&b, &instr->instr);
788 }
789
790 void
791 nir_visitor::visit(ir_return *ir)
792 {
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);
797
798 nir_ssa_def *val = evaluate_rvalue(ir->value);
799 nir_store_deref(&b, ret_deref, val, ~0);
800 }
801
802 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
803 nir_builder_instr_insert(&b, &instr->instr);
804 }
805
806 static void
807 intrinsic_set_std430_align(nir_intrinsic_instr *intrin, const glsl_type *type)
808 {
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);
812 }
813
814 void
815 nir_visitor::visit(ir_call *ir)
816 {
817 if (ir->callee->is_intrinsic()) {
818 nir_intrinsic_op op;
819
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;
824 break;
825 case ir_intrinsic_generic_atomic_and:
826 op = nir_intrinsic_deref_atomic_and;
827 break;
828 case ir_intrinsic_generic_atomic_or:
829 op = nir_intrinsic_deref_atomic_or;
830 break;
831 case ir_intrinsic_generic_atomic_xor:
832 op = nir_intrinsic_deref_atomic_xor;
833 break;
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;
842 else
843 unreachable("Invalid type");
844 break;
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;
853 else
854 unreachable("Invalid type");
855 break;
856 case ir_intrinsic_generic_atomic_exchange:
857 op = nir_intrinsic_deref_atomic_exchange;
858 break;
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;
863 break;
864 case ir_intrinsic_atomic_counter_read:
865 op = nir_intrinsic_atomic_counter_read_deref;
866 break;
867 case ir_intrinsic_atomic_counter_increment:
868 op = nir_intrinsic_atomic_counter_inc_deref;
869 break;
870 case ir_intrinsic_atomic_counter_predecrement:
871 op = nir_intrinsic_atomic_counter_pre_dec_deref;
872 break;
873 case ir_intrinsic_atomic_counter_add:
874 op = nir_intrinsic_atomic_counter_add_deref;
875 break;
876 case ir_intrinsic_atomic_counter_and:
877 op = nir_intrinsic_atomic_counter_and_deref;
878 break;
879 case ir_intrinsic_atomic_counter_or:
880 op = nir_intrinsic_atomic_counter_or_deref;
881 break;
882 case ir_intrinsic_atomic_counter_xor:
883 op = nir_intrinsic_atomic_counter_xor_deref;
884 break;
885 case ir_intrinsic_atomic_counter_min:
886 op = nir_intrinsic_atomic_counter_min_deref;
887 break;
888 case ir_intrinsic_atomic_counter_max:
889 op = nir_intrinsic_atomic_counter_max_deref;
890 break;
891 case ir_intrinsic_atomic_counter_exchange:
892 op = nir_intrinsic_atomic_counter_exchange_deref;
893 break;
894 case ir_intrinsic_atomic_counter_comp_swap:
895 op = nir_intrinsic_atomic_counter_comp_swap_deref;
896 break;
897 case ir_intrinsic_image_load:
898 op = nir_intrinsic_image_deref_load;
899 break;
900 case ir_intrinsic_image_store:
901 op = nir_intrinsic_image_deref_store;
902 break;
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;
907 break;
908 case ir_intrinsic_image_atomic_min:
909 op = nir_intrinsic_image_deref_atomic_min;
910 break;
911 case ir_intrinsic_image_atomic_max:
912 op = nir_intrinsic_image_deref_atomic_max;
913 break;
914 case ir_intrinsic_image_atomic_and:
915 op = nir_intrinsic_image_deref_atomic_and;
916 break;
917 case ir_intrinsic_image_atomic_or:
918 op = nir_intrinsic_image_deref_atomic_or;
919 break;
920 case ir_intrinsic_image_atomic_xor:
921 op = nir_intrinsic_image_deref_atomic_xor;
922 break;
923 case ir_intrinsic_image_atomic_exchange:
924 op = nir_intrinsic_image_deref_atomic_exchange;
925 break;
926 case ir_intrinsic_image_atomic_comp_swap:
927 op = nir_intrinsic_image_deref_atomic_comp_swap;
928 break;
929 case ir_intrinsic_memory_barrier:
930 op = nir_intrinsic_memory_barrier;
931 break;
932 case ir_intrinsic_image_size:
933 op = nir_intrinsic_image_deref_size;
934 break;
935 case ir_intrinsic_image_samples:
936 op = nir_intrinsic_image_deref_samples;
937 break;
938 case ir_intrinsic_ssbo_store:
939 op = nir_intrinsic_store_ssbo;
940 break;
941 case ir_intrinsic_ssbo_load:
942 op = nir_intrinsic_load_ssbo;
943 break;
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;
947 break;
948 case ir_intrinsic_ssbo_atomic_and:
949 op = nir_intrinsic_ssbo_atomic_and;
950 break;
951 case ir_intrinsic_ssbo_atomic_or:
952 op = nir_intrinsic_ssbo_atomic_or;
953 break;
954 case ir_intrinsic_ssbo_atomic_xor:
955 op = nir_intrinsic_ssbo_atomic_xor;
956 break;
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;
965 else
966 unreachable("Invalid type");
967 break;
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;
976 else
977 unreachable("Invalid type");
978 break;
979 case ir_intrinsic_ssbo_atomic_exchange:
980 op = nir_intrinsic_ssbo_atomic_exchange;
981 break;
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;
986 break;
987 case ir_intrinsic_shader_clock:
988 op = nir_intrinsic_shader_clock;
989 break;
990 case ir_intrinsic_begin_invocation_interlock:
991 op = nir_intrinsic_begin_invocation_interlock;
992 break;
993 case ir_intrinsic_end_invocation_interlock:
994 op = nir_intrinsic_end_invocation_interlock;
995 break;
996 case ir_intrinsic_group_memory_barrier:
997 op = nir_intrinsic_group_memory_barrier;
998 break;
999 case ir_intrinsic_memory_barrier_atomic_counter:
1000 op = nir_intrinsic_memory_barrier_atomic_counter;
1001 break;
1002 case ir_intrinsic_memory_barrier_buffer:
1003 op = nir_intrinsic_memory_barrier_buffer;
1004 break;
1005 case ir_intrinsic_memory_barrier_image:
1006 op = nir_intrinsic_memory_barrier_image;
1007 break;
1008 case ir_intrinsic_memory_barrier_shared:
1009 op = nir_intrinsic_memory_barrier_shared;
1010 break;
1011 case ir_intrinsic_shared_load:
1012 op = nir_intrinsic_load_shared;
1013 break;
1014 case ir_intrinsic_shared_store:
1015 op = nir_intrinsic_store_shared;
1016 break;
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;
1021 break;
1022 case ir_intrinsic_shared_atomic_and:
1023 op = nir_intrinsic_shared_atomic_and;
1024 break;
1025 case ir_intrinsic_shared_atomic_or:
1026 op = nir_intrinsic_shared_atomic_or;
1027 break;
1028 case ir_intrinsic_shared_atomic_xor:
1029 op = nir_intrinsic_shared_atomic_xor;
1030 break;
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;
1039 else
1040 unreachable("Invalid type");
1041 break;
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;
1050 else
1051 unreachable("Invalid type");
1052 break;
1053 case ir_intrinsic_shared_atomic_exchange:
1054 op = nir_intrinsic_shared_atomic_exchange;
1055 break;
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;
1060 break;
1061 case ir_intrinsic_vote_any:
1062 op = nir_intrinsic_vote_any;
1063 break;
1064 case ir_intrinsic_vote_all:
1065 op = nir_intrinsic_vote_all;
1066 break;
1067 case ir_intrinsic_vote_eq:
1068 op = nir_intrinsic_vote_ieq;
1069 break;
1070 case ir_intrinsic_ballot:
1071 op = nir_intrinsic_ballot;
1072 break;
1073 case ir_intrinsic_read_invocation:
1074 op = nir_intrinsic_read_invocation;
1075 break;
1076 case ir_intrinsic_read_first_invocation:
1077 op = nir_intrinsic_read_first_invocation;
1078 break;
1079 default:
1080 unreachable("not reached");
1081 }
1082
1083 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
1084 nir_ssa_def *ret = &instr->dest.ssa;
1085
1086 switch (op) {
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);
1103
1104 /* Deref */
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;
1109 if (!deref) {
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();
1114 assert(deref);
1115 }
1116 nir_deref_instr *nir_deref = evaluate_deref(deref);
1117 if (swizzle) {
1118 nir_deref = nir_build_deref_array_imm(&b, nir_deref,
1119 swizzle->mask.x);
1120 }
1121 instr->src[0] = nir_src_for_ssa(&nir_deref->dest.ssa);
1122
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()));
1127
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()));
1135 }
1136
1137 /* Atomic result */
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);
1142 break;
1143 }
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;
1158
1159 instr->src[0] = nir_src_for_ssa(&evaluate_deref(counter)->dest.ssa);
1160 param = param->get_next();
1161
1162 /* Set the intrinsic destination. */
1163 if (ir->return_deref) {
1164 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
1165 }
1166
1167 /* Set the intrinsic parameters. */
1168 if (!param->is_tail_sentinel()) {
1169 instr->src[1] =
1170 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1171 param = param->get_next();
1172 }
1173
1174 if (!param->is_tail_sentinel()) {
1175 instr->src[2] =
1176 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1177 param = param->get_next();
1178 }
1179
1180 nir_builder_instr_insert(&b, &instr->instr);
1181 break;
1182 }
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);
1199
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;
1205
1206 instr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
1207 param = param->get_next();
1208
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);
1214 }
1215
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;
1221 }
1222
1223 if (op == nir_intrinsic_image_deref_size ||
1224 op == nir_intrinsic_image_deref_samples) {
1225 nir_builder_instr_insert(&b, &instr->instr);
1226 break;
1227 }
1228
1229 /* Set the address argument, extending the coordinate vector to four
1230 * components.
1231 */
1232 nir_ssa_def *src_addr =
1233 evaluate_rvalue((ir_dereference *)param);
1234 nir_ssa_def *srcs[4];
1235
1236 for (int i = 0; i < 4; i++) {
1237 if (i < type->coordinate_components())
1238 srcs[i] = nir_channel(&b, src_addr, i);
1239 else
1240 srcs[i] = &instr_undef->def;
1241 }
1242
1243 instr->src[1] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
1244 param = param->get_next();
1245
1246 /* Set the sample argument, which is undefined for single-sample
1247 * images.
1248 */
1249 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
1250 instr->src[2] =
1251 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1252 param = param->get_next();
1253 } else {
1254 instr->src[2] = nir_src_for_ssa(&instr_undef->def);
1255 }
1256
1257 /* Set the intrinsic parameters. */
1258 if (!param->is_tail_sentinel()) {
1259 instr->src[3] =
1260 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1261 param = param->get_next();
1262 }
1263
1264 if (!param->is_tail_sentinel()) {
1265 instr->src[4] =
1266 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1267 param = param->get_next();
1268 }
1269 nir_builder_instr_insert(&b, &instr->instr);
1270 break;
1271 }
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);
1279 break;
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);
1284 break;
1285 case nir_intrinsic_begin_invocation_interlock:
1286 nir_builder_instr_insert(&b, &instr->instr);
1287 break;
1288 case nir_intrinsic_end_invocation_interlock:
1289 nir_builder_instr_insert(&b, &instr->instr);
1290 break;
1291 case nir_intrinsic_store_ssbo: {
1292 exec_node *param = ir->actual_parameters.get_head();
1293 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
1294
1295 param = param->get_next();
1296 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1297
1298 param = param->get_next();
1299 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1300
1301 param = param->get_next();
1302 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1303 assert(write_mask);
1304
1305 nir_ssa_def *nir_val = evaluate_rvalue(val);
1306 if (val->type->is_boolean())
1307 nir_val = nir_b2i32(&b, nir_val);
1308
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;
1315
1316 nir_builder_instr_insert(&b, &instr->instr);
1317 break;
1318 }
1319 case nir_intrinsic_load_ssbo: {
1320 exec_node *param = ir->actual_parameters.get_head();
1321 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
1322
1323 param = param->get_next();
1324 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1325
1326 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(block));
1327 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
1328
1329 const glsl_type *type = ir->return_deref->var->type;
1330 instr->num_components = type->vector_elements;
1331 intrinsic_set_std430_align(instr, type);
1332
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);
1337
1338 /* Insert the created nir instruction now since in the case of boolean
1339 * result we will need to emit another instruction after it
1340 */
1341 nir_builder_instr_insert(&b, &instr->instr);
1342
1343 /*
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
1346 * comparison.
1347 */
1348 if (type->is_boolean())
1349 ret = nir_i2b(&b, &instr->dest.ssa);
1350 break;
1351 }
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);
1368
1369 /* Block index */
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()));
1373
1374 /* Offset */
1375 param = param->get_next();
1376 inst = (ir_instruction *) param;
1377 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1378
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()));
1383
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()));
1391 }
1392
1393 /* Atomic result */
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);
1398 break;
1399 }
1400 case nir_intrinsic_load_shared: {
1401 exec_node *param = ir->actual_parameters.get_head();
1402 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1403
1404 nir_intrinsic_set_base(instr, 0);
1405 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(offset));
1406
1407 const glsl_type *type = ir->return_deref->var->type;
1408 instr->num_components = type->vector_elements;
1409 intrinsic_set_std430_align(instr, type);
1410
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);
1415
1416 nir_builder_instr_insert(&b, &instr->instr);
1417
1418 /* The value in shared memory is a 32-bit value */
1419 if (type->is_boolean())
1420 ret = nir_i2b(&b, &instr->dest.ssa);
1421 break;
1422 }
1423 case nir_intrinsic_store_shared: {
1424 exec_node *param = ir->actual_parameters.get_head();
1425 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1426
1427 param = param->get_next();
1428 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1429
1430 param = param->get_next();
1431 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1432 assert(write_mask);
1433
1434 nir_intrinsic_set_base(instr, 0);
1435 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
1436
1437 nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
1438
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);
1443
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);
1447
1448 nir_builder_instr_insert(&b, &instr->instr);
1449 break;
1450 }
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);
1467
1468 /* Offset */
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()));
1472
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()));
1477
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;
1484 instr->src[2] =
1485 nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1486 }
1487
1488 /* Atomic result */
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,
1493 bit_size, NULL);
1494 nir_builder_instr_insert(&b, &instr->instr);
1495 break;
1496 }
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;
1502
1503 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1504 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1505
1506 nir_builder_instr_insert(&b, &instr->instr);
1507 break;
1508 }
1509
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;
1514
1515 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1516 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1517
1518 nir_builder_instr_insert(&b, &instr->instr);
1519 break;
1520 }
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;
1525
1526 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1527 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1528
1529 ir_rvalue *invocation = (ir_rvalue *) ir->actual_parameters.get_head()->next;
1530 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(invocation));
1531
1532 nir_builder_instr_insert(&b, &instr->instr);
1533 break;
1534 }
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;
1539
1540 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1541 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1542
1543 nir_builder_instr_insert(&b, &instr->instr);
1544 break;
1545 }
1546 default:
1547 unreachable("not reached");
1548 }
1549
1550 if (ir->return_deref)
1551 nir_store_deref(&b, evaluate_deref(ir->return_deref), ret, ~0);
1552
1553 return;
1554 }
1555
1556 struct hash_entry *entry =
1557 _mesa_hash_table_search(this->overload_table, ir->callee);
1558 assert(entry);
1559 nir_function *callee = (nir_function *) entry->data;
1560
1561 nir_call_instr *call = nir_call_instr_create(this->shader, callee);
1562
1563 unsigned i = 0;
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,
1568 "return_tmp");
1569 ret_deref = nir_build_deref_var(&b, ret_tmp);
1570 call->params[i++] = nir_src_for_ssa(&ret_deref->dest.ssa);
1571 }
1572
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;
1577
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);
1584
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");
1588 }
1589
1590 i++;
1591 }
1592
1593 nir_builder_instr_insert(&b, &call->instr);
1594
1595 if (ir->return_deref)
1596 nir_store_deref(&b, evaluate_deref(ir->return_deref), nir_load_deref(&b, ret_deref), ~0);
1597 }
1598
1599 void
1600 nir_visitor::visit(ir_assignment *ir)
1601 {
1602 unsigned num_components = ir->lhs->type->vector_elements;
1603
1604 b.exact = ir->lhs->variable_referenced()->data.invariant ||
1605 ir->lhs->variable_referenced()->data.precise;
1606
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);
1613 } else {
1614 nir_copy_deref(&b, evaluate_deref(ir->lhs), evaluate_deref(ir->rhs));
1615 }
1616 return;
1617 }
1618
1619 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
1620
1621 ir->lhs->accept(this);
1622 nir_deref_instr *lhs_deref = this->deref;
1623 nir_ssa_def *src = evaluate_rvalue(ir->rhs);
1624
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
1629 * from the load.
1630 */
1631 unsigned swiz[4];
1632 unsigned component = 0;
1633 for (unsigned i = 0; i < 4; i++) {
1634 swiz[i] = ir->write_mask & (1 << i) ? component++ : 0;
1635 }
1636 src = nir_swizzle(&b, src, swiz, num_components, false);
1637 }
1638
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);
1643 } else {
1644 nir_store_deref(&b, lhs_deref, src, ir->write_mask);
1645 }
1646 }
1647
1648 /*
1649 * Given an instruction, returns a pointer to its destination or NULL if there
1650 * is no destination.
1651 *
1652 * Note that this only handles instructions we generate at this level.
1653 */
1654 static nir_dest *
1655 get_instr_dest(nir_instr *instr)
1656 {
1657 nir_alu_instr *alu_instr;
1658 nir_intrinsic_instr *intrinsic_instr;
1659 nir_tex_instr *tex_instr;
1660
1661 switch (instr->type) {
1662 case nir_instr_type_alu:
1663 alu_instr = nir_instr_as_alu(instr);
1664 return &alu_instr->dest.dest;
1665
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;
1670 else
1671 return NULL;
1672
1673 case nir_instr_type_tex:
1674 tex_instr = nir_instr_as_tex(instr);
1675 return &tex_instr->dest;
1676
1677 default:
1678 unreachable("not reached");
1679 }
1680
1681 return NULL;
1682 }
1683
1684 void
1685 nir_visitor::add_instr(nir_instr *instr, unsigned num_components,
1686 unsigned bit_size)
1687 {
1688 nir_dest *dest = get_instr_dest(instr);
1689
1690 if (dest)
1691 nir_ssa_dest_init(instr, dest, num_components, bit_size, NULL);
1692
1693 nir_builder_instr_insert(&b, instr);
1694
1695 if (dest) {
1696 assert(dest->is_ssa);
1697 this->result = &dest->ssa;
1698 }
1699 }
1700
1701 nir_ssa_def *
1702 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1703 {
1704 ir->accept(this);
1705 if (ir->as_dereference() || ir->as_constant()) {
1706 /*
1707 * A dereference is being used on the right hand side, which means we
1708 * must emit a variable load.
1709 */
1710
1711 this->result = nir_load_deref(&b, this->deref);
1712 }
1713
1714 return this->result;
1715 }
1716
1717 static bool
1718 type_is_float(glsl_base_type type)
1719 {
1720 return type == GLSL_TYPE_FLOAT || type == GLSL_TYPE_DOUBLE ||
1721 type == GLSL_TYPE_FLOAT16;
1722 }
1723
1724 static bool
1725 type_is_signed(glsl_base_type type)
1726 {
1727 return type == GLSL_TYPE_INT || type == GLSL_TYPE_INT64 ||
1728 type == GLSL_TYPE_INT16;
1729 }
1730
1731 static bool
1732 type_is_int(glsl_base_type type)
1733 {
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;
1738 }
1739
1740 void
1741 nir_visitor::visit(ir_expression *ir)
1742 {
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);
1755
1756 /*
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.
1759 */
1760
1761 if (ir->type->is_boolean())
1762 this->result = nir_i2b(&b, &load->dest.ssa);
1763
1764 return;
1765 }
1766
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;
1772 if (!deref) {
1773 /* the api does not allow a swizzle here, but the varying packing code
1774 * may have pushed one into here.
1775 */
1776 swizzle = ir->operands[0]->as_swizzle();
1777 assert(swizzle);
1778 deref = swizzle->val->as_dereference();
1779 assert(deref);
1780 }
1781
1782 deref->accept(this);
1783
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;
1789 break;
1790 case ir_binop_interpolate_at_offset:
1791 op = nir_intrinsic_interp_deref_at_offset;
1792 break;
1793 case ir_binop_interpolate_at_sample:
1794 op = nir_intrinsic_interp_deref_at_sample;
1795 break;
1796 default:
1797 unreachable("Invalid interpolation intrinsic");
1798 }
1799 } else {
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.
1805 */
1806 assert(this->deref->mode == nir_var_shader_temp);
1807 op = nir_intrinsic_load_deref;
1808 }
1809
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);
1813
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]));
1817
1818 unsigned bit_size = glsl_get_bit_size(deref->type);
1819 add_instr(&intrin->instr, deref->type->vector_elements, bit_size);
1820
1821 if (swizzle) {
1822 unsigned swiz[4] = {
1823 swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
1824 };
1825
1826 result = nir_swizzle(&b, result, swiz,
1827 swizzle->type->vector_elements, false);
1828 }
1829
1830 return;
1831 }
1832
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);
1837
1838 ir_dereference *deref = ir->operands[0]->as_dereference();
1839 intrin->src[0] = nir_src_for_ssa(&evaluate_deref(deref)->dest.ssa);
1840
1841 add_instr(&intrin->instr, 1, 32);
1842 return;
1843 }
1844
1845 default:
1846 break;
1847 }
1848
1849 nir_ssa_def *srcs[4];
1850 for (unsigned i = 0; i < ir->num_operands; i++)
1851 srcs[i] = evaluate_rvalue(ir->operands[i]);
1852
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;
1856
1857 glsl_base_type out_type = ir->type->base_type;
1858
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]);
1863 break;
1864 case ir_unop_neg:
1865 result = type_is_float(types[0]) ? nir_fneg(&b, srcs[0])
1866 : nir_ineg(&b, srcs[0]);
1867 break;
1868 case ir_unop_abs:
1869 result = type_is_float(types[0]) ? nir_fabs(&b, srcs[0])
1870 : nir_iabs(&b, srcs[0]);
1871 break;
1872 case ir_unop_saturate:
1873 assert(type_is_float(types[0]));
1874 result = nir_fsat(&b, srcs[0]);
1875 break;
1876 case ir_unop_sign:
1877 result = type_is_float(types[0]) ? nir_fsign(&b, srcs[0])
1878 : nir_isign(&b, srcs[0]);
1879 break;
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;
1887 case ir_unop_i2f:
1888 result = nir_i2f32(&b, srcs[0]);
1889 break;
1890 case ir_unop_u2f:
1891 result = nir_u2f32(&b, srcs[0]);
1892 break;
1893 case ir_unop_b2f:
1894 result = nir_b2f32(&b, srcs[0]);
1895 break;
1896 case ir_unop_f2i:
1897 result = nir_f2i32(&b, srcs[0]);
1898 break;
1899 case ir_unop_f2u:
1900 result = nir_f2u32(&b, srcs[0]);
1901 break;
1902 case ir_unop_f2b:
1903 case ir_unop_i2b:
1904 case ir_unop_b2i:
1905 case ir_unop_b2i64:
1906 case ir_unop_d2f:
1907 case ir_unop_f2d:
1908 case ir_unop_d2i:
1909 case ir_unop_d2u:
1910 case ir_unop_d2b:
1911 case ir_unop_i2d:
1912 case ir_unop_u2d:
1913 case ir_unop_i642i:
1914 case ir_unop_i642u:
1915 case ir_unop_i642f:
1916 case ir_unop_i642b:
1917 case ir_unop_i642d:
1918 case ir_unop_u642i:
1919 case ir_unop_u642u:
1920 case ir_unop_u642f:
1921 case ir_unop_u642d:
1922 case ir_unop_i2i64:
1923 case ir_unop_u2i64:
1924 case ir_unop_f2i64:
1925 case ir_unop_d2i64:
1926 case ir_unop_i2u64:
1927 case ir_unop_u2u64:
1928 case ir_unop_f2u64:
1929 case ir_unop_d2u64:
1930 case ir_unop_i2u:
1931 case ir_unop_u2i:
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.
1941 */
1942 result->bit_size = nir_alu_type_get_type_size(dst_type);
1943 break;
1944 }
1945
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:
1955 /* no-op */
1956 result = nir_imov(&b, srcs[0]);
1957 break;
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]);
1975 break;
1976 case ir_unop_pack_snorm_4x8:
1977 result = nir_pack_snorm_4x8(&b, srcs[0]);
1978 break;
1979 case ir_unop_pack_unorm_2x16:
1980 result = nir_pack_unorm_2x16(&b, srcs[0]);
1981 break;
1982 case ir_unop_pack_unorm_4x8:
1983 result = nir_pack_unorm_4x8(&b, srcs[0]);
1984 break;
1985 case ir_unop_pack_half_2x16:
1986 result = nir_pack_half_2x16(&b, srcs[0]);
1987 break;
1988 case ir_unop_unpack_snorm_2x16:
1989 result = nir_unpack_snorm_2x16(&b, srcs[0]);
1990 break;
1991 case ir_unop_unpack_snorm_4x8:
1992 result = nir_unpack_snorm_4x8(&b, srcs[0]);
1993 break;
1994 case ir_unop_unpack_unorm_2x16:
1995 result = nir_unpack_unorm_2x16(&b, srcs[0]);
1996 break;
1997 case ir_unop_unpack_unorm_4x8:
1998 result = nir_unpack_unorm_4x8(&b, srcs[0]);
1999 break;
2000 case ir_unop_unpack_half_2x16:
2001 result = nir_unpack_half_2x16(&b, srcs[0]);
2002 break;
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]);
2009 break;
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]);
2016 break;
2017 case ir_unop_bitfield_reverse:
2018 result = nir_bitfield_reverse(&b, srcs[0]);
2019 break;
2020 case ir_unop_bit_count:
2021 result = nir_bit_count(&b, srcs[0]);
2022 break;
2023 case ir_unop_find_msb:
2024 switch (types[0]) {
2025 case GLSL_TYPE_UINT:
2026 result = nir_ufind_msb(&b, srcs[0]);
2027 break;
2028 case GLSL_TYPE_INT:
2029 result = nir_ifind_msb(&b, srcs[0]);
2030 break;
2031 default:
2032 unreachable("Invalid type for findMSB()");
2033 }
2034 break;
2035 case ir_unop_find_lsb:
2036 result = nir_find_lsb(&b, srcs[0]);
2037 break;
2038
2039 case ir_unop_noise:
2040 switch (ir->type->vector_elements) {
2041 case 1:
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");
2048 }
2049 break;
2050 case 2:
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");
2057 }
2058 break;
2059 case 3:
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");
2066 }
2067 break;
2068 case 4:
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");
2075 }
2076 break;
2077 default:
2078 unreachable("not reached");
2079 }
2080 break;
2081 case ir_unop_get_buffer_size: {
2082 nir_intrinsic_instr *load = nir_intrinsic_instr_create(
2083 this->shader,
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);
2089 return;
2090 }
2091
2092 case ir_binop_add:
2093 result = type_is_float(out_type) ? nir_fadd(&b, srcs[0], srcs[1])
2094 : nir_iadd(&b, srcs[0], srcs[1]);
2095 break;
2096 case ir_binop_sub:
2097 result = type_is_float(out_type) ? nir_fsub(&b, srcs[0], srcs[1])
2098 : nir_isub(&b, srcs[0], srcs[1]);
2099 break;
2100 case ir_binop_mul:
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]);
2111 else
2112 result = nir_imul(&b, srcs[0], srcs[1]);
2113 break;
2114 case ir_binop_div:
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]);
2119 else
2120 result = nir_udiv(&b, srcs[0], srcs[1]);
2121 break;
2122 case ir_binop_mod:
2123 result = type_is_float(out_type) ? nir_fmod(&b, srcs[0], srcs[1])
2124 : nir_umod(&b, srcs[0], srcs[1]);
2125 break;
2126 case ir_binop_min:
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]);
2131 else
2132 result = nir_umin(&b, srcs[0], srcs[1]);
2133 break;
2134 case ir_binop_max:
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]);
2139 else
2140 result = nir_umax(&b, srcs[0], srcs[1]);
2141 break;
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]);
2148 break;
2149 case ir_binop_logic_or:
2150 result = nir_ior(&b, srcs[0], srcs[1]);
2151 break;
2152 case ir_binop_logic_xor:
2153 result = nir_ixor(&b, srcs[0], srcs[1]);
2154 break;
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]);
2159 break;
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]);
2163 break;
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;
2166 case ir_binop_less:
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]);
2171 else
2172 result = nir_ult(&b, srcs[0], srcs[1]);
2173 break;
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]);
2179 else
2180 result = nir_uge(&b, srcs[0], srcs[1]);
2181 break;
2182 case ir_binop_equal:
2183 if (type_is_float(types[0]))
2184 result = nir_feq(&b, srcs[0], srcs[1]);
2185 else
2186 result = nir_ieq(&b, srcs[0], srcs[1]);
2187 break;
2188 case ir_binop_nequal:
2189 if (type_is_float(types[0]))
2190 result = nir_fne(&b, srcs[0], srcs[1]);
2191 else
2192 result = nir_ine(&b, srcs[0], srcs[1]);
2193 break;
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;
2201 default:
2202 unreachable("not reached");
2203 }
2204 } else {
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;
2210 default:
2211 unreachable("not reached");
2212 }
2213 }
2214 break;
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;
2222 default:
2223 unreachable("not reached");
2224 }
2225 } else {
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;
2231 default:
2232 unreachable("not reached");
2233 }
2234 }
2235 break;
2236 case ir_binop_dot:
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;
2241 default:
2242 unreachable("not reached");
2243 }
2244 break;
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)),
2250 swizzled, result);
2251 }
2252 break;
2253 }
2254
2255 case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
2256 case ir_triop_fma:
2257 result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
2258 break;
2259 case ir_triop_lrp:
2260 result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
2261 break;
2262 case ir_triop_csel:
2263 result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
2264 break;
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]);
2269 break;
2270 case ir_quadop_bitfield_insert:
2271 result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
2272 break;
2273 case ir_quadop_vector:
2274 result = nir_vec(&b, srcs, ir->type->vector_elements);
2275 break;
2276
2277 default:
2278 unreachable("not reached");
2279 }
2280 }
2281
2282 void
2283 nir_visitor::visit(ir_swizzle *ir)
2284 {
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);
2288 }
2289
2290 void
2291 nir_visitor::visit(ir_texture *ir)
2292 {
2293 unsigned num_srcs;
2294 nir_texop op;
2295 switch (ir->op) {
2296 case ir_tex:
2297 op = nir_texop_tex;
2298 num_srcs = 1; /* coordinate */
2299 break;
2300
2301 case ir_txb:
2302 case ir_txl:
2303 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
2304 num_srcs = 2; /* coordinate, bias/lod */
2305 break;
2306
2307 case ir_txd:
2308 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
2309 num_srcs = 3;
2310 break;
2311
2312 case ir_txf:
2313 op = nir_texop_txf;
2314 if (ir->lod_info.lod != NULL)
2315 num_srcs = 2; /* coordinate, lod */
2316 else
2317 num_srcs = 1; /* coordinate */
2318 break;
2319
2320 case ir_txf_ms:
2321 op = nir_texop_txf_ms;
2322 num_srcs = 2; /* coordinate, sample_index */
2323 break;
2324
2325 case ir_txs:
2326 op = nir_texop_txs;
2327 if (ir->lod_info.lod != NULL)
2328 num_srcs = 1; /* lod */
2329 else
2330 num_srcs = 0;
2331 break;
2332
2333 case ir_lod:
2334 op = nir_texop_lod;
2335 num_srcs = 1; /* coordinate */
2336 break;
2337
2338 case ir_tg4:
2339 op = nir_texop_tg4;
2340 num_srcs = 1; /* coordinate */
2341 break;
2342
2343 case ir_query_levels:
2344 op = nir_texop_query_levels;
2345 num_srcs = 0;
2346 break;
2347
2348 case ir_texture_samples:
2349 op = nir_texop_texture_samples;
2350 num_srcs = 0;
2351 break;
2352
2353 case ir_samples_identical:
2354 op = nir_texop_samples_identical;
2355 num_srcs = 1; /* coordinate */
2356 break;
2357
2358 default:
2359 unreachable("not reached");
2360 }
2361
2362 if (ir->projector != NULL)
2363 num_srcs++;
2364 if (ir->shadow_comparator != NULL)
2365 num_srcs++;
2366 /* offsets are constants we store inside nir_tex_intrs.offsets */
2367 if (ir->offset != NULL && !ir->offset->type->is_array())
2368 num_srcs++;
2369
2370 /* Add one for the texture deref */
2371 num_srcs += 2;
2372
2373 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
2374
2375 instr->op = op;
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;
2385 break;
2386 case GLSL_TYPE_INT:
2387 instr->dest_type = nir_type_int;
2388 break;
2389 case GLSL_TYPE_BOOL:
2390 case GLSL_TYPE_UINT:
2391 instr->dest_type = nir_type_uint;
2392 break;
2393 default:
2394 unreachable("not reached");
2395 }
2396
2397 nir_deref_instr *sampler_deref = evaluate_deref(ir->sampler);
2398
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;
2407 } else {
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;
2412 }
2413
2414 unsigned src_number = 2;
2415
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;
2421 src_number++;
2422 }
2423
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;
2428 src_number++;
2429 }
2430
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;
2435 src_number++;
2436 }
2437
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);
2443
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;
2448 }
2449 }
2450 } else {
2451 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
2452
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;
2456 src_number++;
2457 }
2458 }
2459
2460 switch (ir->op) {
2461 case ir_txb:
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;
2465 src_number++;
2466 break;
2467
2468 case ir_txl:
2469 case ir_txf:
2470 case ir_txs:
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;
2475 src_number++;
2476 }
2477 break;
2478
2479 case ir_txd:
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;
2483 src_number++;
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;
2487 src_number++;
2488 break;
2489
2490 case ir_txf_ms:
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;
2494 src_number++;
2495 break;
2496
2497 case ir_tg4:
2498 instr->component = ir->lod_info.component->as_constant()->value.u[0];
2499 break;
2500
2501 default:
2502 break;
2503 }
2504
2505 assert(src_number == num_srcs);
2506
2507 unsigned bit_size = glsl_get_bit_size(ir->type);
2508 add_instr(&instr->instr, nir_tex_instr_dest_size(instr), bit_size);
2509 }
2510
2511 void
2512 nir_visitor::visit(ir_constant *ir)
2513 {
2514 /*
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.
2518 */
2519
2520 nir_variable *var =
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);
2524
2525 this->deref = nir_build_deref_var(&b, var);
2526 }
2527
2528 void
2529 nir_visitor::visit(ir_dereference_variable *ir)
2530 {
2531 if (ir->variable_referenced()->data.mode == ir_var_function_out) {
2532 unsigned i = (sig->return_type != glsl_type::void_type) ? 1 : 0;
2533
2534 foreach_in_list(ir_variable, param, &sig->parameters) {
2535 if (param == ir->variable_referenced()) {
2536 break;
2537 }
2538 i++;
2539 }
2540
2541 this->deref = nir_build_deref_cast(&b, nir_load_param(&b, i),
2542 nir_var_function_temp, ir->type, 0);
2543 return;
2544 }
2545
2546 assert(ir->variable_referenced()->data.mode != ir_var_function_inout);
2547
2548 struct hash_entry *entry =
2549 _mesa_hash_table_search(this->var_table, ir->var);
2550 assert(entry);
2551 nir_variable *var = (nir_variable *) entry->data;
2552
2553 this->deref = nir_build_deref_var(&b, var);
2554 }
2555
2556 void
2557 nir_visitor::visit(ir_dereference_record *ir)
2558 {
2559 ir->record->accept(this);
2560
2561 int field_index = ir->field_idx;
2562 assert(field_index >= 0);
2563
2564 this->deref = nir_build_deref_struct(&b, this->deref, field_index);
2565 }
2566
2567 void
2568 nir_visitor::visit(ir_dereference_array *ir)
2569 {
2570 nir_ssa_def *index = evaluate_rvalue(ir->array_index);
2571
2572 ir->array->accept(this);
2573
2574 this->deref = nir_build_deref_array(&b, this->deref, index);
2575 }
2576
2577 void
2578 nir_visitor::visit(ir_barrier *)
2579 {
2580 nir_intrinsic_instr *instr =
2581 nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
2582 nir_builder_instr_insert(&b, &instr->instr);
2583 }
2584
2585 nir_shader *
2586 glsl_float64_funcs_to_nir(struct gl_context *ctx,
2587 const nir_shader_compiler_options *options)
2588 {
2589 /* We pretend it's a vertex shader. Ultimately, the stage shouldn't
2590 * matter because we're not optimizing anything here.
2591 */
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);
2596
2597 if (!sh->CompileStatus) {
2598 if (sh->InfoLog) {
2599 _mesa_problem(ctx,
2600 "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
2601 sh->InfoLog, float64_source);
2602 }
2603 return NULL;
2604 }
2605
2606 nir_shader *nir = nir_shader_create(NULL, MESA_SHADER_VERTEX, options, NULL);
2607
2608 nir_visitor v1(ctx, nir);
2609 nir_function_visitor v2(&v1);
2610 v2.run(sh->ir);
2611 visit_exec_list(sh->ir, &v1);
2612
2613 /* _mesa_delete_shader will try to free sh->Source but it's static const */
2614 sh->Source = NULL;
2615 _mesa_delete_shader(ctx, sh);
2616
2617 nir_validate_shader(nir, "float64_funcs_to_nir");
2618
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);
2623
2624 return nir;
2625 }