nir/glsl_to_nir: use _mesa_fls() to compute num_textures
[mesa.git] / src / glsl / nir / 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 "glsl_to_nir.h"
29 #include "nir_control_flow.h"
30 #include "nir_builder.h"
31 #include "ir_visitor.h"
32 #include "ir_hierarchical_visitor.h"
33 #include "ir.h"
34 #include "main/imports.h"
35
36 /*
37 * pass to lower GLSL IR to NIR
38 *
39 * This will lower variable dereferences to loads/stores of corresponding
40 * variables in NIR - the variables will be converted to registers in a later
41 * pass.
42 */
43
44 namespace {
45
46 class nir_visitor : public ir_visitor
47 {
48 public:
49 nir_visitor(nir_shader *shader);
50 ~nir_visitor();
51
52 virtual void visit(ir_variable *);
53 virtual void visit(ir_function *);
54 virtual void visit(ir_function_signature *);
55 virtual void visit(ir_loop *);
56 virtual void visit(ir_if *);
57 virtual void visit(ir_discard *);
58 virtual void visit(ir_loop_jump *);
59 virtual void visit(ir_return *);
60 virtual void visit(ir_call *);
61 virtual void visit(ir_assignment *);
62 virtual void visit(ir_emit_vertex *);
63 virtual void visit(ir_end_primitive *);
64 virtual void visit(ir_expression *);
65 virtual void visit(ir_swizzle *);
66 virtual void visit(ir_texture *);
67 virtual void visit(ir_constant *);
68 virtual void visit(ir_dereference_variable *);
69 virtual void visit(ir_dereference_record *);
70 virtual void visit(ir_dereference_array *);
71 virtual void visit(ir_barrier *);
72
73 void create_function(ir_function *ir);
74
75 private:
76 void create_overload(ir_function_signature *ir, nir_function *function);
77 void add_instr(nir_instr *instr, unsigned num_components);
78 nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
79
80 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def **srcs);
81 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1);
82 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
83 nir_ssa_def *src2);
84 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
85 nir_ssa_def *src2, nir_ssa_def *src3);
86
87 bool supports_ints;
88
89 nir_shader *shader;
90 nir_function_impl *impl;
91 nir_builder b;
92 nir_ssa_def *result; /* result of the expression tree last visited */
93
94 nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
95
96 /* the head of the dereference chain we're creating */
97 nir_deref_var *deref_head;
98 /* the tail of the dereference chain we're creating */
99 nir_deref *deref_tail;
100
101 nir_variable *var; /* variable created by ir_variable visitor */
102
103 /* whether the IR we're operating on is per-function or global */
104 bool is_global;
105
106 /* map of ir_variable -> nir_variable */
107 struct hash_table *var_table;
108
109 /* map of ir_function_signature -> nir_function_overload */
110 struct hash_table *overload_table;
111 };
112
113 /*
114 * This visitor runs before the main visitor, calling create_function() for
115 * each function so that the main visitor can resolve forward references in
116 * calls.
117 */
118
119 class nir_function_visitor : public ir_hierarchical_visitor
120 {
121 public:
122 nir_function_visitor(nir_visitor *v) : visitor(v)
123 {
124 }
125 virtual ir_visitor_status visit_enter(ir_function *);
126
127 private:
128 nir_visitor *visitor;
129 };
130
131 }; /* end of anonymous namespace */
132
133 nir_shader *
134 glsl_to_nir(const struct gl_shader_program *shader_prog,
135 gl_shader_stage stage,
136 const nir_shader_compiler_options *options)
137 {
138 struct gl_shader *sh = shader_prog->_LinkedShaders[stage];
139
140 nir_shader *shader = nir_shader_create(NULL, stage, options);
141
142 nir_visitor v1(shader);
143 nir_function_visitor v2(&v1);
144 v2.run(sh->ir);
145 visit_exec_list(sh->ir, &v1);
146
147 nir_lower_outputs_to_temporaries(shader);
148
149 shader->info.name = ralloc_asprintf(shader, "GLSL%d", shader_prog->Name);
150 if (shader_prog->Label)
151 shader->info.label = ralloc_strdup(shader, shader_prog->Label);
152 shader->info.num_textures = _mesa_fls(sh->Program->SamplersUsed);
153 shader->info.num_ubos = sh->NumUniformBlocks;
154 shader->info.num_abos = shader_prog->NumAtomicBuffers;
155 shader->info.num_ssbos = sh->NumShaderStorageBlocks;
156 shader->info.num_images = sh->NumImages;
157 shader->info.inputs_read = sh->Program->InputsRead;
158 shader->info.outputs_written = sh->Program->OutputsWritten;
159 shader->info.patch_inputs_read = sh->Program->PatchInputsRead;
160 shader->info.patch_outputs_written = sh->Program->PatchOutputsWritten;
161 shader->info.system_values_read = sh->Program->SystemValuesRead;
162 shader->info.uses_texture_gather = sh->Program->UsesGather;
163 shader->info.uses_clip_distance_out =
164 sh->Program->ClipDistanceArraySize != 0;
165 shader->info.separate_shader = shader_prog->SeparateShader;
166 shader->info.has_transform_feedback_varyings =
167 shader_prog->TransformFeedback.NumVarying > 0;
168
169 switch (stage) {
170 case MESA_SHADER_GEOMETRY:
171 shader->info.gs.vertices_in = shader_prog->Geom.VerticesIn;
172 shader->info.gs.output_primitive = sh->Geom.OutputType;
173 shader->info.gs.vertices_out = sh->Geom.VerticesOut;
174 shader->info.gs.invocations = sh->Geom.Invocations;
175 shader->info.gs.uses_end_primitive = shader_prog->Geom.UsesEndPrimitive;
176 shader->info.gs.uses_streams = shader_prog->Geom.UsesStreams;
177 break;
178
179 case MESA_SHADER_FRAGMENT: {
180 struct gl_fragment_program *fp =
181 (struct gl_fragment_program *)sh->Program;
182
183 shader->info.fs.uses_discard = fp->UsesKill;
184 shader->info.fs.early_fragment_tests = sh->EarlyFragmentTests;
185 shader->info.fs.depth_layout = fp->FragDepthLayout;
186 break;
187 }
188
189 case MESA_SHADER_COMPUTE: {
190 struct gl_compute_program *cp = (struct gl_compute_program *)sh->Program;
191 shader->info.cs.local_size[0] = cp->LocalSize[0];
192 shader->info.cs.local_size[1] = cp->LocalSize[1];
193 shader->info.cs.local_size[2] = cp->LocalSize[2];
194 break;
195 }
196
197 default:
198 break; /* No stage-specific info */
199 }
200
201 return shader;
202 }
203
204 nir_visitor::nir_visitor(nir_shader *shader)
205 {
206 this->supports_ints = shader->options->native_integers;
207 this->shader = shader;
208 this->is_global = true;
209 this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
210 _mesa_key_pointer_equal);
211 this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
212 _mesa_key_pointer_equal);
213 }
214
215 nir_visitor::~nir_visitor()
216 {
217 _mesa_hash_table_destroy(this->var_table, NULL);
218 _mesa_hash_table_destroy(this->overload_table, NULL);
219 }
220
221 nir_deref_var *
222 nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
223 {
224 ir->accept(this);
225 ralloc_steal(mem_ctx, this->deref_head);
226 return this->deref_head;
227 }
228
229 static nir_constant *
230 constant_copy(ir_constant *ir, void *mem_ctx)
231 {
232 if (ir == NULL)
233 return NULL;
234
235 nir_constant *ret = ralloc(mem_ctx, nir_constant);
236
237 unsigned total_elems = ir->type->components();
238 unsigned i;
239 switch (ir->type->base_type) {
240 case GLSL_TYPE_UINT:
241 for (i = 0; i < total_elems; i++)
242 ret->value.u[i] = ir->value.u[i];
243 break;
244
245 case GLSL_TYPE_INT:
246 for (i = 0; i < total_elems; i++)
247 ret->value.i[i] = ir->value.i[i];
248 break;
249
250 case GLSL_TYPE_FLOAT:
251 for (i = 0; i < total_elems; i++)
252 ret->value.f[i] = ir->value.f[i];
253 break;
254
255 case GLSL_TYPE_BOOL:
256 for (i = 0; i < total_elems; i++)
257 ret->value.b[i] = ir->value.b[i];
258 break;
259
260 case GLSL_TYPE_STRUCT:
261 ret->elements = ralloc_array(mem_ctx, nir_constant *,
262 ir->type->length);
263 i = 0;
264 foreach_in_list(ir_constant, field, &ir->components) {
265 ret->elements[i] = constant_copy(field, mem_ctx);
266 i++;
267 }
268 break;
269
270 case GLSL_TYPE_ARRAY:
271 ret->elements = ralloc_array(mem_ctx, nir_constant *,
272 ir->type->length);
273
274 for (i = 0; i < ir->type->length; i++)
275 ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
276 break;
277
278 default:
279 unreachable("not reached");
280 }
281
282 return ret;
283 }
284
285 void
286 nir_visitor::visit(ir_variable *ir)
287 {
288 nir_variable *var = ralloc(shader, nir_variable);
289 var->type = ir->type;
290 var->name = ralloc_strdup(var, ir->name);
291
292 if (ir->is_interface_instance() && ir->get_max_ifc_array_access() != NULL) {
293 unsigned size = ir->get_interface_type()->length;
294 var->max_ifc_array_access = ralloc_array(var, unsigned, size);
295 memcpy(var->max_ifc_array_access, ir->get_max_ifc_array_access(),
296 size * sizeof(unsigned));
297 } else {
298 var->max_ifc_array_access = NULL;
299 }
300
301 var->data.read_only = ir->data.read_only;
302 var->data.centroid = ir->data.centroid;
303 var->data.sample = ir->data.sample;
304 var->data.patch = ir->data.patch;
305 var->data.invariant = ir->data.invariant;
306 var->data.location = ir->data.location;
307
308 switch(ir->data.mode) {
309 case ir_var_auto:
310 case ir_var_temporary:
311 if (is_global)
312 var->data.mode = nir_var_global;
313 else
314 var->data.mode = nir_var_local;
315 break;
316
317 case ir_var_function_in:
318 case ir_var_function_out:
319 case ir_var_function_inout:
320 case ir_var_const_in:
321 var->data.mode = nir_var_local;
322 break;
323
324 case ir_var_shader_in:
325 if (shader->stage == MESA_SHADER_FRAGMENT &&
326 ir->data.location == VARYING_SLOT_FACE) {
327 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
328 var->data.location = SYSTEM_VALUE_FRONT_FACE;
329 var->data.mode = nir_var_system_value;
330 } else if (shader->stage == MESA_SHADER_GEOMETRY &&
331 ir->data.location == VARYING_SLOT_PRIMITIVE_ID) {
332 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
333 var->data.location = SYSTEM_VALUE_PRIMITIVE_ID;
334 var->data.mode = nir_var_system_value;
335 } else {
336 var->data.mode = nir_var_shader_in;
337 }
338 break;
339
340 case ir_var_shader_out:
341 var->data.mode = nir_var_shader_out;
342 break;
343
344 case ir_var_uniform:
345 var->data.mode = nir_var_uniform;
346 break;
347
348 case ir_var_shader_storage:
349 var->data.mode = nir_var_shader_storage;
350 break;
351
352 case ir_var_system_value:
353 var->data.mode = nir_var_system_value;
354 break;
355
356 default:
357 unreachable("not reached");
358 }
359
360 var->data.interpolation = ir->data.interpolation;
361 var->data.origin_upper_left = ir->data.origin_upper_left;
362 var->data.pixel_center_integer = ir->data.pixel_center_integer;
363 var->data.explicit_location = ir->data.explicit_location;
364 var->data.explicit_index = ir->data.explicit_index;
365 var->data.explicit_binding = ir->data.explicit_binding;
366 var->data.has_initializer = ir->data.has_initializer;
367 var->data.is_unmatched_generic_inout = ir->data.is_unmatched_generic_inout;
368 var->data.location_frac = ir->data.location_frac;
369 var->data.from_named_ifc_block_array = ir->data.from_named_ifc_block_array;
370 var->data.from_named_ifc_block_nonarray = ir->data.from_named_ifc_block_nonarray;
371
372 switch (ir->data.depth_layout) {
373 case ir_depth_layout_none:
374 var->data.depth_layout = nir_depth_layout_none;
375 break;
376 case ir_depth_layout_any:
377 var->data.depth_layout = nir_depth_layout_any;
378 break;
379 case ir_depth_layout_greater:
380 var->data.depth_layout = nir_depth_layout_greater;
381 break;
382 case ir_depth_layout_less:
383 var->data.depth_layout = nir_depth_layout_less;
384 break;
385 case ir_depth_layout_unchanged:
386 var->data.depth_layout = nir_depth_layout_unchanged;
387 break;
388 default:
389 unreachable("not reached");
390 }
391
392 var->data.index = ir->data.index;
393 var->data.binding = ir->data.binding;
394 var->data.atomic.offset = ir->data.atomic.offset;
395 var->data.image.read_only = ir->data.image_read_only;
396 var->data.image.write_only = ir->data.image_write_only;
397 var->data.image.coherent = ir->data.image_coherent;
398 var->data.image._volatile = ir->data.image_volatile;
399 var->data.image.restrict_flag = ir->data.image_restrict;
400 var->data.image.format = ir->data.image_format;
401 var->data.max_array_access = ir->data.max_array_access;
402
403 var->num_state_slots = ir->get_num_state_slots();
404 if (var->num_state_slots > 0) {
405 var->state_slots = ralloc_array(var, nir_state_slot,
406 var->num_state_slots);
407
408 ir_state_slot *state_slots = ir->get_state_slots();
409 for (unsigned i = 0; i < var->num_state_slots; i++) {
410 for (unsigned j = 0; j < 5; j++)
411 var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
412 var->state_slots[i].swizzle = state_slots[i].swizzle;
413 }
414 } else {
415 var->state_slots = NULL;
416 }
417
418 var->constant_initializer = constant_copy(ir->constant_initializer, var);
419
420 var->interface_type = ir->get_interface_type();
421
422 if (var->data.mode == nir_var_local)
423 nir_function_impl_add_variable(impl, var);
424 else
425 nir_shader_add_variable(shader, var);
426
427 _mesa_hash_table_insert(var_table, ir, var);
428 this->var = var;
429 }
430
431 ir_visitor_status
432 nir_function_visitor::visit_enter(ir_function *ir)
433 {
434 visitor->create_function(ir);
435 return visit_continue_with_parent;
436 }
437
438
439 void
440 nir_visitor::create_function(ir_function *ir)
441 {
442 nir_function *func = nir_function_create(this->shader, ir->name);
443 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
444 create_overload(sig, func);
445 }
446 }
447
448
449
450 void
451 nir_visitor::create_overload(ir_function_signature *ir, nir_function *function)
452 {
453 if (ir->is_intrinsic)
454 return;
455
456 nir_function_overload *overload = nir_function_overload_create(function);
457
458 unsigned num_params = ir->parameters.length();
459 overload->num_params = num_params;
460 overload->params = ralloc_array(shader, nir_parameter, num_params);
461
462 unsigned i = 0;
463 foreach_in_list(ir_variable, param, &ir->parameters) {
464 switch (param->data.mode) {
465 case ir_var_function_in:
466 overload->params[i].param_type = nir_parameter_in;
467 break;
468
469 case ir_var_function_out:
470 overload->params[i].param_type = nir_parameter_out;
471 break;
472
473 case ir_var_function_inout:
474 overload->params[i].param_type = nir_parameter_inout;
475 break;
476
477 default:
478 unreachable("not reached");
479 }
480
481 overload->params[i].type = param->type;
482 i++;
483 }
484
485 overload->return_type = ir->return_type;
486
487 _mesa_hash_table_insert(this->overload_table, ir, overload);
488 }
489
490 void
491 nir_visitor::visit(ir_function *ir)
492 {
493 foreach_in_list(ir_function_signature, sig, &ir->signatures)
494 sig->accept(this);
495 }
496
497 void
498 nir_visitor::visit(ir_function_signature *ir)
499 {
500 if (ir->is_intrinsic)
501 return;
502
503 struct hash_entry *entry =
504 _mesa_hash_table_search(this->overload_table, ir);
505
506 assert(entry);
507 nir_function_overload *overload = (nir_function_overload *) entry->data;
508
509 if (ir->is_defined) {
510 nir_function_impl *impl = nir_function_impl_create(overload);
511 this->impl = impl;
512
513 unsigned num_params = overload->num_params;
514 impl->num_params = num_params;
515 impl->params = ralloc_array(this->shader, nir_variable *, num_params);
516 unsigned i = 0;
517 foreach_in_list(ir_variable, param, &ir->parameters) {
518 param->accept(this);
519 impl->params[i] = this->var;
520 i++;
521 }
522
523 if (overload->return_type == glsl_type::void_type) {
524 impl->return_var = NULL;
525 } else {
526 impl->return_var = ralloc(this->shader, nir_variable);
527 impl->return_var->name = ralloc_strdup(impl->return_var,
528 "return_var");
529 impl->return_var->type = overload->return_type;
530 }
531
532 this->is_global = false;
533
534 nir_builder_init(&b, impl);
535 b.cursor = nir_after_cf_list(&impl->body);
536 visit_exec_list(&ir->body, this);
537
538 this->is_global = true;
539 } else {
540 overload->impl = NULL;
541 }
542 }
543
544 void
545 nir_visitor::visit(ir_loop *ir)
546 {
547 nir_loop *loop = nir_loop_create(this->shader);
548 nir_builder_cf_insert(&b, &loop->cf_node);
549
550 b.cursor = nir_after_cf_list(&loop->body);
551 visit_exec_list(&ir->body_instructions, this);
552 b.cursor = nir_after_cf_node(&loop->cf_node);
553 }
554
555 void
556 nir_visitor::visit(ir_if *ir)
557 {
558 nir_src condition =
559 nir_src_for_ssa(evaluate_rvalue(ir->condition));
560
561 nir_if *if_stmt = nir_if_create(this->shader);
562 if_stmt->condition = condition;
563 nir_builder_cf_insert(&b, &if_stmt->cf_node);
564
565 b.cursor = nir_after_cf_list(&if_stmt->then_list);
566 visit_exec_list(&ir->then_instructions, this);
567
568 b.cursor = nir_after_cf_list(&if_stmt->else_list);
569 visit_exec_list(&ir->else_instructions, this);
570
571 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
572 }
573
574 void
575 nir_visitor::visit(ir_discard *ir)
576 {
577 /*
578 * discards aren't treated as control flow, because before we lower them
579 * they can appear anywhere in the shader and the stuff after them may still
580 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
581 * discards will be immediately followed by a return.
582 */
583
584 nir_intrinsic_instr *discard;
585 if (ir->condition) {
586 discard = nir_intrinsic_instr_create(this->shader,
587 nir_intrinsic_discard_if);
588 discard->src[0] =
589 nir_src_for_ssa(evaluate_rvalue(ir->condition));
590 } else {
591 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
592 }
593
594 nir_builder_instr_insert(&b, &discard->instr);
595 }
596
597 void
598 nir_visitor::visit(ir_emit_vertex *ir)
599 {
600 nir_intrinsic_instr *instr =
601 nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
602 instr->const_index[0] = ir->stream_id();
603 nir_builder_instr_insert(&b, &instr->instr);
604 }
605
606 void
607 nir_visitor::visit(ir_end_primitive *ir)
608 {
609 nir_intrinsic_instr *instr =
610 nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
611 instr->const_index[0] = ir->stream_id();
612 nir_builder_instr_insert(&b, &instr->instr);
613 }
614
615 void
616 nir_visitor::visit(ir_loop_jump *ir)
617 {
618 nir_jump_type type;
619 switch (ir->mode) {
620 case ir_loop_jump::jump_break:
621 type = nir_jump_break;
622 break;
623 case ir_loop_jump::jump_continue:
624 type = nir_jump_continue;
625 break;
626 default:
627 unreachable("not reached");
628 }
629
630 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
631 nir_builder_instr_insert(&b, &instr->instr);
632 }
633
634 void
635 nir_visitor::visit(ir_return *ir)
636 {
637 if (ir->value != NULL) {
638 nir_intrinsic_instr *copy =
639 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
640
641 copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
642 copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
643 }
644
645 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
646 nir_builder_instr_insert(&b, &instr->instr);
647 }
648
649 void
650 nir_visitor::visit(ir_call *ir)
651 {
652 if (ir->callee->is_intrinsic) {
653 nir_intrinsic_op op;
654 if (strcmp(ir->callee_name(), "__intrinsic_atomic_read") == 0) {
655 op = nir_intrinsic_atomic_counter_read_var;
656 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_increment") == 0) {
657 op = nir_intrinsic_atomic_counter_inc_var;
658 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
659 op = nir_intrinsic_atomic_counter_dec_var;
660 } else if (strcmp(ir->callee_name(), "__intrinsic_image_load") == 0) {
661 op = nir_intrinsic_image_load;
662 } else if (strcmp(ir->callee_name(), "__intrinsic_image_store") == 0) {
663 op = nir_intrinsic_image_store;
664 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_add") == 0) {
665 op = nir_intrinsic_image_atomic_add;
666 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_min") == 0) {
667 op = nir_intrinsic_image_atomic_min;
668 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_max") == 0) {
669 op = nir_intrinsic_image_atomic_max;
670 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_and") == 0) {
671 op = nir_intrinsic_image_atomic_and;
672 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_or") == 0) {
673 op = nir_intrinsic_image_atomic_or;
674 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
675 op = nir_intrinsic_image_atomic_xor;
676 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
677 op = nir_intrinsic_image_atomic_exchange;
678 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
679 op = nir_intrinsic_image_atomic_comp_swap;
680 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier") == 0) {
681 op = nir_intrinsic_memory_barrier;
682 } else if (strcmp(ir->callee_name(), "__intrinsic_image_size") == 0) {
683 op = nir_intrinsic_image_size;
684 } else if (strcmp(ir->callee_name(), "__intrinsic_image_samples") == 0) {
685 op = nir_intrinsic_image_samples;
686 } else if (strcmp(ir->callee_name(), "__intrinsic_store_ssbo") == 0) {
687 op = nir_intrinsic_store_ssbo;
688 } else if (strcmp(ir->callee_name(), "__intrinsic_load_ssbo") == 0) {
689 op = nir_intrinsic_load_ssbo;
690 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_add_internal") == 0) {
691 op = nir_intrinsic_ssbo_atomic_add;
692 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_and_internal") == 0) {
693 op = nir_intrinsic_ssbo_atomic_and;
694 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_or_internal") == 0) {
695 op = nir_intrinsic_ssbo_atomic_or;
696 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_xor_internal") == 0) {
697 op = nir_intrinsic_ssbo_atomic_xor;
698 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_min_internal") == 0) {
699 assert(ir->return_deref);
700 if (ir->return_deref->type == glsl_type::int_type)
701 op = nir_intrinsic_ssbo_atomic_imin;
702 else if (ir->return_deref->type == glsl_type::uint_type)
703 op = nir_intrinsic_ssbo_atomic_umin;
704 else
705 unreachable("Invalid type");
706 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_max_internal") == 0) {
707 assert(ir->return_deref);
708 if (ir->return_deref->type == glsl_type::int_type)
709 op = nir_intrinsic_ssbo_atomic_imax;
710 else if (ir->return_deref->type == glsl_type::uint_type)
711 op = nir_intrinsic_ssbo_atomic_umax;
712 else
713 unreachable("Invalid type");
714 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_exchange_internal") == 0) {
715 op = nir_intrinsic_ssbo_atomic_exchange;
716 } else if (strcmp(ir->callee_name(), "__intrinsic_ssbo_atomic_comp_swap_internal") == 0) {
717 op = nir_intrinsic_ssbo_atomic_comp_swap;
718 } else if (strcmp(ir->callee_name(), "__intrinsic_shader_clock") == 0) {
719 op = nir_intrinsic_shader_clock;
720 } else if (strcmp(ir->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
721 op = nir_intrinsic_group_memory_barrier;
722 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
723 op = nir_intrinsic_memory_barrier_atomic_counter;
724 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
725 op = nir_intrinsic_memory_barrier_buffer;
726 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
727 op = nir_intrinsic_memory_barrier_image;
728 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
729 op = nir_intrinsic_memory_barrier_shared;
730 } else {
731 unreachable("not reached");
732 }
733
734 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
735 nir_dest *dest = &instr->dest;
736
737 switch (op) {
738 case nir_intrinsic_atomic_counter_read_var:
739 case nir_intrinsic_atomic_counter_inc_var:
740 case nir_intrinsic_atomic_counter_dec_var: {
741 ir_dereference *param =
742 (ir_dereference *) ir->actual_parameters.get_head();
743 instr->variables[0] = evaluate_deref(&instr->instr, param);
744 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
745 nir_builder_instr_insert(&b, &instr->instr);
746 break;
747 }
748 case nir_intrinsic_image_load:
749 case nir_intrinsic_image_store:
750 case nir_intrinsic_image_atomic_add:
751 case nir_intrinsic_image_atomic_min:
752 case nir_intrinsic_image_atomic_max:
753 case nir_intrinsic_image_atomic_and:
754 case nir_intrinsic_image_atomic_or:
755 case nir_intrinsic_image_atomic_xor:
756 case nir_intrinsic_image_atomic_exchange:
757 case nir_intrinsic_image_atomic_comp_swap:
758 case nir_intrinsic_image_samples:
759 case nir_intrinsic_image_size: {
760 nir_ssa_undef_instr *instr_undef =
761 nir_ssa_undef_instr_create(shader, 1);
762 nir_builder_instr_insert(&b, &instr_undef->instr);
763
764 /* Set the image variable dereference. */
765 exec_node *param = ir->actual_parameters.get_head();
766 ir_dereference *image = (ir_dereference *)param;
767 const glsl_type *type =
768 image->variable_referenced()->type->without_array();
769
770 instr->variables[0] = evaluate_deref(&instr->instr, image);
771 param = param->get_next();
772
773 /* Set the intrinsic destination. */
774 if (ir->return_deref) {
775 const nir_intrinsic_info *info =
776 &nir_intrinsic_infos[instr->intrinsic];
777 nir_ssa_dest_init(&instr->instr, &instr->dest,
778 info->dest_components, NULL);
779 }
780
781 if (op == nir_intrinsic_image_size ||
782 op == nir_intrinsic_image_samples) {
783 nir_builder_instr_insert(&b, &instr->instr);
784 break;
785 }
786
787 /* Set the address argument, extending the coordinate vector to four
788 * components.
789 */
790 nir_ssa_def *src_addr =
791 evaluate_rvalue((ir_dereference *)param);
792 nir_ssa_def *srcs[4];
793
794 for (int i = 0; i < 4; i++) {
795 if (i < type->coordinate_components())
796 srcs[i] = nir_channel(&b, src_addr, i);
797 else
798 srcs[i] = &instr_undef->def;
799 }
800
801 instr->src[0] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
802 param = param->get_next();
803
804 /* Set the sample argument, which is undefined for single-sample
805 * images.
806 */
807 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
808 instr->src[1] =
809 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
810 param = param->get_next();
811 } else {
812 instr->src[1] = nir_src_for_ssa(&instr_undef->def);
813 }
814
815 /* Set the intrinsic parameters. */
816 if (!param->is_tail_sentinel()) {
817 instr->src[2] =
818 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
819 param = param->get_next();
820 }
821
822 if (!param->is_tail_sentinel()) {
823 instr->src[3] =
824 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
825 param = param->get_next();
826 }
827 nir_builder_instr_insert(&b, &instr->instr);
828 break;
829 }
830 case nir_intrinsic_memory_barrier:
831 case nir_intrinsic_group_memory_barrier:
832 case nir_intrinsic_memory_barrier_atomic_counter:
833 case nir_intrinsic_memory_barrier_buffer:
834 case nir_intrinsic_memory_barrier_image:
835 case nir_intrinsic_memory_barrier_shared:
836 nir_builder_instr_insert(&b, &instr->instr);
837 break;
838 case nir_intrinsic_shader_clock:
839 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
840 nir_builder_instr_insert(&b, &instr->instr);
841 break;
842 case nir_intrinsic_store_ssbo: {
843 exec_node *param = ir->actual_parameters.get_head();
844 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
845
846 param = param->get_next();
847 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
848
849 param = param->get_next();
850 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
851
852 param = param->get_next();
853 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
854 assert(write_mask);
855
856 /* Check if we need the indirect version */
857 ir_constant *const_offset = offset->as_constant();
858 if (!const_offset) {
859 op = nir_intrinsic_store_ssbo_indirect;
860 ralloc_free(instr);
861 instr = nir_intrinsic_instr_create(shader, op);
862 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(offset));
863 instr->const_index[0] = 0;
864 } else {
865 instr->const_index[0] = const_offset->value.u[0];
866 }
867
868 instr->const_index[1] = write_mask->value.u[0];
869
870 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
871 instr->num_components = val->type->vector_elements;
872
873 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(block));
874 nir_builder_instr_insert(&b, &instr->instr);
875 break;
876 }
877 case nir_intrinsic_load_ssbo: {
878 exec_node *param = ir->actual_parameters.get_head();
879 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
880
881 param = param->get_next();
882 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
883
884 /* Check if we need the indirect version */
885 ir_constant *const_offset = offset->as_constant();
886 if (!const_offset) {
887 op = nir_intrinsic_load_ssbo_indirect;
888 ralloc_free(instr);
889 instr = nir_intrinsic_instr_create(shader, op);
890 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
891 instr->const_index[0] = 0;
892 dest = &instr->dest;
893 } else {
894 instr->const_index[0] = const_offset->value.u[0];
895 }
896
897 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(block));
898
899 const glsl_type *type = ir->return_deref->var->type;
900 instr->num_components = type->vector_elements;
901
902 /* Setup destination register */
903 nir_ssa_dest_init(&instr->instr, &instr->dest,
904 type->vector_elements, NULL);
905
906 /* Insert the created nir instruction now since in the case of boolean
907 * result we will need to emit another instruction after it
908 */
909 nir_builder_instr_insert(&b, &instr->instr);
910
911 /*
912 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
913 * consider a true boolean to be ~0. Fix this up with a != 0
914 * comparison.
915 */
916 if (type->base_type == GLSL_TYPE_BOOL) {
917 nir_alu_instr *load_ssbo_compare =
918 nir_alu_instr_create(shader, nir_op_ine);
919 load_ssbo_compare->src[0].src.is_ssa = true;
920 load_ssbo_compare->src[0].src.ssa = &instr->dest.ssa;
921 load_ssbo_compare->src[1].src =
922 nir_src_for_ssa(nir_imm_int(&b, 0));
923 for (unsigned i = 0; i < type->vector_elements; i++)
924 load_ssbo_compare->src[1].swizzle[i] = 0;
925 nir_ssa_dest_init(&load_ssbo_compare->instr,
926 &load_ssbo_compare->dest.dest,
927 type->vector_elements, NULL);
928 load_ssbo_compare->dest.write_mask = (1 << type->vector_elements) - 1;
929 nir_builder_instr_insert(&b, &load_ssbo_compare->instr);
930 dest = &load_ssbo_compare->dest.dest;
931 }
932 break;
933 }
934 case nir_intrinsic_ssbo_atomic_add:
935 case nir_intrinsic_ssbo_atomic_imin:
936 case nir_intrinsic_ssbo_atomic_umin:
937 case nir_intrinsic_ssbo_atomic_imax:
938 case nir_intrinsic_ssbo_atomic_umax:
939 case nir_intrinsic_ssbo_atomic_and:
940 case nir_intrinsic_ssbo_atomic_or:
941 case nir_intrinsic_ssbo_atomic_xor:
942 case nir_intrinsic_ssbo_atomic_exchange:
943 case nir_intrinsic_ssbo_atomic_comp_swap: {
944 int param_count = ir->actual_parameters.length();
945 assert(param_count == 3 || param_count == 4);
946
947 /* Block index */
948 exec_node *param = ir->actual_parameters.get_head();
949 ir_instruction *inst = (ir_instruction *) param;
950 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
951
952 /* Offset */
953 param = param->get_next();
954 inst = (ir_instruction *) param;
955 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
956
957 /* data1 parameter (this is always present) */
958 param = param->get_next();
959 inst = (ir_instruction *) param;
960 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
961
962 /* data2 parameter (only with atomic_comp_swap) */
963 if (param_count == 4) {
964 assert(op == nir_intrinsic_ssbo_atomic_comp_swap);
965 param = param->get_next();
966 inst = (ir_instruction *) param;
967 instr->src[3] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
968 }
969
970 /* Atomic result */
971 assert(ir->return_deref);
972 nir_ssa_dest_init(&instr->instr, &instr->dest,
973 ir->return_deref->type->vector_elements, NULL);
974 nir_builder_instr_insert(&b, &instr->instr);
975 break;
976 }
977 default:
978 unreachable("not reached");
979 }
980
981 if (ir->return_deref) {
982 nir_intrinsic_instr *store_instr =
983 nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
984 store_instr->num_components = ir->return_deref->type->vector_elements;
985
986 store_instr->variables[0] =
987 evaluate_deref(&store_instr->instr, ir->return_deref);
988 store_instr->src[0] = nir_src_for_ssa(&dest->ssa);
989
990 nir_builder_instr_insert(&b, &store_instr->instr);
991 }
992
993 return;
994 }
995
996 struct hash_entry *entry =
997 _mesa_hash_table_search(this->overload_table, ir->callee);
998 assert(entry);
999 nir_function_overload *callee = (nir_function_overload *) entry->data;
1000
1001 nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
1002
1003 unsigned i = 0;
1004 foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
1005 instr->params[i] = evaluate_deref(&instr->instr, param);
1006 i++;
1007 }
1008
1009 instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
1010 nir_builder_instr_insert(&b, &instr->instr);
1011 }
1012
1013 void
1014 nir_visitor::visit(ir_assignment *ir)
1015 {
1016 unsigned num_components = ir->lhs->type->vector_elements;
1017
1018 if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
1019 (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
1020 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1021 nir_intrinsic_instr *copy =
1022 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
1023
1024 copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
1025 copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
1026
1027 if (ir->condition) {
1028 nir_if *if_stmt = nir_if_create(this->shader);
1029 if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1030 nir_builder_cf_insert(&b, &if_stmt->cf_node);
1031 nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
1032 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1033 } else {
1034 nir_builder_instr_insert(&b, &copy->instr);
1035 }
1036 return;
1037 }
1038
1039 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
1040
1041 ir->lhs->accept(this);
1042 nir_deref_var *lhs_deref = this->deref_head;
1043 nir_ssa_def *src = evaluate_rvalue(ir->rhs);
1044
1045 if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
1046 /*
1047 * We have no good way to update only part of a variable, so just load
1048 * the LHS and do a vec operation to combine the old with the new, and
1049 * then store it
1050 * back into the LHS. Copy propagation should get rid of the mess.
1051 */
1052
1053 nir_intrinsic_instr *load =
1054 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
1055 load->num_components = ir->lhs->type->vector_elements;
1056 nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
1057 load->variables[0] = lhs_deref;
1058 ralloc_steal(load, load->variables[0]);
1059 nir_builder_instr_insert(&b, &load->instr);
1060
1061 nir_ssa_def *srcs[4];
1062
1063 unsigned component = 0;
1064 for (unsigned i = 0; i < ir->lhs->type->vector_elements; i++) {
1065 if (ir->write_mask & (1 << i)) {
1066 /* GLSL IR will give us the input to the write-masked assignment
1067 * in a single packed vector. So, for example, if the
1068 * writemask is xzw, then we have to swizzle x -> x, y -> z,
1069 * and z -> w and get the y component from the load.
1070 */
1071 srcs[i] = nir_channel(&b, src, component++);
1072 } else {
1073 srcs[i] = nir_channel(&b, &load->dest.ssa, i);
1074 }
1075 }
1076
1077 src = nir_vec(&b, srcs, ir->lhs->type->vector_elements);
1078 }
1079
1080 nir_intrinsic_instr *store =
1081 nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
1082 store->num_components = ir->lhs->type->vector_elements;
1083 nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
1084 store->variables[0] = nir_deref_as_var(store_deref);
1085 store->src[0] = nir_src_for_ssa(src);
1086
1087 if (ir->condition) {
1088 nir_if *if_stmt = nir_if_create(this->shader);
1089 if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1090 nir_builder_cf_insert(&b, &if_stmt->cf_node);
1091 nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
1092 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1093 } else {
1094 nir_builder_instr_insert(&b, &store->instr);
1095 }
1096 }
1097
1098 /*
1099 * Given an instruction, returns a pointer to its destination or NULL if there
1100 * is no destination.
1101 *
1102 * Note that this only handles instructions we generate at this level.
1103 */
1104 static nir_dest *
1105 get_instr_dest(nir_instr *instr)
1106 {
1107 nir_alu_instr *alu_instr;
1108 nir_intrinsic_instr *intrinsic_instr;
1109 nir_tex_instr *tex_instr;
1110
1111 switch (instr->type) {
1112 case nir_instr_type_alu:
1113 alu_instr = nir_instr_as_alu(instr);
1114 return &alu_instr->dest.dest;
1115
1116 case nir_instr_type_intrinsic:
1117 intrinsic_instr = nir_instr_as_intrinsic(instr);
1118 if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
1119 return &intrinsic_instr->dest;
1120 else
1121 return NULL;
1122
1123 case nir_instr_type_tex:
1124 tex_instr = nir_instr_as_tex(instr);
1125 return &tex_instr->dest;
1126
1127 default:
1128 unreachable("not reached");
1129 }
1130
1131 return NULL;
1132 }
1133
1134 void
1135 nir_visitor::add_instr(nir_instr *instr, unsigned num_components)
1136 {
1137 nir_dest *dest = get_instr_dest(instr);
1138
1139 if (dest)
1140 nir_ssa_dest_init(instr, dest, num_components, NULL);
1141
1142 nir_builder_instr_insert(&b, instr);
1143
1144 if (dest) {
1145 assert(dest->is_ssa);
1146 this->result = &dest->ssa;
1147 }
1148 }
1149
1150 nir_ssa_def *
1151 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1152 {
1153 ir->accept(this);
1154 if (ir->as_dereference() || ir->as_constant()) {
1155 /*
1156 * A dereference is being used on the right hand side, which means we
1157 * must emit a variable load.
1158 */
1159
1160 nir_intrinsic_instr *load_instr =
1161 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
1162 load_instr->num_components = ir->type->vector_elements;
1163 load_instr->variables[0] = this->deref_head;
1164 ralloc_steal(load_instr, load_instr->variables[0]);
1165 add_instr(&load_instr->instr, ir->type->vector_elements);
1166 }
1167
1168 return this->result;
1169 }
1170
1171 void
1172 nir_visitor::visit(ir_expression *ir)
1173 {
1174 /* Some special cases */
1175 switch (ir->operation) {
1176 case ir_binop_ubo_load: {
1177 ir_constant *const_index = ir->operands[1]->as_constant();
1178
1179 nir_intrinsic_op op;
1180 if (const_index) {
1181 op = nir_intrinsic_load_ubo;
1182 } else {
1183 op = nir_intrinsic_load_ubo_indirect;
1184 }
1185 nir_intrinsic_instr *load = nir_intrinsic_instr_create(this->shader, op);
1186 load->num_components = ir->type->vector_elements;
1187 load->const_index[0] = const_index ? const_index->value.u[0] : 0; /* base offset */
1188 load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1189 if (!const_index)
1190 load->src[1] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1191 add_instr(&load->instr, ir->type->vector_elements);
1192
1193 /*
1194 * In UBO's, a true boolean value is any non-zero value, but we consider
1195 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1196 */
1197
1198 if (ir->type->base_type == GLSL_TYPE_BOOL)
1199 this->result = nir_ine(&b, &load->dest.ssa, nir_imm_int(&b, 0));
1200
1201 return;
1202 }
1203
1204 case ir_unop_interpolate_at_centroid:
1205 case ir_binop_interpolate_at_offset:
1206 case ir_binop_interpolate_at_sample: {
1207 ir_dereference *deref = ir->operands[0]->as_dereference();
1208 ir_swizzle *swizzle = NULL;
1209 if (!deref) {
1210 /* the api does not allow a swizzle here, but the varying packing code
1211 * may have pushed one into here.
1212 */
1213 swizzle = ir->operands[0]->as_swizzle();
1214 assert(swizzle);
1215 deref = swizzle->val->as_dereference();
1216 assert(deref);
1217 }
1218
1219 deref->accept(this);
1220
1221 nir_intrinsic_op op;
1222 if (this->deref_head->var->data.mode == nir_var_shader_in) {
1223 switch (ir->operation) {
1224 case ir_unop_interpolate_at_centroid:
1225 op = nir_intrinsic_interp_var_at_centroid;
1226 break;
1227 case ir_binop_interpolate_at_offset:
1228 op = nir_intrinsic_interp_var_at_offset;
1229 break;
1230 case ir_binop_interpolate_at_sample:
1231 op = nir_intrinsic_interp_var_at_sample;
1232 break;
1233 default:
1234 unreachable("Invalid interpolation intrinsic");
1235 }
1236 } else {
1237 /* This case can happen if the vertex shader does not write the
1238 * given varying. In this case, the linker will lower it to a
1239 * global variable. Since interpolating a variable makes no
1240 * sense, we'll just turn it into a load which will probably
1241 * eventually end up as an SSA definition.
1242 */
1243 assert(this->deref_head->var->data.mode == nir_var_global);
1244 op = nir_intrinsic_load_var;
1245 }
1246
1247 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1248 intrin->num_components = deref->type->vector_elements;
1249 intrin->variables[0] = this->deref_head;
1250 ralloc_steal(intrin, intrin->variables[0]);
1251
1252 if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
1253 intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
1254 intrin->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1255
1256 add_instr(&intrin->instr, deref->type->vector_elements);
1257
1258 if (swizzle) {
1259 unsigned swiz[4] = {
1260 swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
1261 };
1262
1263 result = nir_swizzle(&b, result, swiz,
1264 swizzle->type->vector_elements, false);
1265 }
1266
1267 return;
1268 }
1269
1270 default:
1271 break;
1272 }
1273
1274 nir_ssa_def *srcs[4];
1275 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1276 srcs[i] = evaluate_rvalue(ir->operands[i]);
1277
1278 glsl_base_type types[4];
1279 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1280 if (supports_ints)
1281 types[i] = ir->operands[i]->type->base_type;
1282 else
1283 types[i] = GLSL_TYPE_FLOAT;
1284
1285 glsl_base_type out_type;
1286 if (supports_ints)
1287 out_type = ir->type->base_type;
1288 else
1289 out_type = GLSL_TYPE_FLOAT;
1290
1291 switch (ir->operation) {
1292 case ir_unop_bit_not: result = nir_inot(&b, srcs[0]); break;
1293 case ir_unop_logic_not:
1294 result = supports_ints ? nir_inot(&b, srcs[0]) : nir_fnot(&b, srcs[0]);
1295 break;
1296 case ir_unop_neg:
1297 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fneg(&b, srcs[0])
1298 : nir_ineg(&b, srcs[0]);
1299 break;
1300 case ir_unop_abs:
1301 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fabs(&b, srcs[0])
1302 : nir_iabs(&b, srcs[0]);
1303 break;
1304 case ir_unop_saturate:
1305 assert(types[0] == GLSL_TYPE_FLOAT);
1306 result = nir_fsat(&b, srcs[0]);
1307 break;
1308 case ir_unop_sign:
1309 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fsign(&b, srcs[0])
1310 : nir_isign(&b, srcs[0]);
1311 break;
1312 case ir_unop_rcp: result = nir_frcp(&b, srcs[0]); break;
1313 case ir_unop_rsq: result = nir_frsq(&b, srcs[0]); break;
1314 case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
1315 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1316 case ir_unop_log: unreachable("ir_unop_log should have been lowered");
1317 case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
1318 case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
1319 case ir_unop_i2f:
1320 result = supports_ints ? nir_i2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1321 break;
1322 case ir_unop_u2f:
1323 result = supports_ints ? nir_u2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1324 break;
1325 case ir_unop_b2f:
1326 result = supports_ints ? nir_b2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1327 break;
1328 case ir_unop_f2i: result = nir_f2i(&b, srcs[0]); break;
1329 case ir_unop_f2u: result = nir_f2u(&b, srcs[0]); break;
1330 case ir_unop_f2b: result = nir_f2b(&b, srcs[0]); break;
1331 case ir_unop_i2b: result = nir_i2b(&b, srcs[0]); break;
1332 case ir_unop_b2i: result = nir_b2i(&b, srcs[0]); break;
1333 case ir_unop_i2u:
1334 case ir_unop_u2i:
1335 case ir_unop_bitcast_i2f:
1336 case ir_unop_bitcast_f2i:
1337 case ir_unop_bitcast_u2f:
1338 case ir_unop_bitcast_f2u:
1339 case ir_unop_subroutine_to_int:
1340 /* no-op */
1341 result = nir_imov(&b, srcs[0]);
1342 break;
1343 case ir_unop_any:
1344 switch (ir->operands[0]->type->vector_elements) {
1345 case 2:
1346 result = supports_ints ? nir_bany2(&b, srcs[0])
1347 : nir_fany2(&b, srcs[0]);
1348 break;
1349 case 3:
1350 result = supports_ints ? nir_bany3(&b, srcs[0])
1351 : nir_fany3(&b, srcs[0]);
1352 break;
1353 case 4:
1354 result = supports_ints ? nir_bany4(&b, srcs[0])
1355 : nir_fany4(&b, srcs[0]);
1356 break;
1357 default:
1358 unreachable("not reached");
1359 }
1360 break;
1361 case ir_unop_trunc: result = nir_ftrunc(&b, srcs[0]); break;
1362 case ir_unop_ceil: result = nir_fceil(&b, srcs[0]); break;
1363 case ir_unop_floor: result = nir_ffloor(&b, srcs[0]); break;
1364 case ir_unop_fract: result = nir_ffract(&b, srcs[0]); break;
1365 case ir_unop_round_even: result = nir_fround_even(&b, srcs[0]); break;
1366 case ir_unop_sin: result = nir_fsin(&b, srcs[0]); break;
1367 case ir_unop_cos: result = nir_fcos(&b, srcs[0]); break;
1368 case ir_unop_dFdx: result = nir_fddx(&b, srcs[0]); break;
1369 case ir_unop_dFdy: result = nir_fddy(&b, srcs[0]); break;
1370 case ir_unop_dFdx_fine: result = nir_fddx_fine(&b, srcs[0]); break;
1371 case ir_unop_dFdy_fine: result = nir_fddy_fine(&b, srcs[0]); break;
1372 case ir_unop_dFdx_coarse: result = nir_fddx_coarse(&b, srcs[0]); break;
1373 case ir_unop_dFdy_coarse: result = nir_fddy_coarse(&b, srcs[0]); break;
1374 case ir_unop_pack_snorm_2x16:
1375 result = nir_pack_snorm_2x16(&b, srcs[0]);
1376 break;
1377 case ir_unop_pack_snorm_4x8:
1378 result = nir_pack_snorm_4x8(&b, srcs[0]);
1379 break;
1380 case ir_unop_pack_unorm_2x16:
1381 result = nir_pack_unorm_2x16(&b, srcs[0]);
1382 break;
1383 case ir_unop_pack_unorm_4x8:
1384 result = nir_pack_unorm_4x8(&b, srcs[0]);
1385 break;
1386 case ir_unop_pack_half_2x16:
1387 result = nir_pack_half_2x16(&b, srcs[0]);
1388 break;
1389 case ir_unop_unpack_snorm_2x16:
1390 result = nir_unpack_snorm_2x16(&b, srcs[0]);
1391 break;
1392 case ir_unop_unpack_snorm_4x8:
1393 result = nir_unpack_snorm_4x8(&b, srcs[0]);
1394 break;
1395 case ir_unop_unpack_unorm_2x16:
1396 result = nir_unpack_unorm_2x16(&b, srcs[0]);
1397 break;
1398 case ir_unop_unpack_unorm_4x8:
1399 result = nir_unpack_unorm_4x8(&b, srcs[0]);
1400 break;
1401 case ir_unop_unpack_half_2x16:
1402 result = nir_unpack_half_2x16(&b, srcs[0]);
1403 break;
1404 case ir_unop_unpack_half_2x16_split_x:
1405 result = nir_unpack_half_2x16_split_x(&b, srcs[0]);
1406 break;
1407 case ir_unop_unpack_half_2x16_split_y:
1408 result = nir_unpack_half_2x16_split_y(&b, srcs[0]);
1409 break;
1410 case ir_unop_bitfield_reverse:
1411 result = nir_bitfield_reverse(&b, srcs[0]);
1412 break;
1413 case ir_unop_bit_count:
1414 result = nir_bit_count(&b, srcs[0]);
1415 break;
1416 case ir_unop_find_msb:
1417 switch (types[0]) {
1418 case GLSL_TYPE_UINT:
1419 result = nir_ufind_msb(&b, srcs[0]);
1420 break;
1421 case GLSL_TYPE_INT:
1422 result = nir_ifind_msb(&b, srcs[0]);
1423 break;
1424 default:
1425 unreachable("Invalid type for findMSB()");
1426 }
1427 break;
1428 case ir_unop_find_lsb:
1429 result = nir_find_lsb(&b, srcs[0]);
1430 break;
1431
1432 case ir_unop_noise:
1433 switch (ir->type->vector_elements) {
1434 case 1:
1435 switch (ir->operands[0]->type->vector_elements) {
1436 case 1: result = nir_fnoise1_1(&b, srcs[0]); break;
1437 case 2: result = nir_fnoise1_2(&b, srcs[0]); break;
1438 case 3: result = nir_fnoise1_3(&b, srcs[0]); break;
1439 case 4: result = nir_fnoise1_4(&b, srcs[0]); break;
1440 default: unreachable("not reached");
1441 }
1442 break;
1443 case 2:
1444 switch (ir->operands[0]->type->vector_elements) {
1445 case 1: result = nir_fnoise2_1(&b, srcs[0]); break;
1446 case 2: result = nir_fnoise2_2(&b, srcs[0]); break;
1447 case 3: result = nir_fnoise2_3(&b, srcs[0]); break;
1448 case 4: result = nir_fnoise2_4(&b, srcs[0]); break;
1449 default: unreachable("not reached");
1450 }
1451 break;
1452 case 3:
1453 switch (ir->operands[0]->type->vector_elements) {
1454 case 1: result = nir_fnoise3_1(&b, srcs[0]); break;
1455 case 2: result = nir_fnoise3_2(&b, srcs[0]); break;
1456 case 3: result = nir_fnoise3_3(&b, srcs[0]); break;
1457 case 4: result = nir_fnoise3_4(&b, srcs[0]); break;
1458 default: unreachable("not reached");
1459 }
1460 break;
1461 case 4:
1462 switch (ir->operands[0]->type->vector_elements) {
1463 case 1: result = nir_fnoise4_1(&b, srcs[0]); break;
1464 case 2: result = nir_fnoise4_2(&b, srcs[0]); break;
1465 case 3: result = nir_fnoise4_3(&b, srcs[0]); break;
1466 case 4: result = nir_fnoise4_4(&b, srcs[0]); break;
1467 default: unreachable("not reached");
1468 }
1469 break;
1470 default:
1471 unreachable("not reached");
1472 }
1473 break;
1474 case ir_unop_get_buffer_size: {
1475 nir_intrinsic_instr *load = nir_intrinsic_instr_create(
1476 this->shader,
1477 nir_intrinsic_get_buffer_size);
1478 load->num_components = ir->type->vector_elements;
1479 load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1480 add_instr(&load->instr, ir->type->vector_elements);
1481 return;
1482 }
1483
1484 case ir_binop_add:
1485 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fadd(&b, srcs[0], srcs[1])
1486 : nir_iadd(&b, srcs[0], srcs[1]);
1487 break;
1488 case ir_binop_sub:
1489 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fsub(&b, srcs[0], srcs[1])
1490 : nir_isub(&b, srcs[0], srcs[1]);
1491 break;
1492 case ir_binop_mul:
1493 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fmul(&b, srcs[0], srcs[1])
1494 : nir_imul(&b, srcs[0], srcs[1]);
1495 break;
1496 case ir_binop_div:
1497 if (out_type == GLSL_TYPE_FLOAT)
1498 result = nir_fdiv(&b, srcs[0], srcs[1]);
1499 else if (out_type == GLSL_TYPE_INT)
1500 result = nir_idiv(&b, srcs[0], srcs[1]);
1501 else
1502 result = nir_udiv(&b, srcs[0], srcs[1]);
1503 break;
1504 case ir_binop_mod:
1505 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fmod(&b, srcs[0], srcs[1])
1506 : nir_umod(&b, srcs[0], srcs[1]);
1507 break;
1508 case ir_binop_min:
1509 if (out_type == GLSL_TYPE_FLOAT)
1510 result = nir_fmin(&b, srcs[0], srcs[1]);
1511 else if (out_type == GLSL_TYPE_INT)
1512 result = nir_imin(&b, srcs[0], srcs[1]);
1513 else
1514 result = nir_umin(&b, srcs[0], srcs[1]);
1515 break;
1516 case ir_binop_max:
1517 if (out_type == GLSL_TYPE_FLOAT)
1518 result = nir_fmax(&b, srcs[0], srcs[1]);
1519 else if (out_type == GLSL_TYPE_INT)
1520 result = nir_imax(&b, srcs[0], srcs[1]);
1521 else
1522 result = nir_umax(&b, srcs[0], srcs[1]);
1523 break;
1524 case ir_binop_pow: result = nir_fpow(&b, srcs[0], srcs[1]); break;
1525 case ir_binop_bit_and: result = nir_iand(&b, srcs[0], srcs[1]); break;
1526 case ir_binop_bit_or: result = nir_ior(&b, srcs[0], srcs[1]); break;
1527 case ir_binop_bit_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
1528 case ir_binop_logic_and:
1529 result = supports_ints ? nir_iand(&b, srcs[0], srcs[1])
1530 : nir_fand(&b, srcs[0], srcs[1]);
1531 break;
1532 case ir_binop_logic_or:
1533 result = supports_ints ? nir_ior(&b, srcs[0], srcs[1])
1534 : nir_for(&b, srcs[0], srcs[1]);
1535 break;
1536 case ir_binop_logic_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
1537 result = supports_ints ? nir_ior(&b, srcs[0], srcs[1])
1538 : nir_for(&b, srcs[0], srcs[1]);
1539 break;
1540 case ir_binop_lshift: result = nir_ishl(&b, srcs[0], srcs[1]); break;
1541 case ir_binop_rshift:
1542 result = (out_type == GLSL_TYPE_INT) ? nir_ishr(&b, srcs[0], srcs[1])
1543 : nir_ushr(&b, srcs[0], srcs[1]);
1544 break;
1545 case ir_binop_imul_high:
1546 result = (out_type == GLSL_TYPE_INT) ? nir_imul_high(&b, srcs[0], srcs[1])
1547 : nir_umul_high(&b, srcs[0], srcs[1]);
1548 break;
1549 case ir_binop_carry: result = nir_uadd_carry(&b, srcs[0], srcs[1]); break;
1550 case ir_binop_borrow: result = nir_usub_borrow(&b, srcs[0], srcs[1]); break;
1551 case ir_binop_less:
1552 if (supports_ints) {
1553 if (types[0] == GLSL_TYPE_FLOAT)
1554 result = nir_flt(&b, srcs[0], srcs[1]);
1555 else if (types[0] == GLSL_TYPE_INT)
1556 result = nir_ilt(&b, srcs[0], srcs[1]);
1557 else
1558 result = nir_ult(&b, srcs[0], srcs[1]);
1559 } else {
1560 result = nir_slt(&b, srcs[0], srcs[1]);
1561 }
1562 break;
1563 case ir_binop_greater:
1564 if (supports_ints) {
1565 if (types[0] == GLSL_TYPE_FLOAT)
1566 result = nir_flt(&b, srcs[1], srcs[0]);
1567 else if (types[0] == GLSL_TYPE_INT)
1568 result = nir_ilt(&b, srcs[1], srcs[0]);
1569 else
1570 result = nir_ult(&b, srcs[1], srcs[0]);
1571 } else {
1572 result = nir_slt(&b, srcs[1], srcs[0]);
1573 }
1574 break;
1575 case ir_binop_lequal:
1576 if (supports_ints) {
1577 if (types[0] == GLSL_TYPE_FLOAT)
1578 result = nir_fge(&b, srcs[1], srcs[0]);
1579 else if (types[0] == GLSL_TYPE_INT)
1580 result = nir_ige(&b, srcs[1], srcs[0]);
1581 else
1582 result = nir_uge(&b, srcs[1], srcs[0]);
1583 } else {
1584 result = nir_slt(&b, srcs[1], srcs[0]);
1585 }
1586 break;
1587 case ir_binop_gequal:
1588 if (supports_ints) {
1589 if (types[0] == GLSL_TYPE_FLOAT)
1590 result = nir_fge(&b, srcs[0], srcs[1]);
1591 else if (types[0] == GLSL_TYPE_INT)
1592 result = nir_ige(&b, srcs[0], srcs[1]);
1593 else
1594 result = nir_uge(&b, srcs[0], srcs[1]);
1595 } else {
1596 result = nir_slt(&b, srcs[0], srcs[1]);
1597 }
1598 break;
1599 case ir_binop_equal:
1600 if (supports_ints) {
1601 if (types[0] == GLSL_TYPE_FLOAT)
1602 result = nir_feq(&b, srcs[0], srcs[1]);
1603 else
1604 result = nir_ieq(&b, srcs[0], srcs[1]);
1605 } else {
1606 result = nir_seq(&b, srcs[0], srcs[1]);
1607 }
1608 break;
1609 case ir_binop_nequal:
1610 if (supports_ints) {
1611 if (types[0] == GLSL_TYPE_FLOAT)
1612 result = nir_fne(&b, srcs[0], srcs[1]);
1613 else
1614 result = nir_ine(&b, srcs[0], srcs[1]);
1615 } else {
1616 result = nir_sne(&b, srcs[0], srcs[1]);
1617 }
1618 break;
1619 case ir_binop_all_equal:
1620 if (supports_ints) {
1621 if (types[0] == GLSL_TYPE_FLOAT) {
1622 switch (ir->operands[0]->type->vector_elements) {
1623 case 1: result = nir_feq(&b, srcs[0], srcs[1]); break;
1624 case 2: result = nir_ball_fequal2(&b, srcs[0], srcs[1]); break;
1625 case 3: result = nir_ball_fequal3(&b, srcs[0], srcs[1]); break;
1626 case 4: result = nir_ball_fequal4(&b, srcs[0], srcs[1]); break;
1627 default:
1628 unreachable("not reached");
1629 }
1630 } else {
1631 switch (ir->operands[0]->type->vector_elements) {
1632 case 1: result = nir_ieq(&b, srcs[0], srcs[1]); break;
1633 case 2: result = nir_ball_iequal2(&b, srcs[0], srcs[1]); break;
1634 case 3: result = nir_ball_iequal3(&b, srcs[0], srcs[1]); break;
1635 case 4: result = nir_ball_iequal4(&b, srcs[0], srcs[1]); break;
1636 default:
1637 unreachable("not reached");
1638 }
1639 }
1640 } else {
1641 switch (ir->operands[0]->type->vector_elements) {
1642 case 1: result = nir_seq(&b, srcs[0], srcs[1]); break;
1643 case 2: result = nir_fall_equal2(&b, srcs[0], srcs[1]); break;
1644 case 3: result = nir_fall_equal3(&b, srcs[0], srcs[1]); break;
1645 case 4: result = nir_fall_equal4(&b, srcs[0], srcs[1]); break;
1646 default:
1647 unreachable("not reached");
1648 }
1649 }
1650 break;
1651 case ir_binop_any_nequal:
1652 if (supports_ints) {
1653 if (types[0] == GLSL_TYPE_FLOAT) {
1654 switch (ir->operands[0]->type->vector_elements) {
1655 case 1: result = nir_fne(&b, srcs[0], srcs[1]); break;
1656 case 2: result = nir_bany_fnequal2(&b, srcs[0], srcs[1]); break;
1657 case 3: result = nir_bany_fnequal3(&b, srcs[0], srcs[1]); break;
1658 case 4: result = nir_bany_fnequal4(&b, srcs[0], srcs[1]); break;
1659 default:
1660 unreachable("not reached");
1661 }
1662 } else {
1663 switch (ir->operands[0]->type->vector_elements) {
1664 case 1: result = nir_ine(&b, srcs[0], srcs[1]); break;
1665 case 2: result = nir_bany_inequal2(&b, srcs[0], srcs[1]); break;
1666 case 3: result = nir_bany_inequal3(&b, srcs[0], srcs[1]); break;
1667 case 4: result = nir_bany_inequal4(&b, srcs[0], srcs[1]); break;
1668 default:
1669 unreachable("not reached");
1670 }
1671 }
1672 } else {
1673 switch (ir->operands[0]->type->vector_elements) {
1674 case 1: result = nir_sne(&b, srcs[0], srcs[1]); break;
1675 case 2: result = nir_fany_nequal2(&b, srcs[0], srcs[1]); break;
1676 case 3: result = nir_fany_nequal3(&b, srcs[0], srcs[1]); break;
1677 case 4: result = nir_fany_nequal4(&b, srcs[0], srcs[1]); break;
1678 default:
1679 unreachable("not reached");
1680 }
1681 }
1682 break;
1683 case ir_binop_dot:
1684 switch (ir->operands[0]->type->vector_elements) {
1685 case 2: result = nir_fdot2(&b, srcs[0], srcs[1]); break;
1686 case 3: result = nir_fdot3(&b, srcs[0], srcs[1]); break;
1687 case 4: result = nir_fdot4(&b, srcs[0], srcs[1]); break;
1688 default:
1689 unreachable("not reached");
1690 }
1691 break;
1692
1693 case ir_binop_pack_half_2x16_split:
1694 result = nir_pack_half_2x16_split(&b, srcs[0], srcs[1]);
1695 break;
1696 case ir_binop_bfm: result = nir_bfm(&b, srcs[0], srcs[1]); break;
1697 case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
1698 case ir_triop_fma:
1699 result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
1700 break;
1701 case ir_triop_lrp:
1702 result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
1703 break;
1704 case ir_triop_csel:
1705 if (supports_ints)
1706 result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
1707 else
1708 result = nir_fcsel(&b, srcs[0], srcs[1], srcs[2]);
1709 break;
1710 case ir_triop_bfi:
1711 result = nir_bfi(&b, srcs[0], srcs[1], srcs[2]);
1712 break;
1713 case ir_triop_bitfield_extract:
1714 result = (out_type == GLSL_TYPE_INT) ?
1715 nir_ibitfield_extract(&b, srcs[0], srcs[1], srcs[2]) :
1716 nir_ubitfield_extract(&b, srcs[0], srcs[1], srcs[2]);
1717 break;
1718 case ir_quadop_bitfield_insert:
1719 result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
1720 break;
1721 case ir_quadop_vector:
1722 result = nir_vec(&b, srcs, ir->type->vector_elements);
1723 break;
1724
1725 default:
1726 unreachable("not reached");
1727 }
1728 }
1729
1730 void
1731 nir_visitor::visit(ir_swizzle *ir)
1732 {
1733 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1734 result = nir_swizzle(&b, evaluate_rvalue(ir->val), swizzle,
1735 ir->type->vector_elements, !supports_ints);
1736 }
1737
1738 void
1739 nir_visitor::visit(ir_texture *ir)
1740 {
1741 unsigned num_srcs;
1742 nir_texop op;
1743 switch (ir->op) {
1744 case ir_tex:
1745 op = nir_texop_tex;
1746 num_srcs = 1; /* coordinate */
1747 break;
1748
1749 case ir_txb:
1750 case ir_txl:
1751 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1752 num_srcs = 2; /* coordinate, bias/lod */
1753 break;
1754
1755 case ir_txd:
1756 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1757 num_srcs = 3;
1758 break;
1759
1760 case ir_txf:
1761 op = nir_texop_txf;
1762 if (ir->lod_info.lod != NULL)
1763 num_srcs = 2; /* coordinate, lod */
1764 else
1765 num_srcs = 1; /* coordinate */
1766 break;
1767
1768 case ir_txf_ms:
1769 op = nir_texop_txf_ms;
1770 num_srcs = 2; /* coordinate, sample_index */
1771 break;
1772
1773 case ir_txs:
1774 op = nir_texop_txs;
1775 if (ir->lod_info.lod != NULL)
1776 num_srcs = 1; /* lod */
1777 else
1778 num_srcs = 0;
1779 break;
1780
1781 case ir_lod:
1782 op = nir_texop_lod;
1783 num_srcs = 1; /* coordinate */
1784 break;
1785
1786 case ir_tg4:
1787 op = nir_texop_tg4;
1788 num_srcs = 1; /* coordinate */
1789 break;
1790
1791 case ir_query_levels:
1792 op = nir_texop_query_levels;
1793 num_srcs = 0;
1794 break;
1795
1796 case ir_texture_samples:
1797 op = nir_texop_texture_samples;
1798 num_srcs = 0;
1799 break;
1800
1801 default:
1802 unreachable("not reached");
1803 }
1804
1805 if (ir->projector != NULL)
1806 num_srcs++;
1807 if (ir->shadow_comparitor != NULL)
1808 num_srcs++;
1809 if (ir->offset != NULL && ir->offset->as_constant() == NULL)
1810 num_srcs++;
1811
1812 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1813
1814 instr->op = op;
1815 instr->sampler_dim =
1816 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1817 instr->is_array = ir->sampler->type->sampler_array;
1818 instr->is_shadow = ir->sampler->type->sampler_shadow;
1819 if (instr->is_shadow)
1820 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1821 switch (ir->type->base_type) {
1822 case GLSL_TYPE_FLOAT:
1823 instr->dest_type = nir_type_float;
1824 break;
1825 case GLSL_TYPE_INT:
1826 instr->dest_type = nir_type_int;
1827 break;
1828 case GLSL_TYPE_UINT:
1829 instr->dest_type = nir_type_unsigned;
1830 break;
1831 default:
1832 unreachable("not reached");
1833 }
1834
1835 instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
1836
1837 unsigned src_number = 0;
1838
1839 if (ir->coordinate != NULL) {
1840 instr->coord_components = ir->coordinate->type->vector_elements;
1841 instr->src[src_number].src =
1842 nir_src_for_ssa(evaluate_rvalue(ir->coordinate));
1843 instr->src[src_number].src_type = nir_tex_src_coord;
1844 src_number++;
1845 }
1846
1847 if (ir->projector != NULL) {
1848 instr->src[src_number].src =
1849 nir_src_for_ssa(evaluate_rvalue(ir->projector));
1850 instr->src[src_number].src_type = nir_tex_src_projector;
1851 src_number++;
1852 }
1853
1854 if (ir->shadow_comparitor != NULL) {
1855 instr->src[src_number].src =
1856 nir_src_for_ssa(evaluate_rvalue(ir->shadow_comparitor));
1857 instr->src[src_number].src_type = nir_tex_src_comparitor;
1858 src_number++;
1859 }
1860
1861 if (ir->offset != NULL) {
1862 /* we don't support multiple offsets yet */
1863 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1864
1865 ir_constant *const_offset = ir->offset->as_constant();
1866 if (const_offset != NULL) {
1867 for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
1868 instr->const_offset[i] = const_offset->value.i[i];
1869 } else {
1870 instr->src[src_number].src =
1871 nir_src_for_ssa(evaluate_rvalue(ir->offset));
1872 instr->src[src_number].src_type = nir_tex_src_offset;
1873 src_number++;
1874 }
1875 }
1876
1877 switch (ir->op) {
1878 case ir_txb:
1879 instr->src[src_number].src =
1880 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.bias));
1881 instr->src[src_number].src_type = nir_tex_src_bias;
1882 src_number++;
1883 break;
1884
1885 case ir_txl:
1886 case ir_txf:
1887 case ir_txs:
1888 if (ir->lod_info.lod != NULL) {
1889 instr->src[src_number].src =
1890 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.lod));
1891 instr->src[src_number].src_type = nir_tex_src_lod;
1892 src_number++;
1893 }
1894 break;
1895
1896 case ir_txd:
1897 instr->src[src_number].src =
1898 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdx));
1899 instr->src[src_number].src_type = nir_tex_src_ddx;
1900 src_number++;
1901 instr->src[src_number].src =
1902 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdy));
1903 instr->src[src_number].src_type = nir_tex_src_ddy;
1904 src_number++;
1905 break;
1906
1907 case ir_txf_ms:
1908 instr->src[src_number].src =
1909 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.sample_index));
1910 instr->src[src_number].src_type = nir_tex_src_ms_index;
1911 src_number++;
1912 break;
1913
1914 case ir_tg4:
1915 instr->component = ir->lod_info.component->as_constant()->value.u[0];
1916 break;
1917
1918 default:
1919 break;
1920 }
1921
1922 assert(src_number == num_srcs);
1923
1924 add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
1925 }
1926
1927 void
1928 nir_visitor::visit(ir_constant *ir)
1929 {
1930 /*
1931 * We don't know if this variable is an an array or struct that gets
1932 * dereferenced, so do the safe thing an make it a variable with a
1933 * constant initializer and return a dereference.
1934 */
1935
1936 nir_variable *var =
1937 nir_local_variable_create(this->impl, ir->type, "const_temp");
1938 var->data.read_only = true;
1939 var->constant_initializer = constant_copy(ir, var);
1940
1941 this->deref_head = nir_deref_var_create(this->shader, var);
1942 this->deref_tail = &this->deref_head->deref;
1943 }
1944
1945 void
1946 nir_visitor::visit(ir_dereference_variable *ir)
1947 {
1948 struct hash_entry *entry =
1949 _mesa_hash_table_search(this->var_table, ir->var);
1950 assert(entry);
1951 nir_variable *var = (nir_variable *) entry->data;
1952
1953 nir_deref_var *deref = nir_deref_var_create(this->shader, var);
1954 this->deref_head = deref;
1955 this->deref_tail = &deref->deref;
1956 }
1957
1958 void
1959 nir_visitor::visit(ir_dereference_record *ir)
1960 {
1961 ir->record->accept(this);
1962
1963 int field_index = this->deref_tail->type->field_index(ir->field);
1964 assert(field_index >= 0);
1965
1966 nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
1967 deref->deref.type = ir->type;
1968 this->deref_tail->child = &deref->deref;
1969 this->deref_tail = &deref->deref;
1970 }
1971
1972 void
1973 nir_visitor::visit(ir_dereference_array *ir)
1974 {
1975 nir_deref_array *deref = nir_deref_array_create(this->shader);
1976 deref->deref.type = ir->type;
1977
1978 ir_constant *const_index = ir->array_index->as_constant();
1979 if (const_index != NULL) {
1980 deref->deref_array_type = nir_deref_array_type_direct;
1981 deref->base_offset = const_index->value.u[0];
1982 } else {
1983 deref->deref_array_type = nir_deref_array_type_indirect;
1984 deref->indirect =
1985 nir_src_for_ssa(evaluate_rvalue(ir->array_index));
1986 }
1987
1988 ir->array->accept(this);
1989
1990 this->deref_tail->child = &deref->deref;
1991 ralloc_steal(this->deref_tail, deref);
1992 this->deref_tail = &deref->deref;
1993 }
1994
1995 void
1996 nir_visitor::visit(ir_barrier *ir)
1997 {
1998 nir_intrinsic_instr *instr =
1999 nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
2000 nir_builder_instr_insert(&b, &instr->instr);
2001 }