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