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