i965: add double support packing support to gs inputs
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_nir.cpp
1 /*
2 * Copyright © 2010 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 "compiler/glsl/ir.h"
25 #include "brw_fs.h"
26 #include "brw_fs_surface_builder.h"
27 #include "brw_nir.h"
28 #include "brw_program.h"
29
30 using namespace brw;
31 using namespace brw::surface_access;
32
33 void
34 fs_visitor::emit_nir_code()
35 {
36 /* emit the arrays used for inputs and outputs - load/store intrinsics will
37 * be converted to reads/writes of these arrays
38 */
39 nir_setup_inputs();
40 nir_setup_outputs();
41 nir_setup_uniforms();
42 nir_emit_system_values();
43
44 /* get the main function and emit it */
45 nir_foreach_function(function, nir) {
46 assert(strcmp(function->name, "main") == 0);
47 assert(function->impl);
48 nir_emit_impl(function->impl);
49 }
50 }
51
52 void
53 fs_visitor::nir_setup_inputs()
54 {
55 if (stage != MESA_SHADER_FRAGMENT)
56 return;
57
58 nir_inputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_inputs);
59
60 nir_foreach_variable(var, &nir->inputs) {
61 fs_reg input = offset(nir_inputs, bld, var->data.driver_location);
62
63 fs_reg reg;
64 if (var->data.location == VARYING_SLOT_POS) {
65 reg = *emit_fragcoord_interpolation();
66 emit_percomp(bld, fs_inst(BRW_OPCODE_MOV, bld.dispatch_width(),
67 input, reg), 0xF);
68 } else if (var->data.location == VARYING_SLOT_LAYER) {
69 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_LAYER, 1), 3);
70 reg.type = BRW_REGISTER_TYPE_D;
71 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
72 } else if (var->data.location == VARYING_SLOT_VIEWPORT) {
73 struct brw_reg reg = suboffset(interp_reg(VARYING_SLOT_VIEWPORT, 2), 3);
74 reg.type = BRW_REGISTER_TYPE_D;
75 bld.emit(FS_OPCODE_CINTERP, retype(input, BRW_REGISTER_TYPE_D), reg);
76 } else {
77 int location = var->data.location;
78 emit_general_interpolation(&input, var->name, var->type,
79 (glsl_interp_qualifier) var->data.interpolation,
80 &location, var->data.centroid,
81 var->data.sample);
82 }
83 }
84 }
85
86 void
87 fs_visitor::nir_setup_single_output_varying(fs_reg *reg,
88 const glsl_type *type,
89 unsigned *location)
90 {
91 if (type->is_array() || type->is_matrix()) {
92 const struct glsl_type *elem_type = glsl_get_array_element(type);
93 const unsigned length = glsl_get_length(type);
94
95 for (unsigned i = 0; i < length; i++) {
96 nir_setup_single_output_varying(reg, elem_type, location);
97 }
98 } else if (type->is_record()) {
99 for (unsigned i = 0; i < type->length; i++) {
100 const struct glsl_type *field_type = type->fields.structure[i].type;
101 nir_setup_single_output_varying(reg, field_type, location);
102 }
103 } else {
104 assert(type->is_scalar() || type->is_vector());
105 unsigned num_elements = type->vector_elements;
106 if (type->is_double())
107 num_elements *= 2;
108 for (unsigned count = 0; count < num_elements; count += 4) {
109 this->outputs[*location] = *reg;
110 this->output_components[*location] = MIN2(4, num_elements - count);
111 *reg = offset(*reg, bld, this->output_components[*location]);
112 (*location)++;
113 }
114 }
115 }
116
117 void
118 fs_visitor::nir_setup_outputs()
119 {
120 if (stage == MESA_SHADER_TESS_CTRL)
121 return;
122
123 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
124
125 nir_outputs = bld.vgrf(BRW_REGISTER_TYPE_F, nir->num_outputs);
126
127 nir_foreach_variable(var, &nir->outputs) {
128 fs_reg reg = offset(nir_outputs, bld, var->data.driver_location);
129
130 switch (stage) {
131 case MESA_SHADER_VERTEX:
132 case MESA_SHADER_TESS_EVAL:
133 case MESA_SHADER_GEOMETRY: {
134 unsigned location = var->data.location;
135 nir_setup_single_output_varying(&reg, var->type, &location);
136 break;
137 }
138 case MESA_SHADER_FRAGMENT:
139 if (key->force_dual_color_blend &&
140 var->data.location == FRAG_RESULT_DATA1) {
141 this->dual_src_output = reg;
142 this->do_dual_src = true;
143 } else if (var->data.index > 0) {
144 assert(var->data.location == FRAG_RESULT_DATA0);
145 assert(var->data.index == 1);
146 this->dual_src_output = reg;
147 this->do_dual_src = true;
148 } else if (var->data.location == FRAG_RESULT_COLOR) {
149 /* Writing gl_FragColor outputs to all color regions. */
150 for (unsigned int i = 0; i < MAX2(key->nr_color_regions, 1); i++) {
151 this->outputs[i] = reg;
152 this->output_components[i] = 4;
153 }
154 } else if (var->data.location == FRAG_RESULT_DEPTH) {
155 this->frag_depth = reg;
156 } else if (var->data.location == FRAG_RESULT_STENCIL) {
157 this->frag_stencil = reg;
158 } else if (var->data.location == FRAG_RESULT_SAMPLE_MASK) {
159 this->sample_mask = reg;
160 } else {
161 int vector_elements = var->type->without_array()->vector_elements;
162
163 /* gl_FragData or a user-defined FS output */
164 assert(var->data.location >= FRAG_RESULT_DATA0 &&
165 var->data.location < FRAG_RESULT_DATA0+BRW_MAX_DRAW_BUFFERS);
166
167 /* General color output. */
168 for (unsigned int i = 0; i < MAX2(1, var->type->length); i++) {
169 int output = var->data.location - FRAG_RESULT_DATA0 + i;
170 this->outputs[output] = offset(reg, bld, vector_elements * i);
171 this->output_components[output] = vector_elements;
172 }
173 }
174 break;
175 default:
176 unreachable("unhandled shader stage");
177 }
178 }
179 }
180
181 void
182 fs_visitor::nir_setup_uniforms()
183 {
184 if (dispatch_width != min_dispatch_width)
185 return;
186
187 uniforms = nir->num_uniforms / 4;
188 }
189
190 static bool
191 emit_system_values_block(nir_block *block, fs_visitor *v)
192 {
193 fs_reg *reg;
194
195 nir_foreach_instr(instr, block) {
196 if (instr->type != nir_instr_type_intrinsic)
197 continue;
198
199 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
200 switch (intrin->intrinsic) {
201 case nir_intrinsic_load_vertex_id:
202 unreachable("should be lowered by lower_vertex_id().");
203
204 case nir_intrinsic_load_vertex_id_zero_base:
205 assert(v->stage == MESA_SHADER_VERTEX);
206 reg = &v->nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
207 if (reg->file == BAD_FILE)
208 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE);
209 break;
210
211 case nir_intrinsic_load_base_vertex:
212 assert(v->stage == MESA_SHADER_VERTEX);
213 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
214 if (reg->file == BAD_FILE)
215 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_VERTEX);
216 break;
217
218 case nir_intrinsic_load_instance_id:
219 assert(v->stage == MESA_SHADER_VERTEX);
220 reg = &v->nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
221 if (reg->file == BAD_FILE)
222 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_INSTANCE_ID);
223 break;
224
225 case nir_intrinsic_load_base_instance:
226 assert(v->stage == MESA_SHADER_VERTEX);
227 reg = &v->nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
228 if (reg->file == BAD_FILE)
229 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_BASE_INSTANCE);
230 break;
231
232 case nir_intrinsic_load_draw_id:
233 assert(v->stage == MESA_SHADER_VERTEX);
234 reg = &v->nir_system_values[SYSTEM_VALUE_DRAW_ID];
235 if (reg->file == BAD_FILE)
236 *reg = *v->emit_vs_system_value(SYSTEM_VALUE_DRAW_ID);
237 break;
238
239 case nir_intrinsic_load_invocation_id:
240 if (v->stage == MESA_SHADER_TESS_CTRL)
241 break;
242 assert(v->stage == MESA_SHADER_GEOMETRY);
243 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
244 if (reg->file == BAD_FILE) {
245 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
246 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
247 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
248 abld.SHR(iid, g1, brw_imm_ud(27u));
249 *reg = iid;
250 }
251 break;
252
253 case nir_intrinsic_load_sample_pos:
254 assert(v->stage == MESA_SHADER_FRAGMENT);
255 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
256 if (reg->file == BAD_FILE)
257 *reg = *v->emit_samplepos_setup();
258 break;
259
260 case nir_intrinsic_load_sample_id:
261 assert(v->stage == MESA_SHADER_FRAGMENT);
262 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
263 if (reg->file == BAD_FILE)
264 *reg = *v->emit_sampleid_setup();
265 break;
266
267 case nir_intrinsic_load_sample_mask_in:
268 assert(v->stage == MESA_SHADER_FRAGMENT);
269 assert(v->devinfo->gen >= 7);
270 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
271 if (reg->file == BAD_FILE)
272 *reg = *v->emit_samplemaskin_setup();
273 break;
274
275 case nir_intrinsic_load_work_group_id:
276 assert(v->stage == MESA_SHADER_COMPUTE);
277 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
278 if (reg->file == BAD_FILE)
279 *reg = *v->emit_cs_work_group_id_setup();
280 break;
281
282 case nir_intrinsic_load_helper_invocation:
283 assert(v->stage == MESA_SHADER_FRAGMENT);
284 reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
285 if (reg->file == BAD_FILE) {
286 const fs_builder abld =
287 v->bld.annotate("gl_HelperInvocation", NULL);
288
289 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
290 * pixel mask is in g1.7 of the thread payload.
291 *
292 * We move the per-channel pixel enable bit to the low bit of each
293 * channel by shifting the byte containing the pixel mask by the
294 * vector immediate 0x76543210UV.
295 *
296 * The region of <1,8,0> reads only 1 byte (the pixel masks for
297 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
298 * masks for 2 and 3) in SIMD16.
299 */
300 fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
301 abld.SHR(shifted,
302 stride(byte_offset(retype(brw_vec1_grf(1, 0),
303 BRW_REGISTER_TYPE_UB), 28),
304 1, 8, 0),
305 brw_imm_v(0x76543210));
306
307 /* A set bit in the pixel mask means the channel is enabled, but
308 * that is the opposite of gl_HelperInvocation so we need to invert
309 * the mask.
310 *
311 * The negate source-modifier bit of logical instructions on Gen8+
312 * performs 1's complement negation, so we can use that instead of
313 * a NOT instruction.
314 */
315 fs_reg inverted = negate(shifted);
316 if (v->devinfo->gen < 8) {
317 inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
318 abld.NOT(inverted, shifted);
319 }
320
321 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
322 * with 1 and negating.
323 */
324 fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
325 abld.AND(anded, inverted, brw_imm_uw(1));
326
327 fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
328 abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
329 *reg = dst;
330 }
331 break;
332
333 default:
334 break;
335 }
336 }
337
338 return true;
339 }
340
341 void
342 fs_visitor::nir_emit_system_values()
343 {
344 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
345 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
346 nir_system_values[i] = fs_reg();
347 }
348
349 nir_foreach_function(function, nir) {
350 assert(strcmp(function->name, "main") == 0);
351 assert(function->impl);
352 nir_foreach_block(block, function->impl) {
353 emit_system_values_block(block, this);
354 }
355 }
356 }
357
358 void
359 fs_visitor::nir_emit_impl(nir_function_impl *impl)
360 {
361 nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
362 for (unsigned i = 0; i < impl->reg_alloc; i++) {
363 nir_locals[i] = fs_reg();
364 }
365
366 foreach_list_typed(nir_register, reg, node, &impl->registers) {
367 unsigned array_elems =
368 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
369 unsigned size = array_elems * reg->num_components;
370 const brw_reg_type reg_type =
371 reg->bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
372 nir_locals[reg->index] = bld.vgrf(reg_type, size);
373 }
374
375 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
376 impl->ssa_alloc);
377
378 nir_emit_cf_list(&impl->body);
379 }
380
381 void
382 fs_visitor::nir_emit_cf_list(exec_list *list)
383 {
384 exec_list_validate(list);
385 foreach_list_typed(nir_cf_node, node, node, list) {
386 switch (node->type) {
387 case nir_cf_node_if:
388 nir_emit_if(nir_cf_node_as_if(node));
389 break;
390
391 case nir_cf_node_loop:
392 nir_emit_loop(nir_cf_node_as_loop(node));
393 break;
394
395 case nir_cf_node_block:
396 nir_emit_block(nir_cf_node_as_block(node));
397 break;
398
399 default:
400 unreachable("Invalid CFG node block");
401 }
402 }
403 }
404
405 void
406 fs_visitor::nir_emit_if(nir_if *if_stmt)
407 {
408 /* first, put the condition into f0 */
409 fs_inst *inst = bld.MOV(bld.null_reg_d(),
410 retype(get_nir_src(if_stmt->condition),
411 BRW_REGISTER_TYPE_D));
412 inst->conditional_mod = BRW_CONDITIONAL_NZ;
413
414 bld.IF(BRW_PREDICATE_NORMAL);
415
416 nir_emit_cf_list(&if_stmt->then_list);
417
418 /* note: if the else is empty, dead CF elimination will remove it */
419 bld.emit(BRW_OPCODE_ELSE);
420
421 nir_emit_cf_list(&if_stmt->else_list);
422
423 bld.emit(BRW_OPCODE_ENDIF);
424 }
425
426 void
427 fs_visitor::nir_emit_loop(nir_loop *loop)
428 {
429 bld.emit(BRW_OPCODE_DO);
430
431 nir_emit_cf_list(&loop->body);
432
433 bld.emit(BRW_OPCODE_WHILE);
434 }
435
436 void
437 fs_visitor::nir_emit_block(nir_block *block)
438 {
439 nir_foreach_instr(instr, block) {
440 nir_emit_instr(instr);
441 }
442 }
443
444 void
445 fs_visitor::nir_emit_instr(nir_instr *instr)
446 {
447 const fs_builder abld = bld.annotate(NULL, instr);
448
449 switch (instr->type) {
450 case nir_instr_type_alu:
451 nir_emit_alu(abld, nir_instr_as_alu(instr));
452 break;
453
454 case nir_instr_type_intrinsic:
455 switch (stage) {
456 case MESA_SHADER_VERTEX:
457 nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
458 break;
459 case MESA_SHADER_TESS_CTRL:
460 nir_emit_tcs_intrinsic(abld, nir_instr_as_intrinsic(instr));
461 break;
462 case MESA_SHADER_TESS_EVAL:
463 nir_emit_tes_intrinsic(abld, nir_instr_as_intrinsic(instr));
464 break;
465 case MESA_SHADER_GEOMETRY:
466 nir_emit_gs_intrinsic(abld, nir_instr_as_intrinsic(instr));
467 break;
468 case MESA_SHADER_FRAGMENT:
469 nir_emit_fs_intrinsic(abld, nir_instr_as_intrinsic(instr));
470 break;
471 case MESA_SHADER_COMPUTE:
472 nir_emit_cs_intrinsic(abld, nir_instr_as_intrinsic(instr));
473 break;
474 default:
475 unreachable("unsupported shader stage");
476 }
477 break;
478
479 case nir_instr_type_tex:
480 nir_emit_texture(abld, nir_instr_as_tex(instr));
481 break;
482
483 case nir_instr_type_load_const:
484 nir_emit_load_const(abld, nir_instr_as_load_const(instr));
485 break;
486
487 case nir_instr_type_ssa_undef:
488 nir_emit_undef(abld, nir_instr_as_ssa_undef(instr));
489 break;
490
491 case nir_instr_type_jump:
492 nir_emit_jump(abld, nir_instr_as_jump(instr));
493 break;
494
495 default:
496 unreachable("unknown instruction type");
497 }
498 }
499
500 /**
501 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
502 * match instr.
503 */
504 bool
505 fs_visitor::optimize_extract_to_float(nir_alu_instr *instr,
506 const fs_reg &result)
507 {
508 if (!instr->src[0].src.is_ssa ||
509 !instr->src[0].src.ssa->parent_instr)
510 return false;
511
512 if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
513 return false;
514
515 nir_alu_instr *src0 =
516 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
517
518 if (src0->op != nir_op_extract_u8 && src0->op != nir_op_extract_u16 &&
519 src0->op != nir_op_extract_i8 && src0->op != nir_op_extract_i16)
520 return false;
521
522 nir_const_value *element = nir_src_as_const_value(src0->src[1].src);
523 assert(element != NULL);
524
525 /* Element type to extract.*/
526 const brw_reg_type type = brw_int_type(
527 src0->op == nir_op_extract_u16 || src0->op == nir_op_extract_i16 ? 2 : 1,
528 src0->op == nir_op_extract_i16 || src0->op == nir_op_extract_i8);
529
530 fs_reg op0 = get_nir_src(src0->src[0].src);
531 op0.type = brw_type_for_nir_type(
532 (nir_alu_type)(nir_op_infos[src0->op].input_types[0] |
533 nir_src_bit_size(src0->src[0].src)));
534 op0 = offset(op0, bld, src0->src[0].swizzle[0]);
535
536 set_saturate(instr->dest.saturate,
537 bld.MOV(result, subscript(op0, type, element->u32[0])));
538 return true;
539 }
540
541 bool
542 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
543 const fs_reg &result)
544 {
545 if (!instr->src[0].src.is_ssa ||
546 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_intrinsic)
547 return false;
548
549 nir_intrinsic_instr *src0 =
550 nir_instr_as_intrinsic(instr->src[0].src.ssa->parent_instr);
551
552 if (src0->intrinsic != nir_intrinsic_load_front_face)
553 return false;
554
555 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
556 if (!value1 || fabsf(value1->f32[0]) != 1.0f)
557 return false;
558
559 nir_const_value *value2 = nir_src_as_const_value(instr->src[2].src);
560 if (!value2 || fabsf(value2->f32[0]) != 1.0f)
561 return false;
562
563 fs_reg tmp = vgrf(glsl_type::int_type);
564
565 if (devinfo->gen >= 6) {
566 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
567 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
568
569 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
570 *
571 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
572 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
573 *
574 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
575 *
576 * This negation looks like it's safe in practice, because bits 0:4 will
577 * surely be TRIANGLES
578 */
579
580 if (value1->f32[0] == -1.0f) {
581 g0.negate = true;
582 }
583
584 tmp.type = BRW_REGISTER_TYPE_W;
585 tmp.subreg_offset = 2;
586 tmp.stride = 2;
587
588 bld.OR(tmp, g0, brw_imm_uw(0x3f80));
589
590 tmp.type = BRW_REGISTER_TYPE_D;
591 tmp.subreg_offset = 0;
592 tmp.stride = 1;
593 } else {
594 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
595 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
596
597 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
598 *
599 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
600 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
601 *
602 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
603 *
604 * This negation looks like it's safe in practice, because bits 0:4 will
605 * surely be TRIANGLES
606 */
607
608 if (value1->f32[0] == -1.0f) {
609 g1_6.negate = true;
610 }
611
612 bld.OR(tmp, g1_6, brw_imm_d(0x3f800000));
613 }
614 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, brw_imm_d(0xbf800000));
615
616 return true;
617 }
618
619 void
620 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
621 {
622 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
623 fs_inst *inst;
624
625 fs_reg result = get_nir_dest(instr->dest.dest);
626 result.type = brw_type_for_nir_type(
627 (nir_alu_type)(nir_op_infos[instr->op].output_type |
628 nir_dest_bit_size(instr->dest.dest)));
629
630 fs_reg op[4];
631 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
632 op[i] = get_nir_src(instr->src[i].src);
633 op[i].type = brw_type_for_nir_type(
634 (nir_alu_type)(nir_op_infos[instr->op].input_types[i] |
635 nir_src_bit_size(instr->src[i].src)));
636 op[i].abs = instr->src[i].abs;
637 op[i].negate = instr->src[i].negate;
638 }
639
640 /* We get a bunch of mov's out of the from_ssa pass and they may still
641 * be vectorized. We'll handle them as a special-case. We'll also
642 * handle vecN here because it's basically the same thing.
643 */
644 switch (instr->op) {
645 case nir_op_imov:
646 case nir_op_fmov:
647 case nir_op_vec2:
648 case nir_op_vec3:
649 case nir_op_vec4: {
650 fs_reg temp = result;
651 bool need_extra_copy = false;
652 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
653 if (!instr->src[i].src.is_ssa &&
654 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
655 need_extra_copy = true;
656 temp = bld.vgrf(result.type, 4);
657 break;
658 }
659 }
660
661 for (unsigned i = 0; i < 4; i++) {
662 if (!(instr->dest.write_mask & (1 << i)))
663 continue;
664
665 if (instr->op == nir_op_imov || instr->op == nir_op_fmov) {
666 inst = bld.MOV(offset(temp, bld, i),
667 offset(op[0], bld, instr->src[0].swizzle[i]));
668 } else {
669 inst = bld.MOV(offset(temp, bld, i),
670 offset(op[i], bld, instr->src[i].swizzle[0]));
671 }
672 inst->saturate = instr->dest.saturate;
673 }
674
675 /* In this case the source and destination registers were the same,
676 * so we need to insert an extra set of moves in order to deal with
677 * any swizzling.
678 */
679 if (need_extra_copy) {
680 for (unsigned i = 0; i < 4; i++) {
681 if (!(instr->dest.write_mask & (1 << i)))
682 continue;
683
684 bld.MOV(offset(result, bld, i), offset(temp, bld, i));
685 }
686 }
687 return;
688 }
689 default:
690 break;
691 }
692
693 /* At this point, we have dealt with any instruction that operates on
694 * more than a single channel. Therefore, we can just adjust the source
695 * and destination registers for that channel and emit the instruction.
696 */
697 unsigned channel = 0;
698 if (nir_op_infos[instr->op].output_size == 0) {
699 /* Since NIR is doing the scalarizing for us, we should only ever see
700 * vectorized operations with a single channel.
701 */
702 assert(_mesa_bitcount(instr->dest.write_mask) == 1);
703 channel = ffs(instr->dest.write_mask) - 1;
704
705 result = offset(result, bld, channel);
706 }
707
708 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
709 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
710 op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
711 }
712
713 switch (instr->op) {
714 case nir_op_i2f:
715 case nir_op_u2f:
716 if (optimize_extract_to_float(instr, result))
717 return;
718 inst = bld.MOV(result, op[0]);
719 inst->saturate = instr->dest.saturate;
720 break;
721
722 case nir_op_f2d:
723 case nir_op_i2d:
724 case nir_op_u2d:
725 /* CHV PRM, vol07, 3D Media GPGPU Engine, Register Region Restrictions:
726 *
727 * "When source or destination is 64b (...), regioning in Align1
728 * must follow these rules:
729 *
730 * 1. Source and destination horizontal stride must be aligned to
731 * the same qword.
732 * (...)"
733 *
734 * This means that 32-bit to 64-bit conversions need to have the 32-bit
735 * data elements aligned to 64-bit. This restriction does not apply to
736 * BDW and later.
737 */
738 if (devinfo->is_cherryview || devinfo->is_broxton) {
739 fs_reg tmp = bld.vgrf(result.type, 1);
740 tmp = subscript(tmp, op[0].type, 0);
741 inst = bld.MOV(tmp, op[0]);
742 inst = bld.MOV(result, tmp);
743 inst->saturate = instr->dest.saturate;
744 break;
745 }
746 /* fallthrough */
747 case nir_op_d2f:
748 case nir_op_d2i:
749 case nir_op_d2u:
750 inst = bld.MOV(result, op[0]);
751 inst->saturate = instr->dest.saturate;
752 break;
753
754 case nir_op_f2i:
755 case nir_op_f2u:
756 bld.MOV(result, op[0]);
757 break;
758
759 case nir_op_fsign: {
760 if (type_sz(op[0].type) < 8) {
761 /* AND(val, 0x80000000) gives the sign bit.
762 *
763 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
764 * zero.
765 */
766 bld.CMP(bld.null_reg_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
767
768 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
769 op[0].type = BRW_REGISTER_TYPE_UD;
770 result.type = BRW_REGISTER_TYPE_UD;
771 bld.AND(result_int, op[0], brw_imm_ud(0x80000000u));
772
773 inst = bld.OR(result_int, result_int, brw_imm_ud(0x3f800000u));
774 inst->predicate = BRW_PREDICATE_NORMAL;
775 if (instr->dest.saturate) {
776 inst = bld.MOV(result, result);
777 inst->saturate = true;
778 }
779 } else {
780 /* For doubles we do the same but we need to consider:
781 *
782 * - 2-src instructions can't operate with 64-bit immediates
783 * - The sign is encoded in the high 32-bit of each DF
784 * - CMP with DF requires special handling in SIMD16
785 * - We need to produce a DF result.
786 */
787
788 /* 2-src instructions can't have 64-bit immediates, so put 0.0 in
789 * a register and compare with that.
790 */
791 fs_reg tmp = vgrf(glsl_type::double_type);
792 bld.MOV(tmp, brw_imm_df(0.0));
793
794 /* A direct DF CMP using the flag register (null dst) won't work in
795 * SIMD16 because the CMP will be split in two by lower_simd_width,
796 * resulting in two CMP instructions with the same dst (NULL),
797 * leading to dead code elimination of the first one. In SIMD8,
798 * however, there is no need to split the CMP and we can save some
799 * work.
800 */
801 fs_reg dst_tmp = vgrf(glsl_type::double_type);
802 bld.CMP(dst_tmp, op[0], tmp, BRW_CONDITIONAL_NZ);
803
804 /* In SIMD16 we want to avoid using a NULL dst register with DF CMP,
805 * so we store the result of the comparison in a vgrf instead and
806 * then we generate a UD comparison from that that won't have to
807 * be split by lower_simd_width. This is what NIR does to handle
808 * double comparisons in the general case.
809 */
810 if (bld.dispatch_width() == 16 ) {
811 fs_reg dst_tmp_ud = retype(dst_tmp, BRW_REGISTER_TYPE_UD);
812 bld.MOV(dst_tmp_ud, subscript(dst_tmp, BRW_REGISTER_TYPE_UD, 0));
813 bld.CMP(bld.null_reg_ud(),
814 dst_tmp_ud, brw_imm_ud(0), BRW_CONDITIONAL_NZ);
815 }
816
817 /* Get the high 32-bit of each double component where the sign is */
818 fs_reg result_int = retype(result, BRW_REGISTER_TYPE_UD);
819 bld.MOV(result_int, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
820
821 /* Get the sign bit */
822 bld.AND(result_int, result_int, brw_imm_ud(0x80000000u));
823
824 /* Add 1.0 to the sign, predicated to skip the case of op[0] == 0.0 */
825 inst = bld.OR(result_int, result_int, brw_imm_ud(0x3f800000u));
826 inst->predicate = BRW_PREDICATE_NORMAL;
827
828 /* Convert from 32-bit float to 64-bit double */
829 result.type = BRW_REGISTER_TYPE_DF;
830 inst = bld.MOV(result, retype(result_int, BRW_REGISTER_TYPE_F));
831
832 if (instr->dest.saturate) {
833 inst = bld.MOV(result, result);
834 inst->saturate = true;
835 }
836 }
837 break;
838 }
839
840 case nir_op_isign:
841 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
842 * -> non-negative val generates 0x00000000.
843 * Predicated OR sets 1 if val is positive.
844 */
845 assert(nir_dest_bit_size(instr->dest.dest) < 64);
846 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G);
847 bld.ASR(result, op[0], brw_imm_d(31));
848 inst = bld.OR(result, result, brw_imm_d(1));
849 inst->predicate = BRW_PREDICATE_NORMAL;
850 break;
851
852 case nir_op_frcp:
853 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
854 inst->saturate = instr->dest.saturate;
855 break;
856
857 case nir_op_fexp2:
858 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
859 inst->saturate = instr->dest.saturate;
860 break;
861
862 case nir_op_flog2:
863 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
864 inst->saturate = instr->dest.saturate;
865 break;
866
867 case nir_op_fsin:
868 inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
869 inst->saturate = instr->dest.saturate;
870 break;
871
872 case nir_op_fcos:
873 inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
874 inst->saturate = instr->dest.saturate;
875 break;
876
877 case nir_op_fddx:
878 if (fs_key->high_quality_derivatives) {
879 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
880 } else {
881 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
882 }
883 inst->saturate = instr->dest.saturate;
884 break;
885 case nir_op_fddx_fine:
886 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
887 inst->saturate = instr->dest.saturate;
888 break;
889 case nir_op_fddx_coarse:
890 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
891 inst->saturate = instr->dest.saturate;
892 break;
893 case nir_op_fddy:
894 if (fs_key->high_quality_derivatives) {
895 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
896 } else {
897 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
898 }
899 inst->saturate = instr->dest.saturate;
900 break;
901 case nir_op_fddy_fine:
902 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
903 inst->saturate = instr->dest.saturate;
904 break;
905 case nir_op_fddy_coarse:
906 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
907 inst->saturate = instr->dest.saturate;
908 break;
909
910 case nir_op_iadd:
911 assert(nir_dest_bit_size(instr->dest.dest) < 64);
912 case nir_op_fadd:
913 inst = bld.ADD(result, op[0], op[1]);
914 inst->saturate = instr->dest.saturate;
915 break;
916
917 case nir_op_fmul:
918 inst = bld.MUL(result, op[0], op[1]);
919 inst->saturate = instr->dest.saturate;
920 break;
921
922 case nir_op_imul:
923 assert(nir_dest_bit_size(instr->dest.dest) < 64);
924 bld.MUL(result, op[0], op[1]);
925 break;
926
927 case nir_op_imul_high:
928 case nir_op_umul_high:
929 assert(nir_dest_bit_size(instr->dest.dest) < 64);
930 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
931 break;
932
933 case nir_op_idiv:
934 case nir_op_udiv:
935 assert(nir_dest_bit_size(instr->dest.dest) < 64);
936 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
937 break;
938
939 case nir_op_uadd_carry:
940 unreachable("Should have been lowered by carry_to_arith().");
941
942 case nir_op_usub_borrow:
943 unreachable("Should have been lowered by borrow_to_arith().");
944
945 case nir_op_umod:
946 case nir_op_irem:
947 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
948 * appears that our hardware just does the right thing for signed
949 * remainder.
950 */
951 assert(nir_dest_bit_size(instr->dest.dest) < 64);
952 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
953 break;
954
955 case nir_op_imod: {
956 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
957 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
958
959 /* Math instructions don't support conditional mod */
960 inst = bld.MOV(bld.null_reg_d(), result);
961 inst->conditional_mod = BRW_CONDITIONAL_NZ;
962
963 /* Now, we need to determine if signs of the sources are different.
964 * When we XOR the sources, the top bit is 0 if they are the same and 1
965 * if they are different. We can then use a conditional modifier to
966 * turn that into a predicate. This leads us to an XOR.l instruction.
967 *
968 * Technically, according to the PRM, you're not allowed to use .l on a
969 * XOR instruction. However, emperical experiments and Curro's reading
970 * of the simulator source both indicate that it's safe.
971 */
972 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
973 inst = bld.XOR(tmp, op[0], op[1]);
974 inst->predicate = BRW_PREDICATE_NORMAL;
975 inst->conditional_mod = BRW_CONDITIONAL_L;
976
977 /* If the result of the initial remainder operation is non-zero and the
978 * two sources have different signs, add in a copy of op[1] to get the
979 * final integer modulus value.
980 */
981 inst = bld.ADD(result, result, op[1]);
982 inst->predicate = BRW_PREDICATE_NORMAL;
983 break;
984 }
985
986 case nir_op_flt:
987 case nir_op_fge:
988 case nir_op_feq:
989 case nir_op_fne: {
990 fs_reg dest = result;
991 if (nir_src_bit_size(instr->src[0].src) > 32) {
992 dest = bld.vgrf(BRW_REGISTER_TYPE_DF, 1);
993 }
994 brw_conditional_mod cond;
995 switch (instr->op) {
996 case nir_op_flt:
997 cond = BRW_CONDITIONAL_L;
998 break;
999 case nir_op_fge:
1000 cond = BRW_CONDITIONAL_GE;
1001 break;
1002 case nir_op_feq:
1003 cond = BRW_CONDITIONAL_Z;
1004 break;
1005 case nir_op_fne:
1006 cond = BRW_CONDITIONAL_NZ;
1007 break;
1008 default:
1009 unreachable("bad opcode");
1010 }
1011 bld.CMP(dest, op[0], op[1], cond);
1012 if (nir_src_bit_size(instr->src[0].src) > 32) {
1013 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
1014 }
1015 break;
1016 }
1017
1018 case nir_op_ilt:
1019 case nir_op_ult:
1020 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1021 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_L);
1022 break;
1023
1024 case nir_op_ige:
1025 case nir_op_uge:
1026 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1027 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_GE);
1028 break;
1029
1030 case nir_op_ieq:
1031 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1032 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_Z);
1033 break;
1034
1035 case nir_op_ine:
1036 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1037 bld.CMP(result, op[0], op[1], BRW_CONDITIONAL_NZ);
1038 break;
1039
1040 case nir_op_inot:
1041 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1042 if (devinfo->gen >= 8) {
1043 op[0] = resolve_source_modifiers(op[0]);
1044 }
1045 bld.NOT(result, op[0]);
1046 break;
1047 case nir_op_ixor:
1048 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1049 if (devinfo->gen >= 8) {
1050 op[0] = resolve_source_modifiers(op[0]);
1051 op[1] = resolve_source_modifiers(op[1]);
1052 }
1053 bld.XOR(result, op[0], op[1]);
1054 break;
1055 case nir_op_ior:
1056 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1057 if (devinfo->gen >= 8) {
1058 op[0] = resolve_source_modifiers(op[0]);
1059 op[1] = resolve_source_modifiers(op[1]);
1060 }
1061 bld.OR(result, op[0], op[1]);
1062 break;
1063 case nir_op_iand:
1064 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1065 if (devinfo->gen >= 8) {
1066 op[0] = resolve_source_modifiers(op[0]);
1067 op[1] = resolve_source_modifiers(op[1]);
1068 }
1069 bld.AND(result, op[0], op[1]);
1070 break;
1071
1072 case nir_op_fdot2:
1073 case nir_op_fdot3:
1074 case nir_op_fdot4:
1075 case nir_op_ball_fequal2:
1076 case nir_op_ball_iequal2:
1077 case nir_op_ball_fequal3:
1078 case nir_op_ball_iequal3:
1079 case nir_op_ball_fequal4:
1080 case nir_op_ball_iequal4:
1081 case nir_op_bany_fnequal2:
1082 case nir_op_bany_inequal2:
1083 case nir_op_bany_fnequal3:
1084 case nir_op_bany_inequal3:
1085 case nir_op_bany_fnequal4:
1086 case nir_op_bany_inequal4:
1087 unreachable("Lowered by nir_lower_alu_reductions");
1088
1089 case nir_op_fnoise1_1:
1090 case nir_op_fnoise1_2:
1091 case nir_op_fnoise1_3:
1092 case nir_op_fnoise1_4:
1093 case nir_op_fnoise2_1:
1094 case nir_op_fnoise2_2:
1095 case nir_op_fnoise2_3:
1096 case nir_op_fnoise2_4:
1097 case nir_op_fnoise3_1:
1098 case nir_op_fnoise3_2:
1099 case nir_op_fnoise3_3:
1100 case nir_op_fnoise3_4:
1101 case nir_op_fnoise4_1:
1102 case nir_op_fnoise4_2:
1103 case nir_op_fnoise4_3:
1104 case nir_op_fnoise4_4:
1105 unreachable("not reached: should be handled by lower_noise");
1106
1107 case nir_op_ldexp:
1108 unreachable("not reached: should be handled by ldexp_to_arith()");
1109
1110 case nir_op_fsqrt:
1111 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
1112 inst->saturate = instr->dest.saturate;
1113 break;
1114
1115 case nir_op_frsq:
1116 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
1117 inst->saturate = instr->dest.saturate;
1118 break;
1119
1120 case nir_op_b2i:
1121 case nir_op_b2f:
1122 bld.MOV(result, negate(op[0]));
1123 break;
1124
1125 case nir_op_f2b:
1126 bld.CMP(result, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
1127 break;
1128 case nir_op_d2b: {
1129 /* two-argument instructions can't take 64-bit immediates */
1130 fs_reg zero = vgrf(glsl_type::double_type);
1131 bld.MOV(zero, brw_imm_df(0.0));
1132 /* A SIMD16 execution needs to be split in two instructions, so use
1133 * a vgrf instead of the flag register as dst so instruction splitting
1134 * works
1135 */
1136 fs_reg tmp = vgrf(glsl_type::double_type);
1137 bld.CMP(tmp, op[0], zero, BRW_CONDITIONAL_NZ);
1138 bld.MOV(result, subscript(tmp, BRW_REGISTER_TYPE_UD, 0));
1139 break;
1140 }
1141 case nir_op_i2b:
1142 bld.CMP(result, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1143 break;
1144
1145 case nir_op_ftrunc:
1146 inst = bld.RNDZ(result, op[0]);
1147 inst->saturate = instr->dest.saturate;
1148 break;
1149
1150 case nir_op_fceil: {
1151 op[0].negate = !op[0].negate;
1152 fs_reg temp = vgrf(glsl_type::float_type);
1153 bld.RNDD(temp, op[0]);
1154 temp.negate = true;
1155 inst = bld.MOV(result, temp);
1156 inst->saturate = instr->dest.saturate;
1157 break;
1158 }
1159 case nir_op_ffloor:
1160 inst = bld.RNDD(result, op[0]);
1161 inst->saturate = instr->dest.saturate;
1162 break;
1163 case nir_op_ffract:
1164 inst = bld.FRC(result, op[0]);
1165 inst->saturate = instr->dest.saturate;
1166 break;
1167 case nir_op_fround_even:
1168 inst = bld.RNDE(result, op[0]);
1169 inst->saturate = instr->dest.saturate;
1170 break;
1171
1172 case nir_op_fquantize2f16: {
1173 fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
1174 fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
1175 fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
1176
1177 /* The destination stride must be at least as big as the source stride. */
1178 tmp16.type = BRW_REGISTER_TYPE_W;
1179 tmp16.stride = 2;
1180
1181 /* Check for denormal */
1182 fs_reg abs_src0 = op[0];
1183 abs_src0.abs = true;
1184 bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1185 BRW_CONDITIONAL_L);
1186 /* Get the appropriately signed zero */
1187 bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
1188 retype(op[0], BRW_REGISTER_TYPE_UD),
1189 brw_imm_ud(0x80000000));
1190 /* Do the actual F32 -> F16 -> F32 conversion */
1191 bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
1192 bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
1193 /* Select that or zero based on normal status */
1194 inst = bld.SEL(result, zero, tmp32);
1195 inst->predicate = BRW_PREDICATE_NORMAL;
1196 inst->saturate = instr->dest.saturate;
1197 break;
1198 }
1199
1200 case nir_op_imin:
1201 case nir_op_umin:
1202 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1203 case nir_op_fmin:
1204 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_L);
1205 inst->saturate = instr->dest.saturate;
1206 break;
1207
1208 case nir_op_imax:
1209 case nir_op_umax:
1210 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1211 case nir_op_fmax:
1212 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_GE);
1213 inst->saturate = instr->dest.saturate;
1214 break;
1215
1216 case nir_op_pack_snorm_2x16:
1217 case nir_op_pack_snorm_4x8:
1218 case nir_op_pack_unorm_2x16:
1219 case nir_op_pack_unorm_4x8:
1220 case nir_op_unpack_snorm_2x16:
1221 case nir_op_unpack_snorm_4x8:
1222 case nir_op_unpack_unorm_2x16:
1223 case nir_op_unpack_unorm_4x8:
1224 case nir_op_unpack_half_2x16:
1225 case nir_op_pack_half_2x16:
1226 unreachable("not reached: should be handled by lower_packing_builtins");
1227
1228 case nir_op_unpack_half_2x16_split_x:
1229 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, result, op[0]);
1230 inst->saturate = instr->dest.saturate;
1231 break;
1232 case nir_op_unpack_half_2x16_split_y:
1233 inst = bld.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, result, op[0]);
1234 inst->saturate = instr->dest.saturate;
1235 break;
1236
1237 case nir_op_pack_double_2x32_split:
1238 /* Optimize the common case where we are re-packing a double with
1239 * the result of a previous double unpack. In this case we can take the
1240 * 32-bit value to use in the re-pack from the original double and bypass
1241 * the unpack operation.
1242 */
1243 for (int i = 0; i < 2; i++) {
1244 if (instr->src[i].src.is_ssa)
1245 continue;
1246
1247 const nir_instr *parent_instr = instr->src[i].src.ssa->parent_instr;
1248 if (parent_instr->type == nir_instr_type_alu)
1249 continue;
1250
1251 const nir_alu_instr *alu_parent = nir_instr_as_alu(parent_instr);
1252 if (alu_parent->op == nir_op_unpack_double_2x32_split_x ||
1253 alu_parent->op == nir_op_unpack_double_2x32_split_y)
1254 continue;
1255
1256 if (!alu_parent->src[0].src.is_ssa)
1257 continue;
1258
1259 op[i] = get_nir_src(alu_parent->src[0].src);
1260 op[i] = offset(retype(op[i], BRW_REGISTER_TYPE_DF), bld,
1261 alu_parent->src[0].swizzle[channel]);
1262 if (alu_parent->op == nir_op_unpack_double_2x32_split_y)
1263 op[i] = subscript(op[i], BRW_REGISTER_TYPE_UD, 1);
1264 else
1265 op[i] = subscript(op[i], BRW_REGISTER_TYPE_UD, 0);
1266 }
1267 bld.emit(FS_OPCODE_PACK, result, op[0], op[1]);
1268 break;
1269
1270 case nir_op_unpack_double_2x32_split_x:
1271 case nir_op_unpack_double_2x32_split_y: {
1272 /* Optimize the common case where we are unpacking from a double we have
1273 * previously packed. In this case we can just bypass the pack operation
1274 * and source directly from its arguments.
1275 */
1276 unsigned index = (instr->op == nir_op_unpack_double_2x32_split_x) ? 0 : 1;
1277 if (instr->src[0].src.is_ssa) {
1278 nir_instr *parent_instr = instr->src[0].src.ssa->parent_instr;
1279 if (parent_instr->type == nir_instr_type_alu) {
1280 nir_alu_instr *alu_parent = nir_instr_as_alu(parent_instr);
1281 if (alu_parent->op == nir_op_pack_double_2x32_split &&
1282 alu_parent->src[index].src.is_ssa) {
1283 op[0] = retype(get_nir_src(alu_parent->src[index].src),
1284 BRW_REGISTER_TYPE_UD);
1285 op[0] =
1286 offset(op[0], bld, alu_parent->src[index].swizzle[channel]);
1287 bld.MOV(result, op[0]);
1288 break;
1289 }
1290 }
1291 }
1292
1293 if (instr->op == nir_op_unpack_double_2x32_split_x)
1294 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 0));
1295 else
1296 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
1297 break;
1298 }
1299
1300 case nir_op_fpow:
1301 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1302 inst->saturate = instr->dest.saturate;
1303 break;
1304
1305 case nir_op_bitfield_reverse:
1306 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1307 bld.BFREV(result, op[0]);
1308 break;
1309
1310 case nir_op_bit_count:
1311 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1312 bld.CBIT(result, op[0]);
1313 break;
1314
1315 case nir_op_ufind_msb:
1316 case nir_op_ifind_msb: {
1317 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1318 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1319
1320 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1321 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1322 * subtract the result from 31 to convert the MSB count into an LSB count.
1323 */
1324 bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1325
1326 inst = bld.ADD(result, result, brw_imm_d(31));
1327 inst->predicate = BRW_PREDICATE_NORMAL;
1328 inst->src[0].negate = true;
1329 break;
1330 }
1331
1332 case nir_op_find_lsb:
1333 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1334 bld.FBL(result, op[0]);
1335 break;
1336
1337 case nir_op_ubitfield_extract:
1338 case nir_op_ibitfield_extract:
1339 unreachable("should have been lowered");
1340 case nir_op_ubfe:
1341 case nir_op_ibfe:
1342 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1343 bld.BFE(result, op[2], op[1], op[0]);
1344 break;
1345 case nir_op_bfm:
1346 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1347 bld.BFI1(result, op[0], op[1]);
1348 break;
1349 case nir_op_bfi:
1350 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1351 bld.BFI2(result, op[0], op[1], op[2]);
1352 break;
1353
1354 case nir_op_bitfield_insert:
1355 unreachable("not reached: should have been lowered");
1356
1357 case nir_op_ishl:
1358 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1359 bld.SHL(result, op[0], op[1]);
1360 break;
1361 case nir_op_ishr:
1362 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1363 bld.ASR(result, op[0], op[1]);
1364 break;
1365 case nir_op_ushr:
1366 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1367 bld.SHR(result, op[0], op[1]);
1368 break;
1369
1370 case nir_op_pack_half_2x16_split:
1371 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1372 break;
1373
1374 case nir_op_ffma:
1375 inst = bld.MAD(result, op[2], op[1], op[0]);
1376 inst->saturate = instr->dest.saturate;
1377 break;
1378
1379 case nir_op_flrp:
1380 inst = bld.LRP(result, op[0], op[1], op[2]);
1381 inst->saturate = instr->dest.saturate;
1382 break;
1383
1384 case nir_op_bcsel:
1385 if (optimize_frontfacing_ternary(instr, result))
1386 return;
1387
1388 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1389 inst = bld.SEL(result, op[1], op[2]);
1390 inst->predicate = BRW_PREDICATE_NORMAL;
1391 break;
1392
1393 case nir_op_extract_u8:
1394 case nir_op_extract_i8: {
1395 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1396 nir_const_value *byte = nir_src_as_const_value(instr->src[1].src);
1397 assert(byte != NULL);
1398 bld.MOV(result, subscript(op[0], type, byte->u32[0]));
1399 break;
1400 }
1401
1402 case nir_op_extract_u16:
1403 case nir_op_extract_i16: {
1404 const brw_reg_type type = brw_int_type(2, instr->op == nir_op_extract_i16);
1405 nir_const_value *word = nir_src_as_const_value(instr->src[1].src);
1406 assert(word != NULL);
1407 bld.MOV(result, subscript(op[0], type, word->u32[0]));
1408 break;
1409 }
1410
1411 default:
1412 unreachable("unhandled instruction");
1413 }
1414
1415 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1416 * to sign extend the low bit to 0/~0
1417 */
1418 if (devinfo->gen <= 5 &&
1419 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1420 fs_reg masked = vgrf(glsl_type::int_type);
1421 bld.AND(masked, result, brw_imm_d(1));
1422 masked.negate = true;
1423 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1424 }
1425 }
1426
1427 void
1428 fs_visitor::nir_emit_load_const(const fs_builder &bld,
1429 nir_load_const_instr *instr)
1430 {
1431 const brw_reg_type reg_type =
1432 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1433 fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
1434
1435 switch (instr->def.bit_size) {
1436 case 32:
1437 for (unsigned i = 0; i < instr->def.num_components; i++)
1438 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value.i32[i]));
1439 break;
1440
1441 case 64:
1442 for (unsigned i = 0; i < instr->def.num_components; i++)
1443 bld.MOV(offset(reg, bld, i), brw_imm_df(instr->value.f64[i]));
1444 break;
1445
1446 default:
1447 unreachable("Invalid bit size");
1448 }
1449
1450 nir_ssa_values[instr->def.index] = reg;
1451 }
1452
1453 void
1454 fs_visitor::nir_emit_undef(const fs_builder &bld, nir_ssa_undef_instr *instr)
1455 {
1456 const brw_reg_type reg_type =
1457 instr->def.bit_size == 32 ? BRW_REGISTER_TYPE_D : BRW_REGISTER_TYPE_DF;
1458 nir_ssa_values[instr->def.index] =
1459 bld.vgrf(reg_type, instr->def.num_components);
1460 }
1461
1462 fs_reg
1463 fs_visitor::get_nir_src(const nir_src &src)
1464 {
1465 fs_reg reg;
1466 if (src.is_ssa) {
1467 reg = nir_ssa_values[src.ssa->index];
1468 } else {
1469 /* We don't handle indirects on locals */
1470 assert(src.reg.indirect == NULL);
1471 reg = offset(nir_locals[src.reg.reg->index], bld,
1472 src.reg.base_offset * src.reg.reg->num_components);
1473 }
1474
1475 /* to avoid floating-point denorm flushing problems, set the type by
1476 * default to D - instructions that need floating point semantics will set
1477 * this to F if they need to
1478 */
1479 return retype(reg, BRW_REGISTER_TYPE_D);
1480 }
1481
1482 /**
1483 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1484 */
1485 fs_reg
1486 fs_visitor::get_nir_src_imm(const nir_src &src)
1487 {
1488 nir_const_value *val = nir_src_as_const_value(src);
1489 return val ? fs_reg(brw_imm_d(val->i32[0])) : get_nir_src(src);
1490 }
1491
1492 fs_reg
1493 fs_visitor::get_nir_dest(const nir_dest &dest)
1494 {
1495 if (dest.is_ssa) {
1496 const brw_reg_type reg_type =
1497 dest.ssa.bit_size == 32 ? BRW_REGISTER_TYPE_F : BRW_REGISTER_TYPE_DF;
1498 nir_ssa_values[dest.ssa.index] =
1499 bld.vgrf(reg_type, dest.ssa.num_components);
1500 return nir_ssa_values[dest.ssa.index];
1501 } else {
1502 /* We don't handle indirects on locals */
1503 assert(dest.reg.indirect == NULL);
1504 return offset(nir_locals[dest.reg.reg->index], bld,
1505 dest.reg.base_offset * dest.reg.reg->num_components);
1506 }
1507 }
1508
1509 fs_reg
1510 fs_visitor::get_nir_image_deref(const nir_deref_var *deref)
1511 {
1512 fs_reg image(UNIFORM, deref->var->data.driver_location / 4,
1513 BRW_REGISTER_TYPE_UD);
1514 fs_reg indirect;
1515 unsigned indirect_max = 0;
1516
1517 for (const nir_deref *tail = &deref->deref; tail->child;
1518 tail = tail->child) {
1519 const nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1520 assert(tail->child->deref_type == nir_deref_type_array);
1521 const unsigned size = glsl_get_length(tail->type);
1522 const unsigned element_size = type_size_scalar(deref_array->deref.type);
1523 const unsigned base = MIN2(deref_array->base_offset, size - 1);
1524 image = offset(image, bld, base * element_size);
1525
1526 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1527 fs_reg tmp = vgrf(glsl_type::uint_type);
1528
1529 /* Accessing an invalid surface index with the dataport can result
1530 * in a hang. According to the spec "if the index used to
1531 * select an individual element is negative or greater than or
1532 * equal to the size of the array, the results of the operation
1533 * are undefined but may not lead to termination" -- which is one
1534 * of the possible outcomes of the hang. Clamp the index to
1535 * prevent access outside of the array bounds.
1536 */
1537 bld.emit_minmax(tmp, retype(get_nir_src(deref_array->indirect),
1538 BRW_REGISTER_TYPE_UD),
1539 brw_imm_ud(size - base - 1), BRW_CONDITIONAL_L);
1540
1541 indirect_max += element_size * (tail->type->length - 1);
1542
1543 bld.MUL(tmp, tmp, brw_imm_ud(element_size * 4));
1544 if (indirect.file == BAD_FILE) {
1545 indirect = tmp;
1546 } else {
1547 bld.ADD(indirect, indirect, tmp);
1548 }
1549 }
1550 }
1551
1552 if (indirect.file == BAD_FILE) {
1553 return image;
1554 } else {
1555 /* Emit a pile of MOVs to load the uniform into a temporary. The
1556 * dead-code elimination pass will get rid of what we don't use.
1557 */
1558 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, BRW_IMAGE_PARAM_SIZE);
1559 for (unsigned j = 0; j < BRW_IMAGE_PARAM_SIZE; j++) {
1560 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
1561 offset(tmp, bld, j), offset(image, bld, j),
1562 indirect, brw_imm_ud((indirect_max + 1) * 4));
1563 }
1564 return tmp;
1565 }
1566 }
1567
1568 void
1569 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
1570 unsigned wr_mask)
1571 {
1572 for (unsigned i = 0; i < 4; i++) {
1573 if (!((wr_mask >> i) & 1))
1574 continue;
1575
1576 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
1577 new_inst->dst = offset(new_inst->dst, bld, i);
1578 for (unsigned j = 0; j < new_inst->sources; j++)
1579 if (new_inst->src[j].file == VGRF)
1580 new_inst->src[j] = offset(new_inst->src[j], bld, i);
1581
1582 bld.emit(new_inst);
1583 }
1584 }
1585
1586 /**
1587 * Get the matching channel register datatype for an image intrinsic of the
1588 * specified GLSL image type.
1589 */
1590 static brw_reg_type
1591 get_image_base_type(const glsl_type *type)
1592 {
1593 switch ((glsl_base_type)type->sampled_type) {
1594 case GLSL_TYPE_UINT:
1595 return BRW_REGISTER_TYPE_UD;
1596 case GLSL_TYPE_INT:
1597 return BRW_REGISTER_TYPE_D;
1598 case GLSL_TYPE_FLOAT:
1599 return BRW_REGISTER_TYPE_F;
1600 default:
1601 unreachable("Not reached.");
1602 }
1603 }
1604
1605 /**
1606 * Get the appropriate atomic op for an image atomic intrinsic.
1607 */
1608 static unsigned
1609 get_image_atomic_op(nir_intrinsic_op op, const glsl_type *type)
1610 {
1611 switch (op) {
1612 case nir_intrinsic_image_atomic_add:
1613 return BRW_AOP_ADD;
1614 case nir_intrinsic_image_atomic_min:
1615 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1616 BRW_AOP_IMIN : BRW_AOP_UMIN);
1617 case nir_intrinsic_image_atomic_max:
1618 return (get_image_base_type(type) == BRW_REGISTER_TYPE_D ?
1619 BRW_AOP_IMAX : BRW_AOP_UMAX);
1620 case nir_intrinsic_image_atomic_and:
1621 return BRW_AOP_AND;
1622 case nir_intrinsic_image_atomic_or:
1623 return BRW_AOP_OR;
1624 case nir_intrinsic_image_atomic_xor:
1625 return BRW_AOP_XOR;
1626 case nir_intrinsic_image_atomic_exchange:
1627 return BRW_AOP_MOV;
1628 case nir_intrinsic_image_atomic_comp_swap:
1629 return BRW_AOP_CMPWR;
1630 default:
1631 unreachable("Not reachable.");
1632 }
1633 }
1634
1635 static fs_inst *
1636 emit_pixel_interpolater_send(const fs_builder &bld,
1637 enum opcode opcode,
1638 const fs_reg &dst,
1639 const fs_reg &src,
1640 const fs_reg &desc,
1641 glsl_interp_qualifier interpolation)
1642 {
1643 fs_inst *inst;
1644 fs_reg payload;
1645 int mlen;
1646
1647 if (src.file == BAD_FILE) {
1648 /* Dummy payload */
1649 payload = bld.vgrf(BRW_REGISTER_TYPE_F, 1);
1650 mlen = 1;
1651 } else {
1652 payload = src;
1653 mlen = 2 * bld.dispatch_width() / 8;
1654 }
1655
1656 inst = bld.emit(opcode, dst, payload, desc);
1657 inst->mlen = mlen;
1658 /* 2 floats per slot returned */
1659 inst->regs_written = 2 * bld.dispatch_width() / 8;
1660 inst->pi_noperspective = interpolation == INTERP_QUALIFIER_NOPERSPECTIVE;
1661
1662 return inst;
1663 }
1664
1665 /**
1666 * Computes 1 << x, given a D/UD register containing some value x.
1667 */
1668 static fs_reg
1669 intexp2(const fs_builder &bld, const fs_reg &x)
1670 {
1671 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
1672
1673 fs_reg result = bld.vgrf(x.type, 1);
1674 fs_reg one = bld.vgrf(x.type, 1);
1675
1676 bld.MOV(one, retype(brw_imm_d(1), one.type));
1677 bld.SHL(result, one, x);
1678 return result;
1679 }
1680
1681 void
1682 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
1683 {
1684 assert(stage == MESA_SHADER_GEOMETRY);
1685
1686 struct brw_gs_prog_data *gs_prog_data =
1687 (struct brw_gs_prog_data *) prog_data;
1688
1689 if (gs_compile->control_data_header_size_bits == 0)
1690 return;
1691
1692 /* We can only do EndPrimitive() functionality when the control data
1693 * consists of cut bits. Fortunately, the only time it isn't is when the
1694 * output type is points, in which case EndPrimitive() is a no-op.
1695 */
1696 if (gs_prog_data->control_data_format !=
1697 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
1698 return;
1699 }
1700
1701 /* Cut bits use one bit per vertex. */
1702 assert(gs_compile->control_data_bits_per_vertex == 1);
1703
1704 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1705 vertex_count.type = BRW_REGISTER_TYPE_UD;
1706
1707 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1708 * vertex n, 0 otherwise. So all we need to do here is mark bit
1709 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1710 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1711 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1712 *
1713 * Note that if EndPrimitive() is called before emitting any vertices, this
1714 * will cause us to set bit 31 of the control_data_bits register to 1.
1715 * That's fine because:
1716 *
1717 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1718 * output, so the hardware will ignore cut bit 31.
1719 *
1720 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1721 * last vertex, so setting cut bit 31 has no effect (since the primitive
1722 * is automatically ended when the GS terminates).
1723 *
1724 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1725 * control_data_bits register to 0 when the first vertex is emitted.
1726 */
1727
1728 const fs_builder abld = bld.annotate("end primitive");
1729
1730 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1731 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1732 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1733 fs_reg mask = intexp2(abld, prev_count);
1734 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1735 * attention to the lower 5 bits of its second source argument, so on this
1736 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1737 * ((vertex_count - 1) % 32).
1738 */
1739 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1740 }
1741
1742 void
1743 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
1744 {
1745 assert(stage == MESA_SHADER_GEOMETRY);
1746 assert(gs_compile->control_data_bits_per_vertex != 0);
1747
1748 struct brw_gs_prog_data *gs_prog_data =
1749 (struct brw_gs_prog_data *) prog_data;
1750
1751 const fs_builder abld = bld.annotate("emit control data bits");
1752 const fs_builder fwa_bld = bld.exec_all();
1753
1754 /* We use a single UD register to accumulate control data bits (32 bits
1755 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1756 * at a time.
1757 *
1758 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1759 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1760 * use the Channel Mask phase to enable/disable which DWord within that
1761 * group to write. (Remember, different SIMD8 channels may have emitted
1762 * different numbers of vertices, so we may need per-slot offsets.)
1763 *
1764 * Channel masking presents an annoying problem: we may have to replicate
1765 * the data up to 4 times:
1766 *
1767 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1768 *
1769 * To avoid penalizing shaders that emit a small number of vertices, we
1770 * can avoid these sometimes: if the size of the control data header is
1771 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1772 * land in the same 128-bit group, so we can skip per-slot offsets.
1773 *
1774 * Similarly, if the control data header is <= 32 bits, there is only one
1775 * DWord, so we can skip channel masks.
1776 */
1777 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
1778
1779 fs_reg channel_mask, per_slot_offset;
1780
1781 if (gs_compile->control_data_header_size_bits > 32) {
1782 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
1783 channel_mask = vgrf(glsl_type::uint_type);
1784 }
1785
1786 if (gs_compile->control_data_header_size_bits > 128) {
1787 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
1788 per_slot_offset = vgrf(glsl_type::uint_type);
1789 }
1790
1791 /* Figure out which DWord we're trying to write to using the formula:
1792 *
1793 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1794 *
1795 * Since bits_per_vertex is a power of two, and is known at compile
1796 * time, this can be optimized to:
1797 *
1798 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1799 */
1800 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
1801 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1802 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1803 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
1804 unsigned log2_bits_per_vertex =
1805 _mesa_fls(gs_compile->control_data_bits_per_vertex);
1806 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
1807
1808 if (per_slot_offset.file != BAD_FILE) {
1809 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1810 * the appropriate OWord within the control data header.
1811 */
1812 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
1813 }
1814
1815 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1816 * write to the appropriate DWORD within the OWORD.
1817 */
1818 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1819 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
1820 channel_mask = intexp2(fwa_bld, channel);
1821 /* Then the channel masks need to be in bits 23:16. */
1822 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
1823 }
1824
1825 /* Store the control data bits in the message payload and send it. */
1826 int mlen = 2;
1827 if (channel_mask.file != BAD_FILE)
1828 mlen += 4; /* channel masks, plus 3 extra copies of the data */
1829 if (per_slot_offset.file != BAD_FILE)
1830 mlen++;
1831
1832 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
1833 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
1834 int i = 0;
1835 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
1836 if (per_slot_offset.file != BAD_FILE)
1837 sources[i++] = per_slot_offset;
1838 if (channel_mask.file != BAD_FILE)
1839 sources[i++] = channel_mask;
1840 while (i < mlen) {
1841 sources[i++] = this->control_data_bits;
1842 }
1843
1844 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
1845 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
1846 inst->mlen = mlen;
1847 /* We need to increment Global Offset by 256-bits to make room for
1848 * Broadwell's extra "Vertex Count" payload at the beginning of the
1849 * URB entry. Since this is an OWord message, Global Offset is counted
1850 * in 128-bit units, so we must set it to 2.
1851 */
1852 if (gs_prog_data->static_vertex_count == -1)
1853 inst->offset = 2;
1854 }
1855
1856 void
1857 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
1858 unsigned stream_id)
1859 {
1860 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1861
1862 /* Note: we are calling this *before* increasing vertex_count, so
1863 * this->vertex_count == vertex_count - 1 in the formula above.
1864 */
1865
1866 /* Stream mode uses 2 bits per vertex */
1867 assert(gs_compile->control_data_bits_per_vertex == 2);
1868
1869 /* Must be a valid stream */
1870 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
1871
1872 /* Control data bits are initialized to 0 so we don't have to set any
1873 * bits when sending vertices to stream 0.
1874 */
1875 if (stream_id == 0)
1876 return;
1877
1878 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
1879
1880 /* reg::sid = stream_id */
1881 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1882 abld.MOV(sid, brw_imm_ud(stream_id));
1883
1884 /* reg:shift_count = 2 * (vertex_count - 1) */
1885 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1886 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
1887
1888 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1889 * attention to the lower 5 bits of its second source argument, so on this
1890 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1891 * stream_id << ((2 * (vertex_count - 1)) % 32).
1892 */
1893 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
1894 abld.SHL(mask, sid, shift_count);
1895 abld.OR(this->control_data_bits, this->control_data_bits, mask);
1896 }
1897
1898 void
1899 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
1900 unsigned stream_id)
1901 {
1902 assert(stage == MESA_SHADER_GEOMETRY);
1903
1904 struct brw_gs_prog_data *gs_prog_data =
1905 (struct brw_gs_prog_data *) prog_data;
1906
1907 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
1908 vertex_count.type = BRW_REGISTER_TYPE_UD;
1909
1910 /* Haswell and later hardware ignores the "Render Stream Select" bits
1911 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1912 * and instead sends all primitives down the pipeline for rasterization.
1913 * If the SOL stage is enabled, "Render Stream Select" is honored and
1914 * primitives bound to non-zero streams are discarded after stream output.
1915 *
1916 * Since the only purpose of primives sent to non-zero streams is to
1917 * be recorded by transform feedback, we can simply discard all geometry
1918 * bound to these streams when transform feedback is disabled.
1919 */
1920 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
1921 return;
1922
1923 /* If we're outputting 32 control data bits or less, then we can wait
1924 * until the shader is over to output them all. Otherwise we need to
1925 * output them as we go. Now is the time to do it, since we're about to
1926 * output the vertex_count'th vertex, so it's guaranteed that the
1927 * control data bits associated with the (vertex_count - 1)th vertex are
1928 * correct.
1929 */
1930 if (gs_compile->control_data_header_size_bits > 32) {
1931 const fs_builder abld =
1932 bld.annotate("emit vertex: emit control data bits");
1933
1934 /* Only emit control data bits if we've finished accumulating a batch
1935 * of 32 bits. This is the case when:
1936 *
1937 * (vertex_count * bits_per_vertex) % 32 == 0
1938 *
1939 * (in other words, when the last 5 bits of vertex_count *
1940 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1941 * integer n (which is always the case, since bits_per_vertex is
1942 * always 1 or 2), this is equivalent to requiring that the last 5-n
1943 * bits of vertex_count are 0:
1944 *
1945 * vertex_count & (2^(5-n) - 1) == 0
1946 *
1947 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1948 * equivalent to:
1949 *
1950 * vertex_count & (32 / bits_per_vertex - 1) == 0
1951 *
1952 * TODO: If vertex_count is an immediate, we could do some of this math
1953 * at compile time...
1954 */
1955 fs_inst *inst =
1956 abld.AND(bld.null_reg_d(), vertex_count,
1957 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
1958 inst->conditional_mod = BRW_CONDITIONAL_Z;
1959
1960 abld.IF(BRW_PREDICATE_NORMAL);
1961 /* If vertex_count is 0, then no control data bits have been
1962 * accumulated yet, so we can skip emitting them.
1963 */
1964 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
1965 BRW_CONDITIONAL_NEQ);
1966 abld.IF(BRW_PREDICATE_NORMAL);
1967 emit_gs_control_data_bits(vertex_count);
1968 abld.emit(BRW_OPCODE_ENDIF);
1969
1970 /* Reset control_data_bits to 0 so we can start accumulating a new
1971 * batch.
1972 *
1973 * Note: in the case where vertex_count == 0, this neutralizes the
1974 * effect of any call to EndPrimitive() that the shader may have
1975 * made before outputting its first vertex.
1976 */
1977 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
1978 inst->force_writemask_all = true;
1979 abld.emit(BRW_OPCODE_ENDIF);
1980 }
1981
1982 emit_urb_writes(vertex_count);
1983
1984 /* In stream mode we have to set control data bits for all vertices
1985 * unless we have disabled control data bits completely (which we do
1986 * do for GL_POINTS outputs that don't use streams).
1987 */
1988 if (gs_compile->control_data_header_size_bits > 0 &&
1989 gs_prog_data->control_data_format ==
1990 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
1991 set_gs_stream_control_data_bits(vertex_count, stream_id);
1992 }
1993 }
1994
1995 void
1996 fs_visitor::emit_gs_input_load(const fs_reg &dst,
1997 const nir_src &vertex_src,
1998 unsigned base_offset,
1999 const nir_src &offset_src,
2000 unsigned num_components,
2001 unsigned first_component)
2002 {
2003 struct brw_gs_prog_data *gs_prog_data = (struct brw_gs_prog_data *) prog_data;
2004
2005 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
2006 nir_const_value *offset_const = nir_src_as_const_value(offset_src);
2007 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
2008
2009 /* Offset 0 is the VUE header, which contains VARYING_SLOT_LAYER [.y],
2010 * VARYING_SLOT_VIEWPORT [.z], and VARYING_SLOT_PSIZ [.w]. Only
2011 * gl_PointSize is available as a GS input, however, so it must be that.
2012 */
2013 const bool is_point_size = (base_offset == 0);
2014
2015 /* TODO: figure out push input layout for invocations == 1 */
2016 if (gs_prog_data->invocations == 1 &&
2017 offset_const != NULL && vertex_const != NULL &&
2018 4 * (base_offset + offset_const->u32[0]) < push_reg_count) {
2019 int imm_offset = (base_offset + offset_const->u32[0]) * 4 +
2020 vertex_const->u32[0] * push_reg_count;
2021 /* This input was pushed into registers. */
2022 if (is_point_size) {
2023 /* gl_PointSize comes in .w */
2024 bld.MOV(dst, fs_reg(ATTR, imm_offset + 3, dst.type));
2025 } else {
2026 for (unsigned i = 0; i < num_components; i++) {
2027 bld.MOV(offset(dst, bld, i),
2028 fs_reg(ATTR, imm_offset + i, dst.type));
2029 }
2030 }
2031 return;
2032 }
2033
2034 /* Resort to the pull model. Ensure the VUE handles are provided. */
2035 gs_prog_data->base.include_vue_handles = true;
2036
2037 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
2038 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2039
2040 if (gs_prog_data->invocations == 1) {
2041 if (vertex_const) {
2042 /* The vertex index is constant; just select the proper URB handle. */
2043 icp_handle =
2044 retype(brw_vec8_grf(first_icp_handle + vertex_const->i32[0], 0),
2045 BRW_REGISTER_TYPE_UD);
2046 } else {
2047 /* The vertex index is non-constant. We need to use indirect
2048 * addressing to fetch the proper URB handle.
2049 *
2050 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2051 * indicating that channel <n> should read the handle from
2052 * DWord <n>. We convert that to bytes by multiplying by 4.
2053 *
2054 * Next, we convert the vertex index to bytes by multiplying
2055 * by 32 (shifting by 5), and add the two together. This is
2056 * the final indirect byte offset.
2057 */
2058 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_W, 1);
2059 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2060 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2061 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2062
2063 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2064 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2065 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2066 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2067 /* Convert vertex_index to bytes (multiply by 32) */
2068 bld.SHL(vertex_offset_bytes,
2069 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2070 brw_imm_ud(5u));
2071 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2072
2073 /* Use first_icp_handle as the base offset. There is one register
2074 * of URB handles per vertex, so inform the register allocator that
2075 * we might read up to nir->info.gs.vertices_in registers.
2076 */
2077 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2078 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
2079 fs_reg(icp_offset_bytes),
2080 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
2081 }
2082 } else {
2083 assert(gs_prog_data->invocations > 1);
2084
2085 if (vertex_const) {
2086 assert(devinfo->gen >= 9 || vertex_const->i32[0] <= 5);
2087 bld.MOV(icp_handle,
2088 retype(brw_vec1_grf(first_icp_handle +
2089 vertex_const->i32[0] / 8,
2090 vertex_const->i32[0] % 8),
2091 BRW_REGISTER_TYPE_UD));
2092 } else {
2093 /* The vertex index is non-constant. We need to use indirect
2094 * addressing to fetch the proper URB handle.
2095 *
2096 */
2097 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2098
2099 /* Convert vertex_index to bytes (multiply by 4) */
2100 bld.SHL(icp_offset_bytes,
2101 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2102 brw_imm_ud(2u));
2103
2104 /* Use first_icp_handle as the base offset. There is one DWord
2105 * of URB handles per vertex, so inform the register allocator that
2106 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2107 */
2108 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2109 fs_reg(brw_vec8_grf(first_icp_handle, 0)),
2110 fs_reg(icp_offset_bytes),
2111 brw_imm_ud(DIV_ROUND_UP(nir->info.gs.vertices_in, 8) *
2112 REG_SIZE));
2113 }
2114 }
2115
2116 fs_inst *inst;
2117
2118 fs_reg tmp_dst = dst;
2119 fs_reg indirect_offset = get_nir_src(offset_src);
2120 unsigned num_iterations = 1;
2121 unsigned orig_num_components = num_components;
2122
2123 if (type_sz(dst.type) == 8) {
2124 if (num_components > 2) {
2125 num_iterations = 2;
2126 num_components = 2;
2127 }
2128 fs_reg tmp = fs_reg(VGRF, alloc.allocate(4), dst.type);
2129 tmp_dst = tmp;
2130 first_component = first_component / 2;
2131 }
2132
2133 for (unsigned iter = 0; iter < num_iterations; iter++) {
2134 if (offset_const) {
2135 /* Constant indexing - use global offset. */
2136 if (first_component != 0) {
2137 unsigned read_components = num_components + first_component;
2138 fs_reg tmp = bld.vgrf(dst.type, read_components);
2139 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2140 inst->regs_written = read_components * type_sz(tmp_dst.type) / 4;
2141 for (unsigned i = 0; i < num_components; i++) {
2142 bld.MOV(offset(tmp_dst, bld, i),
2143 offset(tmp, bld, i + first_component));
2144 }
2145 } else {
2146 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp_dst,
2147 icp_handle);
2148 inst->regs_written = num_components * type_sz(tmp_dst.type) / 4;
2149 }
2150 inst->offset = base_offset + offset_const->u32[0];
2151 inst->mlen = 1;
2152 } else {
2153 /* Indirect indexing - use per-slot offsets as well. */
2154 const fs_reg srcs[] = { icp_handle, indirect_offset };
2155 unsigned read_components = num_components + first_component;
2156 fs_reg tmp = bld.vgrf(dst.type, read_components);
2157 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2158 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2159 if (first_component != 0) {
2160 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2161 payload);
2162 inst->regs_written = read_components * type_sz(tmp_dst.type) / 4;
2163 for (unsigned i = 0; i < num_components; i++) {
2164 bld.MOV(offset(tmp_dst, bld, i),
2165 offset(tmp, bld, i + first_component));
2166 }
2167 } else {
2168 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp_dst,
2169 payload);
2170 inst->regs_written = num_components * type_sz(tmp_dst.type) / 4;
2171 }
2172 inst->offset = base_offset;
2173 inst->mlen = 2;
2174 }
2175
2176 if (type_sz(dst.type) == 8) {
2177 shuffle_32bit_load_result_to_64bit_data(
2178 bld, tmp_dst, retype(tmp_dst, BRW_REGISTER_TYPE_F), num_components);
2179
2180 for (unsigned c = 0; c < num_components; c++)
2181 bld.MOV(offset(dst, bld, iter * 2 + c), offset(tmp_dst, bld, c));
2182 }
2183
2184 if (num_iterations > 1) {
2185 num_components = orig_num_components - 2;
2186 if(offset_const) {
2187 base_offset++;
2188 } else {
2189 fs_reg new_indirect = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2190 bld.ADD(new_indirect, indirect_offset, brw_imm_ud(1u));
2191 indirect_offset = new_indirect;
2192 }
2193 }
2194 }
2195
2196 if (is_point_size) {
2197 /* Read the whole VUE header (because of alignment) and read .w. */
2198 fs_reg tmp = bld.vgrf(dst.type, 4);
2199 inst->dst = tmp;
2200 inst->regs_written = 4;
2201 bld.MOV(dst, offset(tmp, bld, 3));
2202 }
2203 }
2204
2205 fs_reg
2206 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
2207 {
2208 nir_src *offset_src = nir_get_io_offset_src(instr);
2209 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
2210
2211 if (const_value) {
2212 /* The only constant offset we should find is 0. brw_nir.c's
2213 * add_const_offset_to_base() will fold other constant offsets
2214 * into instr->const_index[0].
2215 */
2216 assert(const_value->u32[0] == 0);
2217 return fs_reg();
2218 }
2219
2220 return get_nir_src(*offset_src);
2221 }
2222
2223 static void
2224 do_untyped_vector_read(const fs_builder &bld,
2225 const fs_reg dest,
2226 const fs_reg surf_index,
2227 const fs_reg offset_reg,
2228 unsigned num_components)
2229 {
2230 if (type_sz(dest.type) == 4) {
2231 fs_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
2232 1 /* dims */,
2233 num_components,
2234 BRW_PREDICATE_NONE);
2235 read_result.type = dest.type;
2236 for (unsigned i = 0; i < num_components; i++)
2237 bld.MOV(offset(dest, bld, i), offset(read_result, bld, i));
2238 } else if (type_sz(dest.type) == 8) {
2239 /* Reading a dvec, so we need to:
2240 *
2241 * 1. Multiply num_components by 2, to account for the fact that we
2242 * need to read 64-bit components.
2243 * 2. Shuffle the result of the load to form valid 64-bit elements
2244 * 3. Emit a second load (for components z/w) if needed.
2245 */
2246 fs_reg read_offset = bld.vgrf(BRW_REGISTER_TYPE_UD);
2247 bld.MOV(read_offset, offset_reg);
2248
2249 int iters = num_components <= 2 ? 1 : 2;
2250
2251 /* Load the dvec, the first iteration loads components x/y, the second
2252 * iteration, if needed, loads components z/w
2253 */
2254 for (int it = 0; it < iters; it++) {
2255 /* Compute number of components to read in this iteration */
2256 int iter_components = MIN2(2, num_components);
2257 num_components -= iter_components;
2258
2259 /* Read. Since this message reads 32-bit components, we need to
2260 * read twice as many components.
2261 */
2262 fs_reg read_result = emit_untyped_read(bld, surf_index, read_offset,
2263 1 /* dims */,
2264 iter_components * 2,
2265 BRW_PREDICATE_NONE);
2266
2267 /* Shuffle the 32-bit load result into valid 64-bit data */
2268 const fs_reg packed_result = bld.vgrf(dest.type, iter_components);
2269 shuffle_32bit_load_result_to_64bit_data(
2270 bld, packed_result, read_result, iter_components);
2271
2272 /* Move each component to its destination */
2273 read_result = retype(read_result, BRW_REGISTER_TYPE_DF);
2274 for (int c = 0; c < iter_components; c++) {
2275 bld.MOV(offset(dest, bld, it * 2 + c),
2276 offset(packed_result, bld, c));
2277 }
2278
2279 bld.ADD(read_offset, read_offset, brw_imm_ud(16));
2280 }
2281 } else {
2282 unreachable("Unsupported type");
2283 }
2284 }
2285
2286 void
2287 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
2288 nir_intrinsic_instr *instr)
2289 {
2290 assert(stage == MESA_SHADER_VERTEX);
2291
2292 fs_reg dest;
2293 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2294 dest = get_nir_dest(instr->dest);
2295
2296 switch (instr->intrinsic) {
2297 case nir_intrinsic_load_vertex_id:
2298 unreachable("should be lowered by lower_vertex_id()");
2299
2300 case nir_intrinsic_load_vertex_id_zero_base:
2301 case nir_intrinsic_load_base_vertex:
2302 case nir_intrinsic_load_instance_id:
2303 case nir_intrinsic_load_base_instance:
2304 case nir_intrinsic_load_draw_id: {
2305 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
2306 fs_reg val = nir_system_values[sv];
2307 assert(val.file != BAD_FILE);
2308 dest.type = val.type;
2309 bld.MOV(dest, val);
2310 break;
2311 }
2312
2313 default:
2314 nir_emit_intrinsic(bld, instr);
2315 break;
2316 }
2317 }
2318
2319 void
2320 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
2321 nir_intrinsic_instr *instr)
2322 {
2323 assert(stage == MESA_SHADER_TESS_CTRL);
2324 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2325 struct brw_tcs_prog_data *tcs_prog_data =
2326 (struct brw_tcs_prog_data *) prog_data;
2327
2328 fs_reg dst;
2329 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2330 dst = get_nir_dest(instr->dest);
2331
2332 switch (instr->intrinsic) {
2333 case nir_intrinsic_load_primitive_id:
2334 bld.MOV(dst, fs_reg(brw_vec1_grf(0, 1)));
2335 break;
2336 case nir_intrinsic_load_invocation_id:
2337 bld.MOV(retype(dst, invocation_id.type), invocation_id);
2338 break;
2339 case nir_intrinsic_load_patch_vertices_in:
2340 bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
2341 brw_imm_d(tcs_key->input_vertices));
2342 break;
2343
2344 case nir_intrinsic_barrier: {
2345 if (tcs_prog_data->instances == 1)
2346 break;
2347
2348 fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2349 fs_reg m0_2 = byte_offset(m0, 2 * sizeof(uint32_t));
2350
2351 const fs_builder fwa_bld = bld.exec_all();
2352
2353 /* Zero the message header */
2354 fwa_bld.MOV(m0, brw_imm_ud(0u));
2355
2356 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2357 fwa_bld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2358 brw_imm_ud(INTEL_MASK(16, 13)));
2359
2360 /* Shift it up to bits 27:24. */
2361 fwa_bld.SHL(m0_2, m0_2, brw_imm_ud(11));
2362
2363 /* Set the Barrier Count and the enable bit */
2364 fwa_bld.OR(m0_2, m0_2,
2365 brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
2366
2367 bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
2368 break;
2369 }
2370
2371 case nir_intrinsic_load_input:
2372 unreachable("nir_lower_io should never give us these.");
2373 break;
2374
2375 case nir_intrinsic_load_per_vertex_input: {
2376 fs_reg indirect_offset = get_indirect_offset(instr);
2377 unsigned imm_offset = instr->const_index[0];
2378
2379 const nir_src &vertex_src = instr->src[0];
2380 nir_const_value *vertex_const = nir_src_as_const_value(vertex_src);
2381
2382 fs_inst *inst;
2383
2384 fs_reg icp_handle;
2385
2386 if (vertex_const) {
2387 /* Emit a MOV to resolve <0,1,0> regioning. */
2388 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2389 bld.MOV(icp_handle,
2390 retype(brw_vec1_grf(1 + (vertex_const->i32[0] >> 3),
2391 vertex_const->i32[0] & 7),
2392 BRW_REGISTER_TYPE_UD));
2393 } else if (tcs_prog_data->instances == 1 &&
2394 vertex_src.is_ssa &&
2395 vertex_src.ssa->parent_instr->type == nir_instr_type_intrinsic &&
2396 nir_instr_as_intrinsic(vertex_src.ssa->parent_instr)->intrinsic == nir_intrinsic_load_invocation_id) {
2397 /* For the common case of only 1 instance, an array index of
2398 * gl_InvocationID means reading g1. Skip all the indirect work.
2399 */
2400 icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2401 } else {
2402 /* The vertex index is non-constant. We need to use indirect
2403 * addressing to fetch the proper URB handle.
2404 */
2405 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2406
2407 /* Each ICP handle is a single DWord (4 bytes) */
2408 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2409 bld.SHL(vertex_offset_bytes,
2410 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2411 brw_imm_ud(2u));
2412
2413 /* Start at g1. We might read up to 4 registers. */
2414 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2415 fs_reg(brw_vec8_grf(1, 0)), vertex_offset_bytes,
2416 brw_imm_ud(4 * REG_SIZE));
2417 }
2418
2419 /* We can only read two double components with each URB read, so
2420 * we send two read messages in that case, each one loading up to
2421 * two double components.
2422 */
2423 unsigned num_iterations = 1;
2424 unsigned num_components = instr->num_components;
2425 fs_reg orig_dst = dst;
2426 if (type_sz(dst.type) == 8) {
2427 if (instr->num_components > 2) {
2428 num_iterations = 2;
2429 num_components = 2;
2430 }
2431
2432 fs_reg tmp = fs_reg(VGRF, alloc.allocate(4), dst.type);
2433 dst = tmp;
2434 }
2435
2436 unsigned first_component = nir_intrinsic_component(instr);
2437 for (unsigned iter = 0; iter < num_iterations; iter++) {
2438 if (indirect_offset.file == BAD_FILE) {
2439 /* Constant indexing - use global offset. */
2440 if (first_component != 0) {
2441 unsigned read_components = num_components + first_component;
2442 fs_reg tmp = bld.vgrf(dst.type, read_components);
2443 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2444 for (unsigned i = 0; i < num_components; i++) {
2445 bld.MOV(offset(dst, bld, i),
2446 offset(tmp, bld, i + first_component));
2447 }
2448 } else {
2449 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2450 }
2451 inst->offset = imm_offset;
2452 inst->mlen = 1;
2453 } else {
2454 /* Indirect indexing - use per-slot offsets as well. */
2455 const fs_reg srcs[] = { icp_handle, indirect_offset };
2456 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2457 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2458 if (first_component != 0) {
2459 unsigned read_components = num_components + first_component;
2460 fs_reg tmp = bld.vgrf(dst.type, read_components);
2461 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2462 payload);
2463 for (unsigned i = 0; i < num_components; i++) {
2464 bld.MOV(offset(dst, bld, i),
2465 offset(tmp, bld, i + first_component));
2466 }
2467 } else {
2468 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
2469 payload);
2470 }
2471 inst->offset = imm_offset;
2472 inst->mlen = 2;
2473 }
2474 inst->regs_written =
2475 (num_components * type_sz(dst.type) / 4) + first_component;
2476
2477 /* If we are reading 64-bit data using 32-bit read messages we need
2478 * build proper 64-bit data elements by shuffling the low and high
2479 * 32-bit components around like we do for other things like UBOs
2480 * or SSBOs.
2481 */
2482 if (type_sz(dst.type) == 8) {
2483 shuffle_32bit_load_result_to_64bit_data(
2484 bld, dst, retype(dst, BRW_REGISTER_TYPE_F), num_components);
2485
2486 for (unsigned c = 0; c < num_components; c++) {
2487 bld.MOV(offset(orig_dst, bld, iter * 2 + c),
2488 offset(dst, bld, c));
2489 }
2490 }
2491
2492 /* Copy the temporary to the destination to deal with writemasking.
2493 *
2494 * Also attempt to deal with gl_PointSize being in the .w component.
2495 */
2496 if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
2497 assert(type_sz(dst.type) < 8);
2498 inst->dst = bld.vgrf(dst.type, 4);
2499 inst->regs_written = 4;
2500 bld.MOV(dst, offset(inst->dst, bld, 3));
2501 }
2502
2503 /* If we are loading double data and we need a second read message
2504 * adjust the write offset
2505 */
2506 if (num_iterations > 1) {
2507 num_components = instr->num_components - 2;
2508 if (indirect_offset.file == BAD_FILE) {
2509 imm_offset++;
2510 } else {
2511 fs_reg new_indirect = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2512 bld.ADD(new_indirect, indirect_offset, brw_imm_ud(1u));
2513 indirect_offset = new_indirect;
2514 }
2515 }
2516 }
2517 break;
2518 }
2519
2520 case nir_intrinsic_load_output:
2521 case nir_intrinsic_load_per_vertex_output: {
2522 fs_reg indirect_offset = get_indirect_offset(instr);
2523 unsigned imm_offset = instr->const_index[0];
2524
2525 fs_inst *inst;
2526 if (indirect_offset.file == BAD_FILE) {
2527 /* Replicate the patch handle to all enabled channels */
2528 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2529 bld.MOV(patch_handle,
2530 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD));
2531
2532 if (imm_offset == 0) {
2533 /* This is a read of gl_TessLevelInner[], which lives in the
2534 * Patch URB header. The layout depends on the domain.
2535 */
2536 dst.type = BRW_REGISTER_TYPE_F;
2537 switch (tcs_key->tes_primitive_mode) {
2538 case GL_QUADS: {
2539 /* DWords 3-2 (reversed) */
2540 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2541
2542 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2543 inst->offset = 0;
2544 inst->mlen = 1;
2545 inst->regs_written = 4;
2546
2547 /* dst.xy = tmp.wz */
2548 bld.MOV(dst, offset(tmp, bld, 3));
2549 bld.MOV(offset(dst, bld, 1), offset(tmp, bld, 2));
2550 break;
2551 }
2552 case GL_TRIANGLES:
2553 /* DWord 4; hardcode offset = 1 and regs_written = 1 */
2554 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2555 inst->offset = 1;
2556 inst->mlen = 1;
2557 inst->regs_written = 1;
2558 break;
2559 case GL_ISOLINES:
2560 /* All channels are undefined. */
2561 break;
2562 default:
2563 unreachable("Bogus tessellation domain");
2564 }
2565 } else if (imm_offset == 1) {
2566 /* This is a read of gl_TessLevelOuter[], which lives in the
2567 * Patch URB header. The layout depends on the domain.
2568 */
2569 dst.type = BRW_REGISTER_TYPE_F;
2570
2571 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
2572 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, patch_handle);
2573 inst->offset = 1;
2574 inst->mlen = 1;
2575 inst->regs_written = 4;
2576
2577 /* Reswizzle: WZYX */
2578 fs_reg srcs[4] = {
2579 offset(tmp, bld, 3),
2580 offset(tmp, bld, 2),
2581 offset(tmp, bld, 1),
2582 offset(tmp, bld, 0),
2583 };
2584
2585 unsigned num_components;
2586 switch (tcs_key->tes_primitive_mode) {
2587 case GL_QUADS:
2588 num_components = 4;
2589 break;
2590 case GL_TRIANGLES:
2591 num_components = 3;
2592 break;
2593 case GL_ISOLINES:
2594 /* Isolines are not reversed; swizzle .zw -> .xy */
2595 srcs[0] = offset(tmp, bld, 2);
2596 srcs[1] = offset(tmp, bld, 3);
2597 num_components = 2;
2598 break;
2599 default:
2600 unreachable("Bogus tessellation domain");
2601 }
2602 bld.LOAD_PAYLOAD(dst, srcs, num_components, 0);
2603 } else {
2604 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, patch_handle);
2605 inst->offset = imm_offset;
2606 inst->mlen = 1;
2607 inst->regs_written = instr->num_components;
2608 }
2609 } else {
2610 /* Indirect indexing - use per-slot offsets as well. */
2611 const fs_reg srcs[] = {
2612 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2613 indirect_offset
2614 };
2615 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2616 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2617
2618 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2619 inst->offset = imm_offset;
2620 inst->mlen = 2;
2621 inst->regs_written = instr->num_components;
2622 }
2623 break;
2624 }
2625
2626 case nir_intrinsic_store_output:
2627 case nir_intrinsic_store_per_vertex_output: {
2628 fs_reg value = get_nir_src(instr->src[0]);
2629 bool is_64bit = (instr->src[0].is_ssa ?
2630 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size) == 64;
2631 fs_reg indirect_offset = get_indirect_offset(instr);
2632 unsigned imm_offset = instr->const_index[0];
2633 unsigned swiz = BRW_SWIZZLE_XYZW;
2634 unsigned mask = instr->const_index[1];
2635 unsigned header_regs = 0;
2636 fs_reg srcs[7];
2637 srcs[header_regs++] = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
2638
2639 if (indirect_offset.file != BAD_FILE) {
2640 srcs[header_regs++] = indirect_offset;
2641 } else if (!is_passthrough_shader) {
2642 if (imm_offset == 0) {
2643 value.type = BRW_REGISTER_TYPE_F;
2644
2645 mask &= (1 << tesslevel_inner_components(tcs_key->tes_primitive_mode)) - 1;
2646
2647 /* This is a write to gl_TessLevelInner[], which lives in the
2648 * Patch URB header. The layout depends on the domain.
2649 */
2650 switch (tcs_key->tes_primitive_mode) {
2651 case GL_QUADS:
2652 /* gl_TessLevelInner[].xy lives at DWords 3-2 (reversed).
2653 * We use an XXYX swizzle to reverse put .xy in the .wz
2654 * channels, and use a .zw writemask.
2655 */
2656 mask = writemask_for_backwards_vector(mask);
2657 swiz = BRW_SWIZZLE4(0, 0, 1, 0);
2658 break;
2659 case GL_TRIANGLES:
2660 /* gl_TessLevelInner[].x lives at DWord 4, so we set the
2661 * writemask to X and bump the URB offset by 1.
2662 */
2663 imm_offset = 1;
2664 break;
2665 case GL_ISOLINES:
2666 /* Skip; gl_TessLevelInner[] doesn't exist for isolines. */
2667 return;
2668 default:
2669 unreachable("Bogus tessellation domain");
2670 }
2671 } else if (imm_offset == 1) {
2672 /* This is a write to gl_TessLevelOuter[] which lives in the
2673 * Patch URB Header at DWords 4-7. However, it's reversed, so
2674 * instead of .xyzw we have .wzyx.
2675 */
2676 value.type = BRW_REGISTER_TYPE_F;
2677
2678 mask &= (1 << tesslevel_outer_components(tcs_key->tes_primitive_mode)) - 1;
2679
2680 if (tcs_key->tes_primitive_mode == GL_ISOLINES) {
2681 /* Isolines .xy should be stored in .zw, in order. */
2682 swiz = BRW_SWIZZLE4(0, 0, 0, 1);
2683 mask <<= 2;
2684 } else {
2685 /* Other domains are reversed; store .wzyx instead of .xyzw */
2686 swiz = BRW_SWIZZLE_WZYX;
2687 mask = writemask_for_backwards_vector(mask);
2688 }
2689 }
2690 }
2691
2692 if (mask == 0)
2693 break;
2694
2695 unsigned num_components = _mesa_fls(mask);
2696 enum opcode opcode;
2697
2698 /* We can only pack two 64-bit components in a single message, so send
2699 * 2 messages if we have more components
2700 */
2701 unsigned num_iterations = 1;
2702 unsigned iter_components = num_components;
2703 if (is_64bit && instr->num_components > 2) {
2704 num_iterations = 2;
2705 iter_components = 2;
2706 }
2707
2708 /* 64-bit data needs to me shuffled before we can write it to the URB.
2709 * We will use this temporary to shuffle the components in each
2710 * iteration.
2711 */
2712 fs_reg tmp =
2713 fs_reg(VGRF, alloc.allocate(2 * iter_components), value.type);
2714
2715 unsigned first_component = nir_intrinsic_component(instr);
2716 mask = mask << first_component;
2717
2718 for (unsigned iter = 0; iter < num_iterations; iter++) {
2719 if (!is_64bit && mask != WRITEMASK_XYZW) {
2720 srcs[header_regs++] = brw_imm_ud(mask << 16);
2721 opcode = indirect_offset.file != BAD_FILE ?
2722 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2723 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2724 } else if (is_64bit && ((mask & WRITEMASK_XY) != WRITEMASK_XY)) {
2725 /* Expand the 64-bit mask to 32-bit channels. We only handle
2726 * two channels in each iteration, so we only care about X/Y.
2727 */
2728 unsigned mask32 = 0;
2729 if (mask & WRITEMASK_X)
2730 mask32 |= WRITEMASK_XY;
2731 if (mask & WRITEMASK_Y)
2732 mask32 |= WRITEMASK_ZW;
2733
2734 /* If the mask does not include any of the channels X or Y there
2735 * is nothing to do in this iteration. Move on to the next couple
2736 * of 64-bit channels.
2737 */
2738 if (!mask32) {
2739 mask >>= 2;
2740 imm_offset++;
2741 continue;
2742 }
2743
2744 srcs[header_regs++] = brw_imm_ud(mask32 << 16);
2745 opcode = indirect_offset.file != BAD_FILE ?
2746 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2747 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2748 } else {
2749 opcode = indirect_offset.file != BAD_FILE ?
2750 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
2751 SHADER_OPCODE_URB_WRITE_SIMD8;
2752 }
2753
2754 for (unsigned i = 0; i < iter_components; i++) {
2755 if (!(mask & (1 << (i + first_component))))
2756 continue;
2757
2758 if (!is_64bit) {
2759 srcs[header_regs + i + first_component] =
2760 offset(value, bld, BRW_GET_SWZ(swiz, i));
2761 } else {
2762 /* We need to shuffle the 64-bit data to match the layout
2763 * expected by our 32-bit URB write messages. We use a temporary
2764 * for that.
2765 */
2766 unsigned channel = BRW_GET_SWZ(swiz, iter * 2 + i);
2767 shuffle_64bit_data_for_32bit_write(bld,
2768 retype(offset(tmp, bld, 2 * i), BRW_REGISTER_TYPE_F),
2769 retype(offset(value, bld, 2 * channel), BRW_REGISTER_TYPE_DF),
2770 1);
2771
2772 /* Now copy the data to the destination */
2773 fs_reg dest = fs_reg(VGRF, alloc.allocate(2), value.type);
2774 unsigned idx = 2 * i;
2775 bld.MOV(dest, offset(tmp, bld, idx));
2776 bld.MOV(offset(dest, bld, 1), offset(tmp, bld, idx + 1));
2777 srcs[header_regs + idx] = dest;
2778 srcs[header_regs + idx + 1] = offset(dest, bld, 1);
2779 }
2780 }
2781
2782 unsigned mlen =
2783 header_regs + (is_64bit ? 2 * iter_components : iter_components) +
2784 first_component;
2785 fs_reg payload =
2786 bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2787 bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
2788
2789 fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
2790 inst->offset = imm_offset;
2791 inst->mlen = mlen;
2792
2793 /* If this is a 64-bit attribute, select the next two 64-bit channels
2794 * to be handled in the next iteration.
2795 */
2796 if (is_64bit) {
2797 mask >>= 2;
2798 imm_offset++;
2799 }
2800 }
2801 break;
2802 }
2803
2804 default:
2805 nir_emit_intrinsic(bld, instr);
2806 break;
2807 }
2808 }
2809
2810 void
2811 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
2812 nir_intrinsic_instr *instr)
2813 {
2814 assert(stage == MESA_SHADER_TESS_EVAL);
2815 struct brw_tes_prog_data *tes_prog_data = (struct brw_tes_prog_data *) prog_data;
2816
2817 fs_reg dest;
2818 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2819 dest = get_nir_dest(instr->dest);
2820
2821 switch (instr->intrinsic) {
2822 case nir_intrinsic_load_primitive_id:
2823 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
2824 break;
2825 case nir_intrinsic_load_tess_coord:
2826 /* gl_TessCoord is part of the payload in g1-3 */
2827 for (unsigned i = 0; i < 3; i++) {
2828 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
2829 }
2830 break;
2831
2832 case nir_intrinsic_load_tess_level_outer:
2833 /* When the TES reads gl_TessLevelOuter, we ensure that the patch header
2834 * appears as a push-model input. So, we can simply use the ATTR file
2835 * rather than issuing URB read messages. The data is stored in the
2836 * high DWords in reverse order - DWord 7 contains .x, DWord 6 contains
2837 * .y, and so on.
2838 */
2839 switch (tes_prog_data->domain) {
2840 case BRW_TESS_DOMAIN_QUAD:
2841 for (unsigned i = 0; i < 4; i++)
2842 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2843 break;
2844 case BRW_TESS_DOMAIN_TRI:
2845 for (unsigned i = 0; i < 3; i++)
2846 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 7 - i));
2847 break;
2848 case BRW_TESS_DOMAIN_ISOLINE:
2849 for (unsigned i = 0; i < 2; i++)
2850 bld.MOV(offset(dest, bld, i), component(fs_reg(ATTR, 0), 6 + i));
2851 break;
2852 }
2853 break;
2854
2855 case nir_intrinsic_load_tess_level_inner:
2856 /* When the TES reads gl_TessLevelInner, we ensure that the patch header
2857 * appears as a push-model input. So, we can simply use the ATTR file
2858 * rather than issuing URB read messages.
2859 */
2860 switch (tes_prog_data->domain) {
2861 case BRW_TESS_DOMAIN_QUAD:
2862 bld.MOV(dest, component(fs_reg(ATTR, 0), 3));
2863 bld.MOV(offset(dest, bld, 1), component(fs_reg(ATTR, 0), 2));
2864 break;
2865 case BRW_TESS_DOMAIN_TRI:
2866 bld.MOV(dest, component(fs_reg(ATTR, 0), 4));
2867 break;
2868 case BRW_TESS_DOMAIN_ISOLINE:
2869 /* ignore - value is undefined */
2870 break;
2871 }
2872 break;
2873
2874 case nir_intrinsic_load_input:
2875 case nir_intrinsic_load_per_vertex_input: {
2876 fs_reg indirect_offset = get_indirect_offset(instr);
2877 unsigned imm_offset = instr->const_index[0];
2878 unsigned first_component = nir_intrinsic_component(instr);
2879
2880 fs_inst *inst;
2881 if (indirect_offset.file == BAD_FILE) {
2882 /* Arbitrarily only push up to 32 vec4 slots worth of data,
2883 * which is 16 registers (since each holds 2 vec4 slots).
2884 */
2885 const unsigned max_push_slots = 32;
2886 if (imm_offset < max_push_slots) {
2887 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
2888 for (int i = 0; i < instr->num_components; i++) {
2889 unsigned comp = 16 / type_sz(dest.type) * (imm_offset % 2) +
2890 i + first_component;
2891 bld.MOV(offset(dest, bld, i), component(src, comp));
2892 }
2893 tes_prog_data->base.urb_read_length =
2894 MAX2(tes_prog_data->base.urb_read_length,
2895 DIV_ROUND_UP(imm_offset + 1, 2));
2896 } else {
2897 /* Replicate the patch handle to all enabled channels */
2898 const fs_reg srcs[] = {
2899 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
2900 };
2901 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2902 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
2903
2904 if (first_component != 0) {
2905 unsigned read_components =
2906 instr->num_components + first_component;
2907 fs_reg tmp = bld.vgrf(dest.type, read_components);
2908 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
2909 patch_handle);
2910 inst->regs_written = read_components;
2911 for (unsigned i = 0; i < instr->num_components; i++) {
2912 bld.MOV(offset(dest, bld, i),
2913 offset(tmp, bld, i + first_component));
2914 }
2915 } else {
2916 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest,
2917 patch_handle);
2918 inst->regs_written = instr->num_components;
2919 }
2920 inst->mlen = 1;
2921 inst->offset = imm_offset;
2922 }
2923 } else {
2924 /* Indirect indexing - use per-slot offsets as well. */
2925 const fs_reg srcs[] = {
2926 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
2927 indirect_offset
2928 };
2929 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2930 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2931
2932 if (first_component != 0) {
2933 unsigned read_components =
2934 instr->num_components + first_component;
2935 fs_reg tmp = bld.vgrf(dest.type, read_components);
2936 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2937 payload);
2938 inst->regs_written = read_components;
2939 for (unsigned i = 0; i < instr->num_components; i++) {
2940 bld.MOV(offset(dest, bld, i),
2941 offset(tmp, bld, i + first_component));
2942 }
2943 } else {
2944 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest,
2945 payload);
2946 inst->regs_written = instr->num_components;
2947 }
2948 inst->mlen = 2;
2949 inst->offset = imm_offset;
2950 }
2951 break;
2952 }
2953 default:
2954 nir_emit_intrinsic(bld, instr);
2955 break;
2956 }
2957 }
2958
2959 void
2960 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
2961 nir_intrinsic_instr *instr)
2962 {
2963 assert(stage == MESA_SHADER_GEOMETRY);
2964 fs_reg indirect_offset;
2965
2966 fs_reg dest;
2967 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2968 dest = get_nir_dest(instr->dest);
2969
2970 switch (instr->intrinsic) {
2971 case nir_intrinsic_load_primitive_id:
2972 assert(stage == MESA_SHADER_GEOMETRY);
2973 assert(((struct brw_gs_prog_data *)prog_data)->include_primitive_id);
2974 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
2975 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
2976 break;
2977
2978 case nir_intrinsic_load_input:
2979 unreachable("load_input intrinsics are invalid for the GS stage");
2980
2981 case nir_intrinsic_load_per_vertex_input:
2982 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
2983 instr->src[1], instr->num_components,
2984 nir_intrinsic_component(instr));
2985 break;
2986
2987 case nir_intrinsic_emit_vertex_with_counter:
2988 emit_gs_vertex(instr->src[0], instr->const_index[0]);
2989 break;
2990
2991 case nir_intrinsic_end_primitive_with_counter:
2992 emit_gs_end_primitive(instr->src[0]);
2993 break;
2994
2995 case nir_intrinsic_set_vertex_count:
2996 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
2997 break;
2998
2999 case nir_intrinsic_load_invocation_id: {
3000 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
3001 assert(val.file != BAD_FILE);
3002 dest.type = val.type;
3003 bld.MOV(dest, val);
3004 break;
3005 }
3006
3007 default:
3008 nir_emit_intrinsic(bld, instr);
3009 break;
3010 }
3011 }
3012
3013 void
3014 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
3015 nir_intrinsic_instr *instr)
3016 {
3017 assert(stage == MESA_SHADER_FRAGMENT);
3018 struct brw_wm_prog_data *wm_prog_data =
3019 (struct brw_wm_prog_data *) prog_data;
3020 const struct brw_wm_prog_key *wm_key = (const struct brw_wm_prog_key *) key;
3021
3022 fs_reg dest;
3023 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3024 dest = get_nir_dest(instr->dest);
3025
3026 switch (instr->intrinsic) {
3027 case nir_intrinsic_load_front_face:
3028 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
3029 *emit_frontfacing_interpolation());
3030 break;
3031
3032 case nir_intrinsic_load_sample_pos: {
3033 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
3034 assert(sample_pos.file != BAD_FILE);
3035 dest.type = sample_pos.type;
3036 bld.MOV(dest, sample_pos);
3037 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
3038 break;
3039 }
3040
3041 case nir_intrinsic_load_helper_invocation:
3042 case nir_intrinsic_load_sample_mask_in:
3043 case nir_intrinsic_load_sample_id: {
3044 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3045 fs_reg val = nir_system_values[sv];
3046 assert(val.file != BAD_FILE);
3047 dest.type = val.type;
3048 bld.MOV(dest, val);
3049 break;
3050 }
3051
3052 case nir_intrinsic_discard:
3053 case nir_intrinsic_discard_if: {
3054 /* We track our discarded pixels in f0.1. By predicating on it, we can
3055 * update just the flag bits that aren't yet discarded. If there's no
3056 * condition, we emit a CMP of g0 != g0, so all currently executing
3057 * channels will get turned off.
3058 */
3059 fs_inst *cmp;
3060 if (instr->intrinsic == nir_intrinsic_discard_if) {
3061 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
3062 brw_imm_d(0), BRW_CONDITIONAL_Z);
3063 } else {
3064 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
3065 BRW_REGISTER_TYPE_UW));
3066 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
3067 }
3068 cmp->predicate = BRW_PREDICATE_NORMAL;
3069 cmp->flag_subreg = 1;
3070
3071 if (devinfo->gen >= 6) {
3072 emit_discard_jump();
3073 }
3074 break;
3075 }
3076
3077 case nir_intrinsic_interp_var_at_centroid:
3078 case nir_intrinsic_interp_var_at_sample:
3079 case nir_intrinsic_interp_var_at_offset: {
3080 /* Handle ARB_gpu_shader5 interpolation intrinsics
3081 *
3082 * It's worth a quick word of explanation as to why we handle the full
3083 * variable-based interpolation intrinsic rather than a lowered version
3084 * with like we do for other inputs. We have to do that because the way
3085 * we set up inputs doesn't allow us to use the already setup inputs for
3086 * interpolation. At the beginning of the shader, we go through all of
3087 * the input variables and do the initial interpolation and put it in
3088 * the nir_inputs array based on its location as determined in
3089 * nir_lower_io. If the input isn't used, dead code cleans up and
3090 * everything works fine. However, when we get to the ARB_gpu_shader5
3091 * interpolation intrinsics, we need to reinterpolate the input
3092 * differently. If we used an intrinsic that just had an index it would
3093 * only give us the offset into the nir_inputs array. However, this is
3094 * useless because that value is post-interpolation and we need
3095 * pre-interpolation. In order to get the actual location of the bits
3096 * we get from the vertex fetching hardware, we need the variable.
3097 */
3098 wm_prog_data->pulls_bary = true;
3099
3100 fs_reg dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3101 const glsl_interp_qualifier interpolation =
3102 (glsl_interp_qualifier) instr->variables[0]->var->data.interpolation;
3103
3104 switch (instr->intrinsic) {
3105 case nir_intrinsic_interp_var_at_centroid:
3106 emit_pixel_interpolater_send(bld,
3107 FS_OPCODE_INTERPOLATE_AT_CENTROID,
3108 dst_xy,
3109 fs_reg(), /* src */
3110 brw_imm_ud(0u),
3111 interpolation);
3112 break;
3113
3114 case nir_intrinsic_interp_var_at_sample: {
3115 if (!wm_key->multisample_fbo) {
3116 /* From the ARB_gpu_shader5 specification:
3117 * "If multisample buffers are not available, the input varying
3118 * will be evaluated at the center of the pixel."
3119 */
3120 emit_pixel_interpolater_send(bld,
3121 FS_OPCODE_INTERPOLATE_AT_CENTROID,
3122 dst_xy,
3123 fs_reg(), /* src */
3124 brw_imm_ud(0u),
3125 interpolation);
3126 break;
3127 }
3128
3129 nir_const_value *const_sample = nir_src_as_const_value(instr->src[0]);
3130
3131 if (const_sample) {
3132 unsigned msg_data = const_sample->i32[0] << 4;
3133
3134 emit_pixel_interpolater_send(bld,
3135 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3136 dst_xy,
3137 fs_reg(), /* src */
3138 brw_imm_ud(msg_data),
3139 interpolation);
3140 } else {
3141 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
3142 BRW_REGISTER_TYPE_UD);
3143
3144 if (nir_src_is_dynamically_uniform(instr->src[0])) {
3145 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3146 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3147 bld.exec_all().group(1, 0)
3148 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3149 emit_pixel_interpolater_send(bld,
3150 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3151 dst_xy,
3152 fs_reg(), /* src */
3153 msg_data,
3154 interpolation);
3155 } else {
3156 /* Make a loop that sends a message to the pixel interpolater
3157 * for the sample number in each live channel. If there are
3158 * multiple channels with the same sample number then these
3159 * will be handled simultaneously with a single interation of
3160 * the loop.
3161 */
3162 bld.emit(BRW_OPCODE_DO);
3163
3164 /* Get the next live sample number into sample_id_reg */
3165 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3166
3167 /* Set the flag register so that we can perform the send
3168 * message on all channels that have the same sample number
3169 */
3170 bld.CMP(bld.null_reg_ud(),
3171 sample_src, sample_id,
3172 BRW_CONDITIONAL_EQ);
3173 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3174 bld.exec_all().group(1, 0)
3175 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3176 fs_inst *inst =
3177 emit_pixel_interpolater_send(bld,
3178 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3179 dst_xy,
3180 fs_reg(), /* src */
3181 msg_data,
3182 interpolation);
3183 set_predicate(BRW_PREDICATE_NORMAL, inst);
3184
3185 /* Continue the loop if there are any live channels left */
3186 set_predicate_inv(BRW_PREDICATE_NORMAL,
3187 true, /* inverse */
3188 bld.emit(BRW_OPCODE_WHILE));
3189 }
3190 }
3191
3192 break;
3193 }
3194
3195 case nir_intrinsic_interp_var_at_offset: {
3196 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3197
3198 if (const_offset) {
3199 unsigned off_x = MIN2((int)(const_offset->f32[0] * 16), 7) & 0xf;
3200 unsigned off_y = MIN2((int)(const_offset->f32[1] * 16), 7) & 0xf;
3201
3202 emit_pixel_interpolater_send(bld,
3203 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
3204 dst_xy,
3205 fs_reg(), /* src */
3206 brw_imm_ud(off_x | (off_y << 4)),
3207 interpolation);
3208 } else {
3209 fs_reg src = vgrf(glsl_type::ivec2_type);
3210 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
3211 BRW_REGISTER_TYPE_F);
3212 for (int i = 0; i < 2; i++) {
3213 fs_reg temp = vgrf(glsl_type::float_type);
3214 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
3215 fs_reg itemp = vgrf(glsl_type::int_type);
3216 /* float to int */
3217 bld.MOV(itemp, temp);
3218
3219 /* Clamp the upper end of the range to +7/16.
3220 * ARB_gpu_shader5 requires that we support a maximum offset
3221 * of +0.5, which isn't representable in a S0.4 value -- if
3222 * we didn't clamp it, we'd end up with -8/16, which is the
3223 * opposite of what the shader author wanted.
3224 *
3225 * This is legal due to ARB_gpu_shader5's quantization
3226 * rules:
3227 *
3228 * "Not all values of <offset> may be supported; x and y
3229 * offsets may be rounded to fixed-point values with the
3230 * number of fraction bits given by the
3231 * implementation-dependent constant
3232 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3233 */
3234 set_condmod(BRW_CONDITIONAL_L,
3235 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
3236 }
3237
3238 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
3239 emit_pixel_interpolater_send(bld,
3240 opcode,
3241 dst_xy,
3242 src,
3243 brw_imm_ud(0u),
3244 interpolation);
3245 }
3246 break;
3247 }
3248
3249 default:
3250 unreachable("Invalid intrinsic");
3251 }
3252
3253 for (unsigned j = 0; j < instr->num_components; j++) {
3254 fs_reg src = interp_reg(instr->variables[0]->var->data.location, j);
3255 src.type = dest.type;
3256
3257 bld.emit(FS_OPCODE_LINTERP, dest, dst_xy, src);
3258 dest = offset(dest, bld, 1);
3259 }
3260 break;
3261 }
3262 default:
3263 nir_emit_intrinsic(bld, instr);
3264 break;
3265 }
3266 }
3267
3268 void
3269 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
3270 nir_intrinsic_instr *instr)
3271 {
3272 assert(stage == MESA_SHADER_COMPUTE);
3273 struct brw_cs_prog_data *cs_prog_data =
3274 (struct brw_cs_prog_data *) prog_data;
3275
3276 fs_reg dest;
3277 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3278 dest = get_nir_dest(instr->dest);
3279
3280 switch (instr->intrinsic) {
3281 case nir_intrinsic_barrier:
3282 emit_barrier();
3283 cs_prog_data->uses_barrier = true;
3284 break;
3285
3286 case nir_intrinsic_load_local_invocation_id:
3287 case nir_intrinsic_load_work_group_id: {
3288 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3289 fs_reg val = nir_system_values[sv];
3290 assert(val.file != BAD_FILE);
3291 dest.type = val.type;
3292 for (unsigned i = 0; i < 3; i++)
3293 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
3294 break;
3295 }
3296
3297 case nir_intrinsic_load_num_work_groups: {
3298 const unsigned surface =
3299 cs_prog_data->binding_table.work_groups_start;
3300
3301 cs_prog_data->uses_num_work_groups = true;
3302
3303 fs_reg surf_index = brw_imm_ud(surface);
3304 brw_mark_surface_used(prog_data, surface);
3305
3306 /* Read the 3 GLuint components of gl_NumWorkGroups */
3307 for (unsigned i = 0; i < 3; i++) {
3308 fs_reg read_result =
3309 emit_untyped_read(bld, surf_index,
3310 brw_imm_ud(i << 2),
3311 1 /* dims */, 1 /* size */,
3312 BRW_PREDICATE_NONE);
3313 read_result.type = dest.type;
3314 bld.MOV(dest, read_result);
3315 dest = offset(dest, bld, 1);
3316 }
3317 break;
3318 }
3319
3320 case nir_intrinsic_shared_atomic_add:
3321 nir_emit_shared_atomic(bld, BRW_AOP_ADD, instr);
3322 break;
3323 case nir_intrinsic_shared_atomic_imin:
3324 nir_emit_shared_atomic(bld, BRW_AOP_IMIN, instr);
3325 break;
3326 case nir_intrinsic_shared_atomic_umin:
3327 nir_emit_shared_atomic(bld, BRW_AOP_UMIN, instr);
3328 break;
3329 case nir_intrinsic_shared_atomic_imax:
3330 nir_emit_shared_atomic(bld, BRW_AOP_IMAX, instr);
3331 break;
3332 case nir_intrinsic_shared_atomic_umax:
3333 nir_emit_shared_atomic(bld, BRW_AOP_UMAX, instr);
3334 break;
3335 case nir_intrinsic_shared_atomic_and:
3336 nir_emit_shared_atomic(bld, BRW_AOP_AND, instr);
3337 break;
3338 case nir_intrinsic_shared_atomic_or:
3339 nir_emit_shared_atomic(bld, BRW_AOP_OR, instr);
3340 break;
3341 case nir_intrinsic_shared_atomic_xor:
3342 nir_emit_shared_atomic(bld, BRW_AOP_XOR, instr);
3343 break;
3344 case nir_intrinsic_shared_atomic_exchange:
3345 nir_emit_shared_atomic(bld, BRW_AOP_MOV, instr);
3346 break;
3347 case nir_intrinsic_shared_atomic_comp_swap:
3348 nir_emit_shared_atomic(bld, BRW_AOP_CMPWR, instr);
3349 break;
3350
3351 case nir_intrinsic_load_shared: {
3352 assert(devinfo->gen >= 7);
3353
3354 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
3355
3356 /* Get the offset to read from */
3357 fs_reg offset_reg;
3358 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3359 if (const_offset) {
3360 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0]);
3361 } else {
3362 offset_reg = vgrf(glsl_type::uint_type);
3363 bld.ADD(offset_reg,
3364 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
3365 brw_imm_ud(instr->const_index[0]));
3366 }
3367
3368 /* Read the vector */
3369 do_untyped_vector_read(bld, dest, surf_index, offset_reg,
3370 instr->num_components);
3371 break;
3372 }
3373
3374 case nir_intrinsic_store_shared: {
3375 assert(devinfo->gen >= 7);
3376
3377 /* Block index */
3378 fs_reg surf_index = brw_imm_ud(GEN7_BTI_SLM);
3379
3380 /* Value */
3381 fs_reg val_reg = get_nir_src(instr->src[0]);
3382
3383 /* Writemask */
3384 unsigned writemask = instr->const_index[1];
3385
3386 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3387 * since the untyped writes below operate in units of 32-bits, which
3388 * means that we need to write twice as many components each time.
3389 * Also, we have to suffle 64-bit data to be in the appropriate layout
3390 * expected by our 32-bit write messages.
3391 */
3392 unsigned type_size = 4;
3393 unsigned bit_size = instr->src[0].is_ssa ?
3394 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3395 if (bit_size == 64) {
3396 type_size = 8;
3397 fs_reg tmp =
3398 fs_reg(VGRF, alloc.allocate(alloc.sizes[val_reg.nr]), val_reg.type);
3399 shuffle_64bit_data_for_32bit_write(
3400 bld,
3401 retype(tmp, BRW_REGISTER_TYPE_F),
3402 retype(val_reg, BRW_REGISTER_TYPE_DF),
3403 instr->num_components);
3404 val_reg = tmp;
3405 }
3406
3407 unsigned type_slots = type_size / 4;
3408
3409 /* Combine groups of consecutive enabled channels in one write
3410 * message. We use ffs to find the first enabled channel and then ffs on
3411 * the bit-inverse, down-shifted writemask to determine the length of
3412 * the block of enabled bits.
3413 */
3414 while (writemask) {
3415 unsigned first_component = ffs(writemask) - 1;
3416 unsigned length = ffs(~(writemask >> first_component)) - 1;
3417
3418 /* We can't write more than 2 64-bit components at once. Limit the
3419 * length of the write to what we can do and let the next iteration
3420 * handle the rest
3421 */
3422 if (type_size > 4)
3423 length = MIN2(2, length);
3424
3425 fs_reg offset_reg;
3426 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3427 if (const_offset) {
3428 offset_reg = brw_imm_ud(instr->const_index[0] + const_offset->u32[0] +
3429 type_size * first_component);
3430 } else {
3431 offset_reg = vgrf(glsl_type::uint_type);
3432 bld.ADD(offset_reg,
3433 retype(get_nir_src(instr->src[1]), BRW_REGISTER_TYPE_UD),
3434 brw_imm_ud(instr->const_index[0] + type_size * first_component));
3435 }
3436
3437 emit_untyped_write(bld, surf_index, offset_reg,
3438 offset(val_reg, bld, first_component * type_slots),
3439 1 /* dims */, length * type_slots,
3440 BRW_PREDICATE_NONE);
3441
3442 /* Clear the bits in the writemask that we just wrote, then try
3443 * again to see if more channels are left.
3444 */
3445 writemask &= (15 << (first_component + length));
3446 }
3447
3448 break;
3449 }
3450
3451 default:
3452 nir_emit_intrinsic(bld, instr);
3453 break;
3454 }
3455 }
3456
3457 void
3458 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
3459 {
3460 fs_reg dest;
3461 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3462 dest = get_nir_dest(instr->dest);
3463
3464 switch (instr->intrinsic) {
3465 case nir_intrinsic_atomic_counter_inc:
3466 case nir_intrinsic_atomic_counter_dec:
3467 case nir_intrinsic_atomic_counter_read: {
3468 if (stage == MESA_SHADER_FRAGMENT &&
3469 instr->intrinsic != nir_intrinsic_atomic_counter_read)
3470 ((struct brw_wm_prog_data *)prog_data)->has_side_effects = true;
3471
3472 /* Get the arguments of the atomic intrinsic. */
3473 const fs_reg offset = get_nir_src(instr->src[0]);
3474 const unsigned surface = (stage_prog_data->binding_table.abo_start +
3475 instr->const_index[0]);
3476 fs_reg tmp;
3477
3478 /* Emit a surface read or atomic op. */
3479 switch (instr->intrinsic) {
3480 case nir_intrinsic_atomic_counter_read:
3481 tmp = emit_untyped_read(bld, brw_imm_ud(surface), offset, 1, 1);
3482 break;
3483
3484 case nir_intrinsic_atomic_counter_inc:
3485 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
3486 fs_reg(), 1, 1, BRW_AOP_INC);
3487 break;
3488
3489 case nir_intrinsic_atomic_counter_dec:
3490 tmp = emit_untyped_atomic(bld, brw_imm_ud(surface), offset, fs_reg(),
3491 fs_reg(), 1, 1, BRW_AOP_PREDEC);
3492 break;
3493
3494 default:
3495 unreachable("Unreachable");
3496 }
3497
3498 /* Assign the result. */
3499 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), tmp);
3500
3501 /* Mark the surface as used. */
3502 brw_mark_surface_used(stage_prog_data, surface);
3503 break;
3504 }
3505
3506 case nir_intrinsic_image_load:
3507 case nir_intrinsic_image_store:
3508 case nir_intrinsic_image_atomic_add:
3509 case nir_intrinsic_image_atomic_min:
3510 case nir_intrinsic_image_atomic_max:
3511 case nir_intrinsic_image_atomic_and:
3512 case nir_intrinsic_image_atomic_or:
3513 case nir_intrinsic_image_atomic_xor:
3514 case nir_intrinsic_image_atomic_exchange:
3515 case nir_intrinsic_image_atomic_comp_swap: {
3516 using namespace image_access;
3517
3518 if (stage == MESA_SHADER_FRAGMENT &&
3519 instr->intrinsic != nir_intrinsic_image_load)
3520 ((struct brw_wm_prog_data *)prog_data)->has_side_effects = true;
3521
3522 /* Get the referenced image variable and type. */
3523 const nir_variable *var = instr->variables[0]->var;
3524 const glsl_type *type = var->type->without_array();
3525 const brw_reg_type base_type = get_image_base_type(type);
3526
3527 /* Get some metadata from the image intrinsic. */
3528 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
3529 const unsigned arr_dims = type->sampler_array ? 1 : 0;
3530 const unsigned surf_dims = type->coordinate_components() - arr_dims;
3531 const unsigned format = var->data.image.format;
3532
3533 /* Get the arguments of the image intrinsic. */
3534 const fs_reg image = get_nir_image_deref(instr->variables[0]);
3535 const fs_reg addr = retype(get_nir_src(instr->src[0]),
3536 BRW_REGISTER_TYPE_UD);
3537 const fs_reg src0 = (info->num_srcs >= 3 ?
3538 retype(get_nir_src(instr->src[2]), base_type) :
3539 fs_reg());
3540 const fs_reg src1 = (info->num_srcs >= 4 ?
3541 retype(get_nir_src(instr->src[3]), base_type) :
3542 fs_reg());
3543 fs_reg tmp;
3544
3545 /* Emit an image load, store or atomic op. */
3546 if (instr->intrinsic == nir_intrinsic_image_load)
3547 tmp = emit_image_load(bld, image, addr, surf_dims, arr_dims, format);
3548
3549 else if (instr->intrinsic == nir_intrinsic_image_store)
3550 emit_image_store(bld, image, addr, src0, surf_dims, arr_dims,
3551 var->data.image.write_only ? GL_NONE : format);
3552
3553 else
3554 tmp = emit_image_atomic(bld, image, addr, src0, src1,
3555 surf_dims, arr_dims, info->dest_components,
3556 get_image_atomic_op(instr->intrinsic, type));
3557
3558 /* Assign the result. */
3559 for (unsigned c = 0; c < info->dest_components; ++c)
3560 bld.MOV(offset(retype(dest, base_type), bld, c),
3561 offset(tmp, bld, c));
3562 break;
3563 }
3564
3565 case nir_intrinsic_memory_barrier_atomic_counter:
3566 case nir_intrinsic_memory_barrier_buffer:
3567 case nir_intrinsic_memory_barrier_image:
3568 case nir_intrinsic_memory_barrier: {
3569 const fs_builder ubld = bld.group(8, 0);
3570 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
3571 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
3572 ->regs_written = 2;
3573 break;
3574 }
3575
3576 case nir_intrinsic_group_memory_barrier:
3577 case nir_intrinsic_memory_barrier_shared:
3578 /* We treat these workgroup-level barriers as no-ops. This should be
3579 * safe at present and as long as:
3580 *
3581 * - Memory access instructions are not subsequently reordered by the
3582 * compiler back-end.
3583 *
3584 * - All threads from a given compute shader workgroup fit within a
3585 * single subslice and therefore talk to the same HDC shared unit
3586 * what supposedly guarantees ordering and coherency between threads
3587 * from the same workgroup. This may change in the future when we
3588 * start splitting workgroups across multiple subslices.
3589 *
3590 * - The context is not in fault-and-stream mode, which could cause
3591 * memory transactions (including to SLM) prior to the barrier to be
3592 * replayed after the barrier if a pagefault occurs. This shouldn't
3593 * be a problem up to and including SKL because fault-and-stream is
3594 * not usable due to hardware issues, but that's likely to change in
3595 * the future.
3596 */
3597 break;
3598
3599 case nir_intrinsic_shader_clock: {
3600 /* We cannot do anything if there is an event, so ignore it for now */
3601 fs_reg shader_clock = get_timestamp(bld);
3602 const fs_reg srcs[] = { shader_clock.set_smear(0), shader_clock.set_smear(1) };
3603
3604 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
3605 break;
3606 }
3607
3608 case nir_intrinsic_image_size: {
3609 /* Get the referenced image variable and type. */
3610 const nir_variable *var = instr->variables[0]->var;
3611 const glsl_type *type = var->type->without_array();
3612
3613 /* Get the size of the image. */
3614 const fs_reg image = get_nir_image_deref(instr->variables[0]);
3615 const fs_reg size = offset(image, bld, BRW_IMAGE_PARAM_SIZE_OFFSET);
3616
3617 /* For 1DArray image types, the array index is stored in the Z component.
3618 * Fix this by swizzling the Z component to the Y component.
3619 */
3620 const bool is_1d_array_image =
3621 type->sampler_dimensionality == GLSL_SAMPLER_DIM_1D &&
3622 type->sampler_array;
3623
3624 /* For CubeArray images, we should count the number of cubes instead
3625 * of the number of faces. Fix it by dividing the (Z component) by 6.
3626 */
3627 const bool is_cube_array_image =
3628 type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
3629 type->sampler_array;
3630
3631 /* Copy all the components. */
3632 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
3633 for (unsigned c = 0; c < info->dest_components; ++c) {
3634 if ((int)c >= type->coordinate_components()) {
3635 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3636 brw_imm_d(1));
3637 } else if (c == 1 && is_1d_array_image) {
3638 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3639 offset(size, bld, 2));
3640 } else if (c == 2 && is_cube_array_image) {
3641 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
3642 offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3643 offset(size, bld, c), brw_imm_d(6));
3644 } else {
3645 bld.MOV(offset(retype(dest, BRW_REGISTER_TYPE_D), bld, c),
3646 offset(size, bld, c));
3647 }
3648 }
3649
3650 break;
3651 }
3652
3653 case nir_intrinsic_image_samples:
3654 /* The driver does not support multi-sampled images. */
3655 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
3656 break;
3657
3658 case nir_intrinsic_load_uniform: {
3659 /* Offsets are in bytes but they should always be multiples of 4 */
3660 assert(instr->const_index[0] % 4 == 0);
3661
3662 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
3663
3664 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3665 if (const_offset) {
3666 /* Offsets are in bytes but they should always be multiples of 4 */
3667 assert(const_offset->u32[0] % 4 == 0);
3668 src.reg_offset = const_offset->u32[0] / 4;
3669
3670 for (unsigned j = 0; j < instr->num_components; j++) {
3671 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3672 }
3673 } else {
3674 fs_reg indirect = retype(get_nir_src(instr->src[0]),
3675 BRW_REGISTER_TYPE_UD);
3676
3677 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
3678 * go past the end of the uniform. In order to keep the n'th
3679 * component from running past, we subtract off the size of all but
3680 * one component of the vector.
3681 */
3682 assert(instr->const_index[1] >=
3683 instr->num_components * (int) type_sz(dest.type));
3684 unsigned read_size = instr->const_index[1] -
3685 (instr->num_components - 1) * type_sz(dest.type);
3686
3687 fs_reg indirect_chv_high_32bit;
3688 bool is_chv_bxt_64bit =
3689 (devinfo->is_cherryview || devinfo->is_broxton) &&
3690 type_sz(dest.type) == 8;
3691 if (is_chv_bxt_64bit) {
3692 indirect_chv_high_32bit = vgrf(glsl_type::uint_type);
3693 /* Calculate indirect address to read high 32 bits */
3694 bld.ADD(indirect_chv_high_32bit, indirect, brw_imm_ud(4));
3695 }
3696
3697 for (unsigned j = 0; j < instr->num_components; j++) {
3698 if (!is_chv_bxt_64bit) {
3699 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
3700 offset(dest, bld, j), offset(src, bld, j),
3701 indirect, brw_imm_ud(read_size));
3702 } else {
3703 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
3704 subscript(offset(dest, bld, j), BRW_REGISTER_TYPE_UD, 0),
3705 offset(src, bld, j),
3706 indirect, brw_imm_ud(read_size));
3707
3708 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
3709 subscript(offset(dest, bld, j), BRW_REGISTER_TYPE_UD, 1),
3710 offset(src, bld, j),
3711 indirect_chv_high_32bit, brw_imm_ud(read_size));
3712 }
3713 }
3714 }
3715 break;
3716 }
3717
3718 case nir_intrinsic_load_ubo: {
3719 nir_const_value *const_index = nir_src_as_const_value(instr->src[0]);
3720 fs_reg surf_index;
3721
3722 if (const_index) {
3723 const unsigned index = stage_prog_data->binding_table.ubo_start +
3724 const_index->u32[0];
3725 surf_index = brw_imm_ud(index);
3726 brw_mark_surface_used(prog_data, index);
3727 } else {
3728 /* The block index is not a constant. Evaluate the index expression
3729 * per-channel and add the base UBO index; we have to select a value
3730 * from any live channel.
3731 */
3732 surf_index = vgrf(glsl_type::uint_type);
3733 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3734 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
3735 surf_index = bld.emit_uniformize(surf_index);
3736
3737 /* Assume this may touch any UBO. It would be nice to provide
3738 * a tighter bound, but the array information is already lowered away.
3739 */
3740 brw_mark_surface_used(prog_data,
3741 stage_prog_data->binding_table.ubo_start +
3742 nir->info.num_ubos - 1);
3743 }
3744
3745 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3746 if (const_offset == NULL) {
3747 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
3748 BRW_REGISTER_TYPE_UD);
3749
3750 for (int i = 0; i < instr->num_components; i++)
3751 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
3752 base_offset, i * type_sz(dest.type));
3753 } else {
3754 /* Even if we are loading doubles, a pull constant load will load
3755 * a 32-bit vec4, so should only reserve vgrf space for that. If we
3756 * need to load a full dvec4 we will have to emit 2 loads. This is
3757 * similar to demote_pull_constants(), except that in that case we
3758 * see individual accesses to each component of the vector and then
3759 * we let CSE deal with duplicate loads. Here we see a vector access
3760 * and we have to split it if necessary.
3761 */
3762 const unsigned type_size = type_sz(dest.type);
3763 const fs_reg packed_consts = bld.vgrf(BRW_REGISTER_TYPE_F);
3764 for (unsigned c = 0; c < instr->num_components;) {
3765 const unsigned base = const_offset->u32[0] + c * type_size;
3766
3767 /* Number of usable components in the next 16B-aligned load */
3768 const unsigned count = MIN2(instr->num_components - c,
3769 (16 - base % 16) / type_size);
3770
3771 bld.exec_all()
3772 .emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
3773 packed_consts, surf_index, brw_imm_ud(base & ~15));
3774
3775 const fs_reg consts =
3776 retype(byte_offset(packed_consts, base & 15), dest.type);
3777
3778 for (unsigned d = 0; d < count; d++)
3779 bld.MOV(offset(dest, bld, c + d), component(consts, d));
3780
3781 c += count;
3782 }
3783 }
3784 break;
3785 }
3786
3787 case nir_intrinsic_load_ssbo: {
3788 assert(devinfo->gen >= 7);
3789
3790 nir_const_value *const_uniform_block =
3791 nir_src_as_const_value(instr->src[0]);
3792
3793 fs_reg surf_index;
3794 if (const_uniform_block) {
3795 unsigned index = stage_prog_data->binding_table.ssbo_start +
3796 const_uniform_block->u32[0];
3797 surf_index = brw_imm_ud(index);
3798 brw_mark_surface_used(prog_data, index);
3799 } else {
3800 surf_index = vgrf(glsl_type::uint_type);
3801 bld.ADD(surf_index, get_nir_src(instr->src[0]),
3802 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3803
3804 /* Assume this may touch any UBO. It would be nice to provide
3805 * a tighter bound, but the array information is already lowered away.
3806 */
3807 brw_mark_surface_used(prog_data,
3808 stage_prog_data->binding_table.ssbo_start +
3809 nir->info.num_ssbos - 1);
3810 }
3811
3812 fs_reg offset_reg;
3813 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3814 if (const_offset) {
3815 offset_reg = brw_imm_ud(const_offset->u32[0]);
3816 } else {
3817 offset_reg = get_nir_src(instr->src[1]);
3818 }
3819
3820 /* Read the vector */
3821 do_untyped_vector_read(bld, dest, surf_index, offset_reg,
3822 instr->num_components);
3823
3824 break;
3825 }
3826
3827 case nir_intrinsic_load_input: {
3828 fs_reg src;
3829 unsigned num_components = instr->num_components;
3830 enum brw_reg_type type = dest.type;
3831
3832 if (stage == MESA_SHADER_VERTEX) {
3833 src = fs_reg(ATTR, instr->const_index[0], dest.type);
3834 } else {
3835 assert(type_sz(type) >= 4);
3836 if (type == BRW_REGISTER_TYPE_DF) {
3837 /* const_index is in 32-bit type size units that could not be aligned
3838 * with DF. We need to read the double vector as if it was a float
3839 * vector of twice the number of components to fetch the right data.
3840 */
3841 dest = retype(dest, BRW_REGISTER_TYPE_F);
3842 num_components *= 2;
3843 }
3844 src = offset(retype(nir_inputs, dest.type), bld,
3845 instr->const_index[0]);
3846 }
3847
3848 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3849 assert(const_offset && "Indirect input loads not allowed");
3850 src = offset(src, bld, const_offset->u32[0]);
3851
3852 for (unsigned j = 0; j < num_components; j++) {
3853 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
3854 }
3855
3856 if (type == BRW_REGISTER_TYPE_DF) {
3857 /* Once the double vector is read, set again its original register
3858 * type to continue with normal execution.
3859 */
3860 src = retype(src, type);
3861 dest = retype(dest, type);
3862 }
3863
3864 if (type_sz(src.type) == 8) {
3865 shuffle_32bit_load_result_to_64bit_data(bld,
3866 dest,
3867 retype(dest, BRW_REGISTER_TYPE_F),
3868 instr->num_components);
3869 }
3870
3871 break;
3872 }
3873
3874 case nir_intrinsic_store_ssbo: {
3875 assert(devinfo->gen >= 7);
3876
3877 if (stage == MESA_SHADER_FRAGMENT)
3878 ((struct brw_wm_prog_data *)prog_data)->has_side_effects = true;
3879
3880 /* Block index */
3881 fs_reg surf_index;
3882 nir_const_value *const_uniform_block =
3883 nir_src_as_const_value(instr->src[1]);
3884 if (const_uniform_block) {
3885 unsigned index = stage_prog_data->binding_table.ssbo_start +
3886 const_uniform_block->u32[0];
3887 surf_index = brw_imm_ud(index);
3888 brw_mark_surface_used(prog_data, index);
3889 } else {
3890 surf_index = vgrf(glsl_type::uint_type);
3891 bld.ADD(surf_index, get_nir_src(instr->src[1]),
3892 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3893
3894 brw_mark_surface_used(prog_data,
3895 stage_prog_data->binding_table.ssbo_start +
3896 nir->info.num_ssbos - 1);
3897 }
3898
3899 /* Value */
3900 fs_reg val_reg = get_nir_src(instr->src[0]);
3901
3902 /* Writemask */
3903 unsigned writemask = instr->const_index[0];
3904
3905 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3906 * since the untyped writes below operate in units of 32-bits, which
3907 * means that we need to write twice as many components each time.
3908 * Also, we have to suffle 64-bit data to be in the appropriate layout
3909 * expected by our 32-bit write messages.
3910 */
3911 unsigned type_size = 4;
3912 unsigned bit_size = instr->src[0].is_ssa ?
3913 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3914 if (bit_size == 64) {
3915 type_size = 8;
3916 fs_reg tmp =
3917 fs_reg(VGRF, alloc.allocate(alloc.sizes[val_reg.nr]), val_reg.type);
3918 shuffle_64bit_data_for_32bit_write(bld,
3919 retype(tmp, BRW_REGISTER_TYPE_F),
3920 retype(val_reg, BRW_REGISTER_TYPE_DF),
3921 instr->num_components);
3922 val_reg = tmp;
3923 }
3924
3925 unsigned type_slots = type_size / 4;
3926
3927 /* Combine groups of consecutive enabled channels in one write
3928 * message. We use ffs to find the first enabled channel and then ffs on
3929 * the bit-inverse, down-shifted writemask to determine the length of
3930 * the block of enabled bits.
3931 */
3932 while (writemask) {
3933 unsigned first_component = ffs(writemask) - 1;
3934 unsigned length = ffs(~(writemask >> first_component)) - 1;
3935
3936 /* We can't write more than 2 64-bit components at once. Limit the
3937 * length of the write to what we can do and let the next iteration
3938 * handle the rest
3939 */
3940 if (type_size > 4)
3941 length = MIN2(2, length);
3942
3943 fs_reg offset_reg;
3944 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
3945 if (const_offset) {
3946 offset_reg = brw_imm_ud(const_offset->u32[0] +
3947 type_size * first_component);
3948 } else {
3949 offset_reg = vgrf(glsl_type::uint_type);
3950 bld.ADD(offset_reg,
3951 retype(get_nir_src(instr->src[2]), BRW_REGISTER_TYPE_UD),
3952 brw_imm_ud(type_size * first_component));
3953 }
3954
3955
3956 emit_untyped_write(bld, surf_index, offset_reg,
3957 offset(val_reg, bld, first_component * type_slots),
3958 1 /* dims */, length * type_slots,
3959 BRW_PREDICATE_NONE);
3960
3961 /* Clear the bits in the writemask that we just wrote, then try
3962 * again to see if more channels are left.
3963 */
3964 writemask &= (15 << (first_component + length));
3965 }
3966 break;
3967 }
3968
3969 case nir_intrinsic_store_output: {
3970 fs_reg src = get_nir_src(instr->src[0]);
3971 fs_reg new_dest = offset(retype(nir_outputs, src.type), bld,
3972 instr->const_index[0]);
3973
3974 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
3975 assert(const_offset && "Indirect output stores not allowed");
3976 new_dest = offset(new_dest, bld, const_offset->u32[0]);
3977
3978 unsigned num_components = instr->num_components;
3979 unsigned bit_size = instr->src[0].is_ssa ?
3980 instr->src[0].ssa->bit_size : instr->src[0].reg.reg->bit_size;
3981 if (bit_size == 64) {
3982 fs_reg tmp =
3983 fs_reg(VGRF, alloc.allocate(2 * num_components),
3984 BRW_REGISTER_TYPE_F);
3985 shuffle_64bit_data_for_32bit_write(
3986 bld, tmp, retype(src, BRW_REGISTER_TYPE_DF), num_components);
3987 src = retype(tmp, src.type);
3988 num_components *= 2;
3989 }
3990
3991 for (unsigned j = 0; j < num_components; j++) {
3992 bld.MOV(offset(new_dest, bld, j), offset(src, bld, j));
3993 }
3994 break;
3995 }
3996
3997 case nir_intrinsic_ssbo_atomic_add:
3998 nir_emit_ssbo_atomic(bld, BRW_AOP_ADD, instr);
3999 break;
4000 case nir_intrinsic_ssbo_atomic_imin:
4001 nir_emit_ssbo_atomic(bld, BRW_AOP_IMIN, instr);
4002 break;
4003 case nir_intrinsic_ssbo_atomic_umin:
4004 nir_emit_ssbo_atomic(bld, BRW_AOP_UMIN, instr);
4005 break;
4006 case nir_intrinsic_ssbo_atomic_imax:
4007 nir_emit_ssbo_atomic(bld, BRW_AOP_IMAX, instr);
4008 break;
4009 case nir_intrinsic_ssbo_atomic_umax:
4010 nir_emit_ssbo_atomic(bld, BRW_AOP_UMAX, instr);
4011 break;
4012 case nir_intrinsic_ssbo_atomic_and:
4013 nir_emit_ssbo_atomic(bld, BRW_AOP_AND, instr);
4014 break;
4015 case nir_intrinsic_ssbo_atomic_or:
4016 nir_emit_ssbo_atomic(bld, BRW_AOP_OR, instr);
4017 break;
4018 case nir_intrinsic_ssbo_atomic_xor:
4019 nir_emit_ssbo_atomic(bld, BRW_AOP_XOR, instr);
4020 break;
4021 case nir_intrinsic_ssbo_atomic_exchange:
4022 nir_emit_ssbo_atomic(bld, BRW_AOP_MOV, instr);
4023 break;
4024 case nir_intrinsic_ssbo_atomic_comp_swap:
4025 nir_emit_ssbo_atomic(bld, BRW_AOP_CMPWR, instr);
4026 break;
4027
4028 case nir_intrinsic_get_buffer_size: {
4029 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
4030 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u32[0] : 0;
4031
4032 /* A resinfo's sampler message is used to get the buffer size. The
4033 * SIMD8's writeback message consists of four registers and SIMD16's
4034 * writeback message consists of 8 destination registers (two per each
4035 * component). Because we are only interested on the first channel of
4036 * the first returned component, where resinfo returns the buffer size
4037 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4038 * the dispatch width.
4039 */
4040 const fs_builder ubld = bld.exec_all().group(8, 0);
4041 fs_reg src_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4042 fs_reg ret_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
4043
4044 /* Set LOD = 0 */
4045 ubld.MOV(src_payload, brw_imm_d(0));
4046
4047 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
4048 fs_inst *inst = ubld.emit(FS_OPCODE_GET_BUFFER_SIZE, ret_payload,
4049 src_payload, brw_imm_ud(index));
4050 inst->header_size = 0;
4051 inst->mlen = 1;
4052 inst->regs_written = 4;
4053
4054 bld.MOV(retype(dest, ret_payload.type), component(ret_payload, 0));
4055 brw_mark_surface_used(prog_data, index);
4056 break;
4057 }
4058
4059 case nir_intrinsic_load_channel_num: {
4060 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UW);
4061 dest = retype(dest, BRW_REGISTER_TYPE_UD);
4062 const fs_builder allbld8 = bld.group(8, 0).exec_all();
4063 allbld8.MOV(tmp, brw_imm_v(0x76543210));
4064 if (dispatch_width > 8)
4065 allbld8.ADD(byte_offset(tmp, 16), tmp, brw_imm_uw(8u));
4066 if (dispatch_width > 16) {
4067 const fs_builder allbld16 = bld.group(16, 0).exec_all();
4068 allbld16.ADD(byte_offset(tmp, 32), tmp, brw_imm_uw(16u));
4069 }
4070 bld.MOV(dest, tmp);
4071 break;
4072 }
4073
4074 default:
4075 unreachable("unknown intrinsic");
4076 }
4077 }
4078
4079 void
4080 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
4081 int op, nir_intrinsic_instr *instr)
4082 {
4083 if (stage == MESA_SHADER_FRAGMENT)
4084 ((struct brw_wm_prog_data *)prog_data)->has_side_effects = true;
4085
4086 fs_reg dest;
4087 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
4088 dest = get_nir_dest(instr->dest);
4089
4090 fs_reg surface;
4091 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
4092 if (const_surface) {
4093 unsigned surf_index = stage_prog_data->binding_table.ssbo_start +
4094 const_surface->u32[0];
4095 surface = brw_imm_ud(surf_index);
4096 brw_mark_surface_used(prog_data, surf_index);
4097 } else {
4098 surface = vgrf(glsl_type::uint_type);
4099 bld.ADD(surface, get_nir_src(instr->src[0]),
4100 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
4101
4102 /* Assume this may touch any SSBO. This is the same we do for other
4103 * UBO/SSBO accesses with non-constant surface.
4104 */
4105 brw_mark_surface_used(prog_data,
4106 stage_prog_data->binding_table.ssbo_start +
4107 nir->info.num_ssbos - 1);
4108 }
4109
4110 fs_reg offset = get_nir_src(instr->src[1]);
4111 fs_reg data1 = get_nir_src(instr->src[2]);
4112 fs_reg data2;
4113 if (op == BRW_AOP_CMPWR)
4114 data2 = get_nir_src(instr->src[3]);
4115
4116 /* Emit the actual atomic operation operation */
4117
4118 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
4119 data1, data2,
4120 1 /* dims */, 1 /* rsize */,
4121 op,
4122 BRW_PREDICATE_NONE);
4123 dest.type = atomic_result.type;
4124 bld.MOV(dest, atomic_result);
4125 }
4126
4127 void
4128 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
4129 int op, nir_intrinsic_instr *instr)
4130 {
4131 fs_reg dest;
4132 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
4133 dest = get_nir_dest(instr->dest);
4134
4135 fs_reg surface = brw_imm_ud(GEN7_BTI_SLM);
4136 fs_reg offset = get_nir_src(instr->src[0]);
4137 fs_reg data1 = get_nir_src(instr->src[1]);
4138 fs_reg data2;
4139 if (op == BRW_AOP_CMPWR)
4140 data2 = get_nir_src(instr->src[2]);
4141
4142 /* Emit the actual atomic operation operation */
4143
4144 fs_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
4145 data1, data2,
4146 1 /* dims */, 1 /* rsize */,
4147 op,
4148 BRW_PREDICATE_NONE);
4149 dest.type = atomic_result.type;
4150 bld.MOV(dest, atomic_result);
4151 }
4152
4153 void
4154 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
4155 {
4156 unsigned texture = instr->texture_index;
4157 unsigned sampler = instr->sampler_index;
4158
4159 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
4160
4161 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
4162 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
4163
4164 int lod_components = 0;
4165
4166 /* The hardware requires a LOD for buffer textures */
4167 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
4168 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
4169
4170 for (unsigned i = 0; i < instr->num_srcs; i++) {
4171 fs_reg src = get_nir_src(instr->src[i].src);
4172 switch (instr->src[i].src_type) {
4173 case nir_tex_src_bias:
4174 srcs[TEX_LOGICAL_SRC_LOD] =
4175 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
4176 break;
4177 case nir_tex_src_comparitor:
4178 srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
4179 break;
4180 case nir_tex_src_coord:
4181 switch (instr->op) {
4182 case nir_texop_txf:
4183 case nir_texop_txf_ms:
4184 case nir_texop_txf_ms_mcs:
4185 case nir_texop_samples_identical:
4186 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
4187 break;
4188 default:
4189 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
4190 break;
4191 }
4192 break;
4193 case nir_tex_src_ddx:
4194 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
4195 lod_components = nir_tex_instr_src_size(instr, i);
4196 break;
4197 case nir_tex_src_ddy:
4198 srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
4199 break;
4200 case nir_tex_src_lod:
4201 switch (instr->op) {
4202 case nir_texop_txs:
4203 srcs[TEX_LOGICAL_SRC_LOD] =
4204 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_UD);
4205 break;
4206 case nir_texop_txf:
4207 srcs[TEX_LOGICAL_SRC_LOD] =
4208 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_D);
4209 break;
4210 default:
4211 srcs[TEX_LOGICAL_SRC_LOD] =
4212 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
4213 break;
4214 }
4215 break;
4216 case nir_tex_src_ms_index:
4217 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
4218 break;
4219
4220 case nir_tex_src_offset: {
4221 nir_const_value *const_offset =
4222 nir_src_as_const_value(instr->src[i].src);
4223 if (const_offset) {
4224 unsigned header_bits = brw_texture_offset(const_offset->i32, 3);
4225 if (header_bits != 0)
4226 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] = brw_imm_ud(header_bits);
4227 } else {
4228 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE] =
4229 retype(src, BRW_REGISTER_TYPE_D);
4230 }
4231 break;
4232 }
4233
4234 case nir_tex_src_projector:
4235 unreachable("should be lowered");
4236
4237 case nir_tex_src_texture_offset: {
4238 /* Figure out the highest possible texture index and mark it as used */
4239 uint32_t max_used = texture + instr->texture_array_size - 1;
4240 if (instr->op == nir_texop_tg4 && devinfo->gen < 8) {
4241 max_used += stage_prog_data->binding_table.gather_texture_start;
4242 } else {
4243 max_used += stage_prog_data->binding_table.texture_start;
4244 }
4245 brw_mark_surface_used(prog_data, max_used);
4246
4247 /* Emit code to evaluate the actual indexing expression */
4248 fs_reg tmp = vgrf(glsl_type::uint_type);
4249 bld.ADD(tmp, src, brw_imm_ud(texture));
4250 srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
4251 break;
4252 }
4253
4254 case nir_tex_src_sampler_offset: {
4255 /* Emit code to evaluate the actual indexing expression */
4256 fs_reg tmp = vgrf(glsl_type::uint_type);
4257 bld.ADD(tmp, src, brw_imm_ud(sampler));
4258 srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
4259 break;
4260 }
4261
4262 case nir_tex_src_ms_mcs:
4263 assert(instr->op == nir_texop_txf_ms);
4264 srcs[TEX_LOGICAL_SRC_MCS] = retype(src, BRW_REGISTER_TYPE_D);
4265 break;
4266
4267 case nir_tex_src_plane: {
4268 nir_const_value *const_plane =
4269 nir_src_as_const_value(instr->src[i].src);
4270 const uint32_t plane = const_plane->u32[0];
4271 const uint32_t texture_index =
4272 instr->texture_index +
4273 stage_prog_data->binding_table.plane_start[plane] -
4274 stage_prog_data->binding_table.texture_start;
4275
4276 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture_index);
4277 break;
4278 }
4279
4280 default:
4281 unreachable("unknown texture source");
4282 }
4283 }
4284
4285 if (srcs[TEX_LOGICAL_SRC_MCS].file == BAD_FILE &&
4286 (instr->op == nir_texop_txf_ms ||
4287 instr->op == nir_texop_samples_identical)) {
4288 if (devinfo->gen >= 7 &&
4289 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
4290 srcs[TEX_LOGICAL_SRC_MCS] =
4291 emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
4292 instr->coord_components,
4293 srcs[TEX_LOGICAL_SRC_SURFACE]);
4294 } else {
4295 srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
4296 }
4297 }
4298
4299 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
4300 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
4301
4302 if (instr->op == nir_texop_query_levels) {
4303 /* textureQueryLevels() is implemented in terms of TXS so we need to
4304 * pass a valid LOD argument.
4305 */
4306 assert(srcs[TEX_LOGICAL_SRC_LOD].file == BAD_FILE);
4307 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_ud(0u);
4308 }
4309
4310 enum opcode opcode;
4311 switch (instr->op) {
4312 case nir_texop_tex:
4313 opcode = SHADER_OPCODE_TEX_LOGICAL;
4314 break;
4315 case nir_texop_txb:
4316 opcode = FS_OPCODE_TXB_LOGICAL;
4317 break;
4318 case nir_texop_txl:
4319 opcode = SHADER_OPCODE_TXL_LOGICAL;
4320 break;
4321 case nir_texop_txd:
4322 opcode = SHADER_OPCODE_TXD_LOGICAL;
4323 break;
4324 case nir_texop_txf:
4325 opcode = SHADER_OPCODE_TXF_LOGICAL;
4326 break;
4327 case nir_texop_txf_ms:
4328 if ((key_tex->msaa_16 & (1 << sampler)))
4329 opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
4330 else
4331 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
4332 break;
4333 case nir_texop_txf_ms_mcs:
4334 opcode = SHADER_OPCODE_TXF_MCS_LOGICAL;
4335 break;
4336 case nir_texop_query_levels:
4337 case nir_texop_txs:
4338 opcode = SHADER_OPCODE_TXS_LOGICAL;
4339 break;
4340 case nir_texop_lod:
4341 opcode = SHADER_OPCODE_LOD_LOGICAL;
4342 break;
4343 case nir_texop_tg4:
4344 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != BAD_FILE &&
4345 srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file != IMM)
4346 opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
4347 else
4348 opcode = SHADER_OPCODE_TG4_LOGICAL;
4349 break;
4350 case nir_texop_texture_samples:
4351 opcode = SHADER_OPCODE_SAMPLEINFO_LOGICAL;
4352 break;
4353 case nir_texop_samples_identical: {
4354 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
4355
4356 /* If mcs is an immediate value, it means there is no MCS. In that case
4357 * just return false.
4358 */
4359 if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
4360 bld.MOV(dst, brw_imm_ud(0u));
4361 } else if ((key_tex->msaa_16 & (1 << sampler))) {
4362 fs_reg tmp = vgrf(glsl_type::uint_type);
4363 bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
4364 offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
4365 bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
4366 } else {
4367 bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
4368 BRW_CONDITIONAL_EQ);
4369 }
4370 return;
4371 }
4372 default:
4373 unreachable("unknown texture opcode");
4374 }
4375
4376 fs_reg dst = bld.vgrf(brw_type_for_nir_type(instr->dest_type), 4);
4377 fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
4378
4379 const unsigned dest_size = nir_tex_instr_dest_size(instr);
4380 if (devinfo->gen >= 9 &&
4381 instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
4382 unsigned write_mask = instr->dest.is_ssa ?
4383 nir_ssa_def_components_read(&instr->dest.ssa):
4384 (1 << dest_size) - 1;
4385 assert(write_mask != 0); /* dead code should have been eliminated */
4386 inst->regs_written = _mesa_fls(write_mask) * dispatch_width / 8;
4387 } else {
4388 inst->regs_written = 4 * dispatch_width / 8;
4389 }
4390
4391 if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
4392 inst->shadow_compare = true;
4393
4394 if (srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].file == IMM)
4395 inst->offset = srcs[TEX_LOGICAL_SRC_OFFSET_VALUE].ud;
4396
4397 if (instr->op == nir_texop_tg4) {
4398 if (instr->component == 1 &&
4399 key_tex->gather_channel_quirk_mask & (1 << texture)) {
4400 /* gather4 sampler is broken for green channel on RG32F --
4401 * we must ask for blue instead.
4402 */
4403 inst->offset |= 2 << 16;
4404 } else {
4405 inst->offset |= instr->component << 16;
4406 }
4407
4408 if (devinfo->gen == 6)
4409 emit_gen6_gather_wa(key_tex->gen6_gather_wa[texture], dst);
4410 }
4411
4412 fs_reg nir_dest[4];
4413 for (unsigned i = 0; i < dest_size; i++)
4414 nir_dest[i] = offset(dst, bld, i);
4415
4416 bool is_cube_array = instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
4417 instr->is_array;
4418
4419 if (instr->op == nir_texop_query_levels) {
4420 /* # levels is in .w */
4421 nir_dest[0] = offset(dst, bld, 3);
4422 } else if (instr->op == nir_texop_txs && dest_size >= 3 &&
4423 (devinfo->gen < 7 || is_cube_array)) {
4424 fs_reg depth = offset(dst, bld, 2);
4425 fs_reg fixed_depth = vgrf(glsl_type::int_type);
4426
4427 if (is_cube_array) {
4428 /* fixup #layers for cube map arrays */
4429 bld.emit(SHADER_OPCODE_INT_QUOTIENT, fixed_depth, depth, brw_imm_d(6));
4430 } else if (devinfo->gen < 7) {
4431 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
4432 bld.emit_minmax(fixed_depth, depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
4433 }
4434
4435 nir_dest[2] = fixed_depth;
4436 }
4437
4438 bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
4439 }
4440
4441 void
4442 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
4443 {
4444 switch (instr->type) {
4445 case nir_jump_break:
4446 bld.emit(BRW_OPCODE_BREAK);
4447 break;
4448 case nir_jump_continue:
4449 bld.emit(BRW_OPCODE_CONTINUE);
4450 break;
4451 case nir_jump_return:
4452 default:
4453 unreachable("unknown jump");
4454 }
4455 }
4456
4457 /**
4458 * This helper takes the result of a load operation that reads 32-bit elements
4459 * in this format:
4460 *
4461 * x x x x x x x x
4462 * y y y y y y y y
4463 * z z z z z z z z
4464 * w w w w w w w w
4465 *
4466 * and shuffles the data to get this:
4467 *
4468 * x y x y x y x y
4469 * x y x y x y x y
4470 * z w z w z w z w
4471 * z w z w z w z w
4472 *
4473 * Which is exactly what we want if the load is reading 64-bit components
4474 * like doubles, where x represents the low 32-bit of the x double component
4475 * and y represents the high 32-bit of the x double component (likewise with
4476 * z and w for double component y). The parameter @components represents
4477 * the number of 64-bit components present in @src. This would typically be
4478 * 2 at most, since we can only fit 2 double elements in the result of a
4479 * vec4 load.
4480 *
4481 * Notice that @dst and @src can be the same register.
4482 */
4483 void
4484 shuffle_32bit_load_result_to_64bit_data(const fs_builder &bld,
4485 const fs_reg &dst,
4486 const fs_reg &src,
4487 uint32_t components)
4488 {
4489 assert(type_sz(src.type) == 4);
4490 assert(type_sz(dst.type) == 8);
4491
4492 /* A temporary that we will use to shuffle the 32-bit data of each
4493 * component in the vector into valid 64-bit data. We can't write directly
4494 * to dst because dst can be (and would usually be) the same as src
4495 * and in that case the first MOV in the loop below would overwrite the
4496 * data read in the second MOV.
4497 */
4498 fs_reg tmp = bld.vgrf(dst.type);
4499
4500 for (unsigned i = 0; i < components; i++) {
4501 const fs_reg component_i = offset(src, bld, 2 * i);
4502
4503 bld.MOV(subscript(tmp, src.type, 0), component_i);
4504 bld.MOV(subscript(tmp, src.type, 1), offset(component_i, bld, 1));
4505
4506 bld.MOV(offset(dst, bld, i), tmp);
4507 }
4508 }
4509
4510 /**
4511 * This helper does the inverse operation of
4512 * SHUFFLE_32BIT_LOAD_RESULT_TO_64BIT_DATA.
4513 *
4514 * We need to do this when we are going to use untyped write messsages that
4515 * operate with 32-bit components in order to arrange our 64-bit data to be
4516 * in the expected layout.
4517 *
4518 * Notice that callers of this function, unlike in the case of the inverse
4519 * operation, would typically need to call this with dst and src being
4520 * different registers, since they would otherwise corrupt the original
4521 * 64-bit data they are about to write. Because of this the function checks
4522 * that the src and dst regions involved in the operation do not overlap.
4523 */
4524 void
4525 shuffle_64bit_data_for_32bit_write(const fs_builder &bld,
4526 const fs_reg &dst,
4527 const fs_reg &src,
4528 uint32_t components)
4529 {
4530 assert(type_sz(src.type) == 8);
4531 assert(type_sz(dst.type) == 4);
4532
4533 assert(!src.in_range(dst, 2 * components * bld.dispatch_width() / 8));
4534
4535 for (unsigned i = 0; i < components; i++) {
4536 const fs_reg component_i = offset(src, bld, i);
4537 bld.MOV(offset(dst, bld, 2 * i), subscript(component_i, dst.type, 0));
4538 bld.MOV(offset(dst, bld, 2 * i + 1), subscript(component_i, dst.type, 1));
4539 }
4540 }