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