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