i965/vec4: Use the replicated fdot instruction in NIR
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_nir.cpp
1 /*
2 * Copyright © 2015 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
24 #include "brw_nir.h"
25 #include "brw_vec4.h"
26 #include "glsl/ir_uniform.h"
27
28 namespace brw {
29
30 void
31 vec4_visitor::emit_nir_code()
32 {
33 nir_shader *nir = prog->nir;
34
35 if (nir->num_inputs > 0)
36 nir_setup_inputs(nir);
37
38 if (nir->num_uniforms > 0)
39 nir_setup_uniforms(nir);
40
41 nir_setup_system_values(nir);
42
43 /* get the main function and emit it */
44 nir_foreach_overload(nir, overload) {
45 assert(strcmp(overload->function->name, "main") == 0);
46 assert(overload->impl);
47 nir_emit_impl(overload->impl);
48 }
49 }
50
51 void
52 vec4_visitor::nir_setup_system_value_intrinsic(nir_intrinsic_instr *instr)
53 {
54 dst_reg *reg;
55
56 switch (instr->intrinsic) {
57 case nir_intrinsic_load_vertex_id:
58 unreachable("should be lowered by lower_vertex_id().");
59
60 case nir_intrinsic_load_vertex_id_zero_base:
61 reg = &this->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
62 if (reg->file == BAD_FILE)
63 *reg =
64 *this->make_reg_for_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE,
65 glsl_type::int_type);
66 break;
67
68 case nir_intrinsic_load_base_vertex:
69 reg = &this->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
70 if (reg->file == BAD_FILE)
71 *reg = *this->make_reg_for_system_value(SYSTEM_VALUE_BASE_VERTEX,
72 glsl_type::int_type);
73 break;
74
75 case nir_intrinsic_load_instance_id:
76 reg = &this->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
77 if (reg->file == BAD_FILE)
78 *reg = *this->make_reg_for_system_value(SYSTEM_VALUE_INSTANCE_ID,
79 glsl_type::int_type);
80 break;
81
82 default:
83 break;
84 }
85 }
86
87 static bool
88 setup_system_values_block(nir_block *block, void *void_visitor)
89 {
90 vec4_visitor *v = (vec4_visitor *)void_visitor;
91
92 nir_foreach_instr(block, instr) {
93 if (instr->type != nir_instr_type_intrinsic)
94 continue;
95
96 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
97 v->nir_setup_system_value_intrinsic(intrin);
98 }
99
100 return true;
101 }
102
103 void
104 vec4_visitor::nir_setup_system_values(nir_shader *shader)
105 {
106 nir_system_values = ralloc_array(mem_ctx, dst_reg, SYSTEM_VALUE_MAX);
107
108 nir_foreach_overload(shader, overload) {
109 assert(strcmp(overload->function->name, "main") == 0);
110 assert(overload->impl);
111 nir_foreach_block(overload->impl, setup_system_values_block, this);
112 }
113 }
114
115 void
116 vec4_visitor::nir_setup_inputs(nir_shader *shader)
117 {
118 nir_inputs = ralloc_array(mem_ctx, src_reg, shader->num_inputs);
119
120 foreach_list_typed(nir_variable, var, node, &shader->inputs) {
121 int offset = var->data.driver_location;
122 unsigned size = type_size_vec4(var->type);
123 for (unsigned i = 0; i < size; i++) {
124 src_reg src = src_reg(ATTR, var->data.location + i, var->type);
125 nir_inputs[offset + i] = src;
126 }
127 }
128 }
129
130 void
131 vec4_visitor::nir_setup_uniforms(nir_shader *shader)
132 {
133 uniforms = 0;
134
135 if (shader_prog) {
136 foreach_list_typed(nir_variable, var, node, &shader->uniforms) {
137 /* UBO's, atomics and samplers don't take up space in the
138 uniform file */
139 if (var->interface_type != NULL || var->type->contains_atomic() ||
140 type_size_vec4(var->type) == 0) {
141 continue;
142 }
143
144 assert(uniforms < uniform_array_size);
145 this->uniform_size[uniforms] = type_size_vec4(var->type);
146
147 if (strncmp(var->name, "gl_", 3) == 0)
148 nir_setup_builtin_uniform(var);
149 else
150 nir_setup_uniform(var);
151 }
152 } else {
153 /* For ARB_vertex_program, only a single "parameters" variable is
154 * generated to support uniform data.
155 */
156 nir_variable *var = (nir_variable *) shader->uniforms.get_head();
157 assert(shader->uniforms.length() == 1 &&
158 strcmp(var->name, "parameters") == 0);
159
160 assert(uniforms < uniform_array_size);
161 this->uniform_size[uniforms] = type_size_vec4(var->type);
162
163 struct gl_program_parameter_list *plist = prog->Parameters;
164 for (unsigned p = 0; p < plist->NumParameters; p++) {
165 uniform_vector_size[uniforms] = plist->Parameters[p].Size;
166
167 /* Parameters should be either vec4 uniforms or single component
168 * constants; matrices and other larger types should have been broken
169 * down earlier.
170 */
171 assert(uniform_vector_size[uniforms] <= 4);
172
173 int i;
174 for (i = 0; i < uniform_vector_size[uniforms]; i++) {
175 stage_prog_data->param[uniforms * 4 + i] = &plist->ParameterValues[p][i];
176 }
177 for (; i < 4; i++) {
178 static const gl_constant_value zero = { 0.0 };
179 stage_prog_data->param[uniforms * 4 + i] = &zero;
180 }
181
182 uniforms++;
183 }
184 }
185 }
186
187 void
188 vec4_visitor::nir_setup_uniform(nir_variable *var)
189 {
190 int namelen = strlen(var->name);
191
192 /* The data for our (non-builtin) uniforms is stored in a series of
193 * gl_uniform_driver_storage structs for each subcomponent that
194 * glGetUniformLocation() could name. We know it's been set up in the same
195 * order we'd walk the type, so walk the list of storage and find anything
196 * with our name, or the prefix of a component that starts with our name.
197 */
198 for (unsigned u = 0; u < shader_prog->NumUniformStorage; u++) {
199 struct gl_uniform_storage *storage = &shader_prog->UniformStorage[u];
200
201 if (storage->builtin)
202 continue;
203
204 if (strncmp(var->name, storage->name, namelen) != 0 ||
205 (storage->name[namelen] != 0 &&
206 storage->name[namelen] != '.' &&
207 storage->name[namelen] != '[')) {
208 continue;
209 }
210
211 gl_constant_value *components = storage->storage;
212 unsigned vector_count = (MAX2(storage->array_elements, 1) *
213 storage->type->matrix_columns);
214
215 for (unsigned s = 0; s < vector_count; s++) {
216 assert(uniforms < uniform_array_size);
217 uniform_vector_size[uniforms] = storage->type->vector_elements;
218
219 int i;
220 for (i = 0; i < uniform_vector_size[uniforms]; i++) {
221 stage_prog_data->param[uniforms * 4 + i] = components;
222 components++;
223 }
224 for (; i < 4; i++) {
225 static const gl_constant_value zero = { 0.0 };
226 stage_prog_data->param[uniforms * 4 + i] = &zero;
227 }
228
229 uniforms++;
230 }
231 }
232 }
233
234 void
235 vec4_visitor::nir_setup_builtin_uniform(nir_variable *var)
236 {
237 const nir_state_slot *const slots = var->state_slots;
238 assert(var->state_slots != NULL);
239
240 for (unsigned int i = 0; i < var->num_state_slots; i++) {
241 /* This state reference has already been setup by ir_to_mesa,
242 * but we'll get the same index back here. We can reference
243 * ParameterValues directly, since unlike brw_fs.cpp, we never
244 * add new state references during compile.
245 */
246 int index = _mesa_add_state_reference(this->prog->Parameters,
247 (gl_state_index *)slots[i].tokens);
248 gl_constant_value *values =
249 &this->prog->Parameters->ParameterValues[index][0];
250
251 assert(uniforms < uniform_array_size);
252
253 for (unsigned j = 0; j < 4; j++)
254 stage_prog_data->param[uniforms * 4 + j] =
255 &values[GET_SWZ(slots[i].swizzle, j)];
256
257 this->uniform_vector_size[uniforms] =
258 (var->type->is_scalar() || var->type->is_vector() ||
259 var->type->is_matrix() ? var->type->vector_elements : 4);
260
261 uniforms++;
262 }
263 }
264
265 void
266 vec4_visitor::nir_emit_impl(nir_function_impl *impl)
267 {
268 nir_locals = ralloc_array(mem_ctx, dst_reg, impl->reg_alloc);
269
270 foreach_list_typed(nir_register, reg, node, &impl->registers) {
271 unsigned array_elems =
272 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
273
274 nir_locals[reg->index] = dst_reg(GRF, alloc.allocate(array_elems));
275 }
276
277 nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
278
279 nir_emit_cf_list(&impl->body);
280 }
281
282 void
283 vec4_visitor::nir_emit_cf_list(exec_list *list)
284 {
285 exec_list_validate(list);
286 foreach_list_typed(nir_cf_node, node, node, list) {
287 switch (node->type) {
288 case nir_cf_node_if:
289 nir_emit_if(nir_cf_node_as_if(node));
290 break;
291
292 case nir_cf_node_loop:
293 nir_emit_loop(nir_cf_node_as_loop(node));
294 break;
295
296 case nir_cf_node_block:
297 nir_emit_block(nir_cf_node_as_block(node));
298 break;
299
300 default:
301 unreachable("Invalid CFG node block");
302 }
303 }
304 }
305
306 void
307 vec4_visitor::nir_emit_if(nir_if *if_stmt)
308 {
309 /* First, put the condition in f0 */
310 src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
311 vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
312 inst->conditional_mod = BRW_CONDITIONAL_NZ;
313
314 emit(IF(BRW_PREDICATE_NORMAL));
315
316 nir_emit_cf_list(&if_stmt->then_list);
317
318 /* note: if the else is empty, dead CF elimination will remove it */
319 emit(BRW_OPCODE_ELSE);
320
321 nir_emit_cf_list(&if_stmt->else_list);
322
323 emit(BRW_OPCODE_ENDIF);
324 }
325
326 void
327 vec4_visitor::nir_emit_loop(nir_loop *loop)
328 {
329 emit(BRW_OPCODE_DO);
330
331 nir_emit_cf_list(&loop->body);
332
333 emit(BRW_OPCODE_WHILE);
334 }
335
336 void
337 vec4_visitor::nir_emit_block(nir_block *block)
338 {
339 nir_foreach_instr(block, instr) {
340 nir_emit_instr(instr);
341 }
342 }
343
344 void
345 vec4_visitor::nir_emit_instr(nir_instr *instr)
346 {
347 this->base_ir = instr;
348
349 switch (instr->type) {
350 case nir_instr_type_load_const:
351 nir_emit_load_const(nir_instr_as_load_const(instr));
352 break;
353
354 case nir_instr_type_intrinsic:
355 nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
356 break;
357
358 case nir_instr_type_alu:
359 nir_emit_alu(nir_instr_as_alu(instr));
360 break;
361
362 case nir_instr_type_jump:
363 nir_emit_jump(nir_instr_as_jump(instr));
364 break;
365
366 case nir_instr_type_tex:
367 nir_emit_texture(nir_instr_as_tex(instr));
368 break;
369
370 case nir_instr_type_ssa_undef:
371 nir_emit_undef(nir_instr_as_ssa_undef(instr));
372 break;
373
374 default:
375 fprintf(stderr, "VS instruction not yet implemented by NIR->vec4\n");
376 break;
377 }
378 }
379
380 static dst_reg
381 dst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
382 unsigned base_offset, nir_src *indirect)
383 {
384 dst_reg reg;
385
386 reg = v->nir_locals[nir_reg->index];
387 reg = offset(reg, base_offset);
388 if (indirect) {
389 reg.reladdr =
390 new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
391 BRW_REGISTER_TYPE_D,
392 1));
393 }
394 return reg;
395 }
396
397 dst_reg
398 vec4_visitor::get_nir_dest(nir_dest dest)
399 {
400 if (dest.is_ssa) {
401 dst_reg dst = dst_reg(GRF, alloc.allocate(1));
402 nir_ssa_values[dest.ssa.index] = dst;
403 return dst;
404 } else {
405 return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
406 dest.reg.indirect);
407 }
408 }
409
410 dst_reg
411 vec4_visitor::get_nir_dest(nir_dest dest, enum brw_reg_type type)
412 {
413 return retype(get_nir_dest(dest), type);
414 }
415
416 dst_reg
417 vec4_visitor::get_nir_dest(nir_dest dest, nir_alu_type type)
418 {
419 return get_nir_dest(dest, brw_type_for_nir_type(type));
420 }
421
422 src_reg
423 vec4_visitor::get_nir_src(nir_src src, enum brw_reg_type type,
424 unsigned num_components)
425 {
426 dst_reg reg;
427
428 if (src.is_ssa) {
429 assert(src.ssa != NULL);
430 reg = nir_ssa_values[src.ssa->index];
431 }
432 else {
433 reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
434 src.reg.indirect);
435 }
436
437 reg = retype(reg, type);
438
439 src_reg reg_as_src = src_reg(reg);
440 reg_as_src.swizzle = brw_swizzle_for_size(num_components);
441 return reg_as_src;
442 }
443
444 src_reg
445 vec4_visitor::get_nir_src(nir_src src, nir_alu_type type,
446 unsigned num_components)
447 {
448 return get_nir_src(src, brw_type_for_nir_type(type), num_components);
449 }
450
451 src_reg
452 vec4_visitor::get_nir_src(nir_src src, unsigned num_components)
453 {
454 /* if type is not specified, default to signed int */
455 return get_nir_src(src, nir_type_int, num_components);
456 }
457
458 void
459 vec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
460 {
461 dst_reg reg = dst_reg(GRF, alloc.allocate(1));
462 reg.type = BRW_REGISTER_TYPE_D;
463
464 unsigned remaining = brw_writemask_for_size(instr->def.num_components);
465
466 /* @FIXME: consider emitting vector operations to save some MOVs in
467 * cases where the components are representable in 8 bits.
468 * For now, we emit a MOV for each distinct value.
469 */
470 for (unsigned i = 0; i < instr->def.num_components; i++) {
471 unsigned writemask = 1 << i;
472
473 if ((remaining & writemask) == 0)
474 continue;
475
476 for (unsigned j = i; j < instr->def.num_components; j++) {
477 if (instr->value.u[i] == instr->value.u[j]) {
478 writemask |= 1 << j;
479 }
480 }
481
482 reg.writemask = writemask;
483 emit(MOV(reg, src_reg(instr->value.i[i])));
484
485 remaining &= ~writemask;
486 }
487
488 /* Set final writemask */
489 reg.writemask = brw_writemask_for_size(instr->def.num_components);
490
491 nir_ssa_values[instr->def.index] = reg;
492 }
493
494 void
495 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
496 {
497 dst_reg dest;
498 src_reg src;
499
500 bool has_indirect = false;
501
502 switch (instr->intrinsic) {
503
504 case nir_intrinsic_load_input_indirect:
505 has_indirect = true;
506 /* fallthrough */
507 case nir_intrinsic_load_input: {
508 int offset = instr->const_index[0];
509 src = nir_inputs[offset];
510
511 if (has_indirect) {
512 dest.reladdr = new(mem_ctx) src_reg(get_nir_src(instr->src[0],
513 BRW_REGISTER_TYPE_D,
514 1));
515 }
516 dest = get_nir_dest(instr->dest, src.type);
517 dest.writemask = brw_writemask_for_size(instr->num_components);
518
519 emit(MOV(dest, src));
520 break;
521 }
522
523 case nir_intrinsic_store_output_indirect:
524 has_indirect = true;
525 /* fallthrough */
526 case nir_intrinsic_store_output: {
527 int varying = instr->const_index[0];
528
529 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
530 instr->num_components);
531 dest = dst_reg(src);
532
533 if (has_indirect) {
534 dest.reladdr = new(mem_ctx) src_reg(get_nir_src(instr->src[1],
535 BRW_REGISTER_TYPE_D,
536 1));
537 }
538 output_reg[varying] = dest;
539 break;
540 }
541
542 case nir_intrinsic_load_vertex_id:
543 unreachable("should be lowered by lower_vertex_id()");
544
545 case nir_intrinsic_load_vertex_id_zero_base:
546 case nir_intrinsic_load_base_vertex:
547 case nir_intrinsic_load_instance_id: {
548 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
549 src_reg val = src_reg(nir_system_values[sv]);
550 assert(val.file != BAD_FILE);
551 dest = get_nir_dest(instr->dest, val.type);
552 emit(MOV(dest, val));
553 break;
554 }
555
556 case nir_intrinsic_load_uniform_indirect:
557 has_indirect = true;
558 /* fallthrough */
559 case nir_intrinsic_load_uniform: {
560 dest = get_nir_dest(instr->dest);
561
562 src = src_reg(dst_reg(UNIFORM, instr->const_index[0]));
563 src.reg_offset = instr->const_index[1];
564
565 if (has_indirect) {
566 src_reg tmp = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_D, 1);
567 src.reladdr = new(mem_ctx) src_reg(tmp);
568 }
569
570 emit(MOV(dest, src));
571 break;
572 }
573
574 case nir_intrinsic_atomic_counter_read:
575 case nir_intrinsic_atomic_counter_inc:
576 case nir_intrinsic_atomic_counter_dec: {
577 unsigned surf_index = prog_data->base.binding_table.abo_start +
578 (unsigned) instr->const_index[0];
579 src_reg offset = get_nir_src(instr->src[0], nir_type_int,
580 instr->num_components);
581 dest = get_nir_dest(instr->dest);
582
583 switch (instr->intrinsic) {
584 case nir_intrinsic_atomic_counter_inc:
585 emit_untyped_atomic(BRW_AOP_INC, surf_index, dest, offset,
586 src_reg(), src_reg());
587 break;
588 case nir_intrinsic_atomic_counter_dec:
589 emit_untyped_atomic(BRW_AOP_PREDEC, surf_index, dest, offset,
590 src_reg(), src_reg());
591 break;
592 case nir_intrinsic_atomic_counter_read:
593 emit_untyped_surface_read(surf_index, dest, offset);
594 break;
595 default:
596 unreachable("Unreachable");
597 }
598
599 brw_mark_surface_used(stage_prog_data, surf_index);
600 break;
601 }
602
603 case nir_intrinsic_load_ubo_indirect:
604 has_indirect = true;
605 /* fallthrough */
606 case nir_intrinsic_load_ubo: {
607 nir_const_value *const_block_index = nir_src_as_const_value(instr->src[0]);
608 src_reg surf_index;
609
610 dest = get_nir_dest(instr->dest);
611
612 if (const_block_index) {
613 /* The block index is a constant, so just emit the binding table entry
614 * as an immediate.
615 */
616 surf_index = src_reg(prog_data->base.binding_table.ubo_start +
617 const_block_index->u[0]);
618 } else {
619 /* The block index is not a constant. Evaluate the index expression
620 * per-channel and add the base UBO index; we have to select a value
621 * from any live channel.
622 */
623 surf_index = src_reg(this, glsl_type::uint_type);
624 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int,
625 instr->num_components),
626 src_reg(prog_data->base.binding_table.ubo_start)));
627 surf_index = emit_uniformize(surf_index);
628
629 /* Assume this may touch any UBO. It would be nice to provide
630 * a tighter bound, but the array information is already lowered away.
631 */
632 brw_mark_surface_used(&prog_data->base,
633 prog_data->base.binding_table.ubo_start +
634 shader_prog->NumUniformBlocks - 1);
635 }
636
637 unsigned const_offset = instr->const_index[0];
638 src_reg offset;
639
640 if (!has_indirect) {
641 offset = src_reg(const_offset / 16);
642 } else {
643 offset = src_reg(this, glsl_type::uint_type);
644 emit(SHR(dst_reg(offset), get_nir_src(instr->src[1], nir_type_int, 1),
645 src_reg(4u)));
646 }
647
648 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
649 packed_consts.type = dest.type;
650
651 emit_pull_constant_load_reg(dst_reg(packed_consts),
652 surf_index,
653 offset,
654 NULL, NULL /* before_block/inst */);
655
656 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
657 packed_consts.swizzle += BRW_SWIZZLE4(const_offset % 16 / 4,
658 const_offset % 16 / 4,
659 const_offset % 16 / 4,
660 const_offset % 16 / 4);
661
662 emit(MOV(dest, packed_consts));
663 break;
664 }
665
666 default:
667 unreachable("Unknown intrinsic");
668 }
669 }
670
671 static unsigned
672 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
673 {
674 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
675 }
676
677 static enum brw_conditional_mod
678 brw_conditional_for_nir_comparison(nir_op op)
679 {
680 switch (op) {
681 case nir_op_flt:
682 case nir_op_ilt:
683 case nir_op_ult:
684 return BRW_CONDITIONAL_L;
685
686 case nir_op_fge:
687 case nir_op_ige:
688 case nir_op_uge:
689 return BRW_CONDITIONAL_GE;
690
691 case nir_op_feq:
692 case nir_op_ieq:
693 case nir_op_ball_fequal2:
694 case nir_op_ball_iequal2:
695 case nir_op_ball_fequal3:
696 case nir_op_ball_iequal3:
697 case nir_op_ball_fequal4:
698 case nir_op_ball_iequal4:
699 return BRW_CONDITIONAL_Z;
700
701 case nir_op_fne:
702 case nir_op_ine:
703 case nir_op_bany_fnequal2:
704 case nir_op_bany_inequal2:
705 case nir_op_bany_fnequal3:
706 case nir_op_bany_inequal3:
707 case nir_op_bany_fnequal4:
708 case nir_op_bany_inequal4:
709 return BRW_CONDITIONAL_NZ;
710
711 default:
712 unreachable("not reached: bad operation for comparison");
713 }
714 }
715
716 void
717 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
718 {
719 vec4_instruction *inst;
720
721 dst_reg dst = get_nir_dest(instr->dest.dest,
722 nir_op_infos[instr->op].output_type);
723 dst.writemask = instr->dest.write_mask;
724
725 src_reg op[4];
726 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
727 op[i] = get_nir_src(instr->src[i].src,
728 nir_op_infos[instr->op].input_types[i], 4);
729 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
730 op[i].abs = instr->src[i].abs;
731 op[i].negate = instr->src[i].negate;
732 }
733
734 switch (instr->op) {
735 case nir_op_imov:
736 case nir_op_fmov:
737 inst = emit(MOV(dst, op[0]));
738 inst->saturate = instr->dest.saturate;
739 break;
740
741 case nir_op_vec2:
742 case nir_op_vec3:
743 case nir_op_vec4:
744 unreachable("not reached: should be handled by lower_vec_to_movs()");
745
746 case nir_op_i2f:
747 case nir_op_u2f:
748 inst = emit(MOV(dst, op[0]));
749 inst->saturate = instr->dest.saturate;
750 break;
751
752 case nir_op_f2i:
753 case nir_op_f2u:
754 inst = emit(MOV(dst, op[0]));
755 break;
756
757 case nir_op_fadd:
758 /* fall through */
759 case nir_op_iadd:
760 inst = emit(ADD(dst, op[0], op[1]));
761 inst->saturate = instr->dest.saturate;
762 break;
763
764 case nir_op_fmul:
765 inst = emit(MUL(dst, op[0], op[1]));
766 inst->saturate = instr->dest.saturate;
767 break;
768
769 case nir_op_imul: {
770 if (devinfo->gen < 8) {
771 nir_const_value *value0 = nir_src_as_const_value(instr->src[0].src);
772 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
773
774 /* For integer multiplication, the MUL uses the low 16 bits of one of
775 * the operands (src0 through SNB, src1 on IVB and later). The MACH
776 * accumulates in the contribution of the upper 16 bits of that
777 * operand. If we can determine that one of the args is in the low
778 * 16 bits, though, we can just emit a single MUL.
779 */
780 if (value0 && value0->u[0] < (1 << 16)) {
781 if (devinfo->gen < 7)
782 emit(MUL(dst, op[0], op[1]));
783 else
784 emit(MUL(dst, op[1], op[0]));
785 } else if (value1 && value1->u[0] < (1 << 16)) {
786 if (devinfo->gen < 7)
787 emit(MUL(dst, op[1], op[0]));
788 else
789 emit(MUL(dst, op[0], op[1]));
790 } else {
791 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
792
793 emit(MUL(acc, op[0], op[1]));
794 emit(MACH(dst_null_d(), op[0], op[1]));
795 emit(MOV(dst, src_reg(acc)));
796 }
797 } else {
798 emit(MUL(dst, op[0], op[1]));
799 }
800 break;
801 }
802
803 case nir_op_imul_high:
804 case nir_op_umul_high: {
805 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
806
807 emit(MUL(acc, op[0], op[1]));
808 emit(MACH(dst, op[0], op[1]));
809 break;
810 }
811
812 case nir_op_frcp:
813 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
814 inst->saturate = instr->dest.saturate;
815 break;
816
817 case nir_op_fexp2:
818 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
819 inst->saturate = instr->dest.saturate;
820 break;
821
822 case nir_op_flog2:
823 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
824 inst->saturate = instr->dest.saturate;
825 break;
826
827 case nir_op_fsin:
828 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
829 inst->saturate = instr->dest.saturate;
830 break;
831
832 case nir_op_fcos:
833 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
834 inst->saturate = instr->dest.saturate;
835 break;
836
837 case nir_op_idiv:
838 case nir_op_udiv:
839 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
840 break;
841
842 case nir_op_umod:
843 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
844 break;
845
846 case nir_op_ldexp:
847 unreachable("not reached: should be handled by ldexp_to_arith()");
848
849 case nir_op_fsqrt:
850 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
851 inst->saturate = instr->dest.saturate;
852 break;
853
854 case nir_op_frsq:
855 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
856 inst->saturate = instr->dest.saturate;
857 break;
858
859 case nir_op_fpow:
860 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
861 inst->saturate = instr->dest.saturate;
862 break;
863
864 case nir_op_uadd_carry: {
865 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
866
867 emit(ADDC(dst_null_ud(), op[0], op[1]));
868 emit(MOV(dst, src_reg(acc)));
869 break;
870 }
871
872 case nir_op_usub_borrow: {
873 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
874
875 emit(SUBB(dst_null_ud(), op[0], op[1]));
876 emit(MOV(dst, src_reg(acc)));
877 break;
878 }
879
880 case nir_op_ftrunc:
881 inst = emit(RNDZ(dst, op[0]));
882 inst->saturate = instr->dest.saturate;
883 break;
884
885 case nir_op_fceil: {
886 src_reg tmp = src_reg(this, glsl_type::float_type);
887 tmp.swizzle =
888 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
889 instr->src[0].src.ssa->num_components :
890 instr->src[0].src.reg.reg->num_components);
891
892 op[0].negate = !op[0].negate;
893 emit(RNDD(dst_reg(tmp), op[0]));
894 tmp.negate = true;
895 inst = emit(MOV(dst, tmp));
896 inst->saturate = instr->dest.saturate;
897 break;
898 }
899
900 case nir_op_ffloor:
901 inst = emit(RNDD(dst, op[0]));
902 inst->saturate = instr->dest.saturate;
903 break;
904
905 case nir_op_ffract:
906 inst = emit(FRC(dst, op[0]));
907 inst->saturate = instr->dest.saturate;
908 break;
909
910 case nir_op_fround_even:
911 inst = emit(RNDE(dst, op[0]));
912 inst->saturate = instr->dest.saturate;
913 break;
914
915 case nir_op_fmin:
916 case nir_op_imin:
917 case nir_op_umin:
918 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
919 inst->saturate = instr->dest.saturate;
920 break;
921
922 case nir_op_fmax:
923 case nir_op_imax:
924 case nir_op_umax:
925 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
926 inst->saturate = instr->dest.saturate;
927 break;
928
929 case nir_op_fddx:
930 case nir_op_fddx_coarse:
931 case nir_op_fddx_fine:
932 case nir_op_fddy:
933 case nir_op_fddy_coarse:
934 case nir_op_fddy_fine:
935 unreachable("derivatives are not valid in vertex shaders");
936
937 case nir_op_flt:
938 case nir_op_ilt:
939 case nir_op_ult:
940 case nir_op_fge:
941 case nir_op_ige:
942 case nir_op_uge:
943 case nir_op_feq:
944 case nir_op_ieq:
945 case nir_op_fne:
946 case nir_op_ine:
947 emit(CMP(dst, op[0], op[1],
948 brw_conditional_for_nir_comparison(instr->op)));
949 break;
950
951 case nir_op_ball_fequal2:
952 case nir_op_ball_iequal2:
953 case nir_op_ball_fequal3:
954 case nir_op_ball_iequal3:
955 case nir_op_ball_fequal4:
956 case nir_op_ball_iequal4: {
957 dst_reg tmp = dst_reg(this, glsl_type::bool_type);
958
959 switch (instr->op) {
960 case nir_op_ball_fequal2:
961 case nir_op_ball_iequal2:
962 tmp.writemask = WRITEMASK_XY;
963 break;
964 case nir_op_ball_fequal3:
965 case nir_op_ball_iequal3:
966 tmp.writemask = WRITEMASK_XYZ;
967 break;
968 case nir_op_ball_fequal4:
969 case nir_op_ball_iequal4:
970 tmp.writemask = WRITEMASK_XYZW;
971 break;
972 default:
973 unreachable("not reached");
974 }
975
976 emit(CMP(tmp, op[0], op[1],
977 brw_conditional_for_nir_comparison(instr->op)));
978 emit(MOV(dst, src_reg(0)));
979 inst = emit(MOV(dst, src_reg(~0)));
980 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
981 break;
982 }
983
984 case nir_op_bany_fnequal2:
985 case nir_op_bany_inequal2:
986 case nir_op_bany_fnequal3:
987 case nir_op_bany_inequal3:
988 case nir_op_bany_fnequal4:
989 case nir_op_bany_inequal4: {
990 dst_reg tmp = dst_reg(this, glsl_type::bool_type);
991
992 switch (instr->op) {
993 case nir_op_bany_fnequal2:
994 case nir_op_bany_inequal2:
995 tmp.writemask = WRITEMASK_XY;
996 break;
997 case nir_op_bany_fnequal3:
998 case nir_op_bany_inequal3:
999 tmp.writemask = WRITEMASK_XYZ;
1000 break;
1001 case nir_op_bany_fnequal4:
1002 case nir_op_bany_inequal4:
1003 tmp.writemask = WRITEMASK_XYZW;
1004 break;
1005 default:
1006 unreachable("not reached");
1007 }
1008
1009 emit(CMP(tmp, op[0], op[1],
1010 brw_conditional_for_nir_comparison(instr->op)));
1011
1012 emit(MOV(dst, src_reg(0)));
1013 inst = emit(MOV(dst, src_reg(~0)));
1014 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1015 break;
1016 }
1017
1018 case nir_op_inot:
1019 if (devinfo->gen >= 8) {
1020 op[0] = resolve_source_modifiers(op[0]);
1021 }
1022 emit(NOT(dst, op[0]));
1023 break;
1024
1025 case nir_op_ixor:
1026 if (devinfo->gen >= 8) {
1027 op[0] = resolve_source_modifiers(op[0]);
1028 op[1] = resolve_source_modifiers(op[1]);
1029 }
1030 emit(XOR(dst, op[0], op[1]));
1031 break;
1032
1033 case nir_op_ior:
1034 if (devinfo->gen >= 8) {
1035 op[0] = resolve_source_modifiers(op[0]);
1036 op[1] = resolve_source_modifiers(op[1]);
1037 }
1038 emit(OR(dst, op[0], op[1]));
1039 break;
1040
1041 case nir_op_iand:
1042 if (devinfo->gen >= 8) {
1043 op[0] = resolve_source_modifiers(op[0]);
1044 op[1] = resolve_source_modifiers(op[1]);
1045 }
1046 emit(AND(dst, op[0], op[1]));
1047 break;
1048
1049 case nir_op_b2i:
1050 emit(AND(dst, op[0], src_reg(1)));
1051 break;
1052
1053 case nir_op_b2f:
1054 op[0].type = BRW_REGISTER_TYPE_D;
1055 dst.type = BRW_REGISTER_TYPE_D;
1056 emit(AND(dst, op[0], src_reg(0x3f800000u)));
1057 dst.type = BRW_REGISTER_TYPE_F;
1058 break;
1059
1060 case nir_op_f2b:
1061 emit(CMP(dst, op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
1062 break;
1063
1064 case nir_op_i2b:
1065 emit(CMP(dst, op[0], src_reg(0), BRW_CONDITIONAL_NZ));
1066 break;
1067
1068 case nir_op_fnoise1_1:
1069 case nir_op_fnoise1_2:
1070 case nir_op_fnoise1_3:
1071 case nir_op_fnoise1_4:
1072 case nir_op_fnoise2_1:
1073 case nir_op_fnoise2_2:
1074 case nir_op_fnoise2_3:
1075 case nir_op_fnoise2_4:
1076 case nir_op_fnoise3_1:
1077 case nir_op_fnoise3_2:
1078 case nir_op_fnoise3_3:
1079 case nir_op_fnoise3_4:
1080 case nir_op_fnoise4_1:
1081 case nir_op_fnoise4_2:
1082 case nir_op_fnoise4_3:
1083 case nir_op_fnoise4_4:
1084 unreachable("not reached: should be handled by lower_noise");
1085
1086 case nir_op_unpack_half_2x16_split_x:
1087 case nir_op_unpack_half_2x16_split_y:
1088 case nir_op_pack_half_2x16_split:
1089 unreachable("not reached: should not occur in vertex shader");
1090
1091 case nir_op_unpack_snorm_2x16:
1092 case nir_op_unpack_unorm_2x16:
1093 case nir_op_pack_snorm_2x16:
1094 case nir_op_pack_unorm_2x16:
1095 unreachable("not reached: should be handled by lower_packing_builtins");
1096
1097 case nir_op_unpack_half_2x16:
1098 /* As NIR does not guarantee that we have a correct swizzle outside the
1099 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1100 * uses the source operand in an operation with WRITEMASK_Y while our
1101 * source operand has only size 1, it accessed incorrect data producing
1102 * regressions in Piglit. We repeat the swizzle of the first component on the
1103 * rest of components to avoid regressions. In the vec4_visitor IR code path
1104 * this is not needed because the operand has already the correct swizzle.
1105 */
1106 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1107 emit_unpack_half_2x16(dst, op[0]);
1108 break;
1109
1110 case nir_op_pack_half_2x16:
1111 emit_pack_half_2x16(dst, op[0]);
1112 break;
1113
1114 case nir_op_unpack_unorm_4x8:
1115 emit_unpack_unorm_4x8(dst, op[0]);
1116 break;
1117
1118 case nir_op_pack_unorm_4x8:
1119 emit_pack_unorm_4x8(dst, op[0]);
1120 break;
1121
1122 case nir_op_unpack_snorm_4x8:
1123 emit_unpack_snorm_4x8(dst, op[0]);
1124 break;
1125
1126 case nir_op_pack_snorm_4x8:
1127 emit_pack_snorm_4x8(dst, op[0]);
1128 break;
1129
1130 case nir_op_bitfield_reverse:
1131 emit(BFREV(dst, op[0]));
1132 break;
1133
1134 case nir_op_bit_count:
1135 emit(CBIT(dst, op[0]));
1136 break;
1137
1138 case nir_op_ufind_msb:
1139 case nir_op_ifind_msb: {
1140 src_reg temp = src_reg(this, glsl_type::uint_type);
1141
1142 inst = emit(FBH(dst_reg(temp), op[0]));
1143 inst->dst.writemask = WRITEMASK_XYZW;
1144
1145 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1146 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1147 * subtract the result from 31 to convert the MSB count into an LSB count.
1148 */
1149
1150 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
1151 temp.swizzle = BRW_SWIZZLE_NOOP;
1152 emit(MOV(dst, temp));
1153
1154 src_reg src_tmp = src_reg(dst);
1155 emit(CMP(dst_null_d(), src_tmp, src_reg(-1), BRW_CONDITIONAL_NZ));
1156
1157 src_tmp.negate = true;
1158 inst = emit(ADD(dst, src_tmp, src_reg(31)));
1159 inst->predicate = BRW_PREDICATE_NORMAL;
1160 break;
1161 }
1162
1163 case nir_op_find_lsb:
1164 emit(FBL(dst, op[0]));
1165 break;
1166
1167 case nir_op_ubitfield_extract:
1168 case nir_op_ibitfield_extract:
1169 op[0] = fix_3src_operand(op[0]);
1170 op[1] = fix_3src_operand(op[1]);
1171 op[2] = fix_3src_operand(op[2]);
1172
1173 emit(BFE(dst, op[2], op[1], op[0]));
1174 break;
1175
1176 case nir_op_bfm:
1177 emit(BFI1(dst, op[0], op[1]));
1178 break;
1179
1180 case nir_op_bfi:
1181 op[0] = fix_3src_operand(op[0]);
1182 op[1] = fix_3src_operand(op[1]);
1183 op[2] = fix_3src_operand(op[2]);
1184
1185 emit(BFI2(dst, op[0], op[1], op[2]));
1186 break;
1187
1188 case nir_op_bitfield_insert:
1189 unreachable("not reached: should be handled by "
1190 "lower_instructions::bitfield_insert_to_bfm_bfi");
1191
1192 case nir_op_fsign:
1193 /* AND(val, 0x80000000) gives the sign bit.
1194 *
1195 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1196 * zero.
1197 */
1198 emit(CMP(dst_null_f(), op[0], src_reg(0.0f), BRW_CONDITIONAL_NZ));
1199
1200 op[0].type = BRW_REGISTER_TYPE_UD;
1201 dst.type = BRW_REGISTER_TYPE_UD;
1202 emit(AND(dst, op[0], src_reg(0x80000000u)));
1203
1204 inst = emit(OR(dst, src_reg(dst), src_reg(0x3f800000u)));
1205 inst->predicate = BRW_PREDICATE_NORMAL;
1206 dst.type = BRW_REGISTER_TYPE_F;
1207
1208 if (instr->dest.saturate) {
1209 inst = emit(MOV(dst, src_reg(dst)));
1210 inst->saturate = true;
1211 }
1212 break;
1213
1214 case nir_op_isign:
1215 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1216 * -> non-negative val generates 0x00000000.
1217 * Predicated OR sets 1 if val is positive.
1218 */
1219 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_G));
1220 emit(ASR(dst, op[0], src_reg(31)));
1221 inst = emit(OR(dst, src_reg(dst), src_reg(1)));
1222 inst->predicate = BRW_PREDICATE_NORMAL;
1223 break;
1224
1225 case nir_op_ishl:
1226 emit(SHL(dst, op[0], op[1]));
1227 break;
1228
1229 case nir_op_ishr:
1230 emit(ASR(dst, op[0], op[1]));
1231 break;
1232
1233 case nir_op_ushr:
1234 emit(SHR(dst, op[0], op[1]));
1235 break;
1236
1237 case nir_op_ffma:
1238 op[0] = fix_3src_operand(op[0]);
1239 op[1] = fix_3src_operand(op[1]);
1240 op[2] = fix_3src_operand(op[2]);
1241
1242 inst = emit(MAD(dst, op[2], op[1], op[0]));
1243 inst->saturate = instr->dest.saturate;
1244 break;
1245
1246 case nir_op_flrp:
1247 inst = emit_lrp(dst, op[0], op[1], op[2]);
1248 inst->saturate = instr->dest.saturate;
1249 break;
1250
1251 case nir_op_bcsel:
1252 emit(CMP(dst_null_d(), op[0], src_reg(0), BRW_CONDITIONAL_NZ));
1253 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1254 inst->predicate = BRW_PREDICATE_NORMAL;
1255 break;
1256
1257 case nir_op_fdot_replicated2:
1258 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1259 inst->saturate = instr->dest.saturate;
1260 break;
1261
1262 case nir_op_fdot_replicated3:
1263 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1264 inst->saturate = instr->dest.saturate;
1265 break;
1266
1267 case nir_op_fdot_replicated4:
1268 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1269 inst->saturate = instr->dest.saturate;
1270 break;
1271
1272 case nir_op_bany2:
1273 case nir_op_bany3:
1274 case nir_op_bany4: {
1275 dst_reg tmp = dst_reg(this, glsl_type::bool_type);
1276 tmp.writemask = brw_writemask_for_size(nir_op_infos[instr->op].input_sizes[0]);
1277
1278 emit(CMP(tmp, op[0], src_reg(0), BRW_CONDITIONAL_NZ));
1279
1280 emit(MOV(dst, src_reg(0)));
1281 inst = emit(MOV(dst, src_reg(~0)));
1282 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1283 break;
1284 }
1285
1286 case nir_op_fabs:
1287 case nir_op_iabs:
1288 case nir_op_fneg:
1289 case nir_op_ineg:
1290 case nir_op_fsat:
1291 unreachable("not reached: should be lowered by lower_source mods");
1292
1293 case nir_op_fdiv:
1294 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1295
1296 case nir_op_fmod:
1297 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1298
1299 case nir_op_fsub:
1300 case nir_op_isub:
1301 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1302
1303 default:
1304 unreachable("Unimplemented ALU operation");
1305 }
1306
1307 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1308 * to sign extend the low bit to 0/~0
1309 */
1310 if (devinfo->gen <= 5 &&
1311 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1312 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1313 dst_reg masked = dst_reg(this, glsl_type::int_type);
1314 masked.writemask = dst.writemask;
1315 emit(AND(masked, src_reg(dst), src_reg(1)));
1316 src_reg masked_neg = src_reg(masked);
1317 masked_neg.negate = true;
1318 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1319 }
1320 }
1321
1322 void
1323 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1324 {
1325 switch (instr->type) {
1326 case nir_jump_break:
1327 emit(BRW_OPCODE_BREAK);
1328 break;
1329
1330 case nir_jump_continue:
1331 emit(BRW_OPCODE_CONTINUE);
1332 break;
1333
1334 case nir_jump_return:
1335 /* fall through */
1336 default:
1337 unreachable("unknown jump");
1338 }
1339 }
1340
1341 enum ir_texture_opcode
1342 ir_texture_opcode_for_nir_texop(nir_texop texop)
1343 {
1344 enum ir_texture_opcode op;
1345
1346 switch (texop) {
1347 case nir_texop_lod: op = ir_lod; break;
1348 case nir_texop_query_levels: op = ir_query_levels; break;
1349 case nir_texop_texture_samples: op = ir_texture_samples; break;
1350 case nir_texop_tex: op = ir_tex; break;
1351 case nir_texop_tg4: op = ir_tg4; break;
1352 case nir_texop_txb: op = ir_txb; break;
1353 case nir_texop_txd: op = ir_txd; break;
1354 case nir_texop_txf: op = ir_txf; break;
1355 case nir_texop_txf_ms: op = ir_txf_ms; break;
1356 case nir_texop_txl: op = ir_txl; break;
1357 case nir_texop_txs: op = ir_txs; break;
1358 default:
1359 unreachable("unknown texture opcode");
1360 }
1361
1362 return op;
1363 }
1364 const glsl_type *
1365 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
1366 unsigned components)
1367 {
1368 switch (alu_type) {
1369 case nir_type_float:
1370 return glsl_type::vec(components);
1371 case nir_type_int:
1372 return glsl_type::ivec(components);
1373 case nir_type_unsigned:
1374 return glsl_type::uvec(components);
1375 case nir_type_bool:
1376 return glsl_type::bvec(components);
1377 default:
1378 return glsl_type::error_type;
1379 }
1380
1381 return glsl_type::error_type;
1382 }
1383
1384 void
1385 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1386 {
1387 unsigned sampler = instr->sampler_index;
1388 src_reg sampler_reg = src_reg(sampler);
1389 src_reg coordinate;
1390 const glsl_type *coord_type = NULL;
1391 src_reg shadow_comparitor;
1392 src_reg offset_value;
1393 src_reg lod, lod2;
1394 src_reg sample_index;
1395 src_reg mcs;
1396
1397 const glsl_type *dest_type =
1398 glsl_type_for_nir_alu_type(instr->dest_type,
1399 nir_tex_instr_dest_size(instr));
1400 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1401
1402 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
1403 * emitting anything other than setting up the constant result.
1404 */
1405 if (instr->op == nir_texop_tg4) {
1406 int swiz = GET_SWZ(key_tex->swizzles[sampler], instr->component);
1407 if (swiz == SWIZZLE_ZERO || swiz == SWIZZLE_ONE) {
1408 emit(MOV(dest, src_reg(swiz == SWIZZLE_ONE ? 1.0f : 0.0f)));
1409 return;
1410 }
1411 }
1412
1413 /* Load the texture operation sources */
1414 for (unsigned i = 0; i < instr->num_srcs; i++) {
1415 switch (instr->src[i].src_type) {
1416 case nir_tex_src_comparitor:
1417 shadow_comparitor = get_nir_src(instr->src[i].src,
1418 BRW_REGISTER_TYPE_F, 1);
1419 break;
1420
1421 case nir_tex_src_coord: {
1422 unsigned src_size = nir_tex_instr_src_size(instr, i);
1423
1424 switch (instr->op) {
1425 case nir_texop_txf:
1426 case nir_texop_txf_ms:
1427 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1428 src_size);
1429 coord_type = glsl_type::ivec(src_size);
1430 break;
1431
1432 default:
1433 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1434 src_size);
1435 coord_type = glsl_type::vec(src_size);
1436 break;
1437 }
1438 break;
1439 }
1440
1441 case nir_tex_src_ddx:
1442 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1443 nir_tex_instr_src_size(instr, i));
1444 break;
1445
1446 case nir_tex_src_ddy:
1447 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1448 nir_tex_instr_src_size(instr, i));
1449 break;
1450
1451 case nir_tex_src_lod:
1452 switch (instr->op) {
1453 case nir_texop_txs:
1454 case nir_texop_txf:
1455 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1456 break;
1457
1458 default:
1459 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
1460 break;
1461 }
1462 break;
1463
1464 case nir_tex_src_ms_index: {
1465 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1466 assert(coord_type != NULL);
1467 if (devinfo->gen >= 7 &&
1468 key_tex->compressed_multisample_layout_mask & (1 << sampler)) {
1469 mcs = emit_mcs_fetch(coord_type, coordinate, sampler_reg);
1470 } else {
1471 mcs = src_reg(0u);
1472 }
1473 mcs = retype(mcs, BRW_REGISTER_TYPE_UD);
1474 break;
1475 }
1476
1477 case nir_tex_src_offset:
1478 offset_value = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
1479 break;
1480
1481 case nir_tex_src_sampler_offset: {
1482 /* The highest sampler which may be used by this operation is
1483 * the last element of the array. Mark it here, because the generator
1484 * doesn't have enough information to determine the bound.
1485 */
1486 uint32_t array_size = instr->sampler_array_size;
1487 uint32_t max_used = sampler + array_size - 1;
1488 if (instr->op == nir_texop_tg4) {
1489 max_used += prog_data->base.binding_table.gather_texture_start;
1490 } else {
1491 max_used += prog_data->base.binding_table.texture_start;
1492 }
1493
1494 brw_mark_surface_used(&prog_data->base, max_used);
1495
1496 /* Emit code to evaluate the actual indexing expression */
1497 src_reg src = get_nir_src(instr->src[i].src, 1);
1498 src_reg temp(this, glsl_type::uint_type);
1499 emit(ADD(dst_reg(temp), src, src_reg(sampler)));
1500 sampler_reg = emit_uniformize(temp);
1501 break;
1502 }
1503
1504 case nir_tex_src_projector:
1505 unreachable("Should be lowered by do_lower_texture_projection");
1506
1507 case nir_tex_src_bias:
1508 unreachable("LOD bias is not valid for vertex shaders.\n");
1509
1510 default:
1511 unreachable("unknown texture source");
1512 }
1513 }
1514
1515 uint32_t constant_offset = 0;
1516 for (unsigned i = 0; i < 3; i++) {
1517 if (instr->const_offset[i] != 0) {
1518 constant_offset = brw_texture_offset(instr->const_offset, 3);
1519 break;
1520 }
1521 }
1522
1523 /* Stuff the channel select bits in the top of the texture offset */
1524 if (instr->op == nir_texop_tg4)
1525 constant_offset |= gather_channel(instr->component, sampler) << 16;
1526
1527 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
1528
1529 bool is_cube_array =
1530 instr->op == nir_texop_txs &&
1531 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
1532 instr->is_array;
1533
1534 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
1535 shadow_comparitor,
1536 lod, lod2, sample_index,
1537 constant_offset, offset_value,
1538 mcs, is_cube_array, sampler, sampler_reg);
1539 }
1540
1541 void
1542 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
1543 {
1544 nir_ssa_values[instr->def.index] = dst_reg(GRF, alloc.allocate(1));
1545 }
1546
1547 }